Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small refactor and util for OptLevel #570

Merged
merged 3 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 2 additions & 9 deletions src/bin/cairo-native-compile.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::Context;
use cairo_native::{
context::NativeContext, module_to_object, object_to_shared_lib,
utils::cairo_to_sierra_with_debug_info, OptLevel,
utils::cairo_to_sierra_with_debug_info,
};
use clap::{Parser, ValueEnum};
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -60,13 +60,6 @@ fn main() -> anyhow::Result<()> {
.compile(&sierra_program, Some(debug_locations))
.unwrap();

let opt_level = match args.opt_level {
0 => OptLevel::None,
1 => OptLevel::Less,
2 => OptLevel::Default,
_ => OptLevel::Aggressive,
};

let output_mlir = args
.output_mlir
.unwrap_or_else(|| PathBuf::from("out.mlir"));
Expand All @@ -78,7 +71,7 @@ fn main() -> anyhow::Result<()> {
.context("Failed to write output.")?;

if let Some(output_library) = &args.output_library {
let object_data = module_to_object(native_module.module(), opt_level)
let object_data = module_to_object(native_module.module(), args.opt_level.into())
.context("Failed to convert module to object.")?;
object_to_shared_lib(&object_data, output_library)
.context("Failed to write shared library.")?;
Expand Down
16 changes: 6 additions & 10 deletions src/bin/cairo-native-run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use cairo_native::{
executor::{AotNativeExecutor, JitNativeExecutor, NativeExecutor},
metadata::gas::{GasMetadata, MetadataComputationConfig},
values::JitValue,
OptLevel,
};
use clap::{Parser, ValueEnum};
use itertools::Itertools;
Expand Down Expand Up @@ -111,16 +110,13 @@ fn main() -> anyhow::Result<()> {
.compile(&sierra_program, Some(debug_locations))
.unwrap();

let opt_level = match args.opt_level {
0 => OptLevel::None,
1 => OptLevel::Less,
2 => OptLevel::Default,
_ => OptLevel::Aggressive,
};

let native_executor: NativeExecutor = match args.run_mode {
RunMode::Aot => AotNativeExecutor::from_native_module(native_module, opt_level).into(),
RunMode::Jit => JitNativeExecutor::from_native_module(native_module, opt_level).into(),
RunMode::Aot => {
AotNativeExecutor::from_native_module(native_module, args.opt_level.into()).into()
}
RunMode::Jit => {
JitNativeExecutor::from_native_module(native_module, args.opt_level.into()).into()
}
};

let gas_metadata =
Expand Down
16 changes: 6 additions & 10 deletions src/bin/cairo-native-test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use cairo_native::{
executor::{AotNativeExecutor, JitNativeExecutor, NativeExecutor},
metadata::gas::{GasMetadata, MetadataComputationConfig},
values::JitValue,
OptLevel,
};
use clap::{Parser, ValueEnum};
use colored::Colorize;
Expand Down Expand Up @@ -390,16 +389,13 @@ fn run_tests(
)
.unwrap();

let opt_level = match args.opt_level {
0 => OptLevel::None,
1 => OptLevel::Less,
2 => OptLevel::Default,
_ => OptLevel::Aggressive,
};

let native_executor: NativeExecutor = match args.run_mode {
RunMode::Aot => AotNativeExecutor::from_native_module(native_module, opt_level).into(),
RunMode::Jit => JitNativeExecutor::from_native_module(native_module, opt_level).into(),
RunMode::Aot => {
AotNativeExecutor::from_native_module(native_module, args.opt_level.into()).into()
}
RunMode::Jit => {
JitNativeExecutor::from_native_module(native_module, args.opt_level.into()).into()
}
};

let gas_metadata = GasMetadata::new(
Expand Down
7 changes: 2 additions & 5 deletions src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,6 @@ fn invoke_dynamic(
) -> ExecutionResult {
tracing::info!("Invoking function with signature: {function_signature:?}.");

let is_builtin = <CoreTypeConcrete as TypeBuilder>::is_builtin;
let is_zst = <CoreTypeConcrete as TypeBuilder>::is_zst;

let arena = Bump::new();
let mut invoke_data = ArgumentMapper::new(&arena, registry);

Expand Down Expand Up @@ -267,8 +264,8 @@ fn invoke_dynamic(
},
None => {}
},
_ if is_builtin(type_info) => {
if !is_zst(type_info, registry) {
_ if type_info.is_builtin() => {
if !type_info.is_zst(registry) {
let value = match &mut return_ptr {
Some(return_ptr) => unsafe { *read_value::<u64>(return_ptr) },
None => ret_registers[0],
Expand Down
33 changes: 33 additions & 0 deletions src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,39 @@ pub enum OptLevel {
Aggressive,
}

impl From<usize> for OptLevel {
fn from(value: usize) -> Self {
match value {
0 => OptLevel::None,
1 => OptLevel::Less,
2 => OptLevel::Default,
_ => OptLevel::Aggressive,
}
}
}

impl From<OptLevel> for usize {
fn from(val: OptLevel) -> Self {
match val {
OptLevel::None => 0,
OptLevel::Less => 1,
OptLevel::Default => 2,
OptLevel::Aggressive => 3,
}
}
}

impl From<u8> for OptLevel {
fn from(value: u8) -> Self {
match value {
0 => OptLevel::None,
1 => OptLevel::Less,
2 => OptLevel::Default,
_ => OptLevel::Aggressive,
}
}
}

/// Make sure to call
pub fn module_to_object(
module: &Module<'_>,
Expand Down
12 changes: 1 addition & 11 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,17 +224,7 @@ pub fn create_engine(
opt_level: OptLevel,
) -> ExecutionEngine {
// Create the JIT engine.
let engine = ExecutionEngine::new(
module,
match opt_level {
OptLevel::None => 0,
OptLevel::Less => 1,
OptLevel::Default => 2,
OptLevel::Aggressive => 3,
},
&[],
false,
);
let engine = ExecutionEngine::new(module, opt_level.into(), &[], false);

#[cfg(feature = "with-runtime")]
register_runtime_symbols(&engine);
Expand Down
Loading