Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 105 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,11 @@ cairo-lang-sierra = "2.17.0-rc.4"
cairo-lang-sierra-to-casm = "2.17.0-rc.4"
cairo-lang-starknet-classes = "2.17.0-rc.4"
cairo-lang-utils = "2.17.0-rc.4"
cairo-native = "0.9.0-rc.6"
# TEMP: git dep pinned to the tip of the unreleased PR stack
# (starkware-libs/cairo_native#1610 → #1611 → #1612 → #1613) containing
# `ContractExecutor`, `EmuContractInfo`, `AotWithProgram`, and `run_with_profile`.
# Switch back to a crates.io version once those land in a published cairo-native release.
cairo-native = { git = "https://github.com/starkware-libs/cairo_native.git", rev = "d19f2ac5e6f01b07f2805cbea42d02b45d9f885f", version = "0.9.0-rc.6" }
cairo-program-runner-lib = "1.1.0"
cairo-vm = "3.2.0"
camelpaste = "0.1.0"
Expand Down
6 changes: 6 additions & 0 deletions crates/apollo_compile_to_native/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ description = "A utility crate for compiling Sierra code into Cairo native."
[lints]
workspace = true

[features]
# Enables `SierraToNativeCompiler::compile_with_program`, which returns the
# AOT executor paired with the Sierra program required by cairo-native's
# libfunc profiler.
with-libfunc-profiling = ["cairo-native/with-libfunc-profiling"]

[dependencies]
apollo_compilation_utils.workspace = true
apollo_compile_to_native_types.workspace = true
Expand Down
25 changes: 25 additions & 0 deletions crates/apollo_compile_to_native/src/compiler.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::path::{Path, PathBuf};
#[cfg(feature = "with-libfunc-profiling")]
use std::sync::Arc;

use apollo_compilation_utils::compiler_utils::compile_with_args;
use apollo_compilation_utils::errors::CompilationUtilError;
Expand All @@ -7,6 +9,8 @@ use apollo_compilation_utils::resource_limits::ResourceLimits;
use apollo_compile_to_native_types::SierraCompilationConfig;
use cairo_lang_starknet_classes::contract_class::ContractClass;
use cairo_native::executor::AotContractExecutor;
#[cfg(feature = "with-libfunc-profiling")]
use cairo_native::executor::AotWithProgram;
use tempfile::NamedTempFile;

use crate::constants::CAIRO_NATIVE_BINARY_NAME;
Expand Down Expand Up @@ -54,6 +58,27 @@ impl SierraToNativeCompiler {
.map_err(|e| CompilationUtilError::CompilationError(e.to_string()))?
.unwrap())
}

/// Like [`Self::compile`], but also returns the Sierra program so cairo-native's
/// libfunc profiler can resolve runtime libfunc IDs back to declarations. The
/// program is extracted from `contract_class` before compilation so the two are
/// guaranteed to correspond.
#[cfg(feature = "with-libfunc-profiling")]
pub fn compile_with_program(
&self,
contract_class: ContractClass,
) -> Result<AotWithProgram, CompilationUtilError> {
let program = contract_class
.extract_sierra_program(false)
.map(|extracted| Arc::new(extracted.program))
.map_err(|err| {
CompilationUtilError::UnexpectedError(format!(
"Failed to extract Sierra program for profiling: {err}"
))
})?;
let executor = self.compile(contract_class)?;
Ok(AotWithProgram { executor, program })
}
}

