Skip to content

[clang][cuda] Use the source filename for module ID#209239

Merged
mtrofin merged 2 commits into
mainfrom
users/mtrofin/07-13-_clang_cuda_use_the_source_filename_for_module_id
Jul 14, 2026
Merged

[clang][cuda] Use the source filename for module ID#209239
mtrofin merged 2 commits into
mainfrom
users/mtrofin/07-13-_clang_cuda_use_the_source_filename_for_module_id

Conversation

@mtrofin

@mtrofin mtrofin commented Jul 13, 2026

Copy link
Copy Markdown
Member

Prior to #184065 (relanded in #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 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.

FWIW, there's a stronger module ID we could compute, with llvm::getUniqueModuleId (or with a trivial refactoring of it, because it prepends a ".").

mtrofin commented Jul 13, 2026

Copy link
Copy Markdown
Member Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

@mtrofin mtrofin marked this pull request as ready for review July 13, 2026 16:47
@llvmorg-github-actions llvmorg-github-actions Bot added clang Clang issues not falling into any other category clang:codegen IR generation bugs: mangling, exceptions, etc. labels Jul 13, 2026
@llvmorg-github-actions

llvmorg-github-actions Bot commented Jul 13, 2026

Copy link
Copy Markdown

@llvm/pr-subscribers-clang

@llvm/pr-subscribers-clang-codegen

Author: Mircea Trofin (mtrofin)

Changes

Prior to #184065 (relanded in #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 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 --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

@mtrofin mtrofin force-pushed the users/mtrofin/07-13-_clang_cuda_use_the_source_filename_for_module_id branch from 51621d0 to e4bdd34 Compare July 13, 2026 16:48

@alexfh alexfh left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! I've verified that it fixes the compilation problems caused by 39dcb0f.

Comment thread clang/lib/CodeGen/CGCUDANV.cpp

@teresajohnson teresajohnson left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm but with a comment suggestion

Comment thread clang/lib/CodeGen/CGCUDANV.cpp

@yxsamliu yxsamliu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Matches CUDA RDC per-TU registration.

@mtrofin mtrofin force-pushed the users/mtrofin/07-13-_clang_cuda_use_the_source_filename_for_module_id branch from e4bdd34 to 6116c0d Compare July 14, 2026 14:39
@mtrofin mtrofin force-pushed the users/mtrofin/07-13-_clang_cuda_use_the_source_filename_for_module_id branch from 6116c0d to 0521f37 Compare July 14, 2026 14:42
@mtrofin mtrofin enabled auto-merge (squash) July 14, 2026 14:47
@mtrofin

mtrofin commented Jul 14, 2026

Copy link
Copy Markdown
Member Author

ignoring CI LLDB API failures, low chance they are related, plus apparently known issue (@boomanaiden154)

@mtrofin mtrofin disabled auto-merge July 14, 2026 16:30
@mtrofin mtrofin merged commit a80e98e into main Jul 14, 2026
3 of 8 checks passed
@mtrofin mtrofin deleted the users/mtrofin/07-13-_clang_cuda_use_the_source_filename_for_module_id branch July 14, 2026 16:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:codegen IR generation bugs: mangling, exceptions, etc. clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants