diff --git a/clang/lib/CodeGen/CGCUDANV.cpp b/clang/lib/CodeGen/CGCUDANV.cpp index 733f2b7ecb46e..416ed935c1b30 100644 --- a/clang/lib/CodeGen/CGCUDANV.cpp +++ b/clang/lib/CodeGen/CGCUDANV.cpp @@ -29,6 +29,7 @@ #include "llvm/IR/ReplaceConstant.h" #include "llvm/ProfileData/InstrProf.h" #include "llvm/Support/Format.h" +#include "llvm/Support/MD5.h" #include "llvm/Support/VirtualFileSystem.h" #include "llvm/Transforms/Utils/ModuleUtils.h" @@ -1041,12 +1042,19 @@ llvm::Function *CGNVCUDARuntime::makeModuleCtorFunction() { } } else { // Generate a unique module ID. + // Note that this is unique in a build (with some collision probability + // inherent to MD5 hashing) as long as each compilation sees modules with + // different `SourceFileName`s. Builds using absolute paths or paths + // relative to the same base path should be OK. This is similar to the + // guarantees for ThinLTO and GlobalValue's GUID. + // If desired, a stronger uniqueness guarantee could be computed (with a + // small refactoring) with `llvm::getUniqueModuleId`, which hashes the + // module content (and, therefore, a compile-time tradeoff). SmallString<64> ModuleID; llvm::raw_svector_ostream OS(ModuleID); OS << ModuleIDPrefix << llvm::format("%" PRIx64, - llvm::GlobalValue::getGUIDAssumingExternalLinkage( - FatbinWrapper->getName())); + llvm::MD5Hash(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