[clang] [CIR][CUDA] Emit device variables referenced only by host code (PR #236392)
Martin Paul Lücke via cfe-commits
cfe-commits at lists.llvm.org
Thu Oct 29 02:19:28 PDT 2026
https://github.com/martin-luecke updated https://github.com/llvm/llvm-project/pull/236392
>From 1a2f99143a1fa229d179181227ac2707851aa5ca Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Martin=20Paul=20L=C3=BCcke?= <martin.luecke at amd.com>
Date: Wed, 28 Oct 2026 11:29:58 +0000
Subject: [PATCH 1/2] [CIR][CUDA] Emit device variables ODR-used only by host
code
Device compilation skips host-only function bodies, so device variables
referenced only from those functions can remain deferred and be omitted.
Append CUDADeviceVarODRUsedByHost to the deferred worklist during CUDA/HIP
device compilation, matching classic CodeGen. Preserve the AST collection
for subsequent variable handling and reuse the existing definition checks.
Add CUDA and HIP coverage for host-only ODR-use of static device and
constant variables, with negative checks for host functions and a
sizeof-only reference.
---
clang/lib/CIR/CodeGen/CIRGenModule.cpp | 5 +++-
.../CIR/CodeGenCUDA/host-used-device-var.cu | 29 +++++++++++++++++++
2 files changed, 33 insertions(+), 1 deletion(-)
create mode 100644 clang/test/CIR/CodeGenCUDA/host-used-device-var.cu
diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp b/clang/lib/CIR/CodeGen/CIRGenModule.cpp
index 56fb14e5c6baea..06b880a0c690b9 100644
--- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp
@@ -498,7 +498,10 @@ void CIRGenModule::emitDeferred() {
// emitted that then need those vtables.
assert(deferredVTables.empty());
- assert(!cir::MissingFeatures::cudaSupport());
+ // A host ODR-use requires a device definition even without device-side uses.
+ if (getLangOpts().CUDA && getLangOpts().CUDAIsDevice)
+ llvm::append_range(deferredDeclsToEmit,
+ getASTContext().CUDADeviceVarODRUsedByHost);
// Stop if we're out of both deferred vtables and deferred declarations.
if (deferredDeclsToEmit.empty())
diff --git a/clang/test/CIR/CodeGenCUDA/host-used-device-var.cu b/clang/test/CIR/CodeGenCUDA/host-used-device-var.cu
new file mode 100644
index 00000000000000..cd1e6b5bd93c61
--- /dev/null
+++ b/clang/test/CIR/CodeGenCUDA/host-used-device-var.cu
@@ -0,0 +1,29 @@
+// RUN: %clang_cc1 -triple nvptx64-nvidia-cuda -x cuda -fcuda-is-device \
+// RUN: -emit-cir %s -o %t.cir
+// RUN: FileCheck %s --input-file=%t.cir
+// RUN: FileCheck %s --check-prefix=NEG --input-file=%t.cir
+// RUN: %clang_cc1 -triple amdgcn-amd-amdhsa -x hip -fcuda-is-device \
+// RUN: -emit-cir %s -o %t.cir
+// RUN: FileCheck %s --input-file=%t.cir
+// RUN: FileCheck %s --check-prefix=NEG --input-file=%t.cir
+
+#include "Inputs/cuda.h"
+
+// Device variables ODR-used only by host code still need device definitions.
+// CHECK-DAG: cir.global {{.+}} @_ZL10device_var =
+static __device__ int device_var;
+// CHECK-DAG: cir.global {{.+}} @_ZL12constant_var =
+static __constant__ int constant_var;
+
+// NEG-NOT: unused_var
+static __device__ int unused_var;
+
+// NEG-NOT: getDeviceSymbol
+void getDeviceSymbol(const void *);
+
+// NEG-NOT: host_only
+void host_only() {
+ getDeviceSymbol(&device_var);
+ getDeviceSymbol(&constant_var);
+ (void)sizeof(unused_var);
+}
>From 9d20ad8f1926a617dc03a4e75eb54572d76cd8e9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Martin=20Paul=20L=C3=BCcke?= <martin.luecke at amd.com>
Date: Thu, 29 Oct 2026 09:16:23 +0000
Subject: [PATCH 2/2] address review
---
clang/lib/CIR/CodeGen/CIRGenModule.cpp | 4 +++-
clang/test/CIR/CodeGenCUDA/host-used-device-var.cu | 2 +-
2 files changed, 4 insertions(+), 2 deletions(-)
diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp b/clang/lib/CIR/CodeGen/CIRGenModule.cpp
index 06b880a0c690b9..87ccfcf63e1965 100644
--- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp
@@ -498,7 +498,9 @@ void CIRGenModule::emitDeferred() {
// emitted that then need those vtables.
assert(deferredVTables.empty());
- // A host ODR-use requires a device definition even without device-side uses.
+ // Emit CUDA/HIP static device variables referenced by host code only.
+ // Note we should not clear CUDADeviceVarODRUsedByHost since it is still
+ // needed for further handling.
if (getLangOpts().CUDA && getLangOpts().CUDAIsDevice)
llvm::append_range(deferredDeclsToEmit,
getASTContext().CUDADeviceVarODRUsedByHost);
diff --git a/clang/test/CIR/CodeGenCUDA/host-used-device-var.cu b/clang/test/CIR/CodeGenCUDA/host-used-device-var.cu
index cd1e6b5bd93c61..977f9c99879f4d 100644
--- a/clang/test/CIR/CodeGenCUDA/host-used-device-var.cu
+++ b/clang/test/CIR/CodeGenCUDA/host-used-device-var.cu
@@ -2,7 +2,7 @@
// RUN: -emit-cir %s -o %t.cir
// RUN: FileCheck %s --input-file=%t.cir
// RUN: FileCheck %s --check-prefix=NEG --input-file=%t.cir
-// RUN: %clang_cc1 -triple amdgcn-amd-amdhsa -x hip -fcuda-is-device \
+// RUN: %clang_cc1 -triple amdgpu-amd-amdhsa -x hip -fcuda-is-device \
// RUN: -emit-cir %s -o %t.cir
// RUN: FileCheck %s --input-file=%t.cir
// RUN: FileCheck %s --check-prefix=NEG --input-file=%t.cir
More information about the cfe-commits
mailing list