mirror of
https://github.com/pezkuwichain/revive.git
synced 2026-10-20 18:56:17 +00:00
This PR introduces powerful code size optimizations. This is achieved with an additional IR layer in between Yul and LLVM IR. It works quite well (see below for a comparison) because it captures optimization gains that neither solc nor LLVM can realize. The feature is gated behind `--newyork` or a corresponding standard JSON input setting and **off by default**. ### How it works We implemented an SSA IR modelled after or inspired by the [MLIR SCF dialect](https://mlir.llvm.org/docs/Dialects/SCFDialect/). Yul is translated into this IR and the IR implements mainly the following optimizations: - **Type narrowing**: narrows 256-bit values to I1..I160 where provable (bidirectional + interprocedural), so LLVM emits native 32/64-bit ops instead of software 256-bit arithmetic. - **FMP range proof**: exposes the `mload(0x40) < heap_size` bound to LLVM IPSCCP, eliminating trailing overflow checks on FMP-derived offsets. Single largest code-size contributor. - **Heap analysis**: skips big-endian byte-swaps where native little-endian layout is safe. - **mem_opt / keccak fusion+folding**, **compound outlining** (mapping sload/sstore), **guard narrowing**, **inlining** tuned for PolkaVM, and **exact + fuzzy function dedup**. ### Results The optimizations implemented with the newyork IR bring substantial code size improvements. - Integration contracts: approximately **-20% to -50%** code size. - OpenZeppelin Wizard corpus: around **-47%** code size. This table shows the code size improvements on various OpenZeppelin ERC implementations, taken from their [contracts wizard](https://wizard.openzeppelin.com/), with almost all features enabled. | Contract | Default: (bytes) | With `--newyork`: (bytes) | Reduction | |---|---:|---:|---:| | erc1155 | 59,206 | 30,649 | -48.2% | | erc20 | 81,697 | 42,893 | -47.5% | | erc721 | 92,052 | 49,423 | -46.3% | | oz_gov | 141,861 | 81,159 | -42.8% | | oz_rwa | 77,274 | 37,975 | -50.9% | | oz_simple_erc20 | 29,950 | 16,554 | -44.7% | | oz_stable | 82,117 | 39,032 | -52.5% | | **Total** | **564,157** | **297,685** | **-47.2%** | We believe this to be a reasonably representative dataset and conclude that the newyork IR achieves drastic code size reductions on real world dApp implementations. ### Methodology, security and runtime performance Since the main contributors to these improvements are the FMP range proofs and the type narrowing, we don't expect big runtime performance regressions (it may even benefit runtime performance in some cases). The PR also includes docs (`book/src/developer_guide/newyork_{optimizer,ir}.md`) and many new integration tests, including regression tests for known FMP soundness traps. The PR was authored collaboratively and reviewed extensively by the `revive` maintainers, as well as Claude Opus, Claude Fable, Qwen, Minimax and Deepseek LLMs, over a span of many months. The `newyork` compiler pipeline introduces no new regressions over our integration and differential test suites (containing over 30000 test cases). This was achieved by careful manual reviews and many LLM bughunt loops. Additionally, a final security review by Anthropic's Fable 5 LLM found no remaining soundness issues. Nonetheless, like any brand new software, please consider this an experimental feature upon release. --------- Signed-off-by: xermicus <bigcyrill@hotmail.com> Signed-off-by: xermicus <cyrill@parity.io> Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com> Co-authored-by: neo <me@90f9.org> Co-authored-by: LJ <81748770+elle-j@users.noreply.github.com>
45 lines
2.2 KiB
Plaintext
45 lines
2.2 KiB
Plaintext
object "NarrowProbe" {
|
|
code {
|
|
datacopy(0, dataoffset("NarrowProbe_deployed"), datasize("NarrowProbe_deployed"))
|
|
return(0, datasize("NarrowProbe_deployed"))
|
|
}
|
|
object "NarrowProbe_deployed" {
|
|
code {
|
|
let op := calldataload(0)
|
|
let a := calldataload(32)
|
|
let r := 0
|
|
switch op
|
|
// high-bit round trip: narrow via shr then widen back via shl
|
|
case 0 { r := shl(200, shr(200, a)) }
|
|
// narrow to i32 then square (needs up to 64 bits)
|
|
case 1 { let t := and(a, 0xFFFFFFFF) r := mul(t, t) }
|
|
// narrow to i64 then shift into the high half
|
|
case 2 { let t := shr(192, a) r := shl(192, t) }
|
|
// narrow store then load round trip (i16)
|
|
case 3 { let t := and(a, 0xFFFF) mstore(0, t) r := mload(0) }
|
|
// top byte (i8) shifted back to the top
|
|
case 4 { let t := byte(0, a) r := shl(248, t) }
|
|
// narrow to i8 then storage round trip
|
|
case 5 { let t := and(a, 0xFF) sstore(5, t) r := sload(5) }
|
|
// narrow then negate: 0 - small => full-width large value
|
|
case 6 { let t := shr(128, a) r := sub(0, t) }
|
|
// not of a narrow value => high bits all set (full width)
|
|
case 7 { let t := and(a, 0xFFFFFFFF) r := not(t) }
|
|
// i64 + i64 => up to 65 bits
|
|
case 8 { let t := and(a, 0xFFFFFFFFFFFFFFFF) r := add(t, t) }
|
|
// signextend over a narrowed value
|
|
case 9 { let t := shr(64, a) r := signextend(7, t) }
|
|
// narrow then compare-against-large: lt(small, big) must be 1
|
|
case 10 { let t := and(a, 0xFF) r := lt(t, 0x100000000) }
|
|
// div of a narrowed dividend by 1 then widen-add a big constant
|
|
case 11 { let t := and(a, 0xFFFF) r := add(div(t, 1), shl(200, 1)) }
|
|
// mulmod where two operands are narrow but the product overflows 256
|
|
case 12 { let t := and(a, 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF) r := mulmod(t, t, 0x100000000000000000000000000000000) }
|
|
// narrow value used as exp base, large exponent
|
|
case 13 { let t := and(a, 0xFF) r := exp(t, 64) }
|
|
mstore(0, r)
|
|
return(0, 32)
|
|
}
|
|
}
|
|
}
|