Skip to content

Commit 5693200

Browse files
jofrnDavid Salinas
authored andcommitted
[HIP][Clang][CodeGen] Handle hip bin symbols properly. (llvm#107458)
Remove '_' in fatbin and gpubin symbol suffixes when missing TU hash ID. Internalize gpubin symbol so that it is not unresolved at link-time when symbol is not relocatable. Change-Id: Ide44c894eea086e66f67826a08d79151157273d7
1 parent d36d776 commit 5693200

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

clang/lib/CodeGen/CGCUDANV.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -775,8 +775,10 @@ llvm::Function *CGNVCUDARuntime::makeModuleCtorFunction() {
775775
FatBinStr = new llvm::GlobalVariable(
776776
CGM.getModule(), CGM.Int8Ty,
777777
/*isConstant=*/true, llvm::GlobalValue::ExternalLinkage, nullptr,
778-
"__hip_fatbin_" + CGM.getContext().getCUIDHash(), nullptr,
779-
llvm::GlobalVariable::NotThreadLocal);
778+
"__hip_fatbin" + (CGM.getLangOpts().CUID.empty()
779+
? ""
780+
: "_" + CGM.getContext().getCUIDHash()),
781+
nullptr, llvm::GlobalVariable::NotThreadLocal);
780782
cast<llvm::GlobalVariable>(FatBinStr)->setSection(FatbinConstantName);
781783
}
782784

@@ -829,8 +831,8 @@ llvm::Function *CGNVCUDARuntime::makeModuleCtorFunction() {
829831
// thread safety of the loaded program. Therefore we can assume sequential
830832
// execution of constructor functions here.
831833
if (IsHIP) {
832-
auto Linkage = CudaGpuBinary ? llvm::GlobalValue::InternalLinkage
833-
: llvm::GlobalValue::ExternalLinkage;
834+
auto Linkage = RelocatableDeviceCode ? llvm::GlobalValue::ExternalLinkage
835+
: llvm::GlobalValue::InternalLinkage;
834836
llvm::BasicBlock *IfBlock =
835837
llvm::BasicBlock::Create(Context, "if", ModuleCtorFunc);
836838
llvm::BasicBlock *ExitBlock =
@@ -840,10 +842,11 @@ llvm::Function *CGNVCUDARuntime::makeModuleCtorFunction() {
840842
GpuBinaryHandle = new llvm::GlobalVariable(
841843
TheModule, PtrTy, /*isConstant=*/false, Linkage,
842844
/*Initializer=*/
843-
CudaGpuBinary ? llvm::ConstantPointerNull::get(PtrTy) : nullptr,
844-
CudaGpuBinary
845-
? "__hip_gpubin_handle"
846-
: "__hip_gpubin_handle_" + CGM.getContext().getCUIDHash());
845+
!RelocatableDeviceCode ? llvm::ConstantPointerNull::get(PtrTy)
846+
: nullptr,
847+
"__hip_gpubin_handle" + (CGM.getLangOpts().CUID.empty()
848+
? ""
849+
: "_" + CGM.getContext().getCUIDHash()));
847850
GpuBinaryHandle->setAlignment(CGM.getPointerAlign().getAsAlign());
848851
// Prevent the weak symbol in different shared libraries being merged.
849852
if (Linkage != llvm::GlobalValue::InternalLinkage)

clang/test/CodeGenCUDA/device-stub.cu

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ __device__ void device_use() {
175175
// HIP-SAME: section ".hipFatBinSegment"
176176
// * variable to save GPU binary handle after initialization
177177
// CUDANORDC: @__[[PREFIX]]_gpubin_handle = internal global ptr null
178-
// HIPNEF: @__[[PREFIX]]_gpubin_handle_{{[0-9a-f]+}} = external hidden global ptr, align 8
178+
// HIPNEF: @__[[PREFIX]]_gpubin_handle_{{[0-9a-f]+}} = internal global ptr null, align 8
179179
// * constant unnamed string with NVModuleID
180180
// CUDARDC: [[MODULE_ID_GLOBAL:@.*]] = private constant
181181
// CUDARDC-SAME: c"[[MODULE_ID:.+]]\00", section "__nv_module_id", align 32

0 commit comments

Comments
 (0)