Author: Martin Paul Lücke Date: 2026-11-09T11:55:26Z New Revision: d5a665b6383ef0fa3e18a470ad9388ab36a8b46a
URL: https://github.com/llvm/llvm-project/commit/d5a665b6383ef0fa3e18a470ad9388ab36a8b46a DIFF: https://github.com/llvm/llvm-project/commit/d5a665b6383ef0fa3e18a470ad9388ab36a8b46a.diff LOG: [CIR][CUDA] Emit device variables referenced only by host code (#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. --------- Co-authored-by: Steffen Larsen <[email protected]> Added: clang/test/CIR/CodeGenCUDA/host-used-device-var.cu Modified: clang/lib/CIR/CodeGen/CIRGenModule.cpp Removed: ################################################################################ diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp b/clang/lib/CIR/CodeGen/CIRGenModule.cpp index d7632b95309e8e..64d8e8c24f6d5a 100644 --- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp @@ -502,7 +502,12 @@ void CIRGenModule::emitDeferred() { // emitted that then need those vtables. assert(deferredVTables.empty()); - assert(!cir::MissingFeatures::cudaSupport()); + // 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); // 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..977f9c99879f4d --- /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 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 + +#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); +} _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
