From 7ebb683014202da560793591fce5ea117d5e3c30 Mon Sep 17 00:00:00 2001 From: Cormac Relf Date: Sat, 25 Nov 2023 00:32:03 +1100 Subject: [PATCH 1/2] Make -Clinker-plugin-lto compatible with ld64 --- compiler/rustc_codegen_ssa/src/back/linker.rs | 43 ++++++++++++++----- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/linker.rs b/compiler/rustc_codegen_ssa/src/back/linker.rs index 4dd688c22345d..3259d1a9b395d 100644 --- a/compiler/rustc_codegen_ssa/src/back/linker.rs +++ b/compiler/rustc_codegen_ssa/src/back/linker.rs @@ -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. + 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 { @@ -307,13 +313,30 @@ 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=. + // But it surely can't hurt to tell cc about -mcpu anyway. + // + // 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. + if !self.is_ld { + self.cmd.args(&[&format!("-mcpu={}", self.target_cpu)]); + } + } 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) { From 5ad9dfb81798b7e586821babde9cddd4242e7c8a Mon Sep 17 00:00:00 2001 From: Cormac Relf Date: Mon, 11 Dec 2023 21:58:43 +1100 Subject: [PATCH 2/2] Remove -mcpu for Apple linker-plugin-lto. It's not worth the potential trouble if it does nothing. --- compiler/rustc_codegen_ssa/src/back/linker.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/linker.rs b/compiler/rustc_codegen_ssa/src/back/linker.rs index 3259d1a9b395d..1a46878b48c7c 100644 --- a/compiler/rustc_codegen_ssa/src/back/linker.rs +++ b/compiler/rustc_codegen_ssa/src/back/linker.rs @@ -319,15 +319,11 @@ impl<'a> GccLinker<'a> { // 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=. - // But it surely can't hurt to tell cc about -mcpu anyway. // // 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. - if !self.is_ld { - self.cmd.args(&[&format!("-mcpu={}", self.target_cpu)]); - } } else { if let Some(path) = &self.sess.opts.unstable_opts.profile_sample_use { self.linker_arg(&format!("-plugin-opt=sample-profile={}", path.display()));