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>
Supported `polkadot-sdk` rev: `2603`
### Changed
- Updated `LLVM` from `21.1.8` to LLVM `22.1.4`
### Added
- Support for solc v0.8.35.
### Fixed
- A constant folding bug causing incorrect translation of a signed
remainder edge case. The affected pattern (Solidity % or Yul smod):
- SREM over the two constant operands `type(int256).min` and `-1`.
- At least one of the two constants needs folding (i.e. not just
`type(int256).min % -1`),
and is written specifically such that solc doesn't fold but LLVM does,
resulting in UB (signed-overflow).
- Fix `mulmod` returning out-of-range result for large moduli.
---------
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
Existing test coverage didn't catch a signed remainder overflow
semantics edge case behavior where at least one the operators is being
constant folded by LLVM instead of solc. This PR fixes the SREM
translation bug and adds missing test coverage for division ops with
constants.
Closes#524
---------
Signed-off-by: xermicus <cyrill@parity.io>
- Support solc v0.8.35
- The new `erc7201` is comptime (already translated to a constant in
Yul)
Didn't spot any relevant breakage or addition in the release notes so
should be no-brainer.
---------
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
- Recursive processes spawn the currently running executable by default,
removing the annoyance of mixing different compiler builds during
contract compilation.
- Remove the unused `once_cell` dependency.
Signed-off-by: xermicus <bigcyrill@hotmail.com>
As a drive-by fix, the get-llvm action now ignores LLVM releases marked
as pre-release.
---------
Signed-off-by: xermicus <bigcyrill@hotmail.com>
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
In `--yul` mode, resolc now properly exits when solc reports validation
errors (e.g., invalid hex literals, variable redeclarations), matching
`--standard-json behavior.`
`runs_without_input_file` was adapted because it was relying on it where
the new behavior correctly matches solc --strict-assembly.
Closes#477
Co-authored-by: minimax-m2.5 <neo@zion>
---------
Signed-off-by: xermicus <bigcyrill@hotmail.com>
The `--bin` CLI flag was not producing bytecode because the
`outputSelection` was not updated to explicitly requested it.
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
This marks a milestone: Smart contracts on Polkadot Asset Hub are live
and we release the first compiler version!
Signed-off-by: xermicus <bigcyrill@hotmail.com>
- Remove the superfluous byte swap in the `linkersymbol` builtin
translation.
- Support testing the `linkersymbol` builtin translation in the runner
specs.
---------
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
The OOB logic is implemented in the runtime but truncation circumvented
it.
Closes#263
---------
Signed-off-by: xermicus <cyrill@parity.io>
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
This PR fixes a small oversight from #431: Set the debug config of the
revive-runner `Runtime` such that we get the PVM logs back. Since the
revive runner runtime is mainly meant for debugging, I further decided
to enable unlimited contract sizes and disable [EIP
3607](https://eips.ethereum.org/EIPS/eip-3607) too.
---------
Signed-off-by: xermicus <cyrill@parity.io>
This tests the marketplace contracts to compile fine. In prior versions
this fails to compile, hitting the solc "Stack too deep" error.
Closes#409
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
- Emit the `call_evm` and `delegate_call_evm` syscalls for contract
calls.
- The call gas is no longer ignored.
---------
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
Support for `polkadot-sdk` release `unstable2507`. This release will be
deployed to Kusama and is supposed the first one on Polkadot.
---------
Signed-off-by: xermicus <cyrill@parity.io>
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
The `Cargo.lock` kept changing locally on all my hosts. Not sure why
exactly - maybe it got forgotten in a recent PR. I took it as an
opportunity to quickly update the Rust dependencies.
---------
Signed-off-by: xermicus <bigcyrill@hotmail.com>
LLVM is now a submodule and no longer in `.gitignore`. Thus, some dev
tools I use, like `rg` and `fd`, now consider the `llvm/` directory.
This PR fixes that by adding `llvm/` to the `.ignore` file.
Signed-off-by: xermicus <bigcyrill@hotmail.com>
- Fixes multiple bugs in the Yul test util helpers.
- The simple Yul builder utility helper now returns the contract builds.
Signed-off-by: xermicus <bigcyrill@hotmail.com>
These compiler outputs are irrelevant for the `resolc` compilation
pipeline. It can also break the compilation pipeline. For example, solc
may reject the code without via-ir or fail during the EVM codegen phase
with stack too deep errors - which is not relevant to us.
---------
Signed-off-by: xermicus <cyrill@parity.io>
Note:
- The unstable interface in `v2509.0.0` of `polkadot-sdk` is required.
- The differential test fails against EVM in `v2509.0.0` of
`polkadot-sdk`.
---------
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
This PR synchronizes the syscall function signatures in the runtime-api
with the current revive pallet API surface.
Signed-off-by: xermicus <cyrill@parity.io>
This PR switches to the new `get_storage_or_zero` and
`set_storage_or_clear` syscalls which are more tailored towards
Solidity.
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
- do not prune the AST output in standard JSON mode (required for
foundry)
- do not serialize the output format if there was not blob produced
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
Function symbols can clash as we compile multiple YUL `object`
definition into the same `LLVM` module.
- Disambiguate via unique function symbols based its location (runtime
or deploy code).
- Use `LinkOnceODR` linkage for compiler builtin helpers.
---------
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
Prevents unused functions in the emitted ELF object. Drive-by add a
missing test case (which misses a relocation under `-Oz` when all
internal functions are marked as private).
---------
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
Separate between compilation and linker phases to allow deploy time
linking and back-porting era compiler changes to fix#91. Unlinked
contract binaries (caused by missing libraries or missing factory
dependencies in turn) are emitted as raw ELF object.
Few drive by fixes:
- #98
- A compiler panic on missing libraries definitions.
- Fixes some incosistent type forwarding in JSON output (empty string
vs. null object).
- Remove the unused fallback for size optimization setting.
- Remove the broken `--lvm-ir` mode.
- CI workflow fixes.
---------
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
Signed-off-by: xermicus <bigcyrill@hotmail.com>
Signed-off-by: xermicus <cyrill@parity.io>
- Implement the visitor interface. This simplifies working with the AST,
for example transformations into other IRs or collecting and analyzing
various statistics.
- Switch the explorer to use the visitor interface.
- Add the reciprocal function name conversion for function names.
- Some drive-by cosmetic fixes.
---------
Signed-off-by: xermicus <bigcyrill@hotmail.com>
A maintainable and more precise version of what was a hacky but useful
script, exploring the compilers YUL lowering unit.
It analyzes a given shared objects from the debug dump and outputs:
- The count of each YUL statement translated.
- A per YUL statement break-down of bytecode size contributed per.
- Estimated `yul-phaser` cost parameters.
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
Closes #350
- Add the missing `memset` builtin which was accidentally deleted in a
previous PR.
- Add a compilation test to ensure the `memset` builtin is present.
---------
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
The data structure can be used to build the JSON input for `resolc` too.
In that case serializing of provided custom options should not be
dismissed.
Makes the memory settings struct more modular as a drive-by.
---------
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
Do not use wildcard dependency in resolc crate. No sure why the publish
dry-run doesn't catch this.
---------
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
- Fetching the commit SHA must not panic if not executed in a git
repository.
- Remove the license printer.
---------
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
- Factor the YUL crate out of `revive-solidity`.
- `revive-solidity` is in reality not a Solidity implementation but the
revive solidity compiler driver (`resolc`). By renaming we not only get
this straight but also a binary with the same name as the crate which
should be less confusing.
---------
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
Exposes the following PolkaVM specific options via the standard json
interface:
- Heap size
- Stack size
- Whether to emit source level debug information
Additionally it is now forbidden to specify those as CLI option in
standard JSON mode. They are bytecode altering options and having
multiple ways to specify them creates unnecessary room for confusion:
The standard JSON input description should be sufficient and succint for
reproducible builds.
Closes#290
---------
Signed-off-by: xermicus <bigcyrill@hotmail.com>
- Update the Rust version to 1.85
- Update the package-lock.json
- Update the musl-cross docker image
---------
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
- Add support for solc `v0.8.30`. No changes in YUL or the binary
interface.
- Fix a semver bug in the NPM packages to correctly align their solc
dependencies with our last supported version.
---------
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
So far if no optimization level was specified, optimizations for
execution time were applied. However, we currently are a bit limited on
code size. Add to that, this setting is not available in solc and people
generally ignore the docs, generating a lot of support requests.
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
- Allow configuration of the maximum heap and stack size via CLI flags
and JSON input settings.
- Increase the default value for the stack size to 32kb.
---------
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
This PR changes the implementation of the emulated EVM heap memory:
Instead of linking in a C implementation the code is emitted directly
into the contract module. Which allows making it configurable via a
compiler parameter (a follow up PR to this).
---------
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
Make the installation of `revive-runner` easier:
- Use locked dependencies to avoid issues with downstream crates
- Make the llvm-context crate an optional dependency to the runner
- Add it to the default `test` target ensuring that this actually works
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>