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

Make -Clinker-plugin-lto compatible with ld64 #118377

Closed
Closed
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
39 changes: 29 additions & 10 deletions compiler/rustc_codegen_ssa/src/back/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,15 @@ impl<'a> GccLinker<'a> {

fn push_linker_plugin_lto_args(&mut self, plugin_path: Option<&OsStr>) {
if let Some(plugin_path) = plugin_path {
let mut arg = OsString::from("-plugin=");
arg.push(plugin_path);
self.linker_arg(&arg);
// Note that LLD silently ignores these flags.
// It will always use the LLVM it was built with to perform LTO.
Copy link
Member

Choose a reason for hiding this comment

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

Is this documented somewhere? If so, it would be good to add a link.

I'm not sure if we should emit a warning. It could be super helpful -- but it could also be annoying if you know it's fine but for some reason cannot get rid of the commandline argument.

if self.sess.target.is_like_osx {
self.linker_args(&[OsStr::new("-lto_library"), plugin_path]);
} else {
let mut arg = OsString::from("-plugin=");
arg.push(plugin_path);
self.linker_arg(&arg);
}
}

let opt_level = match self.sess.opts.optimize {
Expand All @@ -307,13 +313,26 @@ impl<'a> GccLinker<'a> {
config::OptLevel::Aggressive => "O3",
};

if let Some(path) = &self.sess.opts.unstable_opts.profile_sample_use {
self.linker_arg(&format!("-plugin-opt=sample-profile={}", path.display()));
};
self.linker_args(&[
&format!("-plugin-opt={opt_level}"),
&format!("-plugin-opt=mcpu={}", self.target_cpu),
]);
if self.sess.target.is_like_osx {
// Apple clang 15 passes -O3 through to ld64. We will do the same.
self.linker_arg(&format!("-{opt_level}"));

// Apple clang 15 does not seem to tell the linker about -mcpu. Unlike -O3 etc,
// ld64 does not accept -mcpu or GNU-style -plugin-opt=mcpu=.
//
// It's possible this has something to do with platform minimum versions AKA
// deployment targets; Apple drops support for CPU subfamilies at defined iOS
// versions, for example. Either that or ld64 is meant to infer a target CPU
// from the object files.
} else {
if let Some(path) = &self.sess.opts.unstable_opts.profile_sample_use {
self.linker_arg(&format!("-plugin-opt=sample-profile={}", path.display()));
}
self.linker_args(&[
&format!("-plugin-opt={opt_level}"),
&format!("-plugin-opt=mcpu={}", self.target_cpu),
]);
}
}

fn build_dylib(&mut self, out_filename: &Path) {
Expand Down
Loading