Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trans: Consolidate creating pass manager builders #27224

Merged
merged 1 commit into from
Jul 24, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 6 additions & 12 deletions src/librustc_trans/back/lto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@ use llvm::archive_ro::ArchiveRO;
use llvm::{ModuleRef, TargetMachineRef, True, False};
use rustc::metadata::cstore;
use rustc::util::common::time;
use back::write::{ModuleConfig, with_llvm_pmb};

use libc;
use flate;

use std::ffi::CString;

pub fn run(sess: &session::Session, llmod: ModuleRef,
tm: TargetMachineRef, reachable: &[String]) {
tm: TargetMachineRef, reachable: &[String],
config: &ModuleConfig) {
if sess.opts.cg.prefer_dynamic {
sess.err("cannot prefer dynamic linking when performing LTO");
sess.note("only 'staticlib' and 'bin' outputs are supported with LTO");
Expand Down Expand Up @@ -144,19 +146,11 @@ pub fn run(sess: &session::Session, llmod: ModuleRef,
llvm::LLVMRustAddAnalysisPasses(tm, pm, llmod);
llvm::LLVMRustAddPass(pm, "verify\0".as_ptr() as *const _);

let opt = match sess.opts.optimize {
config::No => 0,
config::Less => 1,
config::Default => 2,
config::Aggressive => 3,
};

let builder = llvm::LLVMPassManagerBuilderCreate();
llvm::LLVMPassManagerBuilderSetOptLevel(builder, opt);
llvm::LLVMPassManagerBuilderPopulateLTOPassManager(builder, pm,
with_llvm_pmb(llmod, config, &mut |b| {
llvm::LLVMPassManagerBuilderPopulateLTOPassManager(b, pm,
/* Internalize = */ False,
/* RunInliner = */ True);
llvm::LLVMPassManagerBuilderDispose(builder);
});

llvm::LLVMRustAddPass(pm, "verify\0".as_ptr() as *const _);

Expand Down
27 changes: 12 additions & 15 deletions src/librustc_trans/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ pub fn create_target_machine(sess: &Session) -> TargetMachineRef {

/// Module-specific configuration for `optimize_and_codegen`.
#[derive(Clone)]
struct ModuleConfig {
pub struct ModuleConfig {
/// LLVM TargetMachine to use for codegen.
tm: TargetMachineRef,
/// Names of additional optimization passes to run.
Expand Down Expand Up @@ -444,8 +444,7 @@ unsafe fn optimize_and_codegen(cgcx: &CodegenContext,
llvm::LLVMWriteBitcodeToFile(llmod, out.as_ptr());
}

match config.opt_level {
Some(opt_level) => {
if config.opt_level.is_some() {
// Create the two optimizing pass managers. These mirror what clang
// does, and are by populated by LLVM's default PassManagerBuilder.
// Each manager has a different set of passes, but they also share
Expand All @@ -464,7 +463,10 @@ unsafe fn optimize_and_codegen(cgcx: &CodegenContext,
if !config.no_prepopulate_passes {
llvm::LLVMRustAddAnalysisPasses(tm, fpm, llmod);
llvm::LLVMRustAddAnalysisPasses(tm, mpm, llmod);
populate_llvm_passes(fpm, mpm, llmod, opt_level, &config);
with_llvm_pmb(llmod, &config, &mut |b| {
llvm::LLVMPassManagerBuilderPopulateFunctionPassManager(b, fpm);
llvm::LLVMPassManagerBuilderPopulateModulePassManager(b, mpm);
})
}

for pass in &config.passes {
Expand Down Expand Up @@ -497,7 +499,7 @@ unsafe fn optimize_and_codegen(cgcx: &CodegenContext,
match cgcx.lto_ctxt {
Some((sess, reachable)) if sess.lto() => {
time(sess.time_passes(), "all lto passes", (), |()|
lto::run(sess, llmod, tm, reachable));
lto::run(sess, llmod, tm, reachable, &config));

if config.emit_lto_bc {
let name = format!("{}.lto.bc", name_extra);
Expand All @@ -508,8 +510,6 @@ unsafe fn optimize_and_codegen(cgcx: &CodegenContext,
},
_ => {},
}
},
None => {},
}

// A codegen-specific pass manager is used to generate object
Expand Down Expand Up @@ -1001,15 +1001,14 @@ pub unsafe fn configure_llvm(sess: &Session) {
llvm_args.as_ptr());
}

unsafe fn populate_llvm_passes(fpm: llvm::PassManagerRef,
mpm: llvm::PassManagerRef,
llmod: ModuleRef,
opt: llvm::CodeGenOptLevel,
config: &ModuleConfig) {
pub unsafe fn with_llvm_pmb(llmod: ModuleRef,
config: &ModuleConfig,
f: &mut FnMut(llvm::PassManagerBuilderRef)) {
// Create the PassManagerBuilder for LLVM. We configure it with
// reasonable defaults and prepare it to actually populate the pass
// manager.
let builder = llvm::LLVMPassManagerBuilderCreate();
let opt = config.opt_level.unwrap_or(llvm::CodeGenLevelNone);

llvm::LLVMRustConfigurePassManagerBuilder(builder, opt,
config.merge_functions,
Expand Down Expand Up @@ -1037,8 +1036,6 @@ unsafe fn populate_llvm_passes(fpm: llvm::PassManagerRef,
}
}

// Use the builder to populate the function/module pass managers.
llvm::LLVMPassManagerBuilderPopulateFunctionPassManager(builder, fpm);
llvm::LLVMPassManagerBuilderPopulateModulePassManager(builder, mpm);
f(builder);
llvm::LLVMPassManagerBuilderDispose(builder);
}