Skip to content

add EmuContractExecutor (sierra-emu backend, interchangeable with AotContractExecutor)#1612

Open
avi-starkware wants to merge 3 commits into
mainfrom
avi/cairo_native/contract-executor-2
Open

add EmuContractExecutor (sierra-emu backend, interchangeable with AotContractExecutor)#1612
avi-starkware wants to merge 3 commits into
mainfrom
avi/cairo_native/contract-executor-2

Conversation

@avi-starkware

@avi-starkware avi-starkware commented May 14, 2026

Copy link
Copy Markdown
Collaborator

Summary

Makes the sierra-emu interpreter usable as a drop-in alternative to AotContractExecutor at contract call sites — without a dispatch enum. No consumer selects a backend at runtime (the choice is always made per build by feature flags), so consumers pick an executor type per build instead of wrapping every executor in an enum on the production path.

Earlier revisions of this PR introduced a ContractExecutor dispatch enum (Aot + Emu); per review it is gone. Each step is its own commit so it can be reverted independently.

Changes

  • debug_utils/sierra-emu: new VirtualMachine::run_contract(...) — one-shot convenience over new_starknet + call_contract + run. Returns None when the VM never produced a final state.
  • src/executor/emu_contract_executor.rs (new, gated on the new sierra-emu cargo feature): EmuContractExecutor { program, entry_points, sierra_version } with an inherent run() that mirrors AotContractExecutor::run — same parameter shape, same StarknetSyscallHandler trait (shared via cairo-native-syscalls, so the handler flows through unchanged). Converts BuiltinCosts and the emu result into cairo-native's ContractExecutionResult (with default builtin_stats).
  • Cargo.toml: new sierra-emu = ["dep:sierra-emu"] feature; with-trace-dump now depends on the feature rather than the optional dep directly.

Commits (revert points)

  1. add ContractExecutor dispatch enum (Aot + Emu) — original version.
  2. move Emu-arm VM setup into sierra_emu::VirtualMachine::run_contract — sierra-emu owns the VM lifecycle.
  3. replace ContractExecutor enum with standalone EmuContractExecutor — drops the enum; reverting this commit restores it.

Stack

  1. align sierra-emu StarknetSyscallHandler trait/types with cairo-native #1610 — trait alignment
  2. extract shared cairo-native-syscalls crate #1611 — extract shared crate
  3. this PR — sierra-emu contract executor
  4. add run_with_libfunc_profile + AotWithProgram pairing #1613run_with_libfunc_profile + AotWithProgram

Test plan

  • cargo check (default features) clean
  • cargo check --features sierra-emu clean
  • cargo check --workspace --all-features clean (on stack tip)
  • CI green

This change is Reviewable

@github-actions

github-actions Bot commented May 14, 2026

Copy link
Copy Markdown

✅ Code is now correctly formatted.

