Skip to content

Commit

Permalink
chore(cairo_native): allow disabling native compilation in the contra…
Browse files Browse the repository at this point in the history
…ct class manager (#2703)
  • Loading branch information
avi-starkware authored Dec 17, 2024
1 parent d7be314 commit aa71cff
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions crates/blockifier/src/state/contract_class_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use starknet_sierra_compile::config::SierraToCasmCompilationConfig;
use starknet_sierra_compile::utils::into_contract_class_for_compilation;
use starknet_sierra_compile::SierraToNativeCompiler;

use crate::blockifier::config::ContractClassManagerConfig;
use crate::execution::contract_class::{CompiledClassV1, RunnableCompiledClass};
use crate::execution::native::contract_class::NativeCompiledClassV1;
use crate::state::global_cache::{CachedCairoNative, ContractCaches};
Expand All @@ -21,25 +22,31 @@ const CHANNEL_SIZE: usize = 1000;
/// * `class_hash` - used to identify the contract class in the cache.
/// * `sierra_contract_class` - the sierra contract class to be compiled.
/// * `casm_compiled_class` - stored in [`NativeCompiledClassV1`] to allow fallback to cairo_vm
/// execution in case of unxecpected failure during native execution.
/// execution in case of unexpected failure during native execution.
type CompilationRequest = (ClassHash, Arc<SierraContractClass>, CompiledClassV1);

/// Manages the global cache of contract classes and handles sierra-to-native compilation requests.
struct ContractClassManager {
// The global cache of contract classes: casm, sierra, and native.
config: ContractClassManagerConfig,
/// The global cache of contract classes: casm, sierra, and native.
contract_caches: Arc<ContractCaches>,
// The sending half of the compilation request channel.
sender: SyncSender<CompilationRequest>,
/// The sending half of the compilation request channel. Set to `None` if native compilation is
/// disabled.
sender: Option<SyncSender<CompilationRequest>>,
}

#[allow(dead_code)]
impl ContractClassManager {
/// Creates a new contract class manager and spawns a thread that listens for compilation
/// requests and processes them (a.k.a. the compilation worker).
/// Returns the contract class manager.
pub fn start(contract_caches: ContractCaches) -> ContractClassManager {
pub fn start(config: ContractClassManagerConfig) -> ContractClassManager {
// TODO(Avi, 15/12/2024): Add the size of the channel to the config.
let contract_caches = Arc::new(contract_caches);
let contract_caches = Arc::new(ContractCaches::new(config.contract_cache_size));
if !config.run_cairo_native {
// Native compilation is disabled - no need to start the compilation worker.
return ContractClassManager { config, contract_caches, sender: None };
}
let (sender, receiver) = sync_channel(CHANNEL_SIZE);
let compiler_config = SierraToCasmCompilationConfig::default();
let compiler = CommandLineCompiler::new(compiler_config);
Expand All @@ -51,15 +58,17 @@ impl ContractClassManager {
move || run_compilation_worker(contract_caches, receiver, compiler)
});

ContractClassManager { contract_caches, sender }
ContractClassManager { config, contract_caches, sender: Some(sender) }
}

/// Sends a compilation request to the compilation worker. Does not block the sender. Logs an
/// error is the channel is full.
/// error if the channel is full.
pub fn send_compilation_request(&self, request: CompilationRequest) {
assert!(!self.config.run_cairo_native, "Native compilation is disabled.");
let sender = self.sender.as_ref().expect("Compilation channel not available.");
self.cache_request_contracts(&request);
// TODO(Avi, 15/12/2024): Check for duplicated requests.
self.sender.try_send(request).unwrap_or_else(|err| match err {
sender.try_send(request).unwrap_or_else(|err| match err {
TrySendError::Full((class_hash, _, _)) => {
error!(
"Compilation request channel is full (size: {}). Compilation request for \
Expand Down Expand Up @@ -88,6 +97,11 @@ impl ContractClassManager {
self.contract_caches.get_casm(class_hash)
}

/// Sets the casm compiled class for the given class hash in the cache.
pub fn set_casm(&self, class_hash: ClassHash, compiled_class: RunnableCompiledClass) {
self.contract_caches.set_casm(class_hash, compiled_class);
}

/// Caches the sierra and casm contract classes of a compilation request.
fn cache_request_contracts(&self, request: &CompilationRequest) {
let (class_hash, sierra, casm) = request.clone();
Expand Down

0 comments on commit aa71cff

Please sign in to comment.