Skip to content

Commit

Permalink
Use new LLVM pass manager
Browse files Browse the repository at this point in the history
  • Loading branch information
tamird committed Jul 25, 2023
1 parent 45bafc8 commit f31a416
Showing 1 changed file with 22 additions and 44 deletions.
66 changes: 22 additions & 44 deletions src/llvm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ use llvm_sys::prelude::*;
use llvm_sys::support::LLVMParseCommandLineOptions;
use llvm_sys::target::*;
use llvm_sys::target_machine::*;
use llvm_sys::transforms::ipo::*;
use llvm_sys::transforms::pass_manager_builder::*;
use llvm_sys::transforms::pass_builder::*;
use llvm_sys::LLVMAttributeFunctionIndex;
use llvm_sys::{LLVMLinkage, LLVMVisibility};
use log::*;
Expand Down Expand Up @@ -182,65 +181,44 @@ pub unsafe fn optimize(
LLVMSetModuleInlineAsm2(module, ptr::null_mut(), 0);
}

let mpm = LLVMCreatePassManager();
let fpm = LLVMCreateFunctionPassManagerForModule(module);

LLVMAddAnalysisPasses(tm, mpm);
LLVMAddAnalysisPasses(tm, fpm);

// even with -O0 and without LTO we still want to avoid linking in unused code from core etc
LLVMAddGlobalDCEPass(mpm);

let pmb = LLVMPassManagerBuilderCreate();

use OptLevel::*;
let (inline_threshold, opt) = match opt_level {
No | SizeMin => (0, 1), // Pretty much nothing compiles with -O0 s∫o make it an alias for -O1
Less => (25, 1),
Default => (225, 2),
Aggressive => (275, 3),
Size => (25, 0),
};
LLVMPassManagerBuilderSetOptLevel(pmb, opt);
LLVMPassManagerBuilderSetSizeLevel(
pmb,
match opt_level {
Size => 1,
SizeMin => 2,
_ => 0,
},
);
LLVMPassManagerBuilderUseInlinerWithThreshold(pmb, inline_threshold);

// populate the pass managers
LLVMPassManagerBuilderPopulateFunctionPassManager(pmb, fpm);
LLVMPassManagerBuilderPopulateModulePassManager(pmb, mpm);

for sym in module.globals_iter() {
internalize(sym, symbol_name(sym), export_symbols);
}
for sym in module.global_aliases_iter() {
internalize(sym, symbol_name(sym), export_symbols);
}

debug!("running function passes");
LLVMInitializeFunctionPassManager(fpm);
for function in module.functions_iter() {
let name = symbol_name(function);
if !name.starts_with("llvm.") {
if ignore_inline_never {
remove_attribute(function, "noinline");
}
internalize(function, name, export_symbols);
if LLVMIsDeclaration(function) == 0 {
LLVMRunFunctionPassManager(fpm, function);
}
}
}
LLVMFinalizeFunctionPassManager(fpm);

debug!("running module passes");
LLVMRunPassManager(mpm, module);
let options = LLVMCreatePassBuilderOptions();

// even with -O0 and without LTO we still want to avoid linking in unused code from core etc
let dce = CString::new("dce").unwrap();
debug!("running dce passe");
LLVMRunPasses(module, dce.as_ptr(), tm, options);

let opt = CString::new(match opt_level {
// Pretty much nothing compiles with -O0 so make it an alias for -O1
OptLevel::No | OptLevel::Less => "default<O1>",
OptLevel::Default => "default<O2>",
OptLevel::Aggressive => "default<O3>",
OptLevel::Size => "default<Os>",
OptLevel::SizeMin => "default<Oz>",
})
.unwrap();

debug!("running optimization pass");
LLVMRunPasses(module, opt.as_ptr(), tm, options);

LLVMDisposePassBuilderOptions(options);

// Some debug info generated by rustc seems to trigger a segfault in the
// BTF code in llvm, so strip it until that is fixed
Expand Down

0 comments on commit f31a416

Please sign in to comment.