Skip to content

Commit 179e193

Browse files
committed
Perform LTO optimisations with wasm-ld + -Clinker-plugin-lto
1 parent b06258c commit 179e193

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

compiler/rustc_codegen_ssa/src/back/linker.rs

+27-1
Original file line numberDiff line numberDiff line change
@@ -1302,6 +1302,8 @@ impl<'a> Linker for WasmLd<'a> {
13021302
}
13031303

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

13561358
fn linker_plugin_lto(&mut self) {
1357-
// Do nothing for now
1359+
match self.sess.opts.cg.linker_plugin_lto {
1360+
LinkerPluginLto::Disabled => {
1361+
// Nothing to do
1362+
}
1363+
LinkerPluginLto::LinkerPluginAuto => {
1364+
self.push_linker_plugin_lto_args();
1365+
}
1366+
LinkerPluginLto::LinkerPlugin(_) => {
1367+
self.push_linker_plugin_lto_args();
1368+
}
1369+
}
1370+
}
1371+
}
1372+
1373+
impl<'a> WasmLd<'a> {
1374+
fn push_linker_plugin_lto_args(&mut self) {
1375+
let opt_level = match self.sess.opts.optimize {
1376+
config::OptLevel::No => "O0",
1377+
config::OptLevel::Less => "O1",
1378+
config::OptLevel::Default => "O2",
1379+
config::OptLevel::Aggressive => "O3",
1380+
// wasm-ld only handles integer LTO opt levels. Use O2
1381+
config::OptLevel::Size | config::OptLevel::SizeMin => "O2",
1382+
};
1383+
self.cmd.arg(&format!("--lto-{opt_level}"));
13581384
}
13591385
}
13601386

0 commit comments

Comments
 (0)