Skip to content

Commit

Permalink
Convert sierra for compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
avi-starkware committed Nov 28, 2024
1 parent 07e49c2 commit fd20b27
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions crates/blockifier/src/state/contract_class_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ use std::thread::JoinHandle;

use log::{error, info};
use starknet_api::core::ClassHash;
use starknet_api::state::ContractClass as SierraContractClass;
use starknet_api::state::SierraContractClass;
use starknet_sierra_compile::command_line_compiler::CommandLineCompiler;
use starknet_sierra_compile::config::SierraToCasmCompilationConfig;
use starknet_sierra_compile::utils::into_contract_class_for_compilation;
use starknet_sierra_compile::SierraToNativeCompiler;

use crate::execution::contract_class::{ContractClassV1, RunnableContractClass};
use crate::execution::native::contract_class::NativeContractClassV1;
use crate::execution::contract_class::{CompiledClassV1, RunnableCompiledClass};
use crate::execution::native::contract_class::NativeCompiledClassV1;
use crate::state::global_cache::{CachedCairoNative, ContractCaches};

const CHANNEL_SIZE: usize = 1000;
Expand All @@ -21,9 +22,9 @@ const CHANNEL_SIZE: usize = 1000;
/// # Fields:
/// * `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 [`NativeContractClassV1`] to allow fallback to cairo_vm
/// * `casm_compiled_class` - stored in [`NativeCompiledClassV1`] to allow fallback to cairo_vm
/// execution in case of unxecpected failure during native execution.
type CompilationRequest = (ClassHash, Arc<SierraContractClass>, ContractClassV1);
type CompilationRequest = (ClassHash, Arc<SierraContractClass>, CompiledClassV1);

/// Manages the global cache of contract classes and handles sierra-to-native compilation requests.
struct ContractClassManager {
Expand All @@ -37,6 +38,7 @@ struct ContractClassManager {
join_handle: JoinHandle<()>,
}

#[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).
Expand All @@ -51,7 +53,9 @@ impl ContractClassManager {

let join_handle = std::thread::spawn({
let contract_caches = Arc::clone(&contract_caches);
let compiler = Arc::new(compiler);
let stop_marker = Arc::clone(&stop_marker);

move || run_compilation_worker(contract_caches, receiver, compiler, stop_marker)
});

Expand All @@ -68,7 +72,7 @@ impl ContractClassManager {
pub fn send_compilation_request(&self, request: CompilationRequest) {
self.cache_request_contracts(&request);
// TODO(Avi, 15/12/2024): Check for duplicated requests.
self.sender.try_send(request).map_err(|err| match err {
self.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 All @@ -93,7 +97,7 @@ impl ContractClassManager {
}

/// Returns the casm compiled class for the given class hash, if it exists in cache.
pub fn get_casm(&self, class_hash: &ClassHash) -> Option<RunnableContractClass> {
pub fn get_casm(&self, class_hash: &ClassHash) -> Option<RunnableCompiledClass> {
self.contract_caches.get_casm(class_hash)
}

Expand All @@ -106,7 +110,7 @@ impl ContractClassManager {
fn cache_request_contracts(&self, request: &CompilationRequest) {
let (class_hash, sierra, casm) = request.clone();
self.contract_caches.set_sierra(class_hash, sierra);
let cached_casm = RunnableContractClass::from(casm);
let cached_casm = RunnableCompiledClass::from(casm);
self.contract_caches.set_casm(class_hash, cached_casm);
}
}
Expand All @@ -117,7 +121,7 @@ impl ContractClassManager {
fn run_compilation_worker(
contract_caches: Arc<ContractCaches>,
receiver: Receiver<CompilationRequest>,
compiler: CommandLineCompiler,
compiler: Arc<dyn SierraToNativeCompiler>,
stop_marker: Arc<AtomicBool>,
) {
info!("Compilation worker started.");
Expand All @@ -130,12 +134,11 @@ fn run_compilation_worker(
// 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 = compiler.compile_to_native(sierra.into());
let sierra_for_compilation = into_contract_class_for_compilation(sierra.as_ref());
let compilation_result = compiler.compile_to_native(sierra_for_compilation);
match compilation_result {
Ok(executor) => {
let native_compiled_class = NativeContractClassV1::new(executor, casm);
let native_compiled_class = NativeCompiledClassV1::new(executor, casm);
contract_caches
.set_native(class_hash, CachedCairoNative::Compiled(native_compiled_class));
}
Expand Down

0 comments on commit fd20b27

Please sign in to comment.