From 3c15f13f6ef6d961226546e45a596e30facab901 Mon Sep 17 00:00:00 2001 From: Esteve Soler Arderiu Date: Thu, 10 Oct 2024 19:06:33 +0200 Subject: [PATCH 01/19] Fix felt252 and enum deserialization bugs. --- src/executor.rs | 18 +++++++++++++----- src/executor/contract.rs | 5 ++++- src/values.rs | 30 ++++++++++++++++++++++++------ 3 files changed, 41 insertions(+), 12 deletions(-) diff --git a/src/executor.rs b/src/executor.rs index 8d5cdfabc..8e10ce037 100644 --- a/src/executor.rs +++ b/src/executor.rs @@ -361,11 +361,13 @@ fn parse_result( return Err(Error::ParseAttributeError); #[cfg(target_arch = "aarch64")] - Ok(Value::Felt252( - starknet_types_core::felt::Felt::from_bytes_le(unsafe { - std::mem::transmute::<&[u64; 4], &[u8; 32]>(&ret_registers) - }), - )) + Ok(Value::Felt252({ + let data = unsafe { + std::mem::transmute::<&mut [u64; 4], &mut [u8; 32]>(&mut ret_registers) + }; + data[31] &= 0x0F; // Filter out first 4 bits (they're outside an i252). + starknet_types_core::felt::Felt::from_bytes_le(data) + })) } }, CoreTypeConcrete::Bytes31(_) => match return_ptr { @@ -479,6 +481,12 @@ fn parse_result( } }; + // Filter out bits that are not part of the enum's tag. + let tag = tag + & 1usize + .wrapping_shl(info.variants.len().next_power_of_two().trailing_zeros()) + .wrapping_sub(1); + ( tag, Ok(unsafe { diff --git a/src/executor/contract.rs b/src/executor/contract.rs index 354b3e5bc..a41676ab7 100644 --- a/src/executor/contract.rs +++ b/src/executor/contract.rs @@ -355,6 +355,8 @@ impl AotContractExecutor { }; let tag = *unsafe { enum_ptr.cast::().as_ref() } as usize; + let tag &= 0x01; // Filter out bits that are not part of the enum's tag. + // layout of both enum variants, both are a array of felts let value_layout = unsafe { Layout::from_size_align_unchecked(24, 8) }; let value_ptr = unsafe { @@ -382,7 +384,8 @@ impl AotContractExecutor { for i in 0..num_elems { // safe to create a NonNull because if the array has elements, the data_ptr can't be null. let cur_elem_ptr = NonNull::new(unsafe { data_ptr.byte_add(elem_stride * i) }).unwrap(); - let data = unsafe { cur_elem_ptr.cast::<[u8; 32]>().as_ref() }; + let data = unsafe { cur_elem_ptr.cast::<[u8; 32]>().as_mut() }; + data[31] &= 0x0F; // Filter out first 4 bits (they're outside an i252). let data = Felt::from_bytes_le_slice(data); array_value.push(data); diff --git a/src/values.rs b/src/values.rs index 873b00cbb..f37af9122 100644 --- a/src/values.rs +++ b/src/values.rs @@ -577,11 +577,19 @@ impl Value { CoreTypeConcrete::EcPoint(_) => { let data = ptr.cast::<[[u8; 32]; 2]>().as_ref(); + data[0][31] &= 0x0F; // Filter out first 4 bits (they're outside an i252). + data[1][31] &= 0x0F; // Filter out first 4 bits (they're outside an i252). + Self::EcPoint(Felt::from_bytes_le(&data[0]), Felt::from_bytes_le(&data[1])) } CoreTypeConcrete::EcState(_) => { let data = ptr.cast::<[[u8; 32]; 4]>().as_ref(); + data[0][31] &= 0x0F; // Filter out first 4 bits (they're outside an i252). + data[1][31] &= 0x0F; // Filter out first 4 bits (they're outside an i252). + data[2][31] &= 0x0F; // Filter out first 4 bits (they're outside an i252). + data[3][31] &= 0x0F; // Filter out first 4 bits (they're outside an i252). + Self::EcState( Felt::from_bytes_le(&data[0]), Felt::from_bytes_le(&data[1]), @@ -590,7 +598,8 @@ impl Value { ) } CoreTypeConcrete::Felt252(_) => { - let data = ptr.cast::<[u8; 32]>().as_ref(); + let data = ptr.cast::<[u8; 32]>().as_mut(); + data[31] &= 0x0F; // Filter out first 4 bits (they're outside an i252). let data = Felt::from_bytes_le_slice(data); Self::Felt252(data) } @@ -645,6 +654,12 @@ impl Value { }, }; + // Filter out bits that are not part of the enum's tag. + let tag_value = tag_value + & 1usize + .wrapping_shl(info.variants.len().next_power_of_two().trailing_zeros()) + .wrapping_sub(1); + let payload_ty = registry.get_type(&info.variants[tag_value])?; let payload_layout = payload_ty.layout(registry)?; @@ -695,21 +710,23 @@ impl Value { ); let mut output_map = HashMap::with_capacity(inner.len()); - for (key, val_ptr) in inner.iter() { + for (mut key, val_ptr) in inner.into_iter() { if val_ptr.is_null() { continue; } - let key = Felt::from_bytes_le(key); + key[31] &= 0x0F; // Filter out first 4 bits (they're outside an i252). + + let key = Felt::from_bytes_le(&key); output_map.insert( key, Self::from_ptr( - NonNull::new(*val_ptr).unwrap().cast(), + NonNull::new(val_ptr).unwrap().cast(), &info.ty, registry, )?, ); - libc_free(*val_ptr); + libc_free(val_ptr); } Self::Felt252Dict { @@ -737,7 +754,8 @@ impl Value { | StarkNetTypeConcrete::StorageBaseAddress(_) | StarkNetTypeConcrete::StorageAddress(_) => { // felt values - let data = ptr.cast::<[u8; 32]>().as_ref(); + let data = ptr.cast::<[u8; 32]>().as_mut(); + data[31] &= 0x0F; // Filter out first 4 bits (they're outside an i252). let data = Felt::from_bytes_le(data); Self::Felt252(data) } From 45be8c17dfaacfdc23aa4609b07c5dec74f298d4 Mon Sep 17 00:00:00 2001 From: Esteve Soler Arderiu Date: Thu, 10 Oct 2024 19:12:19 +0200 Subject: [PATCH 02/19] Fix formatting. --- src/executor/contract.rs | 2 +- src/values.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/executor/contract.rs b/src/executor/contract.rs index a41676ab7..14c4130f2 100644 --- a/src/executor/contract.rs +++ b/src/executor/contract.rs @@ -355,7 +355,7 @@ impl AotContractExecutor { }; let tag = *unsafe { enum_ptr.cast::().as_ref() } as usize; - let tag &= 0x01; // Filter out bits that are not part of the enum's tag. + let tag = tag & 0x01; // Filter out bits that are not part of the enum's tag. // layout of both enum variants, both are a array of felts let value_layout = unsafe { Layout::from_size_align_unchecked(24, 8) }; diff --git a/src/values.rs b/src/values.rs index f37af9122..213360111 100644 --- a/src/values.rs +++ b/src/values.rs @@ -575,7 +575,7 @@ impl Value { value } CoreTypeConcrete::EcPoint(_) => { - let data = ptr.cast::<[[u8; 32]; 2]>().as_ref(); + let data = ptr.cast::<[[u8; 32]; 2]>().as_mut(); data[0][31] &= 0x0F; // Filter out first 4 bits (they're outside an i252). data[1][31] &= 0x0F; // Filter out first 4 bits (they're outside an i252). @@ -583,7 +583,7 @@ impl Value { Self::EcPoint(Felt::from_bytes_le(&data[0]), Felt::from_bytes_le(&data[1])) } CoreTypeConcrete::EcState(_) => { - let data = ptr.cast::<[[u8; 32]; 4]>().as_ref(); + let data = ptr.cast::<[[u8; 32]; 4]>().as_mut(); data[0][31] &= 0x0F; // Filter out first 4 bits (they're outside an i252). data[1][31] &= 0x0F; // Filter out first 4 bits (they're outside an i252). From 524f76f3f652fd60e7eff301078b7ef177e59004 Mon Sep 17 00:00:00 2001 From: Esteve Soler Arderiu Date: Thu, 10 Oct 2024 19:34:07 +0200 Subject: [PATCH 03/19] Also fix the runtime. --- runtime/src/lib.rs | 106 +++++++++++++++++++++++++++++---------------- 1 file changed, 69 insertions(+), 37 deletions(-) diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 2110b0c58..cca5cfd06 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -44,7 +44,8 @@ pub unsafe extern "C" fn cairo_native__libfunc__debug__print( let mut items = Vec::with_capacity(len as usize); for i in 0..len as usize { - let data = *data.add(i); + let mut data = *data.add(i); + data[31] &= 0x0F; // Filter out first 4 bits (they're outside an i252). let value = Felt::from_bytes_le(&data); items.push(value); @@ -76,22 +77,24 @@ pub unsafe extern "C" fn cairo_native__libfunc__debug__print( /// definitely unsafe to use manually. #[no_mangle] pub unsafe extern "C" fn cairo_native__libfunc__pedersen( - dst: *mut u8, - lhs: *const u8, - rhs: *const u8, + dst: &mut [u8; 32], + lhs: &[u8; 32], + rhs: &[u8; 32], ) { // Extract arrays from the pointers. - let dst = slice::from_raw_parts_mut(dst, 32); - let lhs = slice::from_raw_parts(lhs, 32); - let rhs = slice::from_raw_parts(rhs, 32); + let mut lhs = *lhs; + let mut rhs = *rhs; + + lhs[31] &= 0x0F; // Filter out first 4 bits (they're outside an i252). + rhs[31] &= 0x0F; // Filter out first 4 bits (they're outside an i252). // Convert to FieldElement. - let lhs = Felt::from_bytes_le_slice(lhs); - let rhs = Felt::from_bytes_le_slice(rhs); + let lhs = Felt::from_bytes_le(&lhs); + let rhs = Felt::from_bytes_le(&rhs); // Compute pedersen hash and copy the result into `dst`. let res = starknet_types_core::hash::Pedersen::hash(&lhs, &rhs); - dst.copy_from_slice(&res.to_bytes_le()); + *dst = res.to_bytes_le(); } /// Compute `hades_permutation(op0, op1, op2)` and replace the operands with the results. @@ -108,29 +111,28 @@ pub unsafe extern "C" fn cairo_native__libfunc__pedersen( /// definitely unsafe to use manually. #[no_mangle] pub unsafe extern "C" fn cairo_native__libfunc__hades_permutation( - op0: *mut u8, - op1: *mut u8, - op2: *mut u8, + op0: &mut [u8; 32], + op1: &mut [u8; 32], + op2: &mut [u8; 32], ) { - // Extract arrays from the pointers. - let op0 = slice::from_raw_parts_mut(op0, 32); - let op1 = slice::from_raw_parts_mut(op1, 32); - let op2 = slice::from_raw_parts_mut(op2, 32); + op0[31] &= 0x0F; // Filter out first 4 bits (they're outside an i252). + op1[31] &= 0x0F; // Filter out first 4 bits (they're outside an i252). + op2[31] &= 0x0F; // Filter out first 4 bits (they're outside an i252). // Convert to FieldElement. let mut state = [ - Felt::from_bytes_le_slice(op0), - Felt::from_bytes_le_slice(op1), - Felt::from_bytes_le_slice(op2), + Felt::from_bytes_le(op0), + Felt::from_bytes_le(op1), + Felt::from_bytes_le(op2), ]; // Compute Poseidon permutation. starknet_types_core::hash::Poseidon::hades_permutation(&mut state); // Write back the results. - op0.copy_from_slice(&state[0].to_bytes_le()); - op1.copy_from_slice(&state[1].to_bytes_le()); - op2.copy_from_slice(&state[2].to_bytes_le()); + *op0 = state[0].to_bytes_le(); + *op1 = state[1].to_bytes_le(); + *op2 = state[2].to_bytes_le(); } /// Felt252 type used in cairo native runtime @@ -230,8 +232,11 @@ pub unsafe extern "C" fn cairo_native__dict_get( dict: &mut FeltDict, key: &[u8; 32], ) -> *mut c_void { + let mut key = *key; + key[31] &= 0x0F; // Filter out first 4 bits (they're outside an i252). + dict.count += 1; - dict.inner.entry(*key).or_insert(std::ptr::null_mut()) as *mut _ as *mut c_void + dict.inner.entry(key).or_insert(std::ptr::null_mut()) as *mut _ as *mut c_void } /// Compute the total gas refund for the dictionary at squash time. @@ -260,6 +265,7 @@ pub unsafe extern "C" fn cairo_native__dict_gas_refund(ptr: *const FeltDict) -> pub unsafe extern "C" fn cairo_native__libfunc__ec__ec_point_from_x_nz( point_ptr: &mut [[u8; 32]; 2], ) -> bool { + point_ptr[0][31] &= 0x0F; // Filter out first 4 bits (they're outside an i252). let x = Felt::from_bytes_le(&point_ptr[0]); // https://github.com/starkware-libs/cairo/blob/aaad921bba52e729dc24ece07fab2edf09ccfa15/crates/cairo-lang-sierra-to-casm/src/invocations/ec.rs#L63 @@ -276,7 +282,7 @@ pub unsafe extern "C" fn cairo_native__libfunc__ec__ec_point_from_x_nz( match AffinePoint::new(x, y) { Ok(point) => { - point_ptr.as_mut()[1].copy_from_slice(&point.y().to_bytes_le()); + point_ptr[1] = point.y().to_bytes_le(); true } Err(_) => false, @@ -297,13 +303,16 @@ pub unsafe extern "C" fn cairo_native__libfunc__ec__ec_point_from_x_nz( pub unsafe extern "C" fn cairo_native__libfunc__ec__ec_point_try_new_nz( point_ptr: &mut [[u8; 32]; 2], ) -> bool { + point_ptr[0][31] &= 0x0F; // Filter out first 4 bits (they're outside an i252). + point_ptr[1][31] &= 0x0F; // Filter out first 4 bits (they're outside an i252). + let x = Felt::from_bytes_le(&point_ptr[0]); let y = Felt::from_bytes_le(&point_ptr[1]); match AffinePoint::new(x, y) { Ok(point) => { - point_ptr[0].copy_from_slice(&point.x().to_bytes_le()); - point_ptr[1].copy_from_slice(&point.y().to_bytes_le()); + point_ptr[0] = point.x().to_bytes_le(); + point_ptr[1] = point.y().to_bytes_le(); true } Err(_) => false, @@ -333,10 +342,10 @@ pub unsafe extern "C" fn cairo_native__libfunc__ec__ec_state_init(state_ptr: &mu // We already made sure its a valid point. let state = AffinePoint::new_unchecked(random_x, random_y); - state_ptr[0].copy_from_slice(&state.x().to_bytes_le()); - state_ptr[1].copy_from_slice(&state.y().to_bytes_le()); - state_ptr[2].copy_from_slice(&state.x().to_bytes_le()); - state_ptr[3].copy_from_slice(&state.y().to_bytes_le()); + state_ptr[0] = state.x().to_bytes_le(); + state_ptr[1] = state.y().to_bytes_le(); + state_ptr[2] = state_ptr[0]; + state_ptr[3] = state_ptr[1]; } /// Compute `ec_state_add(state, point)` and store the state back. @@ -354,6 +363,13 @@ pub unsafe extern "C" fn cairo_native__libfunc__ec__ec_state_add( state_ptr: &mut [[u8; 32]; 4], point_ptr: &[[u8; 32]; 2], ) { + state_ptr[0][31] &= 0x0F; // Filter out first 4 bits (they're outside an i252). + state_ptr[1][31] &= 0x0F; // Filter out first 4 bits (they're outside an i252). + + let mut point_ptr = *point_ptr; + point_ptr[0][31] &= 0x0F; // Filter out first 4 bits (they're outside an i252). + point_ptr[1][31] &= 0x0F; // Filter out first 4 bits (they're outside an i252). + // We use unchecked methods because the inputs must already be valid points. let mut state = ProjectivePoint::from_affine_unchecked( Felt::from_bytes_le(&state_ptr[0]), @@ -367,8 +383,8 @@ pub unsafe extern "C" fn cairo_native__libfunc__ec__ec_state_add( state += &point; let state = state.to_affine().unwrap(); - state_ptr[0].copy_from_slice(&state.x().to_bytes_le()); - state_ptr[1].copy_from_slice(&state.y().to_bytes_le()); + state_ptr[0] = state.x().to_bytes_le(); + state_ptr[1] = state.y().to_bytes_le(); } /// Compute `ec_state_add_mul(state, scalar, point)` and store the state back. @@ -387,6 +403,16 @@ pub unsafe extern "C" fn cairo_native__libfunc__ec__ec_state_add_mul( scalar_ptr: &[u8; 32], point_ptr: &[[u8; 32]; 2], ) { + state_ptr[0][31] &= 0x0F; // Filter out first 4 bits (they're outside an i252). + state_ptr[1][31] &= 0x0F; // Filter out first 4 bits (they're outside an i252). + + let mut point_ptr = *point_ptr; + point_ptr[0][31] &= 0x0F; // Filter out first 4 bits (they're outside an i252). + point_ptr[1][31] &= 0x0F; // Filter out first 4 bits (they're outside an i252). + + let scalar_ptr = *scalar_ptr; + scalar_ptr[31] &= 0x0F; // Filter out first 4 bits (they're outside an i252). + // Here the points should already be checked as valid, so we can use unchecked. let mut state = ProjectivePoint::from_affine_unchecked( Felt::from_bytes_le(&state_ptr[0]), @@ -401,8 +427,8 @@ pub unsafe extern "C" fn cairo_native__libfunc__ec__ec_state_add_mul( state += &point.mul(scalar); let state = state.to_affine().unwrap(); - state_ptr[0].copy_from_slice(&state.x().to_bytes_le()); - state_ptr[1].copy_from_slice(&state.y().to_bytes_le()); + state_ptr[0] = state.x().to_bytes_le(); + state_ptr[1] = state.y().to_bytes_le(); } /// Compute `ec_state_try_finalize_nz(state)` and store the result. @@ -420,6 +446,12 @@ pub unsafe extern "C" fn cairo_native__libfunc__ec__ec_state_try_finalize_nz( point_ptr: &mut [[u8; 32]; 2], state_ptr: &[[u8; 32]; 4], ) -> bool { + let mut state_ptr = *state_ptr; + state_ptr[0][31] &= 0x0F; // Filter out first 4 bits (they're outside an i252). + state_ptr[1][31] &= 0x0F; // Filter out first 4 bits (they're outside an i252). + state_ptr[2][31] &= 0x0F; // Filter out first 4 bits (they're outside an i252). + state_ptr[3][31] &= 0x0F; // Filter out first 4 bits (they're outside an i252). + // We use unchecked methods because the inputs must already be valid points. let state = ProjectivePoint::from_affine_unchecked( Felt::from_bytes_le(&state_ptr[0]), @@ -436,8 +468,8 @@ pub unsafe extern "C" fn cairo_native__libfunc__ec__ec_state_try_finalize_nz( let point = &state - &random; let point = point.to_affine().unwrap(); - point_ptr[0].copy_from_slice(&point.x().to_bytes_le()); - point_ptr[1].copy_from_slice(&point.y().to_bytes_le()); + point_ptr[0] = point.x().to_bytes_le(); + point_ptr[1] = point.y().to_bytes_le(); true } From 2bbd609a23ec8de1783c43441b9ef36672316582 Mon Sep 17 00:00:00 2001 From: Esteve Soler Arderiu Date: Fri, 11 Oct 2024 15:56:39 +0200 Subject: [PATCH 04/19] Fix errors. --- runtime/src/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index cca5cfd06..fabe8de68 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -13,7 +13,7 @@ use starknet_types_core::{ felt::Felt, hash::StarkHash, }; -use std::{collections::HashMap, ffi::c_void, fs::File, io::Write, os::fd::FromRawFd, slice}; +use std::{collections::HashMap, ffi::c_void, fs::File, io::Write, os::fd::FromRawFd}; use std::{ops::Mul, vec::IntoIter}; lazy_static! { @@ -410,7 +410,7 @@ pub unsafe extern "C" fn cairo_native__libfunc__ec__ec_state_add_mul( point_ptr[0][31] &= 0x0F; // Filter out first 4 bits (they're outside an i252). point_ptr[1][31] &= 0x0F; // Filter out first 4 bits (they're outside an i252). - let scalar_ptr = *scalar_ptr; + let mut scalar_ptr = *scalar_ptr; scalar_ptr[31] &= 0x0F; // Filter out first 4 bits (they're outside an i252). // Here the points should already be checked as valid, so we can use unchecked. @@ -422,7 +422,7 @@ pub unsafe extern "C" fn cairo_native__libfunc__ec__ec_state_add_mul( Felt::from_bytes_le(&point_ptr[0]), Felt::from_bytes_le(&point_ptr[1]), ); - let scalar = Felt::from_bytes_le(scalar_ptr); + let scalar = Felt::from_bytes_le(&scalar_ptr); state += &point.mul(scalar); let state = state.to_affine().unwrap(); From 72911914fec0772d09d3b0cff39e4a047c3c6576 Mon Sep 17 00:00:00 2001 From: Edgar Luque Date: Mon, 14 Oct 2024 13:27:21 +0200 Subject: [PATCH 05/19] try to fix ci --- .github/workflows/bench-hyperfine.yml | 4 ++-- .github/workflows/ci.yml | 13 +++++-------- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/.github/workflows/bench-hyperfine.yml b/.github/workflows/bench-hyperfine.yml index e31bf980a..79980ad24 100644 --- a/.github/workflows/bench-hyperfine.yml +++ b/.github/workflows/bench-hyperfine.yml @@ -34,7 +34,7 @@ jobs: sudo apt-get remove -y 'php.*' sudo apt-get remove -y '^dotnet-.*' sudo apt-get remove -y '^temurin-.*' - sudo apt-get remove -y azure-cli google-cloud-cli microsoft-edge-stable google-chrome-stable firefox powershell mono-devel + sudo apt-get remove -y azure-cli microsoft-edge-stable google-chrome-stable firefox mono-devel sudo apt-get autoremove -y sudo apt-get clean df -h @@ -124,7 +124,7 @@ jobs: sudo apt-get remove -y 'php.*' sudo apt-get remove -y '^dotnet-.*' sudo apt-get remove -y '^temurin-.*' - sudo apt-get remove -y azure-cli google-cloud-cli microsoft-edge-stable google-chrome-stable firefox powershell mono-devel + sudo apt-get remove -y azure-cli microsoft-edge-stable google-chrome-stable firefox mono-devel sudo apt-get autoremove -y sudo apt-get clean df -h diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 217b02a41..14ae1da56 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -86,7 +86,6 @@ jobs: MLIR_SYS_190_PREFIX: /usr/lib/llvm-19/ LLVM_SYS_191_PREFIX: /usr/lib/llvm-19/ TABLEGEN_190_PREFIX: /usr/lib/llvm-19/ - RUSTUP_TOOLCHAIN: nightly # udeps needs nightly steps: - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@master @@ -102,10 +101,8 @@ jobs: keys-asc: https://apt.llvm.org/llvm-snapshot.gpg.key - name: Install LLVM run: sudo apt-get install llvm-19 llvm-19-dev llvm-19-runtime clang-19 clang-tools-19 lld-19 libpolly-19-dev libmlir-19-dev mlir-19-tools - - name: "Download and run cargo-udeps" - run: | - wget -O - -c https://github.com/est31/cargo-udeps/releases/download/v0.1.50/cargo-udeps-v0.1.50-x86_64-unknown-linux-gnu.tar.gz | tar -xz - cargo-udeps-*/cargo-udeps udeps --all-targets --all-features + - name: Machete + uses: bnjbvr/cargo-machete@main test: name: test (linux, amd64) @@ -127,7 +124,7 @@ jobs: sudo apt-get remove -y 'php.*' sudo apt-get remove -y '^dotnet-.*' sudo apt-get remove -y '^temurin-.*' - sudo apt-get remove -y azure-cli google-cloud-cli microsoft-edge-stable google-chrome-stable firefox powershell mono-devel + sudo apt-get remove -y azure-cli microsoft-edge-stable google-chrome-stable firefox mono-devel sudo apt-get autoremove -y sudo apt-get clean df -h @@ -218,7 +215,7 @@ jobs: sudo apt-get remove -y 'php.*' sudo apt-get remove -y '^dotnet-.*' sudo apt-get remove -y '^temurin-.*' - sudo apt-get remove -y azure-cli google-cloud-cli microsoft-edge-stable google-chrome-stable firefox powershell mono-devel + sudo apt-get remove -y azure-cli microsoft-edge-stable google-chrome-stable firefox mono-devel sudo apt-get autoremove -y sudo apt-get clean df -h @@ -333,7 +330,7 @@ jobs: sudo apt-get remove -y 'php.*' sudo apt-get remove -y '^dotnet-.*' sudo apt-get remove -y '^temurin-.*' - sudo apt-get remove -y azure-cli google-cloud-cli microsoft-edge-stable google-chrome-stable firefox powershell mono-devel + sudo apt-get remove -y azure-cli microsoft-edge-stable google-chrome-stable firefox mono-devel sudo apt-get autoremove -y sudo apt-get clean df -h From 6be70fc5e7d609e5e1f92d800951044478f5eaf0 Mon Sep 17 00:00:00 2001 From: Edgar Luque Date: Mon, 14 Oct 2024 14:21:25 +0200 Subject: [PATCH 06/19] remove unused deps --- Cargo.lock | 60 ++++++++++++++++++++++------------------------ Cargo.toml | 1 - runtime/Cargo.toml | 1 - 3 files changed, 29 insertions(+), 33 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a4e14a522..4f1da7652 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -293,9 +293,9 @@ dependencies = [ [[package]] name = "bindgen" -version = "0.69.4" +version = "0.69.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a00dc851838a2120612785d195287475a3ac45514741da670b735818822129a0" +checksum = "271383c67ccabffb7381723dea0672a673f292304fcb45c01cc648c7a8d58088" dependencies = [ "bitflags", "cexpr", @@ -955,7 +955,6 @@ dependencies = [ "bumpalo", "cairo-lang-compiler", "cairo-lang-defs", - "cairo-lang-diagnostics", "cairo-lang-filesystem", "cairo-lang-runner", "cairo-lang-semantic", @@ -1012,7 +1011,6 @@ dependencies = [ "cairo-lang-sierra-gas", "itertools 0.13.0", "lazy_static", - "libc", "num-traits 0.2.19", "rand", "starknet-curve 0.5.1", @@ -1082,9 +1080,9 @@ checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5" [[package]] name = "cc" -version = "1.1.28" +version = "1.1.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2e80e3b6a3ab07840e1cae9b0666a63970dc28e8ed5ffbcdacbfc760c281bfc1" +checksum = "b16803a61b81d9eabb7eae2588776c4c1e584b738ede45fdbb4c972cec1e9945" dependencies = [ "jobserver", "libc", @@ -1156,9 +1154,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.5.19" +version = "4.5.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7be5744db7978a28d9df86a214130d106a89ce49644cbc4e3f0c22c3fba30615" +checksum = "b97f376d85a664d5837dbae44bf546e6477a679ff6610010f17276f686d867e8" dependencies = [ "clap_builder", "clap_derive", @@ -1166,9 +1164,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.5.19" +version = "4.5.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a5fbc17d3ef8278f55b282b2a2e75ae6f6c7d4bb70ed3d0382375104bfafdb4b" +checksum = "19bc80abd44e4bed93ca373a0704ccbd1b710dc5749406201bb018272808dc54" dependencies = [ "anstream", "anstyle", @@ -1470,18 +1468,18 @@ dependencies = [ [[package]] name = "derive_builder" -version = "0.20.1" +version = "0.20.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cd33f37ee6a119146a1781d3356a7c26028f83d779b2e04ecd45fdc75c76877b" +checksum = "507dfb09ea8b7fa618fcf76e953f4f5e192547945816d5358edffe39f6f94947" dependencies = [ "derive_builder_macro", ] [[package]] name = "derive_builder_core" -version = "0.20.1" +version = "0.20.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7431fa049613920234f22c47fdc33e6cf3ee83067091ea4277a3f8c4587aae38" +checksum = "2d5bcf7b024d6835cfb3d473887cd966994907effbe9227e8c8219824d06c4e8" dependencies = [ "darling", "proc-macro2", @@ -1491,9 +1489,9 @@ dependencies = [ [[package]] name = "derive_builder_macro" -version = "0.20.1" +version = "0.20.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4abae7035bf79b9877b779505d8cf3749285b80c43941eda66604841889451dc" +checksum = "ab63b0e2bf4d5928aff72e83a7dace85d7bba5fe12dcc3c5a572d78caffd3f3c" dependencies = [ "derive_builder_core", "syn 2.0.79", @@ -2133,9 +2131,9 @@ dependencies = [ [[package]] name = "js-sys" -version = "0.3.70" +version = "0.3.72" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1868808506b929d7b0cfa8f75951347aa71bb21144b7791bae35d9bccfcfe37a" +checksum = "6a88f1bda2bd75b0452a14784937d796722fdebfe50df998aeb3f0b7603019a9" dependencies = [ "wasm-bindgen", ] @@ -3486,7 +3484,7 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0ecbc9175dd38627cd01d546e7b41c9a115e5773f4c98f64e2185c81ec5f45ab" dependencies = [ - "bindgen 0.69.4", + "bindgen 0.69.5", "cc", "paste", "thiserror", @@ -3899,9 +3897,9 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" [[package]] name = "wasm-bindgen" -version = "0.2.93" +version = "0.2.95" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a82edfc16a6c469f5f44dc7b571814045d60404b55a0ee849f9bcfa2e63dd9b5" +checksum = "128d1e363af62632b8eb57219c8fd7877144af57558fb2ef0368d0087bddeb2e" dependencies = [ "cfg-if", "once_cell", @@ -3910,9 +3908,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-backend" -version = "0.2.93" +version = "0.2.95" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9de396da306523044d3302746f1208fa71d7532227f15e347e2d93e4145dd77b" +checksum = "cb6dd4d3ca0ddffd1dd1c9c04f94b868c37ff5fac97c30b97cff2d74fce3a358" dependencies = [ "bumpalo", "log", @@ -3925,9 +3923,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro" -version = "0.2.93" +version = "0.2.95" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "585c4c91a46b072c92e908d99cb1dcdf95c5218eeb6f3bf1efa991ee7a68cccf" +checksum = "e79384be7f8f5a9dd5d7167216f022090cf1f9ec128e6e6a482a2cb5c5422c56" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -3935,9 +3933,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.93" +version = "0.2.95" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "afc340c74d9005395cf9dd098506f7f44e38f2b4a21c6aaacf9a105ea5e1e836" +checksum = "26c6ab57572f7a24a4985830b120de1594465e5d500f24afe89e16b4e833ef68" dependencies = [ "proc-macro2", "quote", @@ -3948,15 +3946,15 @@ dependencies = [ [[package]] name = "wasm-bindgen-shared" -version = "0.2.93" +version = "0.2.95" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c62a0a307cb4a311d3a07867860911ca130c3494e8c2719593806c08bc5d0484" +checksum = "65fc09f10666a9f147042251e0dda9c18f166ff7de300607007e96bdebc1068d" [[package]] name = "web-sys" -version = "0.3.70" +version = "0.3.72" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26fdeaafd9bd129f65e7c031593c24d62186301e0c72c8978fa1678be7d532c0" +checksum = "f6488b90108c040df0fe62fa815cbdee25124641df01814dd7282749234c6112" dependencies = [ "js-sys", "wasm-bindgen", diff --git a/Cargo.toml b/Cargo.toml index c4b2e6a37..93c83db6f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -65,7 +65,6 @@ cairo-lang-filesystem = "2.8.4" cairo-lang-semantic = "2.8.4" cairo-lang-sierra = "2.8.4" cairo-lang-sierra-generator = "2.8.4" -cairo-lang-diagnostics = "2.8.4" educe = "0.5.11" # can't update until https://github.com/magiclen/educe/issues/27 itertools = "0.13.0" lazy_static = "1.5" diff --git a/runtime/Cargo.toml b/runtime/Cargo.toml index f06d62c67..565cc64e1 100644 --- a/runtime/Cargo.toml +++ b/runtime/Cargo.toml @@ -13,7 +13,6 @@ starknet-types-core = { version = "0.1.7", default-features = false, features = "std", "serde", "hash" ] } cairo-lang-sierra-gas = "2.8.4" -libc = "0.2" starknet-curve = "0.5.1" lazy_static = "1.5.0" rand = "0.8.5" From f214552d7687f0e406432e7f1a2ded726d753f77 Mon Sep 17 00:00:00 2001 From: Edgar Luque Date: Thu, 10 Oct 2024 17:07:45 +0200 Subject: [PATCH 07/19] proper function attributes --- src/compiler.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/compiler.rs b/src/compiler.rs index 9acd14c93..ab94724e4 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -922,7 +922,11 @@ fn compile_func( &[ ( Identifier::new(context, "sym_visibility"), - StringAttribute::new(context, "public").into(), + StringAttribute::new(context, "private").into(), + ), + ( + Identifier::new(context, "linkage"), + Attribute::parse(context, "#llvm.linkage").unwrap(), ), // ( // Identifier::new(context, "CConv"), @@ -1385,6 +1389,10 @@ fn generate_entry_point_wrapper<'c>( Identifier::new(context, "sym_visibility"), StringAttribute::new(context, "public").into(), ), + ( + Identifier::new(context, "llvm.linkage"), + Attribute::parse(context, "#llvm.linkage").unwrap(), + ), ( Identifier::new(context, "llvm.emit_c_interface"), Attribute::unit(context), From a05e29dda15c84652e4edd20a6acfe560bbba5f5 Mon Sep 17 00:00:00 2001 From: Edgar Luque Date: Thu, 10 Oct 2024 18:08:47 +0200 Subject: [PATCH 08/19] add proper function attrs to optimize better, add some passes, run tests with atleast some opts --- Makefile | 3 ++- src/libfuncs/felt252.rs | 6 +++++- src/utils.rs | 10 ++++++++-- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 5d018b1f4..217091874 100644 --- a/Makefile +++ b/Makefile @@ -95,7 +95,8 @@ doc-open: check-llvm cargo doc --all-features --no-deps --workspace --open .PHONY: bench -bench: build needs-cairo2 runtime +bench: needs-cairo2 runtime + cargo b --release --bin cairo-native-run ./scripts/bench-hyperfine.sh .PHONY: bench-ci diff --git a/src/libfuncs/felt252.rs b/src/libfuncs/felt252.rs index 351650ce4..0be53c796 100644 --- a/src/libfuncs/felt252.rs +++ b/src/libfuncs/felt252.rs @@ -100,7 +100,11 @@ pub fn build_binary_operation<'ctx, 'this>( Felt252BinaryOperator::Add => { let lhs = entry.append_op_result(arith::extui(lhs, i256, location))?; let rhs = entry.append_op_result(arith::extui(rhs, i256, location))?; - let result = entry.append_op_result(arith::addi(lhs, rhs, location))?; + // Note: This can be done with subi but it has a bug in the LLVM optimizer. + // Theorically remui is slower, but on our benchmarks we haven't found much difference. + // The pro of using remui right now is that we can reliableenable optimizations which should generally + // add perfomance. + let result = entry.append_op_result(arith::remui(lhs, rhs, location))?; let prime = entry.const_int_from_type(context, location, PRIME.clone(), i256)?; let result_mod = entry.append_op_result(arith::subi(result, prime, location))?; diff --git a/src/utils.rs b/src/utils.rs index 146accc16..cb1b0faf9 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -292,6 +292,13 @@ pub fn run_pass_manager(context: &Context, module: &mut Module) -> Result<(), Er pass_manager.add_pass(pass::transform::create_canonicalizer()); pass_manager.add_pass(pass::conversion::create_scf_to_control_flow()); // needed because to_llvm doesn't include it. pass_manager.add_pass(pass::conversion::create_to_llvm()); + pass_manager.add_pass(pass::transform::create_cse()); + pass_manager.add_pass(pass::transform::create_sccp()); + pass_manager.add_pass(pass::transform::create_control_flow_sink()); + pass_manager.add_pass(pass::transform::create_loop_invariant_code_motion()); + pass_manager.add_pass(pass::transform::create_inliner()); + pass_manager.add_pass(pass::transform::create_topological_sort()); + pass_manager.add_pass(pass::transform::create_canonicalizer()); pass_manager.run(module) } @@ -642,8 +649,7 @@ pub mod test { .compile(program, false) .expect("Could not compile test program to MLIR."); - // FIXME: There are some bugs with non-zero LLVM optimization levels. - let executor = JitNativeExecutor::from_native_module(module, OptLevel::None); + let executor = JitNativeExecutor::from_native_module(module, OptLevel::Less); executor .invoke_dynamic_with_syscall_handler( entry_point_id, From 9eaf4746b5205e18839af346352f0c77bbc3c07d Mon Sep 17 00:00:00 2001 From: Edgar Luque Date: Thu, 10 Oct 2024 18:11:46 +0200 Subject: [PATCH 09/19] dont use remi --- scripts/bench-hyperfine.sh | 1 - src/libfuncs/felt252.rs | 6 +----- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/scripts/bench-hyperfine.sh b/scripts/bench-hyperfine.sh index 47e9453c6..69a26cb9d 100755 --- a/scripts/bench-hyperfine.sh +++ b/scripts/bench-hyperfine.sh @@ -94,7 +94,6 @@ run_bench() { --warmup 3 \ --export-markdown "$OUTPUT_DIR/$base_name.md" \ --export-json "$OUTPUT_DIR/$base_name.json" \ - -n "Cairo-vm (Rust, Cairo 1)" "$CAIRO_RUN --available-gas 18446744073709551615 -s $base_path.cairo" \ -n "cairo-native (embedded AOT)" "$JIT_CLI --run-mode=aot -s $base_path.cairo --opt-level 3 --available-gas 18446744073709551615 " \ -n "cairo-native (embedded JIT using LLVM's ORC Engine)" "$JIT_CLI --run-mode=jit -s $base_path.cairo --opt-level 3 --available-gas 18446744073709551615 " \ -n "cairo-native (standalone AOT)" "$OUTPUT_DIR/$base_name" \ diff --git a/src/libfuncs/felt252.rs b/src/libfuncs/felt252.rs index 0be53c796..cdf74bc6f 100644 --- a/src/libfuncs/felt252.rs +++ b/src/libfuncs/felt252.rs @@ -100,11 +100,7 @@ pub fn build_binary_operation<'ctx, 'this>( Felt252BinaryOperator::Add => { let lhs = entry.append_op_result(arith::extui(lhs, i256, location))?; let rhs = entry.append_op_result(arith::extui(rhs, i256, location))?; - // Note: This can be done with subi but it has a bug in the LLVM optimizer. - // Theorically remui is slower, but on our benchmarks we haven't found much difference. - // The pro of using remui right now is that we can reliableenable optimizations which should generally - // add perfomance. - let result = entry.append_op_result(arith::remui(lhs, rhs, location))?; + let result = entry.append_op_result(arith::subi(lhs, rhs, location))?; let prime = entry.const_int_from_type(context, location, PRIME.clone(), i256)?; let result_mod = entry.append_op_result(arith::subi(result, prime, location))?; From d0490a229ab1aad30a29432df5f7a4111c6ddc57 Mon Sep 17 00:00:00 2001 From: Edgar Luque Date: Fri, 11 Oct 2024 16:20:31 +0200 Subject: [PATCH 10/19] oops --- src/libfuncs/felt252.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libfuncs/felt252.rs b/src/libfuncs/felt252.rs index cdf74bc6f..351650ce4 100644 --- a/src/libfuncs/felt252.rs +++ b/src/libfuncs/felt252.rs @@ -100,7 +100,7 @@ pub fn build_binary_operation<'ctx, 'this>( Felt252BinaryOperator::Add => { let lhs = entry.append_op_result(arith::extui(lhs, i256, location))?; let rhs = entry.append_op_result(arith::extui(rhs, i256, location))?; - let result = entry.append_op_result(arith::subi(lhs, rhs, location))?; + let result = entry.append_op_result(arith::addi(lhs, rhs, location))?; let prime = entry.const_int_from_type(context, location, PRIME.clone(), i256)?; let result_mod = entry.append_op_result(arith::subi(result, prime, location))?; From 5451f28d03752ee5f93fb50197158a8166c98a04 Mon Sep 17 00:00:00 2001 From: Edgar Luque Date: Fri, 11 Oct 2024 16:47:03 +0200 Subject: [PATCH 11/19] maybe with opt level 3 now it works --- src/ffi.rs | 6 +- src/metadata/drop_overrides.rs | 13 +- src/metadata/dup_overrides.rs | 13 +- src/metadata/runtime_bindings.rs | 226 ++++++++++++++++++++++--------- 4 files changed, 186 insertions(+), 72 deletions(-) diff --git a/src/ffi.rs b/src/ffi.rs index 0e6b563ce..12cf3cc7a 100644 --- a/src/ffi.rs +++ b/src/ffi.rs @@ -143,7 +143,7 @@ pub fn module_to_object(module: &Module<'_>, opt_level: OptLevel) -> Result LLVMCodeGenOptLevel::LLVMCodeGenLevelDefault, OptLevel::Aggressive => LLVMCodeGenOptLevel::LLVMCodeGenLevelAggressive, }, - LLVMRelocMode::LLVMRelocDynamicNoPic, + LLVMRelocMode::LLVMRelocPIC, LLVMCodeModel::LLVMCodeModelDefault, ); @@ -152,8 +152,8 @@ pub fn module_to_object(module: &Module<'_>, opt_level: OptLevel) -> Result 0, OptLevel::Less => 1, - OptLevel::Default => 1, // todo: change once slp-vectorizer pass is fixed on llvm - OptLevel::Aggressive => 1, // https://github.com/llvm/llvm-project/issues/107198 + OptLevel::Default => 2, // todo: change once slp-vectorizer pass is fixed on llvm + OptLevel::Aggressive => 3, // https://github.com/llvm/llvm-project/issues/107198 }; let passes = CString::new(format!("default")).unwrap(); let error = LLVMRunPasses(llvm_module, passes.as_ptr(), machine, opts); diff --git a/src/metadata/drop_overrides.rs b/src/metadata/drop_overrides.rs index 85d21ab1d..7a9e3fe18 100644 --- a/src/metadata/drop_overrides.rs +++ b/src/metadata/drop_overrides.rs @@ -32,7 +32,7 @@ use melior::{ ir::{ attribute::{FlatSymbolRefAttribute, StringAttribute, TypeAttribute}, r#type::FunctionType, - Block, Location, Module, Region, Value, + Attribute, Block, Identifier, Location, Module, Region, Value, }, Context, }; @@ -85,7 +85,16 @@ impl DropOverridesMeta { StringAttribute::new(context, &format!("drop${}", id.id)), TypeAttribute::new(FunctionType::new(context, &[ty], &[]).into()), region, - &[], + &[ + ( + Identifier::new(context, "sym_visibility"), + StringAttribute::new(context, "private").into(), + ), + ( + Identifier::new(context, "llvm.linkage"), + Attribute::parse(context, "#llvm.linkage").unwrap(), + ), + ], Location::unknown(context), )); } diff --git a/src/metadata/dup_overrides.rs b/src/metadata/dup_overrides.rs index 164330963..7dad169bd 100644 --- a/src/metadata/dup_overrides.rs +++ b/src/metadata/dup_overrides.rs @@ -32,7 +32,7 @@ use melior::{ ir::{ attribute::{FlatSymbolRefAttribute, StringAttribute, TypeAttribute}, r#type::FunctionType, - Block, Location, Module, Region, Value, ValueLike, + Attribute, Block, Identifier, Location, Module, Region, Value, ValueLike, }, Context, }; @@ -85,7 +85,16 @@ impl DupOverridesMeta { StringAttribute::new(context, &format!("dup${}", id.id)), TypeAttribute::new(FunctionType::new(context, &[ty], &[ty, ty]).into()), region, - &[], + &[ + ( + Identifier::new(context, "sym_visibility"), + StringAttribute::new(context, "private").into(), + ), + ( + Identifier::new(context, "llvm.linkage"), + Attribute::parse(context, "#llvm.linkage").unwrap(), + ), + ], Location::unknown(context), )); } diff --git a/src/metadata/runtime_bindings.rs b/src/metadata/runtime_bindings.rs index 78bf2eca0..3ddbf0441 100644 --- a/src/metadata/runtime_bindings.rs +++ b/src/metadata/runtime_bindings.rs @@ -9,7 +9,7 @@ use melior::{ ir::{ attribute::{FlatSymbolRefAttribute, StringAttribute, TypeAttribute}, r#type::{FunctionType, IntegerType}, - Block, Identifier, Location, Module, OperationRef, Region, Value, + Attribute, Block, Identifier, Location, Module, OperationRef, Region, Value, }, Context, }; @@ -76,10 +76,16 @@ impl RuntimeBindingsMeta { .into(), ), Region::new(), - &[( - Identifier::new(context, "sym_visibility"), - StringAttribute::new(context, "private").into(), - )], + &[ + ( + Identifier::new(context, "sym_visibility"), + StringAttribute::new(context, "private").into(), + ), + ( + Identifier::new(context, "llvm.linkage"), + Attribute::parse(context, "#llvm.linkage").unwrap(), + ), + ], Location::unknown(context), )); } @@ -128,10 +134,16 @@ impl RuntimeBindingsMeta { .into(), ), Region::new(), - &[( - Identifier::new(context, "sym_visibility"), - StringAttribute::new(context, "private").into(), - )], + &[ + ( + Identifier::new(context, "sym_visibility"), + StringAttribute::new(context, "private").into(), + ), + ( + Identifier::new(context, "llvm.linkage"), + Attribute::parse(context, "#llvm.linkage").unwrap(), + ), + ], Location::unknown(context), )); } @@ -178,10 +190,16 @@ impl RuntimeBindingsMeta { .into(), ), Region::new(), - &[( - Identifier::new(context, "sym_visibility"), - StringAttribute::new(context, "private").into(), - )], + &[ + ( + Identifier::new(context, "sym_visibility"), + StringAttribute::new(context, "private").into(), + ), + ( + Identifier::new(context, "llvm.linkage"), + Attribute::parse(context, "#llvm.linkage").unwrap(), + ), + ], Location::unknown(context), )); } @@ -220,10 +238,16 @@ impl RuntimeBindingsMeta { .into(), ), Region::new(), - &[( - Identifier::new(context, "sym_visibility"), - StringAttribute::new(context, "private").into(), - )], + &[ + ( + Identifier::new(context, "sym_visibility"), + StringAttribute::new(context, "private").into(), + ), + ( + Identifier::new(context, "llvm.linkage"), + Attribute::parse(context, "#llvm.linkage").unwrap(), + ), + ], Location::unknown(context), )); } @@ -262,10 +286,16 @@ impl RuntimeBindingsMeta { .into(), ), Region::new(), - &[( - Identifier::new(context, "sym_visibility"), - StringAttribute::new(context, "private").into(), - )], + &[ + ( + Identifier::new(context, "sym_visibility"), + StringAttribute::new(context, "private").into(), + ), + ( + Identifier::new(context, "llvm.linkage"), + Attribute::parse(context, "#llvm.linkage").unwrap(), + ), + ], Location::unknown(context), )); } @@ -299,10 +329,16 @@ impl RuntimeBindingsMeta { FunctionType::new(context, &[llvm::r#type::pointer(context, 0)], &[]).into(), ), Region::new(), - &[( - Identifier::new(context, "sym_visibility"), - StringAttribute::new(context, "private").into(), - )], + &[ + ( + Identifier::new(context, "sym_visibility"), + StringAttribute::new(context, "private").into(), + ), + ( + Identifier::new(context, "llvm.linkage"), + Attribute::parse(context, "#llvm.linkage").unwrap(), + ), + ], Location::unknown(context), )); } @@ -345,10 +381,16 @@ impl RuntimeBindingsMeta { .into(), ), Region::new(), - &[( - Identifier::new(context, "sym_visibility"), - StringAttribute::new(context, "private").into(), - )], + &[ + ( + Identifier::new(context, "sym_visibility"), + StringAttribute::new(context, "private").into(), + ), + ( + Identifier::new(context, "llvm.linkage"), + Attribute::parse(context, "#llvm.linkage").unwrap(), + ), + ], Location::unknown(context), )); } @@ -394,10 +436,16 @@ impl RuntimeBindingsMeta { .into(), ), Region::new(), - &[( - Identifier::new(context, "sym_visibility"), - StringAttribute::new(context, "private").into(), - )], + &[ + ( + Identifier::new(context, "sym_visibility"), + StringAttribute::new(context, "private").into(), + ), + ( + Identifier::new(context, "llvm.linkage"), + Attribute::parse(context, "#llvm.linkage").unwrap(), + ), + ], Location::unknown(context), )); } @@ -442,10 +490,16 @@ impl RuntimeBindingsMeta { .into(), ), Region::new(), - &[( - Identifier::new(context, "sym_visibility"), - StringAttribute::new(context, "private").into(), - )], + &[ + ( + Identifier::new(context, "sym_visibility"), + StringAttribute::new(context, "private").into(), + ), + ( + Identifier::new(context, "llvm.linkage"), + Attribute::parse(context, "#llvm.linkage").unwrap(), + ), + ], location, )); } @@ -489,10 +543,16 @@ impl RuntimeBindingsMeta { .into(), ), Region::new(), - &[( - Identifier::new(context, "sym_visibility"), - StringAttribute::new(context, "private").into(), - )], + &[ + ( + Identifier::new(context, "sym_visibility"), + StringAttribute::new(context, "private").into(), + ), + ( + Identifier::new(context, "llvm.linkage"), + Attribute::parse(context, "#llvm.linkage").unwrap(), + ), + ], Location::unknown(context), )); } @@ -548,10 +608,16 @@ impl RuntimeBindingsMeta { .into(), ), Region::new(), - &[( - Identifier::new(context, "sym_visibility"), - StringAttribute::new(context, "private").into(), - )], + &[ + ( + Identifier::new(context, "sym_visibility"), + StringAttribute::new(context, "private").into(), + ), + ( + Identifier::new(context, "llvm.linkage"), + Attribute::parse(context, "#llvm.linkage").unwrap(), + ), + ], Location::unknown(context), )); } @@ -604,10 +670,16 @@ impl RuntimeBindingsMeta { .into(), ), Region::new(), - &[( - Identifier::new(context, "sym_visibility"), - StringAttribute::new(context, "private").into(), - )], + &[ + ( + Identifier::new(context, "sym_visibility"), + StringAttribute::new(context, "private").into(), + ), + ( + Identifier::new(context, "llvm.linkage"), + Attribute::parse(context, "#llvm.linkage").unwrap(), + ), + ], Location::unknown(context), )); } @@ -655,10 +727,16 @@ impl RuntimeBindingsMeta { .into(), ), Region::new(), - &[( - Identifier::new(context, "sym_visibility"), - StringAttribute::new(context, "private").into(), - )], + &[ + ( + Identifier::new(context, "sym_visibility"), + StringAttribute::new(context, "private").into(), + ), + ( + Identifier::new(context, "llvm.linkage"), + Attribute::parse(context, "#llvm.linkage").unwrap(), + ), + ], Location::unknown(context), )); } @@ -708,10 +786,16 @@ impl RuntimeBindingsMeta { .into(), ), Region::new(), - &[( - Identifier::new(context, "sym_visibility"), - StringAttribute::new(context, "private").into(), - )], + &[ + ( + Identifier::new(context, "sym_visibility"), + StringAttribute::new(context, "private").into(), + ), + ( + Identifier::new(context, "llvm.linkage"), + Attribute::parse(context, "#llvm.linkage").unwrap(), + ), + ], Location::unknown(context), )); } @@ -755,10 +839,16 @@ impl RuntimeBindingsMeta { .into(), ), Region::new(), - &[( - Identifier::new(context, "sym_visibility"), - StringAttribute::new(context, "private").into(), - )], + &[ + ( + Identifier::new(context, "sym_visibility"), + StringAttribute::new(context, "private").into(), + ), + ( + Identifier::new(context, "llvm.linkage"), + Attribute::parse(context, "#llvm.linkage").unwrap(), + ), + ], Location::unknown(context), )); } @@ -809,10 +899,16 @@ impl RuntimeBindingsMeta { .into(), ), Region::new(), - &[( - Identifier::new(context, "sym_visibility"), - StringAttribute::new(context, "private").into(), - )], + &[ + ( + Identifier::new(context, "sym_visibility"), + StringAttribute::new(context, "private").into(), + ), + ( + Identifier::new(context, "llvm.linkage"), + Attribute::parse(context, "#llvm.linkage").unwrap(), + ), + ], Location::unknown(context), )); } From 802dad2a6b9c83e404831dfc339c0e5db802d507 Mon Sep 17 00:00:00 2001 From: Edgar Luque Date: Mon, 14 Oct 2024 17:17:58 +0200 Subject: [PATCH 12/19] test --- src/bin/utils/test.rs | 2 ++ src/compiler.rs | 14 +++++--- src/metadata/drop_overrides.rs | 8 +++-- src/metadata/dup_overrides.rs | 8 +++-- src/metadata/runtime_bindings.rs | 60 ++++++++++++++++++++++++++++++++ src/types/felt252_dict.rs | 32 +++++++++++++++-- src/utils.rs | 7 ---- 7 files changed, 112 insertions(+), 19 deletions(-) diff --git a/src/bin/utils/test.rs b/src/bin/utils/test.rs index 6df09c091..aab0a371e 100644 --- a/src/bin/utils/test.rs +++ b/src/bin/utils/test.rs @@ -135,8 +135,10 @@ pub fn run_tests( ) -> anyhow::Result { let native_context = NativeContext::new(); + dbg!("compiling"); // Compile the sierra program into a MLIR module. let native_module = native_context.compile(&sierra_program, false).unwrap(); + dbg!("compiled"); let native_executor: Box _> = match args.run_mode { RunMode::Aot => { diff --git a/src/compiler.rs b/src/compiler.rs index ab94724e4..a746f2117 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -926,12 +926,12 @@ fn compile_func( ), ( Identifier::new(context, "linkage"), - Attribute::parse(context, "#llvm.linkage").unwrap(), + Attribute::parse(context, "#llvm.linkage").unwrap(), + ), + ( + Identifier::new(context, "CConv"), + Attribute::parse(context, "#llvm.cconv").unwrap(), ), - // ( - // Identifier::new(context, "CConv"), - // Attribute::parse(context, "#llvm.cconv").unwrap(), - // ), ], Location::fused( context, @@ -1393,6 +1393,10 @@ fn generate_entry_point_wrapper<'c>( Identifier::new(context, "llvm.linkage"), Attribute::parse(context, "#llvm.linkage").unwrap(), ), + ( + Identifier::new(context, "llvm.CConv"), + Attribute::parse(context, "#llvm.cconv").unwrap(), + ), ( Identifier::new(context, "llvm.emit_c_interface"), Attribute::unit(context), diff --git a/src/metadata/drop_overrides.rs b/src/metadata/drop_overrides.rs index 7a9e3fe18..e0a1b30b8 100644 --- a/src/metadata/drop_overrides.rs +++ b/src/metadata/drop_overrides.rs @@ -88,11 +88,15 @@ impl DropOverridesMeta { &[ ( Identifier::new(context, "sym_visibility"), - StringAttribute::new(context, "private").into(), + StringAttribute::new(context, "public").into(), + ), + ( + Identifier::new(context, "llvm.CConv"), + Attribute::parse(context, "#llvm.cconv").unwrap(), ), ( Identifier::new(context, "llvm.linkage"), - Attribute::parse(context, "#llvm.linkage").unwrap(), + Attribute::parse(context, "#llvm.linkage").unwrap(), ), ], Location::unknown(context), diff --git a/src/metadata/dup_overrides.rs b/src/metadata/dup_overrides.rs index 7dad169bd..dada18dd5 100644 --- a/src/metadata/dup_overrides.rs +++ b/src/metadata/dup_overrides.rs @@ -88,11 +88,15 @@ impl DupOverridesMeta { &[ ( Identifier::new(context, "sym_visibility"), - StringAttribute::new(context, "private").into(), + StringAttribute::new(context, "public").into(), + ), + ( + Identifier::new(context, "llvm.CConv"), + Attribute::parse(context, "#llvm.cconv").unwrap(), ), ( Identifier::new(context, "llvm.linkage"), - Attribute::parse(context, "#llvm.linkage").unwrap(), + Attribute::parse(context, "#llvm.linkage").unwrap(), ), ], Location::unknown(context), diff --git a/src/metadata/runtime_bindings.rs b/src/metadata/runtime_bindings.rs index 3ddbf0441..bf6fe2f6d 100644 --- a/src/metadata/runtime_bindings.rs +++ b/src/metadata/runtime_bindings.rs @@ -143,6 +143,10 @@ impl RuntimeBindingsMeta { Identifier::new(context, "llvm.linkage"), Attribute::parse(context, "#llvm.linkage").unwrap(), ), + ( + Identifier::new(context, "llvm.will_return"), + Attribute::unit(context), + ), ], Location::unknown(context), )); @@ -195,6 +199,10 @@ impl RuntimeBindingsMeta { Identifier::new(context, "sym_visibility"), StringAttribute::new(context, "private").into(), ), + ( + Identifier::new(context, "llvm.will_return"), + Attribute::unit(context), + ), ( Identifier::new(context, "llvm.linkage"), Attribute::parse(context, "#llvm.linkage").unwrap(), @@ -243,6 +251,10 @@ impl RuntimeBindingsMeta { Identifier::new(context, "sym_visibility"), StringAttribute::new(context, "private").into(), ), + ( + Identifier::new(context, "llvm.will_return"), + Attribute::unit(context), + ), ( Identifier::new(context, "llvm.linkage"), Attribute::parse(context, "#llvm.linkage").unwrap(), @@ -291,6 +303,10 @@ impl RuntimeBindingsMeta { Identifier::new(context, "sym_visibility"), StringAttribute::new(context, "private").into(), ), + ( + Identifier::new(context, "llvm.will_return"), + Attribute::unit(context), + ), ( Identifier::new(context, "llvm.linkage"), Attribute::parse(context, "#llvm.linkage").unwrap(), @@ -334,6 +350,10 @@ impl RuntimeBindingsMeta { Identifier::new(context, "sym_visibility"), StringAttribute::new(context, "private").into(), ), + ( + Identifier::new(context, "llvm.will_return"), + Attribute::unit(context), + ), ( Identifier::new(context, "llvm.linkage"), Attribute::parse(context, "#llvm.linkage").unwrap(), @@ -386,6 +406,10 @@ impl RuntimeBindingsMeta { Identifier::new(context, "sym_visibility"), StringAttribute::new(context, "private").into(), ), + ( + Identifier::new(context, "llvm.will_return"), + Attribute::unit(context), + ), ( Identifier::new(context, "llvm.linkage"), Attribute::parse(context, "#llvm.linkage").unwrap(), @@ -441,6 +465,10 @@ impl RuntimeBindingsMeta { Identifier::new(context, "sym_visibility"), StringAttribute::new(context, "private").into(), ), + ( + Identifier::new(context, "llvm.will_return"), + Attribute::unit(context), + ), ( Identifier::new(context, "llvm.linkage"), Attribute::parse(context, "#llvm.linkage").unwrap(), @@ -495,6 +523,10 @@ impl RuntimeBindingsMeta { Identifier::new(context, "sym_visibility"), StringAttribute::new(context, "private").into(), ), + ( + Identifier::new(context, "llvm.will_return"), + Attribute::unit(context), + ), ( Identifier::new(context, "llvm.linkage"), Attribute::parse(context, "#llvm.linkage").unwrap(), @@ -548,6 +580,10 @@ impl RuntimeBindingsMeta { Identifier::new(context, "sym_visibility"), StringAttribute::new(context, "private").into(), ), + ( + Identifier::new(context, "llvm.will_return"), + Attribute::unit(context), + ), ( Identifier::new(context, "llvm.linkage"), Attribute::parse(context, "#llvm.linkage").unwrap(), @@ -613,6 +649,10 @@ impl RuntimeBindingsMeta { Identifier::new(context, "sym_visibility"), StringAttribute::new(context, "private").into(), ), + ( + Identifier::new(context, "llvm.will_return"), + Attribute::unit(context), + ), ( Identifier::new(context, "llvm.linkage"), Attribute::parse(context, "#llvm.linkage").unwrap(), @@ -675,6 +715,10 @@ impl RuntimeBindingsMeta { Identifier::new(context, "sym_visibility"), StringAttribute::new(context, "private").into(), ), + ( + Identifier::new(context, "llvm.will_return"), + Attribute::unit(context), + ), ( Identifier::new(context, "llvm.linkage"), Attribute::parse(context, "#llvm.linkage").unwrap(), @@ -732,6 +776,10 @@ impl RuntimeBindingsMeta { Identifier::new(context, "sym_visibility"), StringAttribute::new(context, "private").into(), ), + ( + Identifier::new(context, "llvm.will_return"), + Attribute::unit(context), + ), ( Identifier::new(context, "llvm.linkage"), Attribute::parse(context, "#llvm.linkage").unwrap(), @@ -791,6 +839,10 @@ impl RuntimeBindingsMeta { Identifier::new(context, "sym_visibility"), StringAttribute::new(context, "private").into(), ), + ( + Identifier::new(context, "llvm.will_return"), + Attribute::unit(context), + ), ( Identifier::new(context, "llvm.linkage"), Attribute::parse(context, "#llvm.linkage").unwrap(), @@ -844,6 +896,10 @@ impl RuntimeBindingsMeta { Identifier::new(context, "sym_visibility"), StringAttribute::new(context, "private").into(), ), + ( + Identifier::new(context, "llvm.will_return"), + Attribute::unit(context), + ), ( Identifier::new(context, "llvm.linkage"), Attribute::parse(context, "#llvm.linkage").unwrap(), @@ -904,6 +960,10 @@ impl RuntimeBindingsMeta { Identifier::new(context, "sym_visibility"), StringAttribute::new(context, "private").into(), ), + ( + Identifier::new(context, "llvm.will_return"), + Attribute::unit(context), + ), ( Identifier::new(context, "llvm.linkage"), Attribute::parse(context, "#llvm.linkage").unwrap(), diff --git a/src/types/felt252_dict.rs b/src/types/felt252_dict.rs index 768f36d51..80fc38414 100644 --- a/src/types/felt252_dict.rs +++ b/src/types/felt252_dict.rs @@ -28,7 +28,7 @@ use melior::{ dialect::{func, llvm, ods}, ir::{ attribute::{FlatSymbolRefAttribute, StringAttribute, TypeAttribute}, - Block, Location, Module, Region, Type, + Attribute, Block, Identifier, Location, Module, Region, Type, }, Context, }; @@ -126,7 +126,20 @@ fn build_dup<'ctx>( false, )), region, - &[], + &[ + ( + Identifier::new(context, "sym_visibility"), + StringAttribute::new(context, "public").into(), + ), + ( + Identifier::new(context, "CConv"), + Attribute::parse(context, "#llvm.cconv").unwrap(), + ), + ( + Identifier::new(context, "linkage"), + Attribute::parse(context, "#llvm.linkage").unwrap(), + ), + ], location, )); } @@ -191,7 +204,20 @@ fn build_drop<'ctx>( false, )), region, - &[], + &[ + ( + Identifier::new(context, "sym_visibility"), + StringAttribute::new(context, "public").into(), + ), + ( + Identifier::new(context, "llvm.CConv"), + Attribute::parse(context, "#llvm.cconv").unwrap(), + ), + ( + Identifier::new(context, "llvm.linkage"), + Attribute::parse(context, "#llvm.linkage").unwrap(), + ), + ], location, )); diff --git a/src/utils.rs b/src/utils.rs index cb1b0faf9..090b03292 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -292,13 +292,6 @@ pub fn run_pass_manager(context: &Context, module: &mut Module) -> Result<(), Er pass_manager.add_pass(pass::transform::create_canonicalizer()); pass_manager.add_pass(pass::conversion::create_scf_to_control_flow()); // needed because to_llvm doesn't include it. pass_manager.add_pass(pass::conversion::create_to_llvm()); - pass_manager.add_pass(pass::transform::create_cse()); - pass_manager.add_pass(pass::transform::create_sccp()); - pass_manager.add_pass(pass::transform::create_control_flow_sink()); - pass_manager.add_pass(pass::transform::create_loop_invariant_code_motion()); - pass_manager.add_pass(pass::transform::create_inliner()); - pass_manager.add_pass(pass::transform::create_topological_sort()); - pass_manager.add_pass(pass::transform::create_canonicalizer()); pass_manager.run(module) } From 7479b51c8961693913d7046ebfd878a09b1f842c Mon Sep 17 00:00:00 2001 From: Edgar Luque Date: Mon, 14 Oct 2024 17:35:59 +0200 Subject: [PATCH 13/19] works --- src/compiler.rs | 10 +++--- src/libfuncs/function_call.rs | 10 +++--- src/metadata/runtime_bindings.rs | 60 -------------------------------- src/types/felt252_dict.rs | 8 ----- 4 files changed, 10 insertions(+), 78 deletions(-) diff --git a/src/compiler.rs b/src/compiler.rs index a746f2117..85c4e2059 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -1355,10 +1355,10 @@ fn generate_entry_point_wrapper<'c>( Identifier::new(context, "callee"), FlatSymbolRefAttribute::new(context, private_symbol).into(), ), - // ( - // Identifier::new(context, "CConv"), - // Attribute::parse(context, "#llvm.cconv").unwrap(), - // ), + ( + Identifier::new(context, "CConv"), + Attribute::parse(context, "#llvm.cconv").unwrap(), + ), ]) .add_operands(&args) .add_results(&[llvm::r#type::r#struct(context, ret_types, false)]) @@ -1391,7 +1391,7 @@ fn generate_entry_point_wrapper<'c>( ), ( Identifier::new(context, "llvm.linkage"), - Attribute::parse(context, "#llvm.linkage").unwrap(), + Attribute::parse(context, "#llvm.linkage").unwrap(), ), ( Identifier::new(context, "llvm.CConv"), diff --git a/src/libfuncs/function_call.rs b/src/libfuncs/function_call.rs index 4594219ad..b3b80d4a3 100644 --- a/src/libfuncs/function_call.rs +++ b/src/libfuncs/function_call.rs @@ -23,7 +23,7 @@ use melior::{ attribute::{DenseI32ArrayAttribute, FlatSymbolRefAttribute}, operation::OperationBuilder, r#type::IntegerType, - Block, Identifier, Location, Type, Value, + Attribute, Block, Identifier, Location, Type, Value, }, Context, }; @@ -195,10 +195,10 @@ pub fn build<'ctx, 'this>( ) .into(), ), - // ( - // Identifier::new(context, "CConv"), - // Attribute::parse(context, "#llvm.cconv").unwrap(), - // ), + ( + Identifier::new(context, "CConv"), + Attribute::parse(context, "#llvm.cconv").unwrap(), + ), ]) .add_operands(&arguments) .add_results(&[llvm::r#type::r#struct(context, &result_types, false)]) diff --git a/src/metadata/runtime_bindings.rs b/src/metadata/runtime_bindings.rs index bf6fe2f6d..3ddbf0441 100644 --- a/src/metadata/runtime_bindings.rs +++ b/src/metadata/runtime_bindings.rs @@ -143,10 +143,6 @@ impl RuntimeBindingsMeta { Identifier::new(context, "llvm.linkage"), Attribute::parse(context, "#llvm.linkage").unwrap(), ), - ( - Identifier::new(context, "llvm.will_return"), - Attribute::unit(context), - ), ], Location::unknown(context), )); @@ -199,10 +195,6 @@ impl RuntimeBindingsMeta { Identifier::new(context, "sym_visibility"), StringAttribute::new(context, "private").into(), ), - ( - Identifier::new(context, "llvm.will_return"), - Attribute::unit(context), - ), ( Identifier::new(context, "llvm.linkage"), Attribute::parse(context, "#llvm.linkage").unwrap(), @@ -251,10 +243,6 @@ impl RuntimeBindingsMeta { Identifier::new(context, "sym_visibility"), StringAttribute::new(context, "private").into(), ), - ( - Identifier::new(context, "llvm.will_return"), - Attribute::unit(context), - ), ( Identifier::new(context, "llvm.linkage"), Attribute::parse(context, "#llvm.linkage").unwrap(), @@ -303,10 +291,6 @@ impl RuntimeBindingsMeta { Identifier::new(context, "sym_visibility"), StringAttribute::new(context, "private").into(), ), - ( - Identifier::new(context, "llvm.will_return"), - Attribute::unit(context), - ), ( Identifier::new(context, "llvm.linkage"), Attribute::parse(context, "#llvm.linkage").unwrap(), @@ -350,10 +334,6 @@ impl RuntimeBindingsMeta { Identifier::new(context, "sym_visibility"), StringAttribute::new(context, "private").into(), ), - ( - Identifier::new(context, "llvm.will_return"), - Attribute::unit(context), - ), ( Identifier::new(context, "llvm.linkage"), Attribute::parse(context, "#llvm.linkage").unwrap(), @@ -406,10 +386,6 @@ impl RuntimeBindingsMeta { Identifier::new(context, "sym_visibility"), StringAttribute::new(context, "private").into(), ), - ( - Identifier::new(context, "llvm.will_return"), - Attribute::unit(context), - ), ( Identifier::new(context, "llvm.linkage"), Attribute::parse(context, "#llvm.linkage").unwrap(), @@ -465,10 +441,6 @@ impl RuntimeBindingsMeta { Identifier::new(context, "sym_visibility"), StringAttribute::new(context, "private").into(), ), - ( - Identifier::new(context, "llvm.will_return"), - Attribute::unit(context), - ), ( Identifier::new(context, "llvm.linkage"), Attribute::parse(context, "#llvm.linkage").unwrap(), @@ -523,10 +495,6 @@ impl RuntimeBindingsMeta { Identifier::new(context, "sym_visibility"), StringAttribute::new(context, "private").into(), ), - ( - Identifier::new(context, "llvm.will_return"), - Attribute::unit(context), - ), ( Identifier::new(context, "llvm.linkage"), Attribute::parse(context, "#llvm.linkage").unwrap(), @@ -580,10 +548,6 @@ impl RuntimeBindingsMeta { Identifier::new(context, "sym_visibility"), StringAttribute::new(context, "private").into(), ), - ( - Identifier::new(context, "llvm.will_return"), - Attribute::unit(context), - ), ( Identifier::new(context, "llvm.linkage"), Attribute::parse(context, "#llvm.linkage").unwrap(), @@ -649,10 +613,6 @@ impl RuntimeBindingsMeta { Identifier::new(context, "sym_visibility"), StringAttribute::new(context, "private").into(), ), - ( - Identifier::new(context, "llvm.will_return"), - Attribute::unit(context), - ), ( Identifier::new(context, "llvm.linkage"), Attribute::parse(context, "#llvm.linkage").unwrap(), @@ -715,10 +675,6 @@ impl RuntimeBindingsMeta { Identifier::new(context, "sym_visibility"), StringAttribute::new(context, "private").into(), ), - ( - Identifier::new(context, "llvm.will_return"), - Attribute::unit(context), - ), ( Identifier::new(context, "llvm.linkage"), Attribute::parse(context, "#llvm.linkage").unwrap(), @@ -776,10 +732,6 @@ impl RuntimeBindingsMeta { Identifier::new(context, "sym_visibility"), StringAttribute::new(context, "private").into(), ), - ( - Identifier::new(context, "llvm.will_return"), - Attribute::unit(context), - ), ( Identifier::new(context, "llvm.linkage"), Attribute::parse(context, "#llvm.linkage").unwrap(), @@ -839,10 +791,6 @@ impl RuntimeBindingsMeta { Identifier::new(context, "sym_visibility"), StringAttribute::new(context, "private").into(), ), - ( - Identifier::new(context, "llvm.will_return"), - Attribute::unit(context), - ), ( Identifier::new(context, "llvm.linkage"), Attribute::parse(context, "#llvm.linkage").unwrap(), @@ -896,10 +844,6 @@ impl RuntimeBindingsMeta { Identifier::new(context, "sym_visibility"), StringAttribute::new(context, "private").into(), ), - ( - Identifier::new(context, "llvm.will_return"), - Attribute::unit(context), - ), ( Identifier::new(context, "llvm.linkage"), Attribute::parse(context, "#llvm.linkage").unwrap(), @@ -960,10 +904,6 @@ impl RuntimeBindingsMeta { Identifier::new(context, "sym_visibility"), StringAttribute::new(context, "private").into(), ), - ( - Identifier::new(context, "llvm.will_return"), - Attribute::unit(context), - ), ( Identifier::new(context, "llvm.linkage"), Attribute::parse(context, "#llvm.linkage").unwrap(), diff --git a/src/types/felt252_dict.rs b/src/types/felt252_dict.rs index 80fc38414..2de679d19 100644 --- a/src/types/felt252_dict.rs +++ b/src/types/felt252_dict.rs @@ -131,10 +131,6 @@ fn build_dup<'ctx>( Identifier::new(context, "sym_visibility"), StringAttribute::new(context, "public").into(), ), - ( - Identifier::new(context, "CConv"), - Attribute::parse(context, "#llvm.cconv").unwrap(), - ), ( Identifier::new(context, "linkage"), Attribute::parse(context, "#llvm.linkage").unwrap(), @@ -209,10 +205,6 @@ fn build_drop<'ctx>( Identifier::new(context, "sym_visibility"), StringAttribute::new(context, "public").into(), ), - ( - Identifier::new(context, "llvm.CConv"), - Attribute::parse(context, "#llvm.cconv").unwrap(), - ), ( Identifier::new(context, "llvm.linkage"), Attribute::parse(context, "#llvm.linkage").unwrap(), From 456eb40d420587c82254e5ab1877d28495264a8e Mon Sep 17 00:00:00 2001 From: Edgar Luque Date: Mon, 14 Oct 2024 18:11:40 +0200 Subject: [PATCH 14/19] readd deleted bench --- .github/workflows/bench-hyperfine.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/bench-hyperfine.yml b/.github/workflows/bench-hyperfine.yml index 79980ad24..94bd13c27 100644 --- a/.github/workflows/bench-hyperfine.yml +++ b/.github/workflows/bench-hyperfine.yml @@ -219,6 +219,7 @@ jobs: --export-markdown "${f%.*}.md" \ --export-json "${f%.*}.json" \ --warmup 3 \ + -n "Cairo-vm (Rust, Cairo 1)" "$CAIRO_RUN --available-gas 18446744073709551615 -s $base_path.cairo" \ -n "head $(basename $f) (JIT)" "./bin/cairo-native-run-head --run-mode=jit -s $f --opt-level 3 --available-gas 19446744073709551615" \ -n "base $(basename $f) (JIT)" "./bin/cairo-native-run-base --run-mode=jit -s $f --opt-level 3 --available-gas 19446744073709551615" \ -n "head $(basename $f) (AOT)" "CAIRO_NATIVE_RUNTIME_LIBRARY=lib/libcairo-native-runtime-head.a ./bin/cairo-native-run-head --run-mode=aot -s $f --opt-level 3 --available-gas 19446744073709551615" \ From 8cea54eb28d8a60b8e182a7620c1e0dd007b3337 Mon Sep 17 00:00:00 2001 From: Edgar Luque Date: Mon, 14 Oct 2024 18:11:58 +0200 Subject: [PATCH 15/19] remove dbg --- src/bin/utils/test.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/bin/utils/test.rs b/src/bin/utils/test.rs index aab0a371e..6df09c091 100644 --- a/src/bin/utils/test.rs +++ b/src/bin/utils/test.rs @@ -135,10 +135,8 @@ pub fn run_tests( ) -> anyhow::Result { let native_context = NativeContext::new(); - dbg!("compiling"); // Compile the sierra program into a MLIR module. let native_module = native_context.compile(&sierra_program, false).unwrap(); - dbg!("compiled"); let native_executor: Box _> = match args.run_mode { RunMode::Aot => { From 8ce1ab65fd9a19ab8c09ca9a4a5d059af5a04a8f Mon Sep 17 00:00:00 2001 From: Edgar Date: Tue, 15 Oct 2024 13:00:56 +0200 Subject: [PATCH 16/19] Update bench-hyperfine.sh --- scripts/bench-hyperfine.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/bench-hyperfine.sh b/scripts/bench-hyperfine.sh index 69a26cb9d..47e9453c6 100755 --- a/scripts/bench-hyperfine.sh +++ b/scripts/bench-hyperfine.sh @@ -94,6 +94,7 @@ run_bench() { --warmup 3 \ --export-markdown "$OUTPUT_DIR/$base_name.md" \ --export-json "$OUTPUT_DIR/$base_name.json" \ + -n "Cairo-vm (Rust, Cairo 1)" "$CAIRO_RUN --available-gas 18446744073709551615 -s $base_path.cairo" \ -n "cairo-native (embedded AOT)" "$JIT_CLI --run-mode=aot -s $base_path.cairo --opt-level 3 --available-gas 18446744073709551615 " \ -n "cairo-native (embedded JIT using LLVM's ORC Engine)" "$JIT_CLI --run-mode=jit -s $base_path.cairo --opt-level 3 --available-gas 18446744073709551615 " \ -n "cairo-native (standalone AOT)" "$OUTPUT_DIR/$base_name" \ From 9b010f0e6b37ac0b1416b9f454740e6107418952 Mon Sep 17 00:00:00 2001 From: Edgar Luque Date: Tue, 15 Oct 2024 14:21:09 +0200 Subject: [PATCH 17/19] fixci --- .github/workflows/bench-hyperfine.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/bench-hyperfine.yml b/.github/workflows/bench-hyperfine.yml index 94bd13c27..79980ad24 100644 --- a/.github/workflows/bench-hyperfine.yml +++ b/.github/workflows/bench-hyperfine.yml @@ -219,7 +219,6 @@ jobs: --export-markdown "${f%.*}.md" \ --export-json "${f%.*}.json" \ --warmup 3 \ - -n "Cairo-vm (Rust, Cairo 1)" "$CAIRO_RUN --available-gas 18446744073709551615 -s $base_path.cairo" \ -n "head $(basename $f) (JIT)" "./bin/cairo-native-run-head --run-mode=jit -s $f --opt-level 3 --available-gas 19446744073709551615" \ -n "base $(basename $f) (JIT)" "./bin/cairo-native-run-base --run-mode=jit -s $f --opt-level 3 --available-gas 19446744073709551615" \ -n "head $(basename $f) (AOT)" "CAIRO_NATIVE_RUNTIME_LIBRARY=lib/libcairo-native-runtime-head.a ./bin/cairo-native-run-head --run-mode=aot -s $f --opt-level 3 --available-gas 19446744073709551615" \ From 1a3ff3d83fc24180331effdd2ce68f6d7de131cd Mon Sep 17 00:00:00 2001 From: Edgar Luque Date: Tue, 15 Oct 2024 17:28:23 +0200 Subject: [PATCH 18/19] comment --- src/ffi.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/ffi.rs b/src/ffi.rs index 12cf3cc7a..20cec128c 100644 --- a/src/ffi.rs +++ b/src/ffi.rs @@ -152,8 +152,11 @@ pub fn module_to_object(module: &Module<'_>, opt_level: OptLevel) -> Result 0, OptLevel::Less => 1, - OptLevel::Default => 2, // todo: change once slp-vectorizer pass is fixed on llvm - OptLevel::Aggressive => 3, // https://github.com/llvm/llvm-project/issues/107198 + // slp-vectorizer pass may cause issues, but after the change + // on function attributes it seems to not trigger it anymore. + // https://github.com/llvm/llvm-project/issues/107198 + OptLevel::Default => 2, + OptLevel::Aggressive => 3, }; let passes = CString::new(format!("default")).unwrap(); let error = LLVMRunPasses(llvm_module, passes.as_ptr(), machine, opts); From b8346e728e29e4972973d1a52e246f53ff56bdcb Mon Sep 17 00:00:00 2001 From: Edgar Date: Tue, 15 Oct 2024 18:43:02 +0200 Subject: [PATCH 19/19] Update src/ffi.rs Co-authored-by: MrAzteca --- src/ffi.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ffi.rs b/src/ffi.rs index 20cec128c..1d1a7f412 100644 --- a/src/ffi.rs +++ b/src/ffi.rs @@ -152,8 +152,8 @@ pub fn module_to_object(module: &Module<'_>, opt_level: OptLevel) -> Result 0, OptLevel::Less => 1, - // slp-vectorizer pass may cause issues, but after the change - // on function attributes it seems to not trigger it anymore. + // slp-vectorizer pass did cause some issues, but after the change + // on function attributes it seems to not trigger them anymore. // https://github.com/llvm/llvm-project/issues/107198 OptLevel::Default => 2, OptLevel::Aggressive => 3,