[clang] [clang][cuda] Use the source filename for module ID (PR #209239)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Jul 13 09:48:14 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
@llvm/pr-subscribers-clang-codegen
Author: Mircea Trofin (mtrofin)
<details>
<summary>Changes</summary>
Prior to [#<!-- -->184065](vscode-file://vscode-app/Applications/Visual%20Studio%20Code.app/Contents/Resources/app/out/vs/code/electron-browser/workbench/workbench.html "https://github.com/llvm/llvm-project/issues/184065") (relanded in [#<!-- -->201849](vscode-file://vscode-app/Applications/Visual%20Studio%20Code.app/Contents/Resources/app/out/vs/code/electron-browser/workbench/workbench.html "https://github.com/llvm/llvm-project/issues/201849")), the code introduced in https://reviews.llvm.org/D42922 was using the GUID of an internal linkage GlobalValue to create a module id, which would then be used in a few cuda-specific places (including creating a symbol name suffix). [#<!-- -->184065](vscode-file://vscode-app/Applications/Visual%20Studio%20Code.app/Contents/Resources/app/out/vs/code/electron-browser/workbench/workbench.html "https://github.com/llvm/llvm-project/issues/184065") assumed the linkage of that symbol is external - which it isn't - and, thus, all module IDs computed for this would be identical.
The fix is to not rely on GlobalValue GUIDs in the first place. What is needed here is a hash that's specific to this module. So we're creating that and explicitly decoupling that calculation from GlobalValue's notion of GUIDs.
The change should be safe wrt uniqueness / hash collisions: the old behavior was computing a hash where the only distinguishing component was the module's `getSourceFileName`.
---
Full diff: https://github.com/llvm/llvm-project/pull/209239.diff
2 Files Affected:
- (modified) clang/lib/CodeGen/CGCUDANV.cpp (+1-1)
- (modified) clang/test/CodeGenCUDA/device-stub.cu (+13)
``````````diff
diff --git a/clang/lib/CodeGen/CGCUDANV.cpp b/clang/lib/CodeGen/CGCUDANV.cpp
index 733f2b7ecb46e..6467a5883b927 100644
--- a/clang/lib/CodeGen/CGCUDANV.cpp
+++ b/clang/lib/CodeGen/CGCUDANV.cpp
@@ -1046,7 +1046,7 @@ llvm::Function *CGNVCUDARuntime::makeModuleCtorFunction() {
OS << ModuleIDPrefix
<< llvm::format("%" PRIx64,
llvm::GlobalValue::getGUIDAssumingExternalLinkage(
- FatbinWrapper->getName()));
+ TheModule.getSourceFileName()));
llvm::Constant *ModuleIDConstant = makeConstantArray(
std::string(ModuleID), "", ModuleIDSectionName, 32, /*AddNull=*/true);
diff --git a/clang/test/CodeGenCUDA/device-stub.cu b/clang/test/CodeGenCUDA/device-stub.cu
index 38c7dc711ef1d..307ab190aa5be 100644
--- a/clang/test/CodeGenCUDA/device-stub.cu
+++ b/clang/test/CodeGenCUDA/device-stub.cu
@@ -64,6 +64,19 @@
// RUN: -o - -x hip\
// RUN: | FileCheck -allow-deprecated-dag-overlap %s --check-prefixes=ALL,WIN,HIP,HIPNEF
+// Verify that module IDs are distinct when the module source path is distinct.
+// RUN: rm -rf %t_distinct
+// RUN: mkdir -p %t_distinct
+// RUN: cp %s %t_distinct/filename1.cu
+// RUN: cp %s %t_distinct/filename2.cu
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm %t_distinct/filename1.cu -I%S \
+// RUN: -target-sdk-version=8.0 -fgpu-rdc -fcuda-include-gpubinary %t \
+// RUN: -o - | grep __nv_module_id &> %t_module_id_1
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm %t_distinct/filename2.cu -I%S \
+// RUN: -target-sdk-version=8.0 -fgpu-rdc -fcuda-include-gpubinary %t \
+// RUN: -o - | grep __nv_module_id &> %t_module_id_2
+// RUN: not diff %t_module_id_1 %t_module_id_2
+
#include "Inputs/cuda.h"
#ifndef NOGLOBALS
``````````
</details>
https://github.com/llvm/llvm-project/pull/209239
More information about the cfe-commits
mailing list