Skip to content

Commit 3e202ea

Browse files
authored
Rollup merge of rust-lang#118378 - cormacrelf:bugfix/linker-plugin-lto-wasm, r=petrochenkov
Perform LTO optimisations with wasm-ld + -Clinker-plugin-lto Fixes (partially) rust-lang#60059. Technically, `--target wasm32-unknown-unknown -Clinker-plugin-lto` would complete without errors before, but it was not producing optimized code. At least, it may have been but it was probably not the opt-level people intended. Similarly to rust-lang#118377, this could benefit from a warning about using an explicit libLTO path with LLD, which will ignore it and use its internal LLVM. Especially given we always use lld on wasm targets. I left the code open to that possibility rather than making it perfectly neat.
2 parents 8f4ccc1 + 179e193 commit 3e202ea

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

Diff for: compiler/rustc_codegen_ssa/src/back/linker.rs

+27-1
Original file line numberDiff line numberDiff line change
@@ -1308,6 +1308,8 @@ impl<'a> Linker for WasmLd<'a> {
13081308
}
13091309

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

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

0 commit comments

Comments
 (0)