Skip to content

Commit

Permalink
Better function attributes and re-enable >O1 opt (#843)
Browse files Browse the repository at this point in the history
* Fix felt252 and enum deserialization bugs.

* Fix formatting.

* Also fix the runtime.

* Fix errors.

* try to fix ci

* remove unused deps

* proper function attributes

* add proper function attrs to optimize better, add some passes, run tests with atleast some opts

* dont use remi

* oops

* maybe with opt level 3 now it works

* test

* works

* readd deleted bench

* remove dbg

* Update bench-hyperfine.sh

* fixci

* comment

* Update src/ffi.rs

Co-authored-by: MrAzteca <[email protected]>

---------

Co-authored-by: Esteve Soler Arderiu <[email protected]>
Co-authored-by: Esteve Soler Arderiu <[email protected]>
Co-authored-by: MrAzteca <[email protected]>
  • Loading branch information
4 people authored Oct 16, 2024
1 parent 84ceaa3 commit 7c4ff7f
Show file tree
Hide file tree
Showing 9 changed files with 247 additions and 92 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 21 additions & 9 deletions src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<private>").unwrap(),
),
(
Identifier::new(context, "CConv"),
Attribute::parse(context, "#llvm.cconv<fastcc>").unwrap(),
),
// (
// Identifier::new(context, "CConv"),
// Attribute::parse(context, "#llvm.cconv<tailcc>").unwrap(),
// ),
],
Location::fused(
context,
Expand Down Expand Up @@ -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<tailcc>").unwrap(),
// ),
(
Identifier::new(context, "CConv"),
Attribute::parse(context, "#llvm.cconv<fastcc>").unwrap(),
),
])
.add_operands(&args)
.add_results(&[llvm::r#type::r#struct(context, ret_types, false)])
Expand Down Expand Up @@ -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<private>").unwrap(),
),
(
Identifier::new(context, "llvm.CConv"),
Attribute::parse(context, "#llvm.cconv<fastcc>").unwrap(),
),
(
Identifier::new(context, "llvm.emit_c_interface"),
Attribute::unit(context),
Expand Down
9 changes: 6 additions & 3 deletions src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ pub fn module_to_object(module: &Module<'_>, opt_level: OptLevel) -> Result<Vec<
OptLevel::Default => LLVMCodeGenOptLevel::LLVMCodeGenLevelDefault,
OptLevel::Aggressive => LLVMCodeGenOptLevel::LLVMCodeGenLevelAggressive,
},
LLVMRelocMode::LLVMRelocDynamicNoPic,
LLVMRelocMode::LLVMRelocPIC,
LLVMCodeModel::LLVMCodeModelDefault,
);

Expand All @@ -152,8 +152,11 @@ pub fn module_to_object(module: &Module<'_>, opt_level: OptLevel) -> Result<Vec<
let opt = match opt_level {
OptLevel::None => 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<O{opt}>")).unwrap();
let error = LLVMRunPasses(llvm_module, passes.as_ptr(), machine, opts);
Expand Down
10 changes: 5 additions & 5 deletions src/libfuncs/function_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -195,10 +195,10 @@ pub fn build<'ctx, 'this>(
)
.into(),
),
// (
// Identifier::new(context, "CConv"),
// Attribute::parse(context, "#llvm.cconv<tailcc>").unwrap(),
// ),
(
Identifier::new(context, "CConv"),
Attribute::parse(context, "#llvm.cconv<fastcc>").unwrap(),
),
])
.add_operands(&arguments)
.add_results(&[llvm::r#type::r#struct(context, &result_types, false)])
Expand Down
17 changes: 15 additions & 2 deletions src/metadata/drop_overrides.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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<fastcc>").unwrap(),
),
(
Identifier::new(context, "llvm.linkage"),
Attribute::parse(context, "#llvm.linkage<private>").unwrap(),
),
],
Location::unknown(context),
));
}
Expand Down
17 changes: 15 additions & 2 deletions src/metadata/dup_overrides.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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<fastcc>").unwrap(),
),
(
Identifier::new(context, "llvm.linkage"),
Attribute::parse(context, "#llvm.linkage<private>").unwrap(),
),
],
Location::unknown(context),
));
}
Expand Down
Loading

0 comments on commit 7c4ff7f

Please sign in to comment.