Skip to content

Commit

Permalink
Change CLI regarding optimisation level
Browse files Browse the repository at this point in the history
  • Loading branch information
denzp committed Jan 28, 2019
1 parent 9805429 commit faba7c0
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 105 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/bin/legacy-ptx-linker/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ impl<'a> From<ArgMatches<'a>> for CommandLineRequest {

match matches.value_of("optimise") {
Some("0") | None => session.set_opt_level(OptLevel::None),
Some(_) => session.set_opt_level(OptLevel::Default),
Some(_) => session.set_opt_level(OptLevel::LTO),
};

if let Some(outputs) = matches.values_of("emit") {
Expand Down Expand Up @@ -236,7 +236,7 @@ mod tests {
emit: vec![Output::PTXAssembly],
achitectures: vec![],

opt_level: OptLevel::Default,
opt_level: OptLevel::LTO,
debug_info: false,

output: Some(PathBuf::from("/kernel/target/debug/deps/libkernel.ptx")),
Expand Down
93 changes: 6 additions & 87 deletions src/bin/rust-ptx-linker/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ fn get_app() -> App<'static, 'static> {
.short("O")
.help("Optimisation level")
.takes_value(true)
.possible_values(&["0", "1", "2", "3", "s"])
.possible_values(&["lto"])
.value_name("level")
},
{
Expand Down Expand Up @@ -110,16 +110,11 @@ fn parse_session(matches: ArgMatches<'static>) -> Session {
}

match matches.value_of("optimisation") {
Some("0") | None => session.set_opt_level(OptLevel::None),
Some("2") => session.set_opt_level(OptLevel::Default),

Some("1") | Some("3") => {
info!("Using default optimisation level `-O2`.");
session.set_opt_level(OptLevel::Default);
}
Some("lto") => session.set_opt_level(OptLevel::LTO),
None => session.set_opt_level(OptLevel::None),

Some(_) => {
warn!("Not supported optimisation level! Falling back to `-O0`.");
warn!("Not supported optimisation level! Ignoring...");
}
};

Expand Down Expand Up @@ -266,90 +261,14 @@ mod tests {
assert_eq!(
parse_session(
get_app()
.get_matches_from_safe(vec!["rust-ptx-linker", "-O0"])
.unwrap()
),
Session {
emit: vec![Output::PTXAssembly],
achitectures: vec![],

opt_level: OptLevel::None,
debug_info: false,

output: None,
include_bitcode_modules: vec![],
include_rlibs: vec![],
}
);

assert_eq!(
parse_session(
get_app()
.get_matches_from_safe(vec!["rust-ptx-linker", "-O1"])
.unwrap()
),
Session {
emit: vec![Output::PTXAssembly],
achitectures: vec![],

opt_level: OptLevel::Default,
debug_info: false,

output: None,
include_bitcode_modules: vec![],
include_rlibs: vec![],
}
);

assert_eq!(
parse_session(
get_app()
.get_matches_from_safe(vec!["rust-ptx-linker", "-O2"])
.unwrap()
),
Session {
emit: vec![Output::PTXAssembly],
achitectures: vec![],

opt_level: OptLevel::Default,
debug_info: false,

output: None,
include_bitcode_modules: vec![],
include_rlibs: vec![],
}
);

assert_eq!(
parse_session(
get_app()
.get_matches_from_safe(vec!["rust-ptx-linker", "-O3"])
.get_matches_from_safe(vec!["rust-ptx-linker", "-Olto"])
.unwrap()
),
Session {
emit: vec![Output::PTXAssembly],
achitectures: vec![],

opt_level: OptLevel::Default,
debug_info: false,

output: None,
include_bitcode_modules: vec![],
include_rlibs: vec![],
}
);

assert_eq!(
parse_session(
get_app()
.get_matches_from_safe(vec!["rust-ptx-linker", "-Os"])
.unwrap()
),
Session {
emit: vec![Output::PTXAssembly],
achitectures: vec![],

opt_level: OptLevel::None,
opt_level: OptLevel::LTO,
debug_info: false,

output: None,
Expand Down
37 changes: 22 additions & 15 deletions src/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,26 +138,33 @@ impl Linker {

fn run_llvm_passes(&self) {
unsafe {
let builder = LLVMPassManagerBuilderCreate();
let pass_manager = LLVMCreatePassManager();

match self.session.opt_level {
OptLevel::None => {
info!("Linking without optimisations");
LLVMPassManagerBuilderSetOptLevel(builder, 0);
info!("Linking without Link Time Optimisation");
}

OptLevel::Default => {
OptLevel::LTO => {
info!("Linking with Link Time Optimisation");
LLVMPassManagerBuilderSetOptLevel(builder, 3);
LLVMPassManagerBuilderPopulateLTOPassManager(builder, pass_manager, 1, 1);
let pass_manager_builder = LLVMPassManagerBuilderCreate();

LLVMPassManagerBuilderSetOptLevel(pass_manager_builder, 3);
LLVMPassManagerBuilderPopulateLTOPassManager(
pass_manager_builder,
pass_manager,
1,
1,
);

LLVMPassManagerBuilderDispose(pass_manager_builder);
}
}

LLVMPassManagerBuilderPopulateModulePassManager(builder, pass_manager);
LLVMPassManagerBuilderDispose(builder);

// The pass is needed to perform cleanup after our internaliser.
LLVMAddGlobalDCEPass(pass_manager);

// TODO(denzp): check the result
LLVMRunPassManager(pass_manager, self.module);
LLVMDisposePassManager(pass_manager);

Expand All @@ -179,12 +186,12 @@ impl Linker {
let path = CString::new(self.get_output_path_with_ext("ll")?.to_str().unwrap()).unwrap();

unsafe {
// TODO: check result
// TODO(denzp): check result
let mut message = Message::new();
LLVMPrintModuleToFile(self.module, path.as_ptr(), message.as_mut_ptr());

if !message.is_empty() {
// TODO: stderr?
// TODO(denzp): stderr?
println!("{}", message);
}
}
Expand All @@ -197,7 +204,7 @@ impl Linker {
let path = CString::new(self.get_output_path_with_ext("bc")?.to_str().unwrap()).unwrap();

unsafe {
// TODO: check result
// TODO(denzp): check result
LLVMWriteBitcodeToFile(self.module, path.as_ptr());
}

Expand Down Expand Up @@ -251,7 +258,7 @@ impl Linker {
{
let mut message = Message::new();

// TODO: check result
// TODO(denzp): check result
LLVMTargetMachineEmitToFile(
target_machine,
self.module,
Expand Down Expand Up @@ -294,10 +301,10 @@ impl Linker {

let mut temp_module = ptr::null_mut();

// TODO: check result
// TODO(denzp): check result
LLVMParseBitcodeInContext2(self.context, buffer, &mut temp_module);

// TODO: check result
// TODO(denzp): check result
LLVMLinkModules2(module, temp_module);
LLVMDisposeMemoryBuffer(buffer);
}
Expand Down
2 changes: 1 addition & 1 deletion src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use log::*;
#[derive(Debug, PartialEq)]
pub enum OptLevel {
None,
Default,
LTO,
}

impl Default for OptLevel {
Expand Down

0 comments on commit faba7c0

Please sign in to comment.