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

Perform LTO optimisations with wasm-ld + -Clinker-plugin-lto #118378

Merged
merged 1 commit into from
Nov 29, 2023
Merged
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
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 => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
LinkerPluginLto::LinkerPluginAuto => {
LinkerPluginLto::LinkerPluginAuto | LinkerPluginLto::LinkerPlugin(_) => {

push_linker_plugin_lto_args could then be inlined and removed.

Upd: the match on self.sess.opts.optimize could be kept separate function, though, it is shared between fn optimize and fn linker_plugin_lto.

Copy link
Contributor Author

@cormacrelf cormacrelf Nov 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I intentionally left them separate.

  • That the match turns out the same is more of a coincidence than anything else. I anticipate the --lto-O* args may change to support Os and Oz in future. (Especially with those being the most popular opt levels for webassembly.) If that happens, adding Os/Oz to a shared match statement would break the normal fn optimize that controls merging sections only, unrelated to LLVM.
  • push_linker_plugin_lto_args could be inlined, but I was going to do another PR that puts warnings in one of the match arms but not the other.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok.

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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// wasm-ld only handles integer LTO opt levels. Use O2
// `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
Loading