Skip to content

Commit 12e2895

Browse files
committed
Fix linker-lto for amdgpu
This allows linking to amd device-libs: https://github.com/ROCm/llvm-project/tree/amd-staging/amd/device-libs
1 parent 3e87b76 commit 12e2895

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

compiler/rustc_codegen_llvm/src/context.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ use rustc_middle::ty::layout::{
1919
};
2020
use rustc_middle::ty::{self, Instance, Ty, TyCtxt};
2121
use rustc_middle::{bug, span_bug};
22-
use rustc_session::Session;
2322
use rustc_session::config::{
2423
BranchProtection, CFGuard, CFProtection, CrateType, DebugInfo, FunctionReturn, PAuthKey, PacRet,
2524
};
25+
use rustc_session::{Session, config};
2626
use rustc_span::source_map::Spanned;
2727
use rustc_span::{DUMMY_SP, Span};
2828
use rustc_target::spec::{HasTargetSpec, RelocModel, SmallDataThresholdSupport, Target, TlsModel};
@@ -266,6 +266,11 @@ pub(crate) unsafe fn create_module<'ll>(
266266
);
267267
}
268268

269+
// Disable ThinLTO if fat lto is requested.
270+
if sess.lto() == config::Lto::Fat {
271+
llvm::add_module_flag_u32(llmod, llvm::ModuleFlagMergeBehavior::Override, "ThinLTO", 0);
272+
}
273+
269274
// Add "kcfi" module flag if KCFI is enabled. (See https://reviews.llvm.org/D119296.)
270275
if sess.is_sanitizer_kcfi_enabled() {
271276
llvm::add_module_flag_u32(llmod, llvm::ModuleFlagMergeBehavior::Override, "kcfi", 1);

compiler/rustc_codegen_ssa/src/back/link.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2498,8 +2498,10 @@ fn add_order_independent_options(
24982498
// as it appears to be unused. This can then cause the PGO profile file to lose
24992499
// some functions. If we are generating a profile we shouldn't strip those metadata
25002500
// sections to ensure we have all the data for PGO.
2501-
let keep_metadata =
2502-
crate_type == CrateType::Dylib || sess.opts.cg.profile_generate.enabled();
2501+
let keep_metadata = crate_type == CrateType::Dylib
2502+
|| sess.opts.cg.profile_generate.enabled()
2503+
// TODO Needed to get the <kernel>.kd symbol?
2504+
|| (sess.target.arch == "amdgpu" && sess.opts.cg.linker_plugin_lto.enabled());
25032505
if crate_type != CrateType::Executable || !sess.opts.unstable_opts.export_executable_symbols
25042506
{
25052507
cmd.gc_sections(keep_metadata);

compiler/rustc_codegen_ssa/src/back/linker.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -839,7 +839,15 @@ impl<'a> Linker for GccLinker<'a> {
839839
} else {
840840
let mut arg = OsString::from("--version-script=");
841841
arg.push(path);
842-
self.link_arg(arg).link_arg("--no-undefined-version");
842+
self.link_arg(arg);
843+
if self.sess.target.arch == "amdgpu" && self.sess.opts.cg.linker_plugin_lto.enabled() {
844+
// The .kd symbol needs to be visible.
845+
// Workaround because we cannot export a symbol that does not exist before linking.
846+
// At the same time, if we do not specify the .kd symbol, it will not be exported.
847+
self.link_arg("--undefined-version");
848+
} else {
849+
self.link_arg("--no-undefined-version");
850+
}
843851
}
844852
}
845853

0 commit comments

Comments
 (0)