@avi-starkware avi-starkware force-pushed the avi/cairo_native/syscall-types-crate branch from b3a574c to 319a6be Compare May 14, 2026 12:26
@avi-starkware avi-starkware force-pushed the avi/cairo_native/contract-executor-2 branch from 98ed085 to 64c12b3 Compare May 14, 2026 12:26
@avi-starkware avi-starkware force-pushed the avi/cairo_native/syscall-types-crate branch from 319a6be to 92ecf03 Compare May 14, 2026 12:33
@avi-starkware avi-starkware force-pushed the avi/cairo_native/contract-executor-2 branch from 64c12b3 to 7e5fcd7 Compare May 14, 2026 12:33
@avi-starkware avi-starkware force-pushed the avi/cairo_native/syscall-types-crate branch from 92ecf03 to 3b3e5af Compare May 17, 2026 11:57
@avi-starkware avi-starkware force-pushed the avi/cairo_native/contract-executor-2 branch from 7e5fcd7 to 4176352 Compare May 17, 2026 11:57
@avi-starkware avi-starkware force-pushed the avi/cairo_native/syscall-types-crate branch from 3b3e5af to 24f61ba Compare May 17, 2026 12:08
@avi-starkware avi-starkware force-pushed the avi/cairo_native/contract-executor-2 branch from 4176352 to 7af0508 Compare May 17, 2026 12:08
@avi-starkware avi-starkware force-pushed the avi/cairo_native/syscall-types-crate branch from 24f61ba to 0ae0d06 Compare May 17, 2026 13:24
@avi-starkware avi-starkware force-pushed the avi/cairo_native/contract-executor-2 branch from 7af0508 to 38c5f2f Compare May 17, 2026 13:24
@avi-starkware avi-starkware force-pushed the avi/cairo_native/syscall-types-crate branch from 0ae0d06 to 0791b36 Compare May 17, 2026 13:27
@avi-starkware avi-starkware force-pushed the avi/cairo_native/contract-executor-2 branch from 38c5f2f to 81093b3 Compare May 17, 2026 13:27
@avi-starkware avi-starkware force-pushed the avi/cairo_native/syscall-types-crate branch from 0791b36 to 4e80562 Compare May 17, 2026 13:58
@avi-starkware avi-starkware force-pushed the avi/cairo_native/contract-executor-2 branch 2 times, most recently from 4f53f27 to 6f77130 Compare May 20, 2026 09:43
@avi-starkware avi-starkware force-pushed the avi/cairo_native/syscall-types-crate branch from 4e80562 to 34f9e15 Compare May 20, 2026 09:43
@avi-starkware avi-starkware force-pushed the avi/cairo_native/syscall-types-crate branch from 34f9e15 to eec22ef Compare May 28, 2026 14:56
@avi-starkware avi-starkware force-pushed the avi/cairo_native/contract-executor-2 branch 2 times, most recently from aa4bf47 to e6a4ad9 Compare May 31, 2026 12:03
@avi-starkware avi-starkware force-pushed the avi/cairo_native/syscall-types-crate branch from eec22ef to 487deb9 Compare May 31, 2026 12:03
@avi-starkware avi-starkware force-pushed the avi/cairo_native/contract-executor-2 branch 2 times, most recently from 538f655 to c8ed1f7 Compare June 1, 2026 11:26
@avi-starkware avi-starkware force-pushed the avi/cairo_native/syscall-types-crate branch from 701a87c to 61059c1 Compare June 1, 2026 11:26
Base automatically changed from avi/cairo_native/syscall-types-crate to main June 1, 2026 13:59

@orizi orizi left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@orizi made 1 comment.
Reviewable status: 0 of 23 files reviewed, 1 unresolved discussion (waiting on avi-starkware, TomerStarkware, and Yoni-Starkware).


-- commits line 1 at r2:
is this missing a rebase?

@avi-starkware avi-starkware force-pushed the avi/cairo_native/contract-executor-2 branch from c8ed1f7 to 90cd01f Compare July 5, 2026 08:04
@github-actions

github-actions Bot commented Jul 5, 2026

Copy link
Copy Markdown

Benchmarking results

Benchmark for program dict_insert

