Skip to content

Commit 92eeff4

Browse files
authored
[Clang] Forward -Xarch_<arch> -Wl,foo for GPU toolchains (#126248)
Summary: This patch handles the use of `-Xarch_<arch> -Wl,foo` to send an argument to the linker for the embedded offloading jobs in the linker wrapper. This makes it equivalent to `-Xoffload-linker foo`.
1 parent c4c22a5 commit 92eeff4

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

clang/lib/Driver/ToolChains/Clang.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -9187,7 +9187,7 @@ void LinkerWrapper::ConstructJob(Compilation &C, const JobAction &JA,
91879187
OPT_fno_lto,
91889188
OPT_flto,
91899189
OPT_flto_EQ};
9190-
const llvm::DenseSet<unsigned> LinkerOptions{OPT_mllvm};
9190+
const llvm::DenseSet<unsigned> LinkerOptions{OPT_mllvm, OPT_Zlinker_input};
91919191
auto ShouldForward = [&](const llvm::DenseSet<unsigned> &Set, Arg *A) {
91929192
return Set.contains(A->getOption().getID()) ||
91939193
(A->getOption().getGroup().isValid() &&
@@ -9205,7 +9205,9 @@ void LinkerWrapper::ConstructJob(Compilation &C, const JobAction &JA,
92059205
ArgStringList CompilerArgs;
92069206
ArgStringList LinkerArgs;
92079207
for (Arg *A : C.getArgsForToolChain(TC, /*BoundArch=*/"", Kind)) {
9208-
if (ShouldForward(CompilerOptions, A))
9208+
if (A->getOption().matches(OPT_Zlinker_input))
9209+
LinkerArgs.emplace_back(A->getValue());
9210+
else if (ShouldForward(CompilerOptions, A))
92099211
A->render(Args, CompilerArgs);
92109212
else if (ShouldForward(LinkerOptions, A))
92119213
A->render(Args, LinkerArgs);

clang/test/Driver/offload-Xarch.c

+10-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// RUN: --target=x86_64-unknown-linux-gnu -Xopenmp-target=nvptx64-nvidia-cuda --offload-arch=sm_52,sm_60 -nogpuinc \
1515
// RUN: -Xopenmp-target=amdgcn-amd-amdhsa --offload-arch=gfx90a,gfx1030 -ccc-print-bindings -### %s 2>&1 \
1616
// RUN: | FileCheck -check-prefix=OPENMP %s
17-
//
17+
1818
// OPENMP: # "x86_64-unknown-linux-gnu" - "clang", inputs: ["[[INPUT:.+]]"], output: "[[HOST_BC:.+]]"
1919
// OPENMP: # "amdgcn-amd-amdhsa" - "clang", inputs: ["[[INPUT]]", "[[HOST_BC]]"], output: "[[GFX1030_BC:.+]]"
2020
// OPENMP: # "amdgcn-amd-amdhsa" - "clang", inputs: ["[[INPUT]]", "[[HOST_BC]]"], output: "[[GFX90A_BC:.+]]"
@@ -32,3 +32,12 @@
3232
// CUDA: "-cc1" "-triple" "nvptx64-nvidia-cuda" {{.*}}"-target-cpu" "sm_52" {{.*}}"-O3"
3333
// CUDA: "-cc1" "-triple" "nvptx64-nvidia-cuda" {{.*}}"-target-cpu" "sm_60" {{.*}}"-O0"
3434
// CUDA: "-cc1" "-triple" "x86_64-unknown-linux-gnu" {{.*}}"-O3"
35+
36+
// Make sure that `-Xarch_amdgcn` forwards libraries to the device linker.
37+
// RUN: %clang -fopenmp=libomp --offload-arch=gfx90a -nogpulib -nogpuinc \
38+
// RUN: -Xarch_amdgcn -Wl,-lfoo -### %s 2>&1 \
39+
// RUN: | FileCheck -check-prefix=LIBS %s
40+
// RUN: %clang -fopenmp=libomp --offload-arch=gfx90a -nogpulib -nogpuinc \
41+
// RUN: -Xoffload-linker-amdgcn-amd-amdhsa -lfoo -### %s 2>&1 \
42+
// RUN: | FileCheck -check-prefix=LIBS %s
43+
// LIBS: "--device-linker=amdgcn-amd-amdhsa=-lfoo"

0 commit comments

Comments
 (0)