[clang] [CIR][CUDA] Emit device variables referenced only by host code (PR #236392)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Oct 28 05:12:10 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clangir
Author: Martin Paul Lücke (martin-luecke)
<details>
<summary>Changes</summary>
CIR can omit static CUDA/HIP device and constant variables when they are referenced only by host code. For example, a host function may pass a device variable’s address to a device-symbol lookup API even though no device function uses it. CIR skips host function bodies during device compilation, so those references do not cause the variables to be emitted.
Add these variables to the list that CIR emits at the end of compilation, matching classic Clang code generation. Preserve the recorded host references so the compiler can make the variables accessible to host code and prevent them from being removed as unused.
Add a test checking that the device variables are emitted, the host-only function remains absent, and a variable referenced only through `sizeof` remains absent.
---
Full diff: https://github.com/llvm/llvm-project/pull/236392.diff
2 Files Affected:
- (modified) clang/lib/CIR/CodeGen/CIRGenModule.cpp (+4-1)
- (added) clang/test/CIR/CodeGenCUDA/host-used-device-var.cu (+29)
``````````diff
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);
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/236392
More information about the cfe-commits
mailing list