[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
Wed Oct 28 05:11:25 PDT 2026


https://github.com/martin-luecke created https://github.com/llvm/llvm-project/pull/236392

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.

>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] [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);
+}



More information about the cfe-commits mailing list