From cac068600e55e489844156d3581b61eeecee7d4e Mon Sep 17 00:00:00 2001 From: hsmahesha Date: Wed, 4 Mar 2020 10:53:32 +0530 Subject: [HIP] Make sure, unused hip-pinned-shadow global var is kept within device code Summary: hip-pinned-shadow global var should remain in the final code object irrespective of whether it is used or not within the code. Add it to used list, so that it will not get eliminated when it is unused. Reviewers: yaxunl, tra, hliao Reviewed By: yaxunl Subscribers: hliao, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D75402 --- clang/lib/CodeGen/CodeGenModule.cpp | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) (limited to 'clang/lib/CodeGen/CodeGenModule.cpp') diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index faff623a2973..719cb05d9ec8 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -1916,9 +1916,9 @@ void CodeGenModule::SetFunctionAttributes(GlobalDecl GD, llvm::Function *F, } } -void CodeGenModule::addUsedGlobal(llvm::GlobalValue *GV) { - assert(!GV->isDeclaration() && - "Only globals with definition can force usage."); +void CodeGenModule::addUsedGlobal(llvm::GlobalValue *GV, bool SkipCheck) { + assert(SkipCheck || (!GV->isDeclaration() && + "Only globals with definition can force usage.")); LLVMUsed.emplace_back(GV); } @@ -4071,9 +4071,17 @@ void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D, } } - if (!IsHIPPinnedShadowVar) + // HIPPinnedShadowVar should remain in the final code object irrespective of + // whether it is used or not within the code. Add it to used list, so that + // it will not get eliminated when it is unused. Also, it is an extern var + // within device code, and it should *not* get initialized within device code. + if (IsHIPPinnedShadowVar) + addUsedGlobal(GV, /*SkipCheck=*/true); + else GV->setInitializer(Init); - if (emitter) emitter->finalize(GV); + + if (emitter) + emitter->finalize(GV); // If it is safe to mark the global 'constant', do so now. GV->setConstant(!NeedsGlobalCtor && !NeedsGlobalDtor && -- cgit v1.2.3