[clang] [CUDA] Exclude device static locals from CUDADeviceVarODRUsedByHost (PR #245671)

Mariya Podchishchaeva via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 24 08:46:19 PST 2026


https://github.com/Fznamznon created https://github.com/llvm/llvm-project/pull/245671

There is no way host can use them anyway so there is no point to force emission if device code doesn't use them. In fact, some of tests I changed are invalid per CUDA programming guide and should probably be rejected in a separate PR.

Forced emission of static device locals via CUDADeviceVarODRUsedByHost caused emission of duplicated global variables if they are used within device code because CUDADeviceVarODRUsedByHost goes via EmitDeferred path which doesn't know about static locals at all and emits every variable it sees as a namespace scope global and therefore doesn't check whether a static variable is already emitted as normal static local emission would. That didn't fire for classic codegen because LLVM would just attach .n posfix to a duplicated symbol but that fired in CIR whose verifier actually checks if emitted symbol is a duplicate.

Assisted-by: claude

>From 5a2b6fc98375f44f9299dd3135c3750c26c38447 Mon Sep 17 00:00:00 2001
From: Mariya Podchishchaeva <Mariya.Podchishchaeva at amd.com>
Date: Tue, 24 Nov 2026 10:31:36 -0600
Subject: [PATCH] [CUDA] Exclude device static locals from
 CUDADeviceVarODRUsedByHost

There is no way host can use them anyway so there is no point to
force emission if device code doesn't use them. In fact, some of
tests I changed are invalid per CUDA programming guide and should
probably be rejected in a separate PR.

Forced emission of static device locals via CUDADeviceVarODRUsedByHost
caused emission of duplicated global variables if they are used within
device code because CUDADeviceVarODRUsedByHost goes via EmitDeferred
path which doesn't know about static locals at all and emits
every variable it sees as a namespace scope global and therefore
doesn't check whether a static variable is already emitted as normal
static local emission would. That didn't fire for classic codegen
because LLVM would just attach .n posfix to a duplicated symbol but
that fired in CIR whose verifier actually checks if emitted symbol is
a duplicate.

Assisted-by: claude
---
 clang/lib/Sema/SemaExpr.cpp                   |  1 +
 .../test/CodeGenCUDA/host-used-device-var.cu  | 46 ++++++++++++++++---
 2 files changed, 41 insertions(+), 6 deletions(-)

diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 571ef6ea6b19eb..a3528eeb303e95 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -19715,6 +19715,7 @@ MarkVarDeclODRUsed(ValueDecl *V, SourceLocation Loc, Sema &SemaRef,
                  Var->hasAttr<CUDADeviceAttr>() &&
                  !Var->getAttr<CUDADeviceAttr>()->isImplicit())) &&
                !Var->hasAttr<CUDASharedAttr>() &&
