[clang][cuda] Use the source filename for module ID#209239
Conversation
|
@llvm/pr-subscribers-clang @llvm/pr-subscribers-clang-codegen Author: Mircea Trofin (mtrofin) ChangesPrior 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 Full diff: https://github.com/llvm/llvm-project/pull/209239.diff 2 Files Affected:
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
|
51621d0 to
e4bdd34
Compare
teresajohnson
left a comment
There was a problem hiding this comment.
lgtm but with a comment suggestion
yxsamliu
left a comment
There was a problem hiding this comment.
LGTM. Matches CUDA RDC per-TU registration.
e4bdd34 to
6116c0d
Compare
6116c0d to
0521f37
Compare
|
ignoring CI LLDB API failures, low chance they are related, plus apparently known issue (@boomanaiden154) |
…ce_filename_for_module_id

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 ".").