Skip to content

Commit 2c0ec01

Browse files
authored
[AMDGPU] Correctly pass the target-id to ld.lld (#101037)
Summary: The `ld.lld` linker handles LTO, but it does not understand the target-id syntax some AMDGPU targets use. This patch parses the target-id and passes the processor name in `-mcpu` and features in `-mattr`.
1 parent 0d9b439 commit 2c0ec01

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

clang/lib/Driver/ToolChains/AMDGPU.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -629,12 +629,24 @@ void amdgpu::Linker::ConstructJob(Compilation &C, const JobAction &JA,
629629
Args.AddAllArgs(CmdArgs, options::OPT_L);
630630
getToolChain().AddFilePathLibArgs(Args, CmdArgs);
631631
AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs, JA);
632-
if (C.getDriver().isUsingLTO())
632+
if (C.getDriver().isUsingLTO()) {
633633
addLTOOptions(getToolChain(), Args, CmdArgs, Output, Inputs[0],
634634
C.getDriver().getLTOMode() == LTOK_Thin);
635-
else if (Args.hasArg(options::OPT_mcpu_EQ))
635+
} else if (Args.hasArg(options::OPT_mcpu_EQ)) {
636636
CmdArgs.push_back(Args.MakeArgString(
637-
"-plugin-opt=mcpu=" + Args.getLastArgValue(options::OPT_mcpu_EQ)));
637+
"-plugin-opt=mcpu=" +
638+
getProcessorFromTargetID(getToolChain().getTriple(),
639+
Args.getLastArgValue(options::OPT_mcpu_EQ))));
640+
}
641+
642+
// Always pass the target-id features to the LTO job.
643+
std::vector<StringRef> Features;
644+
getAMDGPUTargetFeatures(C.getDriver(), getToolChain().getTriple(), Args,
645+
Features);
646+
if (!Features.empty()) {
647+
CmdArgs.push_back(
648+
Args.MakeArgString("-plugin-opt=-mattr=" + llvm::join(Features, ",")));
649+
}
638650

639651
addGPULibraries(getToolChain(), Args, CmdArgs);
640652

clang/test/Driver/amdgpu-toolchain.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818
// AS_LINK_UR: "-cc1as"
1919
// AS_LINK_UR: ld.lld{{.*}} "--no-undefined"{{.*}} "--unresolved-symbols=ignore-all"
2020

21-
// RUN: %clang -### --target=amdgcn-amd-amdhsa -mcpu=gfx906 -nogpulib \
21+
// RUN: %clang -### --target=amdgcn-amd-amdhsa -mcpu=gfx90a:xnack+:sramecc- -nogpulib \
2222
// RUN: -L. -flto -fconvergent-functions %s 2>&1 | FileCheck -check-prefixes=LTO,MCPU %s
23-
// RUN: %clang -### --target=amdgcn-amd-amdhsa -mcpu=gfx906 -nogpulib \
23+
// RUN: %clang -### --target=amdgcn-amd-amdhsa -mcpu=gfx90a:xnack+:sramecc- -nogpulib \
2424
// RUN: -L. -fconvergent-functions %s 2>&1 | FileCheck -check-prefix=MCPU %s
2525
// LTO: clang{{.*}} "-flto=full"{{.*}}"-fconvergent-functions"
26-
// MCPU: ld.lld{{.*}}"-L."{{.*}}"-plugin-opt=mcpu=gfx906"
26+
// MCPU: ld.lld{{.*}}"-L."{{.*}}"-plugin-opt=mcpu=gfx90a"{{.*}}"-plugin-opt=-mattr=-sramecc,+xnack"
2727

2828
// RUN: %clang -### --target=amdgcn-amd-amdhsa -mcpu=gfx906 -nogpulib \
2929
// RUN: -fuse-ld=ld %s 2>&1 | FileCheck -check-prefixes=LD %s

0 commit comments

Comments
 (0)