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/compiler.rs b/src/compiler.rs index 9acd14c93..85c4e2059 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -922,12 +922,16 @@ 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"), + Attribute::parse(context, "#llvm.cconv").unwrap(), ), - // ( - // Identifier::new(context, "CConv"), - // Attribute::parse(context, "#llvm.cconv").unwrap(), - // ), ], Location::fused( context, @@ -1351,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)]) @@ -1385,6 +1389,14 @@ 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.CConv"), + Attribute::parse(context, "#llvm.cconv").unwrap(), + ), ( Identifier::new(context, "llvm.emit_c_interface"), Attribute::unit(context), diff --git a/src/ffi.rs b/src/ffi.rs index 0e6b563ce..1d1a7f412 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,11 @@ 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 + // 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, }; let passes = CString::new(format!("default")).unwrap(); let error = LLVMRunPasses(llvm_module, passes.as_ptr(), machine, opts); 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/drop_overrides.rs b/src/metadata/drop_overrides.rs index 85d21ab1d..e0a1b30b8 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,20 @@ 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, "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::unknown(context), )); } diff --git a/src/metadata/dup_overrides.rs b/src/metadata/dup_overrides.rs index 164330963..dada18dd5 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,20 @@ 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, "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::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), )); } diff --git a/src/types/felt252_dict.rs b/src/types/felt252_dict.rs index 768f36d51..2de679d19 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,16 @@ fn build_dup<'ctx>( false, )), region, - &[], + &[ + ( + Identifier::new(context, "sym_visibility"), + StringAttribute::new(context, "public").into(), + ), + ( + Identifier::new(context, "linkage"), + Attribute::parse(context, "#llvm.linkage").unwrap(), + ), + ], location, )); } @@ -191,7 +200,16 @@ fn build_drop<'ctx>( false, )), region, - &[], + &[ + ( + Identifier::new(context, "sym_visibility"), + StringAttribute::new(context, "public").into(), + ), + ( + Identifier::new(context, "llvm.linkage"), + Attribute::parse(context, "#llvm.linkage").unwrap(), + ), + ], location, )); diff --git a/src/utils.rs b/src/utils.rs index 146accc16..090b03292 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -642,8 +642,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,