Skip to content

Commit

Permalink
feat(cairo_native): add batcher compiler struct
Browse files Browse the repository at this point in the history
  • Loading branch information
avi-starkware committed Nov 21, 2024
1 parent 0459bde commit a12f54d
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 6 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ clap = "4.5.4"
colored = "2.1.0"
const_format = "0.2.30"
criterion = "0.5.1"
crossbeam-channel = "0.5.13"
deadqueue = "0.2.4"
defaultmap = "0.5.0"
derive_more = "0.99.17"
Expand Down
4 changes: 3 additions & 1 deletion crates/blockifier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ description = "The transaction-executing component in the Starknet sequencer."
workspace = true

[features]
cairo_native = ["dep:cairo-native"]
cairo_native = ["crossbeam-channel", "dep:cairo-native", "starknet_sierra_compile"]
jemalloc = ["dep:tikv-jemallocator"]
reexecution = ["transaction_serde"]
testing = ["rand", "rstest", "starknet_api/testing"]
Expand All @@ -30,6 +30,7 @@ cairo-lang-runner.workspace = true
cairo-lang-starknet-classes.workspace = true
cairo-native = { workspace = true, optional = true }
cairo-vm.workspace = true
crossbeam-channel = { workspace = true, optional = true }
derive_more.workspace = true
indexmap.workspace = true
itertools.workspace = true
Expand All @@ -50,6 +51,7 @@ serde_json = { workspace = true, features = ["arbitrary_precision"] }
sha2.workspace = true
starknet-types-core.workspace = true
starknet_api.workspace = true
starknet_sierra_compile = { workspace = true, optional = true }
strum.workspace = true
strum_macros.workspace = true
tempfile.workspace = true
Expand Down
1 change: 1 addition & 0 deletions crates/blockifier/src/execution/native.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod contract_class;
pub mod contract_class_manager;
pub mod entry_point_execution;
pub mod syscall_handler;
pub mod utils;
Expand Down
63 changes: 63 additions & 0 deletions crates/blockifier/src/execution/native/contract_class_manager.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use std::sync::Arc;

use crossbeam_channel::{Receiver, Sender, TrySendError};
use log::error;
use starknet_api::core::ClassHash;
use starknet_api::state::ContractClass as SierraContractClass;
use starknet_sierra_compile::command_line_compiler::CommandLineCompiler;
use starknet_sierra_compile::SierraToNativeCompiler;

use crate::execution::contract_class::ContractClassV1;
use crate::execution::native::contract_class::NativeContractClassV1;
use crate::state::global_cache::{CachedCairoNative, ContractClassCaches};

type CompilationRequest = (ClassHash, Arc<SierraContractClass>, ContractClassV1);

struct ContractClassManager {
contract_class_caches: ContractClassCaches,
sender: Sender<CompilationRequest>,
receiver: Receiver<CompilationRequest>,
compiler: CommandLineCompiler,
}

impl ContractClassManager {
pub fn new(
contract_class_caches: ContractClassCaches,
sender: Sender<CompilationRequest>,
receiver: Receiver<CompilationRequest>,
compiler: CommandLineCompiler,
) -> Self {
Self { contract_class_caches, sender, receiver, compiler }
}

pub fn process_compilation_requests(&self) {
for (class_hash, sierra, casm) in self.receiver.iter() {
if self.contract_class_caches.get_native(&class_hash).is_some() {
// The contract class is already compiled to native - skip the compilation.
continue;
}
// TODO(Avi): Convert `sierra_contract_class` to
// `cairo_lang_starknet_classes::contract_class::ContractClass`
let compilation_result = self.compiler.compile_to_native(sierra.into());
match compilation_result {
Ok(executor) => {
let native_contract_class = NativeContractClassV1::new(executor, casm);
self.contract_class_caches
.set_native(class_hash, CachedCairoNative::Compiled(native_contract_class));
}
Err(err) => {
error!("Error compiling contract class: {}", err);
self.contract_class_caches
.set_native(class_hash, CachedCairoNative::CompilationFailed);
}
}
}
}

pub fn try_send_compilation_request(
&self,
compilation_request: CompilationRequest,
) -> Result<(), TrySendError<CompilationRequest>> {
Ok(self.sender.try_send(compilation_request)?)
}
}
10 changes: 5 additions & 5 deletions crates/blockifier/src/state/global_cache.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use std::sync::{Arc, Mutex, MutexGuard};

use cached::{Cached, SizedCache};
#[cfg(feature = "cairo_native")]
use cairo_native::executor::AotContractExecutor;
use starknet_api::core::ClassHash;
#[cfg(feature = "cairo_native")]
use starknet_api::state::ContractClass as SierraContractClass;

#[cfg(feature = "cairo_native")]
use crate::execution::contract_class::RunnableContractClass;
#[cfg(feature = "cairo_native")]
use crate::execution::native::contract_class::NativeContractClassV1;

type ContractClassLRUCache<T> = SizedCache<ClassHash, T>;
pub type LockedContractClassCache<'a, T> = MutexGuard<'a, ContractClassLRUCache<T>>;
Expand All @@ -21,7 +21,7 @@ pub struct GlobalContractCache<T: Clone>(pub Arc<Mutex<ContractClassLRUCache<T>>
#[cfg(feature = "cairo_native")]
#[derive(Debug, Clone)]
pub enum CachedCairoNative {
Compiled(AotContractExecutor),
Compiled(NativeContractClassV1),
CompilationFailed,
}

Expand Down Expand Up @@ -52,14 +52,14 @@ impl<T: Clone> GlobalContractCache<T> {
}

#[cfg(feature = "cairo_native")]
pub struct GlobalContractCacheManager {
pub struct ContractClassCaches {
pub casm_cache: GlobalContractCache<RunnableContractClass>,
pub native_cache: GlobalContractCache<CachedCairoNative>,
pub sierra_cache: GlobalContractCache<Arc<SierraContractClass>>,
}

#[cfg(feature = "cairo_native")]
impl GlobalContractCacheManager {
impl ContractClassCaches {
pub fn get_casm(&self, class_hash: &ClassHash) -> Option<RunnableContractClass> {
self.casm_cache.get(class_hash)
}
Expand Down

0 comments on commit a12f54d

Please sign in to comment.