-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
1,281 additions
and
578 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,36 @@ | ||
export MLIR_SYS_170_PREFIX=/opt/homebrew/opt/llvm@17 | ||
export LLVM_SYS_170_PREFIX=/opt/homebrew/opt/llvm@17 | ||
export TABLEGEN_170_PREFIX=/opt/homebrew/opt/llvm@17 | ||
#!/bin/sh | ||
|
||
# Taken from `lambdaclass/cairo_native`. | ||
# | ||
# It sets the LLVM environment variables. | ||
# | ||
# You can copy this file to .envrc/.env and adapt it for your environment. | ||
|
||
case $(uname) in | ||
Darwin) | ||
# If installed with brew | ||
LIBRARY_PATH=/opt/homebrew/lib | ||
MLIR_SYS_190_PREFIX="$(brew --prefix llvm@19)" | ||
LLVM_SYS_191_PREFIX="$(brew --prefix llvm@19)" | ||
TABLEGEN_190_PREFIX="$(brew --prefix llvm@19)" | ||
CAIRO_NATIVE_RUNTIME_LIBRARY="$(pwd)/target/release/libcairo_native_runtime.a" | ||
|
||
export LIBRARY_PATH | ||
export MLIR_SYS_190_PREFIX | ||
export LLVM_SYS_191_PREFIX | ||
export TABLEGEN_190_PREFIX | ||
export CAIRO_NATIVE_RUNTIME_LIBRARY | ||
;; | ||
Linux) | ||
# If installed from Debian/Ubuntu repository: | ||
MLIR_SYS_190_PREFIX=/usr/lib/llvm-19 | ||
LLVM_SYS_191_PREFIX=/usr/lib/llvm-19 | ||
TABLEGEN_190_PREFIX=/usr/lib/llvm-19 | ||
CAIRO_NATIVE_RUNTIME_LIBRARY="$(pwd)/target/release/libcairo_native_runtime.a" | ||
|
||
export MLIR_SYS_190_PREFIX | ||
export LLVM_SYS_191_PREFIX | ||
export TABLEGEN_190_PREFIX | ||
export CAIRO_NATIVE_RUNTIME_LIBRARY | ||
;; | ||
esac |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
crates/katana/executor/src/implementation/blockifier/cache.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
use std::collections::HashMap; | ||
use std::str::FromStr; | ||
use std::sync::{Arc, LazyLock}; | ||
|
||
use blockifier::execution::contract_class::{CompiledClassV1, RunnableCompiledClass}; | ||
use blockifier::execution::native::contract_class::NativeCompiledClassV1; | ||
use cairo_native::executor::AotContractExecutor; | ||
use cairo_native::OptLevel; | ||
use katana_cairo::starknet_api::contract_class::SierraVersion; | ||
use katana_primitives::class::{ClassHash, CompiledClass, ContractClass}; | ||
use parking_lot::Mutex; | ||
use quick_cache::sync::Cache; | ||
use rayon::ThreadPoolBuilder; | ||
use tracing::trace; | ||
|
||
use super::utils::to_class; | ||
|
||
pub static COMPILED_CLASS_CACHE: LazyLock<ClassCache> = | ||
LazyLock::new(|| ClassCache::new().unwrap()); | ||
|
||
#[derive(Debug, thiserror::Error)] | ||
pub enum Error { | ||
#[cfg(feature = "cairo-native")] | ||
#[error(transparent)] | ||
FailedToCreateThreadPool(#[from] rayon::ThreadPoolBuildError), | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct ClassCache { | ||
inner: Arc<Inner>, | ||
} | ||
|
||
#[derive(Debug)] | ||
struct Inner { | ||
#[cfg(feature = "cairo-native")] | ||
pool: rayon::ThreadPool, | ||
cache: Cache<ClassHash, RunnableCompiledClass>, | ||
} | ||
|
||
impl ClassCache { | ||
pub fn new() -> Result<Self, Error> { | ||
const CACHE_SIZE: usize = 100; | ||
let cache = Cache::new(CACHE_SIZE); | ||
|
||
#[cfg(feature = "cairo-native")] | ||
let pool = ThreadPoolBuilder::new() | ||
.num_threads(3) | ||
.thread_name(|i| format!("cache-native-compiler-{i}")) | ||
.build()?; | ||
|
||
Ok(Self { | ||
inner: Arc::new(Inner { | ||
cache, | ||
#[cfg(feature = "cairo-native")] | ||
pool, | ||
}), | ||
}) | ||
} | ||
|
||
pub fn get(&self, hash: &ClassHash) -> Option<RunnableCompiledClass> { | ||
self.inner.cache.get(hash) | ||
} | ||
|
||
pub fn insert(&self, hash: ClassHash, class: ContractClass) -> RunnableCompiledClass { | ||
match class { | ||
ContractClass::Legacy(..) => { | ||
let class = class.compile().unwrap(); | ||
let class = to_class(class).unwrap(); | ||
self.inner.cache.insert(hash, class.clone()); | ||
class | ||
} | ||
|
||
ContractClass::Class(ref sierra) => { | ||
#[cfg(feature = "cairo-native")] | ||
let program = sierra.extract_sierra_program().unwrap(); | ||
#[cfg(feature = "cairo-native")] | ||
let entry_points = sierra.entry_points_by_type.clone(); | ||
|
||
let CompiledClass::Class(casm) = class.compile().unwrap() else { | ||
unreachable!("cant be legacy") | ||
}; | ||
|
||
let version = SierraVersion::from_str(&casm.compiler_version).unwrap(); | ||
let compiled = CompiledClassV1::try_from((casm, version)).unwrap(); | ||
|
||
#[cfg(feature = "cairo-native")] | ||
let inner = self.inner.clone(); | ||
#[cfg(feature = "cairo-native")] | ||
let compiled_clone = compiled.clone(); | ||
|
||
#[cfg(feature = "cairo-native")] | ||
self.inner.pool.spawn(move || { | ||
trace!(target: "class_cache", class = format!("{hash:#x}"), "Compiling native class"); | ||
|
||
let executor = | ||
AotContractExecutor::new(&program, &entry_points, OptLevel::Default) | ||
.unwrap(); | ||
|
||
let native = NativeCompiledClassV1::new(executor, compiled_clone); | ||
inner.cache.insert(hash, RunnableCompiledClass::V1Native(native)); | ||
|
||
trace!(target: "class_cache", class = format!("{hash:#x}"), "Native class compiled") | ||
}); | ||
|
||
let class = RunnableCompiledClass::V1(compiled); | ||
self.inner.cache.insert(hash, class.clone()); | ||
|
||
class | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.