Skip to content

Commit cac0686

Browse files
committed
[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
1 parent 952ad47 commit cac0686

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1916,9 +1916,9 @@ void CodeGenModule::SetFunctionAttributes(GlobalDecl GD, llvm::Function *F,
19161916
}
19171917
}
19181918

1919-
void CodeGenModule::addUsedGlobal(llvm::GlobalValue *GV) {
1920-
assert(!GV->isDeclaration() &&
1921-
"Only globals with definition can force usage.");
1919+
void CodeGenModule::addUsedGlobal(llvm::GlobalValue *GV, bool SkipCheck) {
1920+
assert(SkipCheck || (!GV->isDeclaration() &&
1921+
"Only globals with definition can force usage."));
19221922
LLVMUsed.emplace_back(GV);
19231923
}
19241924

@@ -4071,9 +4071,17 @@ void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D,
40714071
}
40724072
}
40734073

4074-
if (!IsHIPPinnedShadowVar)
4074+
// HIPPinnedShadowVar should remain in the final code object irrespective of
4075+
// whether it is used or not within the code. Add it to used list, so that
4076+
// it will not get eliminated when it is unused. Also, it is an extern var
4077+
// within device code, and it should *not* get initialized within device code.
4078+
if (IsHIPPinnedShadowVar)
4079+
addUsedGlobal(GV, /*SkipCheck=*/true);
4080+
else
40754081
GV->setInitializer(Init);
4076-
if (emitter) emitter->finalize(GV);
4082+
4083+
if (emitter)
4084+
emitter->finalize(GV);
40774085

40784086
// If it is safe to mark the global 'constant', do so now.
40794087
GV->setConstant(!NeedsGlobalCtor && !NeedsGlobalDtor &&

clang/lib/CodeGen/CodeGenModule.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1037,7 +1037,7 @@ class CodeGenModule : public CodeGenTypeCache {
10371037
void MaybeHandleStaticInExternC(const SomeDecl *D, llvm::GlobalValue *GV);
10381038

10391039
/// Add a global to a list to be added to the llvm.used metadata.
1040-
void addUsedGlobal(llvm::GlobalValue *GV);
1040+
void addUsedGlobal(llvm::GlobalValue *GV, bool SkipCheck = false);
10411041

10421042
/// Add a global to a list to be added to the llvm.compiler.used metadata.
10431043
void addCompilerUsedGlobal(llvm::GlobalValue *GV);

clang/test/CodeGenCUDA/hip-pinned-shadow.cu

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
// RUN: -emit-llvm -o - -x hip %s | FileCheck -check-prefixes=HIPDEV %s
55
// RUN: %clang_cc1 -triple x86_64 -std=c++11 \
66
// RUN: -emit-llvm -o - -x hip %s | FileCheck -check-prefixes=HIPHOST %s
7+
// RUN: %clang_cc1 -triple amdgcn -fcuda-is-device -std=c++11 -fvisibility hidden -fapply-global-visibility-to-externs \
8+
// RUN: -O3 -emit-llvm -o - -x hip %s | FileCheck -check-prefixes=HIPDEVUNSED %s
79

810
struct textureReference {
911
int a;
@@ -21,3 +23,5 @@ __attribute__((hip_pinned_shadow)) texture<float, 2, 1> tex;
2123
// HIPDEV-NOT: declare{{.*}}void @_ZN7textureIfLi2ELi1EEC1Ev
2224
// HIPHOST: define{{.*}}@_ZN7textureIfLi2ELi1EEC1Ev
2325
// HIPHOST: call i32 @__hipRegisterVar{{.*}}@tex{{.*}}i32 0, i32 4, i32 0, i32 0)
26+
// HIPDEVUNSED: @tex = external addrspace(1) global %struct.texture
27+
// HIPDEVUNSED-NOT: declare{{.*}}void @_ZN7textureIfLi2ELi1EEC1Ev

0 commit comments

Comments
 (0)