From b008ea304d438f0aa818918caceb3bd864412304 Mon Sep 17 00:00:00 2001 From: "Yaxun (Sam) Liu" Date: Tue, 2 Feb 2021 18:06:33 -0500 Subject: [CUDA][HIP] Fix device variable linkage For -fgpu-rdc, shadow variables should not be internalized, otherwise they cannot be accessed by other TUs. This is necessary because the shadow variable of external device variables are always emitted as undefined symbols, which need to resolve to a global symbols. Managed variables need to be emitted as undefined symbols in device compilations. Reviewed by: Artem Belevich Differential Revision: https://reviews.llvm.org/D95901 --- clang/lib/CodeGen/CodeGenModule.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'clang/lib/CodeGen/CodeGenModule.cpp') diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index b133d2a84a59..0cae5ba59620 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -4169,8 +4169,12 @@ void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D, bool NeedsGlobalDtor = D->needsDestruction(getContext()) == QualType::DK_cxx_destructor; + bool IsHIPManagedVarOnDevice = + getLangOpts().CUDAIsDevice && D->hasAttr(); + const VarDecl *InitDecl; - const Expr *InitExpr = D->getAnyInitializer(InitDecl); + const Expr *InitExpr = + IsHIPManagedVarOnDevice ? nullptr : D->getAnyInitializer(InitDecl); Optional emitter; @@ -4190,8 +4194,6 @@ void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D, (D->getType()->isCUDADeviceBuiltinSurfaceType() || D->getType()->isCUDADeviceBuiltinTextureType() || D->hasAttr()); - // HIP pinned shadow of initialized host-side global variables are also - // left undefined. if (getLangOpts().CUDA && (IsCUDASharedVar || IsCUDAShadowVar || IsCUDADeviceShadowVar)) Init = llvm::UndefValue::get(getTypes().ConvertType(ASTTy)); @@ -4302,7 +4304,10 @@ void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D, } } - GV->setInitializer(Init); + // HIP managed variables need to be emitted as declarations in device + // compilation. + if (!IsHIPManagedVarOnDevice) + GV->setInitializer(Init); if (emitter) emitter->finalize(GV); -- cgit v1.2.3