Skip to content

Commit

Permalink
Merge pull request #72 from aya-rs/llvm-new-pm
Browse files Browse the repository at this point in the history
Use new pass manager
  • Loading branch information
tamird authored Jul 28, 2023
2 parents 92b7410 + 665e1b0 commit 5fe6f8d
Show file tree
Hide file tree
Showing 9 changed files with 201 additions and 235 deletions.
325 changes: 147 additions & 178 deletions Cargo.lock

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ edition = "2021"

[dependencies]
# cli deps
clap = { version = "4.3", features = ["derive"] }
clap = { version = "4.3.0", features = ["derive"] }
simplelog = { version = "0.12.1" }

# lib deps
ar = { version = "0.9.0" }
aya-rustc-llvm-proxy = { version = "0.6.0", optional = true }
libc = { version = "0.2" }
llvm-sys = { version = "160" }
log = { version = "0.4" }
thiserror = { version = "1.0" }
aya-rustc-llvm-proxy = { version = "0.7.0", optional = true }
libc = { version = "0.2.0" }
llvm-sys = { version = "160.0.0" }
log = { version = "0.4.0" }
thiserror = { version = "1.0.0" }

[dev-dependencies]
compiletest_rs = { version = "0.5", path = "third-party/compiletest-rs" }
compiletest_rs = { version = "0.5.0", path = "third-party/compiletest-rs" }
rustc-build-sysroot = { version = "0.4.2" }
which = { version = "4.2" }
which = { version = "4.2.0" }

[[bin]]
name = "bpf-linker"
Expand Down
2 changes: 1 addition & 1 deletion src/bin/bpf-linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ struct CommandLine {
export: Vec<String>,

/// Whether to treat LLVM errors as fatal.
#[clap(long, action = clap::ArgAction::Set, default_value_t = false)]
#[clap(long, action = clap::ArgAction::Set, default_value_t = true)]
fatal_errors: bool,

// The options below are for wasm-ld compatibility
Expand Down
16 changes: 16 additions & 0 deletions src/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,8 +516,24 @@ impl Linker {

impl llvm::LLVMDiagnosticHandler for Linker {
fn handle_diagnostic(&mut self, severity: llvm_sys::LLVMDiagnosticSeverity, message: &str) {
// TODO(https://reviews.llvm.org/D155894): Remove this when LLVM no longer emits these
// errors.
//
// See https://github.com/rust-lang/compiler-builtins/blob/a61823f/src/mem/mod.rs#L22-L68.
const MATCHERS: &[&str] = &[
"A call to built-in function 'memcpy' is not supported.\n",
"A call to built-in function 'memmove' is not supported.\n",
"A call to built-in function 'memset' is not supported.\n",
"A call to built-in function 'memcmp' is not supported.\n",
"A call to built-in function 'bcmp' is not supported.\n",
"A call to built-in function 'strlen' is not supported.\n",
];

match severity {
llvm_sys::LLVMDiagnosticSeverity::LLVMDSError => {
if MATCHERS.iter().any(|matcher| message.ends_with(matcher)) {
return;
}
self.has_errors = true;

error!("llvm: {}", message)
Expand Down
70 changes: 26 additions & 44 deletions src/llvm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,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 @@ -176,65 +175,48 @@ 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();

let passes = [
// NB: "default<_>" must be the first pass in the list, otherwise it will be ignored.
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>",
},
// NB: This seems to be included in most default pipelines, but not obviously all of them.
// See
// https://github.com/llvm/llvm-project/blob/bbe2887f/llvm/lib/Passes/PassBuilderPipelines.cpp#L2011-L2012
// for a case which includes DCE only conditionally. Better safe than sorry; include it always.
"dce",
];

let passes = passes.join(",");
debug!("running passes: {passes}");
let passes = CString::new(passes).unwrap();
LLVMRunPasses(module, passes.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
2 changes: 1 addition & 1 deletion tests/assembly/auxiliary/dep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
#[no_mangle]
fn some_dep() -> u8 {
42
}
}
1 change: 0 additions & 1 deletion tests/assembly/auxiliary/loop-panic-handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@
fn panic_impl(_: &core::panic::PanicInfo) -> ! {
loop {}
}

2 changes: 1 addition & 1 deletion tests/assembly/elf-sections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ static mut COUNTER: u32 = 0;

// CHECK: .section "uprobe/connect","ax"
// CHECK: .section "uprobe/dep","ax"
// CHECK: .section "maps/counter","aw"
// CHECK: .section "maps/counter","aw"
2 changes: 1 addition & 1 deletion tests/assembly/re-export-symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ fn connect() {
}

// CHECK: .globl connect
// CHECK: .globl some_dep
// CHECK: .globl some_dep

0 comments on commit 5fe6f8d

Please sign in to comment.