Open benchmarks
Command Mean [s] Min [s] Max [s] Relative
Cairo-vm (Rust, Cairo 1) 5.785 ± 0.055 5.727 5.875 5.57 ± 0.06
cairo-native (embedded AOT) 1.039 ± 0.003 1.035 1.045 1.00
cairo-native (embedded JIT using LLVM's ORC Engine) 1.045 ± 0.005 1.037 1.055 1.01 ± 0.01

Benchmark for program dict_snapshot

Open benchmarks
Command Mean [ms] Min [ms] Max [ms] Relative
Cairo-vm (Rust, Cairo 1) 306.6 ± 3.4 301.8 313.1 1.00
cairo-native (embedded AOT) 928.7 ± 3.9 923.2 934.4 3.03 ± 0.04
cairo-native (embedded JIT using LLVM's ORC Engine) 945.4 ± 5.1 936.7 951.3 3.08 ± 0.04

Benchmark for program factorial_2M

Open benchmarks
Command Mean [s] Min [s] Max [s] Relative
Cairo-vm (Rust, Cairo 1) 2.501 ± 0.025 2.476 2.565 2.58 ± 0.03
cairo-native (embedded AOT) 0.970 ± 0.003 0.966 0.974 1.00
cairo-native (embedded JIT using LLVM's ORC Engine) 0.977 ± 0.003 0.972 0.982 1.01 ± 0.00

Benchmark for program fib_2M

Open benchmarks
Command Mean [s] Min [s] Max [s] Relative
Cairo-vm (Rust, Cairo 1) 2.432 ± 0.018 2.404 2.457 2.59 ± 0.02
cairo-native (embedded AOT) 0.937 ± 0.004 0.933 0.945 1.00
cairo-native (embedded JIT using LLVM's ORC Engine) 0.957 ± 0.017 0.945 1.003 1.02 ± 0.02

Benchmark for program linear_search

Open benchmarks
Command Mean [ms] Min [ms] Max [ms] Relative
Cairo-vm (Rust, Cairo 1) 330.0 ± 3.9 325.4 336.2 1.00
cairo-native (embedded AOT) 945.7 ± 3.2 939.0 951.5 2.87 ± 0.04
cairo-native (embedded JIT using LLVM's ORC Engine) 962.7 ± 3.5 955.7 967.6 2.92 ± 0.04

Benchmark for program logistic_map

Open benchmarks
Command Mean [ms] Min [ms] Max [ms] Relative
Cairo-vm (Rust, Cairo 1) 282.6 ± 2.2 279.5 286.7 1.00
cairo-native (embedded AOT) 940.8 ± 3.8 935.3 946.1 3.33 ± 0.03
cairo-native (embedded JIT using LLVM's ORC Engine) 965.9 ± 15.1 957.1 1007.1 3.42 ± 0.06

@github-actions

github-actions Bot commented Jul 5, 2026

Copy link
Copy Markdown

Benchmark results Main vs HEAD.

Base

Command Mean [s] Min [s] Max [s] Relative
base dict_insert.cairo (JIT) 1.929 ± 0.019 1.908 1.966 1.01 ± 0.02
base dict_insert.cairo (AOT) 1.914 ± 0.025 1.871 1.957 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head dict_insert.cairo (JIT) 1.851 ± 0.037 1.802 1.944 1.01 ± 0.03
head dict_insert.cairo (AOT) 1.841 ± 0.028 1.818 1.910 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base dict_snapshot.cairo (JIT) 1.711 ± 0.026 1.664 1.753 1.02 ± 0.02
base dict_snapshot.cairo (AOT) 1.672 ± 0.016 1.654 1.699 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head dict_snapshot.cairo (JIT) 1.675 ± 0.030 1.636 1.739 1.04 ± 0.02
head dict_snapshot.cairo (AOT) 1.605 ± 0.010 1.589 1.626 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base factorial_2M.cairo (JIT) 1.772 ± 0.023 1.736 1.797 1.02 ± 0.02
base factorial_2M.cairo (AOT) 1.741 ± 0.019 1.703 1.760 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head factorial_2M.cairo (JIT) 1.701 ± 0.017 1.681 1.733 1.02 ± 0.01
head factorial_2M.cairo (AOT) 1.669 ± 0.008 1.654 1.682 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base fib_2M.cairo (JIT) 1.730 ± 0.032 1.692 1.773 1.02 ± 0.03
base fib_2M.cairo (AOT) 1.695 ± 0.038 1.637 1.748 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head fib_2M.cairo (JIT) 1.630 ± 0.008 1.616 1.642 1.02 ± 0.01
head fib_2M.cairo (AOT) 1.599 ± 0.007 1.590 1.616 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base linear_search.cairo (JIT) 1.746 ± 0.019 1.723 1.779 1.02 ± 0.02
base linear_search.cairo (AOT) 1.713 ± 0.022 1.693 1.758 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head linear_search.cairo (JIT) 1.660 ± 0.011 1.641 1.679 1.02 ± 0.01
head linear_search.cairo (AOT) 1.626 ± 0.009 1.611 1.642 1.00

Base

Command Mean [s] Min [s] Max [s] Relative
base logistic_map.cairo (JIT) 1.728 ± 0.023 1.705 1.768 1.02 ± 0.02
base logistic_map.cairo (AOT) 1.690 ± 0.026 1.647 1.732 1.00

Head

Command Mean [s] Min [s] Max [s] Relative
head logistic_map.cairo (JIT) 1.648 ± 0.037 1.619 1.736 1.03 ± 0.02
head logistic_map.cairo (AOT) 1.606 ± 0.008 1.596 1.626 1.00

Adds a public dispatch enum so a single call site can pick between the
AOT executor and the sierra-emu interpreter at runtime, without forcing
every caller to maintain its own match.

- ContractExecutor { Aot(AotContractExecutor), Emu(EmuContractInfo) },
  with Emu gated on the new sierra-emu cargo feature.
- EmuContractInfo carries Arc<Program> so the program is shared across
  invocations rather than cloned per call.
- ContractExecutor::run dispatches: Aot delegates to
  AotContractExecutor::run; Emu constructs a sierra_emu::VirtualMachine
  and runs it with the caller's syscall handler directly. No adapter is
  needed -- the trait is shared via the cairo-starknet-syscalls crate.
  VirtualMachine::run's `Option` return is propagated as
  Error::UnexpectedValue rather than `.expect()`-aborted, matching the
  Aot arm's error-handling style.
- Cargo: new `sierra-emu` feature (= ["dep:sierra-emu"]). The existing
  `with-trace-dump` feature now activates `sierra-emu` instead of the
  optional dep directly.

Companion to the bridge-free design: with cairo-native and sierra-emu
sharing one trait, the SierraEmuSyscallBridge that PR #1597 / #1607
introduced is no longer necessary.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@avi-starkware avi-starkware force-pushed the avi/cairo_native/contract-executor-2 branch from 90cd01f to a1e3635 Compare July 5, 2026 09:00
@avi-starkware

Copy link
Copy Markdown
Collaborator Author

-- commits line 1 at r2:

Previously, orizi wrote…

is this missing a rebase?

Done.

@orizi orizi left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@orizi reviewed 22 files and all commit messages, and made 1 comment.
Reviewable status: 22 of 23 files reviewed, 2 unresolved discussions (waiting on avi-starkware, TomerStarkware, and Yoni-Starkware).


src/executor/contract_executor.rs line 107 at r3 (raw file):

                    builtin_stats: Default::default(),
                })
            }

any reason to not just add this into sierra-emu and not need this entire new execute file?

Code quote:

                let mut virtual_machine = sierra_emu::VirtualMachine::new_starknet(
                    Arc::clone(program),
                    entry_points,
                    *sierra_version,
                );

                let emu_builtin_costs = builtin_costs.map(convert_builtin_costs);

                virtual_machine.call_contract(selector, gas, args.to_vec(), emu_builtin_costs);

                // `VirtualMachine::run` returns `None` when the VM never produced a
                // final state -- propagate as an error rather than aborting the host.
                let result = virtual_machine.run(&mut syscall_handler).ok_or_else(|| {
                    Error::UnexpectedValue("sierra-emu VM produced no final state".to_string())
                })?;

                Ok(ContractExecutionResult {
                    remaining_gas: result.remaining_gas,
                    failure_flag: result.failure_flag,
                    return_values: result.return_values,
                    error_msg: result.error_msg,
                    builtin_stats: Default::default(),
                })
            }

avi-starkware and others added 2 commits July 6, 2026 13:37
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
The dispatch enum is gone: consumers pick an executor type per build
(feature flags) rather than per value. EmuContractInfo becomes
EmuContractExecutor with an inherent run() that mirrors
AotContractExecutor::run, so the two are interchangeable at call sites.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@avi-starkware avi-starkware changed the title add ContractExecutor dispatch enum (Aot + Emu) add EmuContractExecutor (sierra-emu backend, interchangeable with AotContractExecutor) Jul 6, 2026
@avi-starkware

Copy link
Copy Markdown
Collaborator Author

src/executor/contract_executor.rs line 107 at r3 (raw file):

Previously, orizi wrote…

any reason to not just add this into sierra-emu and not need this entire new execute file?

I removed the ContractExecutor enum and replaced it with a type alias (also removed this file).

I looked at the code and at how lambdaclass used it for replay, and there is no dynamic dispatch of Contract executor types. The type used is determined at compile time. So I think we can safely remove this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants