Skip to content

Commit

Permalink
chore(blockifier): add native config- optimization level and panic if
Browse files Browse the repository at this point in the history
fail compilation
  • Loading branch information
avivg-starkware committed Feb 3, 2025
1 parent 2165f34 commit bc7dbfb
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 3 deletions.
20 changes: 20 additions & 0 deletions config/sequencer/default_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,16 @@
"privacy": "Public",
"value": 15728640
},
"batcher_config.contract_class_manager_config.native_compiler_config.optimization_level": {
"description": "The optimization level to use.",
"privacy": "Public",
"value": 3
},
"batcher_config.contract_class_manager_config.native_compiler_config.panic_on_compilation_failure": {
"description": "Whether to panic on compilation failure.",
"privacy": "Public",
"value": false
},
"batcher_config.contract_class_manager_config.native_compiler_config.sierra_to_native_compiler_path": {
"description": "The path to the Sierra-to-Native compiler binary.",
"privacy": "Public",
Expand Down Expand Up @@ -274,6 +284,16 @@
"privacy": "Public",
"value": 15728640
},
"compiler_config.optimization_level": {
"description": "The optimization level to use.",
"privacy": "Public",
"value": 3
},
"compiler_config.panic_on_compilation_failure": {
"description": "Whether to panic on compilation failure.",
"privacy": "Public",
"value": false
},
"compiler_config.sierra_to_native_compiler_path": {
"description": "The path to the Sierra-to-Native compiler binary.",
"privacy": "Public",
Expand Down
5 changes: 4 additions & 1 deletion crates/bin/starknet-native-compile/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,24 @@ struct Args {
path: PathBuf,
/// The output file path.
output: PathBuf,
/// The optimization level to use.
optimization_level: u8,
}

fn main() {
// TODO(Avi, 01/12/2024): Find a way to restrict time, memory and file size during compilation.
let args = Args::parse();
let path = args.path;
let output = args.output;
let optimization_level = args.optimization_level;

let (contract_class, sierra_program) = load_sierra_program_from_file(&path);

// TODO(Avi, 01/12/2024): Test different optimization levels for best performance.
let mut contract_executor = AotContractExecutor::new(
&sierra_program,
&contract_class.entry_points_by_type,
OptLevel::default(),
optimization_level.into(),
)
.unwrap_or_else(|err| {
eprintln!("Error compiling Sierra program: {}", err);
Expand Down
7 changes: 6 additions & 1 deletion crates/blockifier/src/state/contract_class_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,12 @@ fn process_compilation_request(
}
Err(err) => {
log::error!("Error compiling contract class: {}", err);
contract_caches.set_native(class_hash, CachedCairoNative::CompilationFailed);
match compiler.panic_on_compilation_failure() {
true => panic!("Compilation failed."),
false => {
contract_caches.set_native(class_hash, CachedCairoNative::CompilationFailed)
}
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ impl SierraToNativeCompiler for CommandLineCompiler {
let output_file_path = output_file.path().to_str().ok_or(
CompilationUtilError::UnexpectedError("Failed to get output file path".to_owned()),
)?;
let additional_args = [output_file_path];
let optimization_level = self.config.optimization_level.to_string();
let additional_args = [output_file_path, &optimization_level];
let resource_limits = ResourceLimits::new(
Some(self.config.max_cpu_time),
Some(self.config.max_native_bytecode_size),
Expand All @@ -94,6 +95,11 @@ impl SierraToNativeCompiler for CommandLineCompiler {

Ok(AotContractExecutor::load(Path::new(&output_file_path))?)
}

#[cfg(feature = "cairo_native")]
fn panic_on_compilation_failure(&self) -> bool {
self.config.panic_on_compilation_failure
}
}

fn compile_with_args(
Expand Down
3 changes: 3 additions & 0 deletions crates/starknet_sierra_multicompile/src/compile_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::config::{
DEFAULT_MAX_CPU_TIME,
DEFAULT_MAX_MEMORY_USAGE,
DEFAULT_MAX_NATIVE_BYTECODE_SIZE,
DEFAULT_OPTIMIZATION_LEVEL,
};
use crate::errors::CompilationUtilError;
use crate::test_utils::contract_class_from_file;
Expand All @@ -27,6 +28,8 @@ const SIERRA_COMPILATION_CONFIG: SierraCompilationConfig = SierraCompilationConf
max_native_bytecode_size: DEFAULT_MAX_NATIVE_BYTECODE_SIZE,
max_cpu_time: DEFAULT_MAX_CPU_TIME,
max_memory_usage: DEFAULT_MAX_MEMORY_USAGE,
optimization_level: DEFAULT_OPTIMIZATION_LEVEL,
panic_on_compilation_failure: false, // TODO(AvivG): what should be the default?
};

fn command_line_compiler() -> CommandLineCompiler {
Expand Down
17 changes: 17 additions & 0 deletions crates/starknet_sierra_multicompile/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub const DEFAULT_MAX_CASM_BYTECODE_SIZE: usize = 80 * 1024;
pub const DEFAULT_MAX_NATIVE_BYTECODE_SIZE: u64 = 15 * 1024 * 1024;
pub const DEFAULT_MAX_CPU_TIME: u64 = 20;
pub const DEFAULT_MAX_MEMORY_USAGE: u64 = 5 * 1024 * 1024 * 1024;
pub const DEFAULT_OPTIMIZATION_LEVEL: u8 = 3; //TODO(AvivG): what should be the default?

#[derive(Clone, Debug, Serialize, Deserialize, Validate, PartialEq)]
pub struct SierraCompilationConfig {
Expand All @@ -22,6 +23,8 @@ pub struct SierraCompilationConfig {
pub max_cpu_time: u64,
/// Compilation process’s virtual memory (address space) byte limit.
pub max_memory_usage: u64,
pub optimization_level: u8,
pub panic_on_compilation_failure: bool,
/// Sierra-to-Native compiler binary path.
pub sierra_to_native_compiler_path: Option<PathBuf>,
}
Expand All @@ -34,6 +37,8 @@ impl Default for SierraCompilationConfig {
max_native_bytecode_size: DEFAULT_MAX_NATIVE_BYTECODE_SIZE,
max_cpu_time: DEFAULT_MAX_CPU_TIME,
max_memory_usage: DEFAULT_MAX_MEMORY_USAGE,
optimization_level: DEFAULT_OPTIMIZATION_LEVEL,
panic_on_compilation_failure: false, // TODO(AvivG): what should be the default?
}
}
}
Expand Down Expand Up @@ -65,6 +70,18 @@ impl SerializeConfig for SierraCompilationConfig {
"Limitation of compilation process's virtual memory (bytes).",
ParamPrivacyInput::Public,
),
ser_param(
"optimization_level",
&self.optimization_level,
"The optimization level to use.",
ParamPrivacyInput::Public,
),
ser_param(
"panic_on_compilation_failure",
&self.panic_on_compilation_failure,
"Whether to panic on compilation failure.",
ParamPrivacyInput::Public,
),
]);
dump.extend(ser_optional_param(
&self.sierra_to_native_compiler_path,
Expand Down
3 changes: 3 additions & 0 deletions crates/starknet_sierra_multicompile/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,7 @@ pub trait SierraToNativeCompiler: Send + Sync {
&self,
contract_class: ContractClass,
) -> Result<AotContractExecutor, CompilationUtilError>;

#[cfg(feature = "cairo_native")]
fn panic_on_compilation_failure(&self) -> bool;
}

0 comments on commit bc7dbfb

Please sign in to comment.