// Returns the OUT_DIR. This function is only operable at run time.
Expand Down
12 changes: 12 additions & 0 deletions crates/blockifier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,21 @@ native_blockifier = []
node_api = []
only-native = ["cairo_native"]
reexecution = ["transaction_serde"]
# Enables the sierra-emu execution path of `cairo_native::ContractExecutor`. Pulls in
# cairo-native's `sierra-emu` feature which compiles `SierraEmuSyscallBridge`.
sierra-emu = ["cairo-native/sierra-emu", "cairo_native"]
testing = ["blockifier_test_utils", "rand", "rstest", "rstest_reuse", "starknet_api/testing"]
tracing = []
transaction_serde = []
# Enables libfunc-level profiling for native execution. Pulls in cairo-native's
# `with-libfunc-profiling` so `ContractExecutor::run_with_profile` is available,
# and apollo_compile_to_native's matching feature so `SierraToNativeCompiler` can
# pair the compiled executor with its Sierra program in one call.
with-libfunc-profiling = [
"apollo_compile_to_native/with-libfunc-profiling",
"cairo-native/with-libfunc-profiling",
"cairo_native",
]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
3 changes: 3 additions & 0 deletions crates/blockifier/src/execution/native.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
pub mod contract_class;
pub mod entry_point_execution;
#[cfg(feature = "with-libfunc-profiling")]
pub mod profiling;
mod run_dispatch;
pub mod syscall_handler;
pub mod utils;

Expand Down
21 changes: 17 additions & 4 deletions crates/blockifier/src/execution/native/contract_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use std::borrow::Cow;
use std::ops::Deref;
use std::sync::Arc;

use cairo_native::executor::AotContractExecutor;
#[cfg(feature = "with-libfunc-profiling")]
use cairo_native::executor::AotWithProgram;
use cairo_native::executor::{AotContractExecutor, ContractExecutor};
use starknet_api::contract_class::compiled_class_hash::HashableCompiledClass;
use starknet_api::core::EntryPointSelector;
use starknet_types_core::felt::Felt;
Expand Down Expand Up @@ -30,7 +32,18 @@ impl NativeCompiledClassV1 {
/// executor must be derived from sierra_program which in turn must be derived from
/// sierra_contract_class.
pub fn new(executor: AotContractExecutor, casm: CompiledClassV1) -> NativeCompiledClassV1 {
let contract = NativeCompiledClassV1Inner::new(executor, casm);
let contract = NativeCompiledClassV1Inner::new(executor.into(), casm);

Self(Arc::new(contract))
}

/// Like [`Self::new`], but also stores the Sierra `Program` (via the cairo-native
/// [`AotWithProgram`] pairing) so [`cairo_native::ContractExecutor::run_with_profile`]
/// can resolve libfunc samples. Only callable when the `with-libfunc-profiling`
/// feature is enabled.
#[cfg(feature = "with-libfunc-profiling")]
pub fn new_with_program(info: AotWithProgram, casm: CompiledClassV1) -> NativeCompiledClassV1 {
let contract = NativeCompiledClassV1Inner::new(info.into(), casm);

Self(Arc::new(contract))
}
Expand Down Expand Up @@ -71,12 +84,12 @@ impl HashableCompiledClass<EntryPointV1, NestedFeltCounts> for NativeCompiledCla

#[derive(Debug)]
pub struct NativeCompiledClassV1Inner {
pub executor: AotContractExecutor,
pub executor: ContractExecutor,
casm: CompiledClassV1,
}

impl NativeCompiledClassV1Inner {
fn new(executor: AotContractExecutor, casm: CompiledClassV1) -> Self {
fn new(executor: ContractExecutor, casm: CompiledClassV1) -> Self {
NativeCompiledClassV1Inner { executor, casm }
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use crate::execution::entry_point::{
};
use crate::execution::errors::{EntryPointExecutionError, PostExecutionError, PreExecutionError};
use crate::execution::native::contract_class::NativeCompiledClassV1;
use crate::execution::native::run_dispatch::run_native_executor;
use crate::execution::native::syscall_handler::NativeSyscallHandler;
use crate::state::state_api::State;
use crate::transaction::objects::ExecutionResourcesTraits;
Expand Down Expand Up @@ -59,11 +60,12 @@ pub fn execute_entry_point_call(
.checked_sub(initial_budget)
.ok_or(PreExecutionError::InsufficientEntryPointGas)?;

let execution_result = compiled_class.executor.run(
let execution_result = run_native_executor(
&compiled_class.executor,
entry_point.selector.0,
&syscall_handler.base.call.calldata.0.clone(),
call_initial_gas,
Some(builtin_costs),
builtin_costs,
&mut syscall_handler,
);

Expand Down
Loading