+               !Var->isStaticLocal() &&
                (UserTarget == CUDAFunctionTarget::Host ||
                 UserTarget == CUDAFunctionTarget::HostDevice)) {
       // Record a CUDA/HIP device side variable if it is ODR-used
diff --git a/clang/test/CodeGenCUDA/host-used-device-var.cu b/clang/test/CodeGenCUDA/host-used-device-var.cu
index c780e7b478324b..b9c421a8b74363 100644
--- a/clang/test/CodeGenCUDA/host-used-device-var.cu
+++ b/clang/test/CodeGenCUDA/host-used-device-var.cu
@@ -2,6 +2,13 @@
 // RUN: %clang_cc1 -triple amdgpu-amd-amdhsa -fcuda-is-device -x hip %s \
 // RUN:   -std=c++17 -O3 -mllvm -amdgpu-internalize-symbols -emit-llvm -o - \
 // RUN:   -cuid=123 | FileCheck -check-prefix=DEV %s
+// RUN: %if cir-enabled %{ %clang_cc1 -fclangir -triple amdgpu-amd-amdhsa \
+// RUN:   -fcuda-is-device -x hip %s -std=c++17 -O3 \
+// RUN:   -mllvm -amdgpu-internalize-symbols -emit-llvm -o - -cuid=123 \
+// RUN:   | FileCheck -check-prefix=DEV %s %}
+// RUN: %if cir-enabled %{ %clang_cc1 -fclangir -triple amdgpu-amd-amdhsa \
+// RUN:   -fcuda-is-device -x hip %s -std=c++17 -emit-cir -o - -cuid=123 \
+// RUN:   | FileCheck -check-prefix=CIR %s %}
 // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -x hip %s \
 // RUN:   -std=c++17 -O3 -emit-llvm -o - -cuid=123 | FileCheck -check-prefix=HOST %s
 
@@ -10,6 +17,10 @@
 // RUN: %clang_cc1 -triple amdgpu-amd-amdhsa -fcuda-is-device -x hip %s \
 // RUN:   -std=c++17 -O3 -mllvm -amdgpu-internalize-symbols -emit-llvm -o - \
 // RUN:   | FileCheck -check-prefix=DEV-NEG %s
+// RUN: %if cir-enabled %{ %clang_cc1 -fclangir -triple amdgpu-amd-amdhsa \
+// RUN:   -fcuda-is-device -x hip %s -std=c++17 -O3 \
+// RUN:   -mllvm -amdgpu-internalize-symbols -emit-llvm -o - \
+// RUN:   | FileCheck -check-prefix=DEV-NEG %s %}
 // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -x hip %s \
 // RUN:   -std=c++17 -O3 -emit-llvm -o - | FileCheck -check-prefix=HOST-NEG %s
 
@@ -82,8 +93,7 @@ inline __constant__ constexpr int constexpr_var2b = 2;
 void use(func_t<int> p);
 __host__ __device__ void use(const int *p);
 
-// Check static device variable in host function.
-// DEV-DAG:  @_ZZ4fun1vE11static_var1 = addrspace(1) externally_initialized global i32 3
+// DEV-NEG-NOT: @_ZZ4fun1vE11static_var1
 void fun1() {
   static __device__ int static_var1 = 3;
   use(&u1);
@@ -101,7 +111,7 @@ void fun1() {
 
 // Check static variable in host device function.
 // DEV-DAG:  @_ZZ4fun2vE11static_var2 = internal addrspace(1) global i32 4
-// DEV-DAG:  @_ZZ4fun2vE11static_var3 = addrspace(1) global i32 4
+// DEV-DAG:  @_ZZ4fun2vE11static_var3 = internal addrspace(1) global i32 4
 __host__ __device__ void fun2() {
   static int static_var2 = 4;
   static __device__ int static_var3 = 4;
@@ -121,7 +131,7 @@ __global__ void kern1(int **x) {
 // as host variables on host side, therefore should not be forced
 // to be emitted on device.
 
-// DEV-DAG: @_ZZZN21TestStaticVarInLambda3funEvENKUlPcE_clES0_E4var2 = addrspace(1) externally_initialized global i32 5
+// DEV-NEG-NOT: @_ZZZN21TestStaticVarInLambda3funEvENKUlPcE_clES0_E4var2
 // DEV-NEG-NOT: @_ZZZN21TestStaticVarInLambda3funEvENKUlPcE_clES0_E4var1
 namespace TestStaticVarInLambda {
 class A {
@@ -181,12 +191,36 @@ public:
 };
 }
 
+// Lambdas are always implicitly HD. If a static variable defined in the
+// outer kernel/device function is referenced by lambda, that is technically
+// a host referenced device variable. Make sure it doesn't get into
+// CUDADeviceVarODRUsedByHost and not emitted twice.
+namespace TestDeviceStaticVarIsNotDuplicated {
+struct S { int x; };
+
+template <int N>
+static __global__ void kern1(const S **out) {
+  static constexpr S s{N};
+  *out = [&] { return &s; }();
+}
+
+void fun() {
+  (void)kern1<1>;
+  (void)kern1<2>;
+}
+}
+
+// DEV-DAG: @_ZZN34TestDeviceStaticVarIsNotDuplicatedL5kern1ILi1EEEvPPKNS_1SEE1s = internal addrspace(4) constant %"struct.TestDeviceStaticVarIsNotDuplicated::S" { i32 1 }
+// DEV-DAG: @_ZZN34TestDeviceStaticVarIsNotDuplicatedL5kern1ILi2EEEvPPKNS_1SEE1s = internal addrspace(4) constant %"struct.TestDeviceStaticVarIsNotDuplicated::S" { i32 2 }
+// DEV-NEG-NOT: @_ZZN34TestDeviceStaticVarIsNotDuplicatedL5kern1ILi{{[12]}}EEEvPPKNS_1SEE1s.
+
+// CIR-DAG: cir.global "private" constant internal dso_local target_address_space(4) @_ZZN34TestDeviceStaticVarIsNotDuplicatedL5kern1ILi1EEEvPPKNS_1SEE1s = #cir.const_record<{#cir.int<1> : !s32i}>
+// CIR-DAG: cir.global "private" constant internal dso_local target_address_space(4) @_ZZN34TestDeviceStaticVarIsNotDuplicatedL5kern1ILi2EEEvPPKNS_1SEE1s = #cir.const_record<{#cir.int<2> : !s32i}>
+
 // Check the exact list of variables to ensure @_ZL2u4 is not among them.
 // DEV: @llvm.compiler.used = {{[^@]*}} @_Z10p_add_funcIiE
 // DEV-SAME: {{^[^@]*}} @_ZL15constexpr_var2a
 // DEV-SAME: {{^[^@]*}} @_ZL2u3
-// DEV-SAME: {{^[^@]*}} @_ZZ4fun1vE11static_var1
-// DEV-SAME: {{^[^@]*}} @_ZZZN21TestStaticVarInLambda3funEvENKUlPcE_clES0_E4var2
 // DEV-SAME: {{^[^@]*}} @__hip_cuid_{{[0-9a-f]+}}
 // DEV-SAME: {{^[^@]*}} @constexpr_var2b
 // DEV-SAME: {{^[^@]*}} @inline_var



More information about the cfe-commits mailing list