Skip to content

Commit

Permalink
dev: bump cairo compiler (#412)
Browse files Browse the repository at this point in the history
* bump compiler dep to 2.4.1

* fix API changes

* implement u256_guarantee_inv_mod_n libfunc

* fix compiler to 2.4.2 and fix typos

* update makefile

* match_nullable_snapshot libfunc

* fix CI
  • Loading branch information
greged93 authored Jan 9, 2024
1 parent 21d421b commit 906a1a3
Show file tree
Hide file tree
Showing 14 changed files with 627 additions and 150 deletions.
193 changes: 104 additions & 89 deletions Cargo.lock

Large diffs are not rendered by default.

32 changes: 17 additions & 15 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ with-runtime = ["cairo-native-runtime"]

[dependencies]
bumpalo = "3.14"
cairo-lang-compiler = "2.3.1"
cairo-lang-defs = "2.3.1"
cairo-lang-diagnostics = "2.3.1"
cairo-lang-filesystem = "2.3.1"
cairo-lang-lowering = "2.3.1"
cairo-lang-sierra = "2.3.1"
cairo-lang-sierra-generator = "2.3.1"
cairo-lang-compiler = "2.4.2"
cairo-lang-defs = "2.4.2"
cairo-lang-diagnostics = "2.4.2"
cairo-lang-filesystem = "2.4.2"
cairo-lang-lowering = "2.4.2"
cairo-lang-sierra = "2.4.2"
cairo-lang-sierra-generator = "2.4.2"
educe = "0.5.5"
id-arena = "2.2"
itertools = "0.12"
Expand All @@ -44,34 +44,36 @@ melior = { version = "0.15.0", features = ["ods-dialects"] }
mlir-sys = "0.2.1"
num-bigint = "0.4.4"
num-traits = "0.2"
starknet-types-core = { version = "0.0.5", default-features = false, features = ["serde"] }
starknet-types-core = { version = "0.0.5", default-features = false, features = [
"serde",
] }
tempfile = "3.6"
thiserror = "1.0"
tracing = "0.1"


# CLI dependencies
cairo-lang-sierra-ap-change = "2.3.1"
cairo-lang-sierra-gas = "2.3.1"
cairo-lang-starknet = "2.3.1"
cairo-lang-utils = "2.3.1"
cairo-lang-sierra-ap-change = "2.4.2"
cairo-lang-sierra-gas = "2.4.2"
cairo-lang-starknet = "2.4.2"
cairo-lang-utils = "2.4.2"
cairo-native-runtime = { path = "runtime", optional = true }
clap = { version = "4.3", features = ["derive"], optional = true }
libloading = "0.8.1"
tracing-subscriber = { version = "0.3", features = [
"env-filter",
"env-filter",
], optional = true }

[dev-dependencies]
cairo-felt = "0.8.5"
cairo-lang-runner = "2.3.1"
cairo-lang-runner = "2.4.2"
criterion = { version = "0.5.1", features = ["html_reports"] }
lambdaworks-math = "0.1"
pretty_assertions_sorted = "1.2.3"
proptest = "1.2"
test-case = "3.2.1"
walkdir = "2"
serde_json = { version = "1.0"}
serde_json = { version = "1.0" }

[build-dependencies]
cc = "1.0"
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#

UNAME := $(shell uname)
CAIRO_2_VERSION=2.3.1
CAIRO_2_VERSION=2.4.2

check-llvm:
ifndef MLIR_SYS_170_PREFIX
Expand Down
5 changes: 2 additions & 3 deletions benches/benches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,15 @@ fn criterion_benchmark(c: &mut Criterion) {
fn load_contract(path: impl AsRef<Path>) -> Program {
let mut db = RootDatabase::builder().detect_corelib().build().unwrap();
let main_crate_ids = setup_project(&mut db, path.as_ref()).unwrap();
(*compile_prepared_db(
compile_prepared_db(
&mut db,
main_crate_ids,
CompilerConfig {
replace_ids: true,
..Default::default()
},
)
.unwrap())
.clone()
.unwrap()
}

criterion_group!(benches, criterion_benchmark);
Expand Down
6 changes: 4 additions & 2 deletions runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ edition = "2021"
crate-type = ["rlib", "cdylib", "staticlib"]

[dependencies]
starknet-types-core = { version = "0.0.5", default-features = false, features = ["serde"] }
cairo-lang-runner = "=2.3"
starknet-types-core = { version = "0.0.5", default-features = false, features = [
"serde",
] }
cairo-lang-runner = "=2.4.2"
libc = "0.2"
starknet-crypto = "0.6"
starknet-curve = "0.4"
Expand Down
13 changes: 8 additions & 5 deletions src/bin/cairo-native-compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use cairo_lang_compiler::{
compile_prepared_db, db::RootDatabase, diagnostics::DiagnosticsReporter,
project::setup_project, CompilerConfig,
};
use cairo_lang_defs::plugin::{NamedPlugin, PluginSuite};
use cairo_lang_sierra::{
extensions::core::{CoreLibfunc, CoreType},
program::Program,
Expand Down Expand Up @@ -105,15 +106,14 @@ fn load_program<'c>(
Some("cairo") if !is_contract => {
let mut db = RootDatabase::builder().detect_corelib().build()?;
let main_crate_ids = setup_project(&mut db, path)?;
let program = (*compile_prepared_db(
let program = compile_prepared_db(
&mut db,
main_crate_ids,
CompilerConfig {
replace_ids: true,
..Default::default()
},
)?)
.clone();
)?;

let debug_locations = if let Some(context) = context {
let debug_info = DebugInfo::extract(&db, &program).map_err(|_| {
Expand All @@ -131,10 +131,13 @@ fn load_program<'c>(
}
Some("cairo") if is_contract => {
// mimics cairo_lang_starknet::contract_class::compile_path
let mut plugins = PluginSuite::default();
plugins
.add_plugin::<StarkNetPlugin>()
.add_inline_macro_plugin_ex(SelectorMacro::NAME, Arc::new(SelectorMacro));
let mut db = RootDatabase::builder()
.detect_corelib()
.with_macro_plugin(Arc::new(StarkNetPlugin::default()))
.with_inline_macro_plugin(SelectorMacro::NAME, Arc::new(SelectorMacro))
.with_plugin_suite(plugins)
.build()?;

let main_crate_ids = setup_project(&mut db, Path::new(&path))?;
Expand Down
13 changes: 8 additions & 5 deletions src/bin/cairo-native-dump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use cairo_lang_compiler::{
compile_prepared_db, db::RootDatabase, diagnostics::DiagnosticsReporter,
project::setup_project, CompilerConfig,
};
use cairo_lang_defs::plugin::{NamedPlugin, PluginSuite};
use cairo_lang_sierra::{
extensions::core::{CoreLibfunc, CoreType},
program::Program,
Expand Down Expand Up @@ -93,15 +94,14 @@ fn load_program<'c>(
Some("cairo") if !is_contract => {
let mut db = RootDatabase::builder().detect_corelib().build()?;
let main_crate_ids = setup_project(&mut db, path)?;
let program = (*compile_prepared_db(
let program = compile_prepared_db(
&mut db,
main_crate_ids,
CompilerConfig {
replace_ids: true,
..Default::default()
},
)?)
.clone();
)?;

let debug_locations = if let Some(context) = context {
let debug_info = DebugInfo::extract(&db, &program).map_err(|_| {
Expand All @@ -119,10 +119,13 @@ fn load_program<'c>(
}
Some("cairo") if is_contract => {
// mimics cairo_lang_starknet::contract_class::compile_path
let mut plugins = PluginSuite::default();
plugins
.add_plugin::<StarkNetPlugin>()
.add_inline_macro_plugin_ex(SelectorMacro::NAME, Arc::new(SelectorMacro));
let mut db = RootDatabase::builder()
.detect_corelib()
.with_macro_plugin(Arc::new(StarkNetPlugin::default()))
.with_inline_macro_plugin(SelectorMacro::NAME, Arc::new(SelectorMacro))
.with_plugin_suite(plugins)
.build()?;

let main_crate_ids = setup_project(&mut db, Path::new(&path))?;
Expand Down
5 changes: 2 additions & 3 deletions src/bin/cairo-native-run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,14 @@ fn load_program(path: &Path) -> Result<Program, Box<dyn std::error::Error>> {
Some("cairo") => {
let mut db = RootDatabase::builder().detect_corelib().build()?;
let main_crate_ids = setup_project(&mut db, path)?;
(*compile_prepared_db(
compile_prepared_db(
&mut db,
main_crate_ids,
CompilerConfig {
replace_ids: true,
..Default::default()
},
)?)
.clone()
)?
}
Some("sierra") => {
let program_src = fs::read_to_string(path)?;
Expand Down
6 changes: 3 additions & 3 deletions src/libfuncs/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ where
{
let src_ty = registry.build_type(context, helper, registry, metadata, &info.from_ty)?;
let dst_ty = registry.build_type(context, helper, registry, metadata, &info.to_ty)?;
assert!(info.from_nbits >= info.to_nbits);
assert!(info.from_info.nbits >= info.to_info.nbits);

if info.from_nbits == info.to_nbits {
if info.from_info.nbits == info.to_info.nbits {
let k0 = entry
.append_operation(arith::constant(
context,
Expand Down Expand Up @@ -105,7 +105,7 @@ where
let n_bits = entry
.append_operation(arith::constant(
context,
IntegerAttribute::new(info.to_nbits.try_into()?, src_ty).into(),
IntegerAttribute::new(info.to_info.nbits.try_into()?, src_ty).into(),
location,
))
.result(0)?
Expand Down
75 changes: 75 additions & 0 deletions src/libfuncs/nullable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ where
NullableConcreteLibfunc::MatchNullable(info) => {
build_match_nullable(context, registry, entry, location, helper, metadata, info)
}
NullableConcreteLibfunc::MatchNullableSnapshot(info) => build_match_nullable_snapshot(
context, registry, entry, location, helper, metadata, info,
),
}
}

Expand Down Expand Up @@ -170,6 +173,26 @@ where
Ok(())
}

/// Generate MLIR operations for the `match_nullable_snapshot` libfunc.
#[allow(clippy::too_many_arguments)]
fn build_match_nullable_snapshot<'ctx, 'this, TType, TLibfunc>(
context: &'ctx Context,
registry: &ProgramRegistry<TType, TLibfunc>,
entry: &'this Block<'ctx>,
location: Location<'ctx>,
helper: &LibfuncHelper<'ctx, 'this>,
metadata: &mut MetadataStorage,
info: &SignatureAndTypeConcreteLibfunc,
) -> Result<()>
where
TType: GenericType,
TLibfunc: GenericLibfunc,
<TType as GenericType>::Concrete: TypeBuilder<TType, TLibfunc, Error = CoreTypeBuilderError>,
<TLibfunc as GenericLibfunc>::Concrete: LibfuncBuilder<TType, TLibfunc, Error = Error>,
{
build_match_nullable(context, registry, entry, location, helper, metadata, info)
}

#[cfg(test)]
mod test {
use crate::{
Expand Down Expand Up @@ -283,4 +306,56 @@ mod test {
),
);
}

#[test]
fn run_match_nullable_snapshot() {
let program = load_cairo!(
use nullable::{null, NullableTrait, FromNullableResult, match_nullable_snapshot};

fn run_test(x: u8) -> u8 {
let a = if x == 0 {
@null()
} else {
@NullableTrait::new(x)
};
let b = match match_nullable_snapshot(a) {
FromNullableResult::Null => 99_u8,
FromNullableResult::NotNull(value) => *(value.unbox()),
};
b
}
);

run_program_assert_output(&program, "run_test", &[4u8.into()], 4u8.into());
run_program_assert_output(&program, "run_test", &[0u8.into()], 99u8.into());
}

#[test]
fn run_match_nullable_snapshot_array() {
let program = load_cairo!(
use nullable::{null, NullableTrait, FromNullableResult, match_nullable_snapshot};
use array::ArrayTrait;

fn run_test() -> u8 {
let mut numbers = ArrayTrait::new();
numbers.append(4_u8);
numbers.append(3_u8);
numbers.append(2_u8);
numbers.append(1_u8);
let a = @NullableTrait::new(numbers);
let b = match match_nullable_snapshot(a) {
FromNullableResult::Null => 99_u8,
FromNullableResult::NotNull(value) => *(value.unbox())[0],
};
b
}
);

run_program_assert_output(
&program,
"run_test",
&[4u8.into()],
jit_enum!(0, jit_struct!(4u8.into())),
);
}
}
4 changes: 2 additions & 2 deletions src/libfuncs/sint16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,15 +210,15 @@ where
&[],
location,
));
// Check wether the result is positive to distinguish between undeflowing & overflowing results
// Check wether the result is positive to distinguish between underflowing & overflowing results
block_overflow.append_operation(helper.cond_br(
context,
is_positive,
[1, 2],
[&[range_check, op_result], &[range_check, op_result]],
location,
));
// No Oveflow/Underflow -> In range result
// No Overflow/Underflow -> In range result
block_not_overflow.append_operation(helper.br(0, &[range_check, op_result], location));
Ok(())
}
Expand Down
Loading

0 comments on commit 906a1a3

Please sign in to comment.