Skip to content

Commit

Permalink
Perform LTO optimisations with wasm-ld + -Clinker-plugin-lto
Browse files Browse the repository at this point in the history
  • Loading branch information
cormacrelf committed Nov 27, 2023
1 parent b06258c commit 179e193
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion compiler/rustc_codegen_ssa/src/back/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1302,6 +1302,8 @@ impl<'a> Linker for WasmLd<'a> {
}

fn optimize(&mut self) {
// The -O flag is, as of late 2023, only used for merging of strings and debuginfo, and
// only differentiates -O0 and -O1. It does not apply to LTO.
self.cmd.arg(match self.sess.opts.optimize {
OptLevel::No => "-O0",
OptLevel::Less => "-O1",
Expand Down Expand Up @@ -1354,7 +1356,31 @@ impl<'a> Linker for WasmLd<'a> {
fn subsystem(&mut self, _subsystem: &str) {}

fn linker_plugin_lto(&mut self) {
// Do nothing for now
match self.sess.opts.cg.linker_plugin_lto {
LinkerPluginLto::Disabled => {
// Nothing to do
}
LinkerPluginLto::LinkerPluginAuto => {
self.push_linker_plugin_lto_args();
}
LinkerPluginLto::LinkerPlugin(_) => {
self.push_linker_plugin_lto_args();
}
}
}
}

impl<'a> WasmLd<'a> {
fn push_linker_plugin_lto_args(&mut self) {
let opt_level = match self.sess.opts.optimize {
config::OptLevel::No => "O0",
config::OptLevel::Less => "O1",
config::OptLevel::Default => "O2",
config::OptLevel::Aggressive => "O3",
// wasm-ld only handles integer LTO opt levels. Use O2
config::OptLevel::Size | config::OptLevel::SizeMin => "O2",
};
self.cmd.arg(&format!("--lto-{opt_level}"));
}
}

Expand Down

0 comments on commit 179e193

Please sign in to comment.