Skip to content

Commit afbf291

Browse files
committed
[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. (cherry picked from commit b5fd946) Change-Id: I22936b5db74f89da8c84e42848403c8dfe0a036f
1 parent 97d9178 commit afbf291

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
@@ -762,8 +762,10 @@ llvm::Function *CGNVCUDARuntime::makeModuleCtorFunction() {
762762
FatBinStr = new llvm::GlobalVariable(
763763
CGM.getModule(), CGM.Int8Ty,
764764
/*isConstant=*/true, llvm::GlobalValue::ExternalLinkage, nullptr,
765-
"__hip_fatbin_" + CGM.getContext().getCUIDHash(), nullptr,
766-
llvm::GlobalVariable::NotThreadLocal);
765+
"__hip_fatbin" + (CGM.getLangOpts().CUID.empty()
766+
? ""
767+
: "_" + CGM.getContext().getCUIDHash()),
768+
nullptr, llvm::GlobalVariable::NotThreadLocal);
767769
cast<llvm::GlobalVariable>(FatBinStr)->setSection(FatbinConstantName);
768770
}
769771

@@ -816,8 +818,8 @@ llvm::Function *CGNVCUDARuntime::makeModuleCtorFunction() {
816818
// thread safety of the loaded program. Therefore we can assume sequential
817819
// execution of constructor functions here.
818820
if (IsHIP) {
819-
auto Linkage = CudaGpuBinary ? llvm::GlobalValue::InternalLinkage
820-
: llvm::GlobalValue::ExternalLinkage;
821+
auto Linkage = RelocatableDeviceCode ? llvm::GlobalValue::ExternalLinkage
822+
: llvm::GlobalValue::InternalLinkage;
821823
llvm::BasicBlock *IfBlock =
822824
llvm::BasicBlock::Create(Context, "if", ModuleCtorFunc);
823825
llvm::BasicBlock *ExitBlock =
@@ -827,10 +829,11 @@ llvm::Function *CGNVCUDARuntime::makeModuleCtorFunction() {
827829
GpuBinaryHandle = new llvm::GlobalVariable(
828830
TheModule, PtrTy, /*isConstant=*/false, Linkage,
829831
/*Initializer=*/
830-
CudaGpuBinary ? llvm::ConstantPointerNull::get(PtrTy) : nullptr,
831-
CudaGpuBinary
832-
? "__hip_gpubin_handle"
833-
: "__hip_gpubin_handle_" + CGM.getContext().getCUIDHash());
832+
!RelocatableDeviceCode ? llvm::ConstantPointerNull::get(PtrTy)
833+
: nullptr,
834+
"__hip_gpubin_handle" + (CGM.getLangOpts().CUID.empty()
835+
? ""
836+
: "_" + CGM.getContext().getCUIDHash()));
834837
GpuBinaryHandle->setAlignment(CGM.getPointerAlign().getAsAlign());
835838
// Prevent the weak symbol in different shared libraries being merged.
836839
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)