Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions clang/lib/CodeGen/CGCUDANV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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()));
Comment thread
mtrofin marked this conversation as resolved.
Comment thread
mtrofin marked this conversation as resolved.
llvm::Constant *ModuleIDConstant = makeConstantArray(
std::string(ModuleID), "", ModuleIDSectionName, 32, /*AddNull=*/true);

Expand Down
13 changes: 13 additions & 0 deletions clang/test/CodeGenCUDA/device-stub.cu
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading