Skip to content

add run_with_libfunc_profile + AotWithProgram variant for ContractExecutor#1613

Open
avi-starkware wants to merge 2 commits into
avi/cairo_native/contract-executor-2from
avi/cairo_native/libfunc-profiling-2
Open

add run_with_libfunc_profile + AotWithProgram variant for ContractExecutor#1613
avi-starkware wants to merge 2 commits into
avi/cairo_native/contract-executor-2from
avi/cairo_native/libfunc-profiling-2

Conversation

@avi-starkware
Copy link
Copy Markdown
Collaborator

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

Summary

Exposes the libfunc-profiling primitives that downstream consumers (e.g. the blockifier in starkware-libs/sequencer) currently maintain locally. The profile-collection pattern is callback-driven so the per-call key (tx hash, etc.) stays out of cairo-native.

Replaces #1599 / #1609.

Changes

  • metadata::profiler::Profile is now pub (was a private type alias).
  • AotContractExecutor::run_with_libfunc_profile<H, F> (gated on with-libfunc-profiling, in new file src/executor/libfunc_profile.rs) wraps run: allocates a unique trace ID, points the executor's cairo_native__profiler__profile_id symbol at it, drains the resulting Profile after run returns, and hands it to a caller-supplied FnOnce(Profile). A ProfilerGuard restores the previous trace ID and drops the LIBFUNC_PROFILE slot on both the success and unwind paths.
  • ContractExecutor::AotWithProgram(AotWithProgram { executor, program }) is a new variant that bundles an AOT executor with the Sierra program it was built from. From<AotWithProgram> is provided.
  • ContractExecutor::run dispatches the new variant via run_with_libfunc_profile with a no-op profile callback.
  • ContractExecutor::run_with_profile<H, F> is the profile-capturing counterpart of run; for non-AotWithProgram variants it falls through to run (callback never fires).

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. add ContractExecutor dispatch enum (Aot + Emu) #1612ContractExecutor (supersedes add ContractExecutor dispatch enum (Aot + Emu) #1598 / add ContractExecutor dispatch enum (Aot + Emu) #1608)
  4. this PRrun_with_libfunc_profile + AotWithProgram (supersedes add run_with_libfunc_profile + AotWithProgram variant for ContractExecutor #1599 / add run_with_libfunc_profile + AotWithProgram variant for ContractExecutor #1609)

Test plan

  • cargo check (default features) clean
  • cargo check --features with-libfunc-profiling clean
  • cargo check --features sierra-emu,with-libfunc-profiling clean
  • cargo check --workspace --all-features clean
  • CI green

This change is Reviewable

@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 14, 2026

✅ Code is now correctly formatted.

@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/libfunc-profiling-2 branch from 474462f to 13381fa Compare May 14, 2026 12:26
@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/libfunc-profiling-2 branch 2 times, most recently from b5e8d6b to 94bc246 Compare May 14, 2026 12:49
Copy link
Copy Markdown
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

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

@orizi reviewed 3 files and all commit messages.
Reviewable status: 3 of 4 files reviewed, all discussions resolved (waiting on TomerStarkware and Yoni-Starkware).

@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/libfunc-profiling-2 branch from 94bc246 to 24984b5 Compare May 17, 2026 11:57
@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/libfunc-profiling-2 branch from 24984b5 to 78bf3e4 Compare May 17, 2026 12:08
@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/libfunc-profiling-2 branch from 78bf3e4 to 5b13906 Compare May 17, 2026 13:24
@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/libfunc-profiling-2 branch from 5b13906 to de234b8 Compare May 17, 2026 13:28
@avi-starkware avi-starkware force-pushed the avi/cairo_native/contract-executor-2 branch from 81093b3 to 4f53f27 Compare May 17, 2026 13:58
@avi-starkware avi-starkware force-pushed the avi/cairo_native/libfunc-profiling-2 branch 4 times, most recently from 74aab36 to d19f2ac Compare May 19, 2026 12:34
avi-starkware added a commit to starkware-libs/sequencer that referenced this pull request May 19, 2026
- Drop unused `NativeCompiledClassV1::new_from_emu`; it had no in-tree
  callers, and the sierra-emu construction path will be re-added when
  the benchmarking/replay tool that needs it lands.
- Revert `process_compilation_request`'s `.map(...)` shape to a direct
  `match`, moving `casm` into `NativeCompiledClassV1::new(_with_program)`
  in the `Ok` arm at zero cost instead of cloning it.
- Bump pinned cairo-native rev to pick up `cargo fmt` fix on
  starkware-libs/cairo_native#1613.

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 4f53f27 to 6f77130 Compare May 20, 2026 09:43
@avi-starkware avi-starkware force-pushed the avi/cairo_native/libfunc-profiling-2 branch from d19f2ac to 60e4055 Compare May 20, 2026 09:46
@avi-starkware avi-starkware force-pushed the avi/cairo_native/contract-executor-2 branch from 6f77130 to aa4bf47 Compare May 28, 2026 14:56
@avi-starkware avi-starkware force-pushed the avi/cairo_native/libfunc-profiling-2 branch from 60e4055 to a21f3f5 Compare May 28, 2026 14:57
Copy link
Copy Markdown
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

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

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


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

};
use bumpalo::Bump;
// Re-exported so libfunc-profiling consumers (e.g. blockifier) can refer to the

what kind of reference is expected?
if it is just as a value - since you only use is as Arc<Program> - export:
pub type ArcProgram = std::sync::Arc<cairo_lang_sierra::program::Program>;

avi-starkware added a commit to starkware-libs/sequencer that referenced this pull request May 28, 2026
- Drop unused `NativeCompiledClassV1::new_from_emu`; it had no in-tree
  callers, and the sierra-emu construction path will be re-added when
  the benchmarking/replay tool that needs it lands.
