Skip to content

Commit b5fd946

Browse files
authored
[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.
1 parent 2a9208b commit b5fd946

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
@@ -840,8 +840,10 @@ llvm::Function *CGNVCUDARuntime::makeModuleCtorFunction() {
840840
FatBinStr = new llvm::GlobalVariable(
841841
CGM.getModule(), CGM.Int8Ty,
842842
/*isConstant=*/true, llvm::GlobalValue::ExternalLinkage, nullptr,
843-
"__hip_fatbin_" + CGM.getContext().getCUIDHash(), nullptr,
844-
llvm::GlobalVariable::NotThreadLocal);
843+
"__hip_fatbin" + (CGM.getLangOpts().CUID.empty()
844+
? ""
845+
: "_" + CGM.getContext().getCUIDHash()),
846+
nullptr, llvm::GlobalVariable::NotThreadLocal);
845847
cast<llvm::GlobalVariable>(FatBinStr)->setSection(FatbinConstantName);
846848
}
847849

@@ -894,8 +896,8 @@ llvm::Function *CGNVCUDARuntime::makeModuleCtorFunction() {
894896
// thread safety of the loaded program. Therefore we can assume sequential
895897
// execution of constructor functions here.
896898
if (IsHIP) {
897-
auto Linkage = CudaGpuBinary ? llvm::GlobalValue::InternalLinkage
898-
: llvm::GlobalValue::ExternalLinkage;
899+
auto Linkage = RelocatableDeviceCode ? llvm::GlobalValue::ExternalLinkage
900+
: llvm::GlobalValue::InternalLinkage;
899901
llvm::BasicBlock *IfBlock =
900902
llvm::BasicBlock::Create(Context, "if", ModuleCtorFunc);
901903
llvm::BasicBlock *ExitBlock =
@@ -905,10 +907,11 @@ llvm::Function *CGNVCUDARuntime::makeModuleCtorFunction() {
905907
GpuBinaryHandle = new llvm::GlobalVariable(
906908
TheModule, PtrTy, /*isConstant=*/false, Linkage,
907909
/*Initializer=*/
908-
CudaGpuBinary ? llvm::ConstantPointerNull::get(PtrTy) : nullptr,
909-
CudaGpuBinary
910-
? "__hip_gpubin_handle"
911-
: "__hip_gpubin_handle_" + CGM.getContext().getCUIDHash());
910+
!RelocatableDeviceCode ? llvm::ConstantPointerNull::get(PtrTy)
911+
: nullptr,
912+
"__hip_gpubin_handle" + (CGM.getLangOpts().CUID.empty()
913+
? ""
914+
: "_" + CGM.getContext().getCUIDHash()));
912915
GpuBinaryHandle->setAlignment(CGM.getPointerAlign().getAsAlign());
913916
// Prevent the weak symbol in different shared libraries being merged.
914917
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)