summaryrefslogtreecommitdiff
path: root/clang/lib/CodeGen/CodeGenModule.cpp
diff options
context:
space:
mode:
authorYaxun (Sam) Liu <yaxun.liu@amd.com>2021-02-02 18:06:33 -0500
committerYaxun (Sam) Liu <yaxun.liu@amd.com>2021-02-05 15:11:12 -0500
commitb008ea304d438f0aa818918caceb3bd864412304 (patch)
tree4162dba4bc94d01f43961b686854465ee5455683 /clang/lib/CodeGen/CodeGenModule.cpp
parentc981f6f8e16effdbd5f8c98a80ee8d677fdf8503 (diff)
[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
Diffstat (limited to 'clang/lib/CodeGen/CodeGenModule.cpp')
-rw-r--r--clang/lib/CodeGen/CodeGenModule.cpp13
1 files changed, 9 insertions, 4 deletions
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<HIPManagedAttr>();
+
const VarDecl *InitDecl;
- const Expr *InitExpr = D->getAnyInitializer(InitDecl);
+ const Expr *InitExpr =
+ IsHIPManagedVarOnDevice ? nullptr : D->getAnyInitializer(InitDecl);
Optional<ConstantEmitter> emitter;
@@ -4190,8 +4194,6 @@ void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D,
(D->getType()->isCUDADeviceBuiltinSurfaceType() ||
D->getType()->isCUDADeviceBuiltinTextureType() ||
D->hasAttr<HIPManagedAttr>());
- // 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);