Skip to content

[Clang] Forward -Xarch_<arch> -Wl,foo for GPU toolchains #126248

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 7, 2025

Conversation

jhuber6
Copy link
Contributor

@jhuber6 jhuber6 commented Feb 7, 2025

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.

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`.
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' labels Feb 7, 2025
@llvmbot
Copy link
Member

llvmbot commented Feb 7, 2025

@llvm/pr-subscribers-clang-driver

Author: Joseph Huber (jhuber6)

Changes

Summary:
This patch handles the use of -Xarch_&lt;arch&gt; -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.


Full diff: https://github.com/llvm/llvm-project/pull/126248.diff

2 Files Affected:

  • (modified) clang/lib/Driver/ToolChains/Clang.cpp (+5-2)
  • (modified) clang/test/Driver/offload-Xarch.c (+10-1)
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index c0891d46b0a62cd..55d2936fc5bbb53 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -9184,8 +9184,9 @@ void LinkerWrapper::ConstructJob(Compilation &C, const JobAction &JA,
       OPT_load,
       OPT_fno_lto,
       OPT_flto,
+      OPT_Zlinker_input,
       OPT_flto_EQ};
-  const llvm::DenseSet<unsigned> LinkerOptions{OPT_mllvm};
+  const llvm::DenseSet<unsigned> LinkerOptions{OPT_mllvm, OPT_Zlinker_input};
   auto ShouldForward = [&](const llvm::DenseSet<unsigned> &Set, Arg *A) {
     return Set.contains(A->getOption().getID()) ||
            (A->getOption().getGroup().isValid() &&
@@ -9203,7 +9204,9 @@ void LinkerWrapper::ConstructJob(Compilation &C, const JobAction &JA,
       ArgStringList CompilerArgs;
       ArgStringList LinkerArgs;
       for (Arg *A : C.getArgsForToolChain(TC, /*BoundArch=*/"", Kind)) {
-        if (ShouldForward(CompilerOptions, A))
+        if (A->getOption().matches(OPT_Zlinker_input))
+          LinkerArgs.emplace_back(A->getValue());
+        else if (ShouldForward(CompilerOptions, A))
           A->render(Args, CompilerArgs);
         else if (ShouldForward(LinkerOptions, A))
           A->render(Args, LinkerArgs);
diff --git a/clang/test/Driver/offload-Xarch.c b/clang/test/Driver/offload-Xarch.c
index 17db891b068340b..18c68f2acc884bc 100644
--- a/clang/test/Driver/offload-Xarch.c
+++ b/clang/test/Driver/offload-Xarch.c
@@ -14,7 +14,7 @@
 // RUN:   --target=x86_64-unknown-linux-gnu -Xopenmp-target=nvptx64-nvidia-cuda --offload-arch=sm_52,sm_60 -nogpuinc \
 // RUN:   -Xopenmp-target=amdgcn-amd-amdhsa --offload-arch=gfx90a,gfx1030 -ccc-print-bindings -### %s 2>&1 \
 // RUN: | FileCheck -check-prefix=OPENMP %s
-//
+
 // OPENMP: # "x86_64-unknown-linux-gnu" - "clang", inputs: ["[[INPUT:.+]]"], output: "[[HOST_BC:.+]]"
 // OPENMP: # "amdgcn-amd-amdhsa" - "clang", inputs: ["[[INPUT]]", "[[HOST_BC]]"], output: "[[GFX1030_BC:.+]]"
 // OPENMP: # "amdgcn-amd-amdhsa" - "clang", inputs: ["[[INPUT]]", "[[HOST_BC]]"], output: "[[GFX90A_BC:.+]]"
@@ -32,3 +32,12 @@
 // CUDA: "-cc1" "-triple" "nvptx64-nvidia-cuda" {{.*}}"-target-cpu" "sm_52" {{.*}}"-O3"
 // CUDA: "-cc1" "-triple" "nvptx64-nvidia-cuda" {{.*}}"-target-cpu" "sm_60" {{.*}}"-O0"
 // CUDA: "-cc1" "-triple" "x86_64-unknown-linux-gnu" {{.*}}"-O3"
+
+// Make sure that `-Xarch_amdgcn` forwards libraries to the device linker.
+// RUN: %clang -fopenmp=libomp --offload-arch=gfx90a -nogpulib -nogpuinc \
+// RUN:   -Xarch_amdgcn -Wl,-lfoo -### %s 2>&1 \
+// RUN: | FileCheck -check-prefix=LIBS %s
+// RUN: %clang -fopenmp=libomp --offload-arch=gfx90a -nogpulib -nogpuinc \
+// RUN:   -Xoffload-linker-amdgcn-amd-amdhsa -lfoo -### %s 2>&1 \
+// RUN: | FileCheck -check-prefix=LIBS %s
+// LIBS: "--device-linker=amdgcn-amd-amdhsa=-lfoo"

@llvmbot
Copy link
Member

llvmbot commented Feb 7, 2025

@llvm/pr-subscribers-clang

Author: Joseph Huber (jhuber6)

Changes

Summary:
This patch handles the use of -Xarch_&lt;arch&gt; -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.


Full diff: https://github.com/llvm/llvm-project/pull/126248.diff

2 Files Affected:

  • (modified) clang/lib/Driver/ToolChains/Clang.cpp (+5-2)
  • (modified) clang/test/Driver/offload-Xarch.c (+10-1)
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index c0891d46b0a62cd..55d2936fc5bbb53 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -9184,8 +9184,9 @@ void LinkerWrapper::ConstructJob(Compilation &C, const JobAction &JA,
       OPT_load,
       OPT_fno_lto,
       OPT_flto,
+      OPT_Zlinker_input,
       OPT_flto_EQ};
-  const llvm::DenseSet<unsigned> LinkerOptions{OPT_mllvm};
+  const llvm::DenseSet<unsigned> LinkerOptions{OPT_mllvm, OPT_Zlinker_input};
   auto ShouldForward = [&](const llvm::DenseSet<unsigned> &Set, Arg *A) {
     return Set.contains(A->getOption().getID()) ||
            (A->getOption().getGroup().isValid() &&
@@ -9203,7 +9204,9 @@ void LinkerWrapper::ConstructJob(Compilation &C, const JobAction &JA,
       ArgStringList CompilerArgs;
       ArgStringList LinkerArgs;
       for (Arg *A : C.getArgsForToolChain(TC, /*BoundArch=*/"", Kind)) {
-        if (ShouldForward(CompilerOptions, A))
+        if (A->getOption().matches(OPT_Zlinker_input))
+          LinkerArgs.emplace_back(A->getValue());
+        else if (ShouldForward(CompilerOptions, A))
           A->render(Args, CompilerArgs);
         else if (ShouldForward(LinkerOptions, A))
           A->render(Args, LinkerArgs);
diff --git a/clang/test/Driver/offload-Xarch.c b/clang/test/Driver/offload-Xarch.c
index 17db891b068340b..18c68f2acc884bc 100644
--- a/clang/test/Driver/offload-Xarch.c
+++ b/clang/test/Driver/offload-Xarch.c
@@ -14,7 +14,7 @@
 // RUN:   --target=x86_64-unknown-linux-gnu -Xopenmp-target=nvptx64-nvidia-cuda --offload-arch=sm_52,sm_60 -nogpuinc \
 // RUN:   -Xopenmp-target=amdgcn-amd-amdhsa --offload-arch=gfx90a,gfx1030 -ccc-print-bindings -### %s 2>&1 \
 // RUN: | FileCheck -check-prefix=OPENMP %s
-//
+
 // OPENMP: # "x86_64-unknown-linux-gnu" - "clang", inputs: ["[[INPUT:.+]]"], output: "[[HOST_BC:.+]]"
 // OPENMP: # "amdgcn-amd-amdhsa" - "clang", inputs: ["[[INPUT]]", "[[HOST_BC]]"], output: "[[GFX1030_BC:.+]]"
 // OPENMP: # "amdgcn-amd-amdhsa" - "clang", inputs: ["[[INPUT]]", "[[HOST_BC]]"], output: "[[GFX90A_BC:.+]]"
@@ -32,3 +32,12 @@
 // CUDA: "-cc1" "-triple" "nvptx64-nvidia-cuda" {{.*}}"-target-cpu" "sm_52" {{.*}}"-O3"
 // CUDA: "-cc1" "-triple" "nvptx64-nvidia-cuda" {{.*}}"-target-cpu" "sm_60" {{.*}}"-O0"
 // CUDA: "-cc1" "-triple" "x86_64-unknown-linux-gnu" {{.*}}"-O3"
+
+// Make sure that `-Xarch_amdgcn` forwards libraries to the device linker.
+// RUN: %clang -fopenmp=libomp --offload-arch=gfx90a -nogpulib -nogpuinc \
+// RUN:   -Xarch_amdgcn -Wl,-lfoo -### %s 2>&1 \
+// RUN: | FileCheck -check-prefix=LIBS %s
+// RUN: %clang -fopenmp=libomp --offload-arch=gfx90a -nogpulib -nogpuinc \
+// RUN:   -Xoffload-linker-amdgcn-amd-amdhsa -lfoo -### %s 2>&1 \
+// RUN: | FileCheck -check-prefix=LIBS %s
+// LIBS: "--device-linker=amdgcn-amd-amdhsa=-lfoo"

@jhuber6 jhuber6 requested a review from sarnex February 7, 2025 14:37
@jhuber6 jhuber6 merged commit 92eeff4 into llvm:main Feb 7, 2025
8 checks passed
@jhuber6 jhuber6 deleted the ToolchainLinker branch February 7, 2025 17:43
@dyung
Copy link
Collaborator

dyung commented Feb 9, 2025

@jhuber6 I've seen 2 seemingly random failures of the offload-Xarch.c test on my mac buildbots lately. Any idea why this might be happening?

https://lab.llvm.org/buildbot/#/builders/190/builds/14302
https://lab.llvm.org/buildbot/#/builders/23/builds/7414

The test generally seems to pass, but two of the bots hit the same failure in this test.

@dyung
Copy link
Collaborator

dyung commented Feb 9, 2025

And just now another failure: https://lab.llvm.org/buildbot/#/builders/190/builds/14342

@jhuber6
Copy link
Contributor Author

jhuber6 commented Feb 9, 2025

And just now another failure: https://lab.llvm.org/buildbot/#/builders/190/builds/14342

Honestly, I don't know. I'd say that it's because we don't support offloading on Darwin, but the triple is supposed to be x64. I could probably just set the test to unsupported for darwin since GPU offloading isn't supported there. Hopefully a32efd8 resolved it.

@dyung
Copy link
Collaborator

dyung commented Feb 11, 2025

And just now another failure: https://lab.llvm.org/buildbot/#/builders/190/builds/14342

Honestly, I don't know. I'd say that it's because we don't support offloading on Darwin, but the triple is supposed to be x64. I could probably just set the test to unsupported for darwin since GPU offloading isn't supported there. Hopefully a32efd8 resolved it.

I'm seeing our internal downstream builder hit the exact same error in this test, but that is building a Windows hosted compiler targeting Windows. Looking at our runs, it seems to have started appearing after your change 479ffe8 oddly enough.

@jhuber6
Copy link
Contributor Author

jhuber6 commented Feb 11, 2025

I'm seeing our internal downstream builder hit the exact same error in this test, but that is building a Windows hosted compiler targeting Windows. Looking at our runs, it seems to have started appearing after your change 479ffe8 oddly enough.

Maybe there's some weird implicit behavior here? Why isn't it failing on the public build bots then. I honestly don't have a good guess why.

Icohedron pushed a commit to Icohedron/llvm-project that referenced this pull request Feb 11, 2025
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`.
@dyung
Copy link
Collaborator

dyung commented Feb 18, 2025

I'm seeing our internal downstream builder hit the exact same error in this test, but that is building a Windows hosted compiler targeting Windows. Looking at our runs, it seems to have started appearing after your change 479ffe8 oddly enough.

Maybe there's some weird implicit behavior here? Why isn't it failing on the public build bots then. I honestly don't have a good guess why.

I just saw it failed on our public Windows bot:

https://lab.llvm.org/buildbot/#/builders/46/builds/12224

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants