[clang] [CUDA] pass -fno-threadsafe-statics to GPU sub-compilations. (PR #117074)

Artem Belevich via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 21 10:36:54 PST 2024


https://github.com/Artem-B updated https://github.com/llvm/llvm-project/pull/117074

>From 1c8829a1defa6dd06aacb9a2047e7f79db238e2b Mon Sep 17 00:00:00 2001
From: Artem Belevich <tra at google.com>
Date: Wed, 20 Nov 2024 14:24:00 -0800
Subject: [PATCH 1/2] [CUDA] pass -fno-threadsafe-statics to GPU
 sub-compilations.

We do not have support for the threadsafe statics on the GPU side.

However, we do sometimes end up with empty local static initializers,
and those happen to trigger calls to `__cxa_guard*`, which breaks compilation.

Partially addresses https://github.com/llvm/llvm-project/issues/117023
---
 clang/lib/Driver/ToolChains/Cuda.cpp            |  5 +++--
 clang/test/Driver/cuda-no-threadsafe-statics.cu | 10 ++++++++++
 2 files changed, 13 insertions(+), 2 deletions(-)
 create mode 100644 clang/test/Driver/cuda-no-threadsafe-statics.cu

diff --git a/clang/lib/Driver/ToolChains/Cuda.cpp b/clang/lib/Driver/ToolChains/Cuda.cpp
index ddd5ea248ca0cc..102794829795da 100644
--- a/clang/lib/Driver/ToolChains/Cuda.cpp
+++ b/clang/lib/Driver/ToolChains/Cuda.cpp
@@ -856,8 +856,9 @@ void CudaToolChain::addClangTargetOptions(
           DeviceOffloadingKind == Action::OFK_Cuda) &&
          "Only OpenMP or CUDA offloading kinds are supported for NVIDIA GPUs.");
 
-  CC1Args.append(
-      {"-fcuda-is-device", "-mllvm", "-enable-memcpyopt-without-libcalls"});
+  CC1Args.append({"-fcuda-is-device", "-mllvm",
+                  "-enable-memcpyopt-without-libcalls",
+                  "-fno-threadsafe-statics"});
 
   // Unsized function arguments used for variadics were introduced in CUDA-9.0
   // We still do not support generating code that actually uses variadic
diff --git a/clang/test/Driver/cuda-no-threadsafe-statics.cu b/clang/test/Driver/cuda-no-threadsafe-statics.cu
new file mode 100644
index 00000000000000..fd0465f175846d
--- /dev/null
+++ b/clang/test/Driver/cuda-no-threadsafe-statics.cu
@@ -0,0 +1,10 @@
+// Check that -fno-thread-safe-statics get passed down to device-side
+// compilation only.
+//
+// RUN: not %clang -### --target=x86_64-linux-gnu -c --cuda-gpu-arch=sm_20 %s 2>&1 \
+// RUN:   | FileCheck %s
+//
+// CHECK: "-fcuda-is-device"
+// CHECK-SAME: "-fno-threadsafe-statics"
+// CHECK: "-triple" "x86_64-unknown-linux-gnu"
+// CHECK-NOT: "-fno-threadsafe-statics"

>From fa14b78a76010cde65d702b442027ab0b5cd879f Mon Sep 17 00:00:00 2001
From: Artem Belevich <tra at google.com>
Date: Thu, 21 Nov 2024 10:36:32 -0800
Subject: [PATCH 2/2] Update HIPAMD toolchain. Update tests.

---
 clang/lib/Driver/ToolChains/HIPAMD.cpp          | 2 +-
 clang/test/Driver/cuda-no-threadsafe-statics.cu | 7 +++++--
 2 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Driver/ToolChains/HIPAMD.cpp b/clang/lib/Driver/ToolChains/HIPAMD.cpp
index 4eb8c4f58923fd..42c48f6c9b7743 100644
--- a/clang/lib/Driver/ToolChains/HIPAMD.cpp
+++ b/clang/lib/Driver/ToolChains/HIPAMD.cpp
@@ -238,7 +238,7 @@ void HIPAMDToolChain::addClangTargetOptions(
   assert(DeviceOffloadingKind == Action::OFK_HIP &&
          "Only HIP offloading kinds are supported for GPUs.");
 
-  CC1Args.push_back("-fcuda-is-device");
+  CC1Args.append({"-fcuda-is-device", "-fno-threadsafe-statics"});
 
   if (!DriverArgs.hasFlag(options::OPT_fgpu_rdc, options::OPT_fno_gpu_rdc,
                           false))
diff --git a/clang/test/Driver/cuda-no-threadsafe-statics.cu b/clang/test/Driver/cuda-no-threadsafe-statics.cu
index fd0465f175846d..eb15312f8f7d14 100644
--- a/clang/test/Driver/cuda-no-threadsafe-statics.cu
+++ b/clang/test/Driver/cuda-no-threadsafe-statics.cu
@@ -1,8 +1,11 @@
 // Check that -fno-thread-safe-statics get passed down to device-side
 // compilation only.
 //
-// RUN: not %clang -### --target=x86_64-linux-gnu -c --cuda-gpu-arch=sm_20 %s 2>&1 \
-// RUN:   | FileCheck %s
+// RUN: %clang -### -x cuda --target=x86_64-linux-gnu -c --cuda-gpu-arch=sm_20 %s \
+// RUN:            -nocudainc -nocudalib 2>&1 | FileCheck %s
+
+// RUN: %clang -### -x hip --target=x86_64-linux-gnu -c --cuda-gpu-arch=gfx1010 %s \
+// RUN:            -nocudainc -nocudalib 2>&1 | FileCheck %s
 //
 // CHECK: "-fcuda-is-device"
 // CHECK-SAME: "-fno-threadsafe-statics"



More information about the cfe-commits mailing list