- Revert `process_compilation_request`'s `.map(...)` shape to a direct
  `match`, moving `casm` into `NativeCompiledClassV1::new(_with_program)`
  in the `Ok` arm at zero cost instead of cloning it.
- Bump pinned cairo-native rev to pick up `cargo fmt` fix on
  starkware-libs/cairo_native#1613.

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 aa4bf47 to e6a4ad9 Compare May 31, 2026 12:03
@avi-starkware avi-starkware force-pushed the avi/cairo_native/libfunc-profiling-2 branch from a21f3f5 to be6cb3f Compare May 31, 2026 12:16
@avi-starkware avi-starkware force-pushed the avi/cairo_native/contract-executor-2 branch from e6a4ad9 to 538f655 Compare May 31, 2026 13:33
@avi-starkware avi-starkware force-pushed the avi/cairo_native/libfunc-profiling-2 branch from be6cb3f to 8b6515b Compare May 31, 2026 13:34
…cutor

Exposes the libfunc-profiling primitives that downstream consumers (e.g.
the blockifier in starkware-libs/sequencer) currently maintain locally.
The profile-collection pattern is callback-driven so the per-call key
(tx hash, etc.) stays out of cairo-native.

- metadata::profiler::Profile is now pub (was a private type alias).
- AotContractExecutor::run_with_libfunc_profile<H, F> (gated on
  with-libfunc-profiling, in new file src/executor/libfunc_profile.rs)
  wraps run with the bookkeeping the runtime needs:
    1. Acquires a process-wide PROFILE_LOCK so concurrent profile calls
       serialize on the global trace-id symbol. The lock is recovered
       if poisoned.
    2. Looks up the profile-id symbol before touching any global state.
       Absent symbol -> typed Error::UnexpectedValue rather than panic.
    3. Allocates a trace ID, inserts a slot in LIBFUNC_PROFILE, points
       the global at the new ID.
    4. Calls run; on success drains the slot and hands the Profile to
       on_profile. On failure the callback is not invoked.
    5. A ProfilerGuard restores the previous trace ID on success and
       unwind.
- ContractExecutor::AotWithProgram(AotWithProgram { executor, program })
  is a new variant that bundles an AOT executor with the Sierra program
  it was built from. From<AotWithProgram> is provided.
- ContractExecutor::run dispatches the new variant via
  run_with_libfunc_profile with a no-op profile callback.
- ContractExecutor::run_with_profile<H, F> is the profile-capturing
  counterpart of run; for non-AotWithProgram variants it falls through
  to run (callback never fires).

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 538f655 to c8ed1f7 Compare June 1, 2026 11:26
@avi-starkware avi-starkware force-pushed the avi/cairo_native/libfunc-profiling-2 branch from 8b6515b to 7f27488 Compare June 1, 2026 11:26
…ion fixes

Review-driven fixes to the ContractExecutor libfunc-profiling path:

- ArcProgram alias (orizi review on src/executor.rs): replace the bare
  `pub use ...::Program` re-export with `pub type ArcProgram = Arc<Program>`,
  since profiling/sierra-emu consumers only ever hold the program as a shared
  handle. Gated on `any(sierra-emu, with-libfunc-profiling)` so EmuContractInfo
  consumers can name the field type too, and adopted for EmuContractInfo.program,
  AotWithProgram.program, and the run_with_profile callback.

- Self-deadlock: run_with_libfunc_profile held the non-reentrant process-wide
  PROFILE_LOCK across self.run, which re-enters this function on the same thread
  whenever a profiled contract calls another contract -- a guaranteed hang. Take
  the lock only at the outermost profiled frame per thread (tracked via a
  thread-local PROFILE_DEPTH); nested frames inherit the outer frame's lock, and
  the existing per-call old_trace_id save/restore keeps the global trace-id symbol
  correct across nesting. Cross-thread isolation is preserved (the outermost frame
  holds the lock for the whole run tree). A ProfileDepthGuard decrements the depth
  on every exit path including unwind.

- FFI panic-safety: ProfilerImpl::push_stmt is a `pub extern "C" fn` invoked
  directly from compiled Cairo code; its `.lock().unwrap()` would unwind across the
  C ABI on a poisoned mutex. Recover from poison instead, matching the lock handling
  in run_with_libfunc_profile.

- Instrumented compiler binary: add a `with-libfunc-profiling` feature to
  starknet-native-compile forwarding to `cairo-native/with-libfunc-profiling`, so an
  instrumented binary (whose emitted .so carries the profiler symbol that
  run_with_libfunc_profile looks up) can be built. A binary built without it produces
  .so files with no profiler symbol, making every profiled call fail.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
avi-starkware added a commit to starkware-libs/sequencer that referenced this pull request Jun 1, 2026
- Drop unused `NativeCompiledClassV1::new_from_emu`; it had no in-tree
  callers, and the sierra-emu construction path will be re-added when
  the benchmarking/replay tool that needs it lands.
- Revert `process_compilation_request`'s `.map(...)` shape to a direct
  `match`, moving `casm` into `NativeCompiledClassV1::new(_with_program)`
  in the `Ok` arm at zero cost instead of cloning it.
- Bump pinned cairo-native rev to pick up `cargo fmt` fix on
  starkware-libs/cairo_native#1613.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown
Collaborator Author

@avi-starkware avi-starkware left a comment

Choose a reason for hiding this comment

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

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


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

Previously, orizi wrote…

what kind of reference is expected?
if it is just as a value - since you only use is as Arc<Program> - export:
pub type ArcProgram = std::sync::Arc<cairo_lang_sierra::program::Program>;

Done.

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