Skip to content

Commit b6281e8

Browse files
authored
Fix linker-plugin-lto: use -flto=thin (#1594)
* Fix linker-plugin-lto: use `-flto=thin` The linker-plugin/lld uses thin LTO by default, and it requires the archive to be compiled with thin LTO as well, since thin LTO generates function summary which is needed by thin LTO. * Clarify purpose of unused 'lto' variable in flags.rs Added a comment explaining the purpose of the unused 'lto' variable. * Update comments on thin LTO for linker plugin Clarify comments regarding thin LTO requirements for linker plugin.
1 parent beda6e0 commit b6281e8

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

src/flags.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ impl<'this> RustcCodegenFlags<'this> {
140140
// https://doc.rust-lang.org/rustc/codegen-options/index.html#control-flow-guard
141141
"-Ccontrol-flow-guard" => self.control_flow_guard = value.or(Some("true")),
142142
// https://doc.rust-lang.org/rustc/codegen-options/index.html#lto
143+
//
144+
// This variable is currently unused, we just keep it in case we need it in future
143145
"-Clto" => self.lto = value.or(Some("true")),
144146
// https://doc.rust-lang.org/rustc/linker-plugin-lto.html
145147
"-Clinker-plugin-lto" => self.linker_plugin_lto = Some(true),
@@ -322,11 +324,12 @@ impl<'this> RustcCodegenFlags<'this> {
322324
// https://doc.rust-lang.org/rustc/linker-plugin-lto.html
323325
if self.linker_plugin_lto.unwrap_or(false) {
324326
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-flto
325-
let cc_val = match self.lto {
326-
Some("thin") => "thin",
327-
_ => "full",
328-
};
329-
push_if_supported(format!("-flto={cc_val}").into());
327+
// In order to use linker-plugin-lto to achieve cross-lang lto, cc has to use thin LTO
328+
// to compile the c/c++ libraries because llvm linker plugin/lld uses thin LTO by default.
329+
// And for thin LTO in linker plugin to work, the archive also has to be compiled using thin LTO,
330+
// since thin LTO generates extra information that fat LTO does not generate that
331+
// is required for thin LTO process.
332+
push_if_supported("-flto=thin".into());
330333
}
331334
// https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-mguard
332335
if let Some(value) = self.control_flow_guard {

0 commit comments

Comments
 (0)