[PATCH] D75402: [HIP] Make sure, unused hip-pinned-shadow global var is kept within device code

2020-03-03 Thread Mahesha S via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rGcac068600e55: [HIP] Make sure, unused hip-pinned-shadow 
global var is kept within device code (authored by hsmhsm).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D75402/new/

https://reviews.llvm.org/D75402

Files:
  clang/lib/CodeGen/CodeGenModule.cpp
  clang/lib/CodeGen/CodeGenModule.h
  clang/test/CodeGenCUDA/hip-pinned-shadow.cu


Index: clang/test/CodeGenCUDA/hip-pinned-shadow.cu
===
--- clang/test/CodeGenCUDA/hip-pinned-shadow.cu
+++ clang/test/CodeGenCUDA/hip-pinned-shadow.cu
@@ -4,6 +4,8 @@
 // RUN: -emit-llvm -o - -x hip %s | FileCheck -check-prefixes=HIPDEV %s
 // RUN: %clang_cc1 -triple x86_64 -std=c++11 \
 // RUN: -emit-llvm -o - -x hip %s | FileCheck -check-prefixes=HIPHOST %s
+// RUN: %clang_cc1 -triple amdgcn -fcuda-is-device -std=c++11 -fvisibility 
hidden -fapply-global-visibility-to-externs \
+// RUN: -O3 -emit-llvm -o - -x hip %s | FileCheck 
-check-prefixes=HIPDEVUNSED %s
 
 struct textureReference {
   int a;
@@ -21,3 +23,5 @@
 // HIPDEV-NOT: declare{{.*}}void @_ZN7textureIfLi2ELi1EEC1Ev
 // HIPHOST:  define{{.*}}@_ZN7textureIfLi2ELi1EEC1Ev
 // HIPHOST:  call i32 @__hipRegisterVar{{.*}}@tex{{.*}}i32 0, i32 4, i32 0, 
i32 0)
+// HIPDEVUNSED: @tex = external addrspace(1) global %struct.texture
+// HIPDEVUNSED-NOT: declare{{.*}}void @_ZN7textureIfLi2ELi1EEC1Ev
Index: clang/lib/CodeGen/CodeGenModule.h
===
--- clang/lib/CodeGen/CodeGenModule.h
+++ clang/lib/CodeGen/CodeGenModule.h
@@ -1037,7 +1037,7 @@
   void MaybeHandleStaticInExternC(const SomeDecl *D, llvm::GlobalValue *GV);
 
   /// Add a global to a list to be added to the llvm.used metadata.
-  void addUsedGlobal(llvm::GlobalValue *GV);
+  void addUsedGlobal(llvm::GlobalValue *GV, bool SkipCheck = false);
 
   /// Add a global to a list to be added to the llvm.compiler.used metadata.
   void addCompilerUsedGlobal(llvm::GlobalValue *GV);
Index: clang/lib/CodeGen/CodeGenModule.cpp
===
--- clang/lib/CodeGen/CodeGenModule.cpp
+++ clang/lib/CodeGen/CodeGenModule.cpp
@@ -1916,9 +1916,9 @@
   }
 }
 
-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 @@
 }
   }
 
-  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 &&


Index: clang/test/CodeGenCUDA/hip-pinned-shadow.cu
===
--- clang/test/CodeGenCUDA/hip-pinned-shadow.cu
+++ clang/test/CodeGenCUDA/hip-pinned-shadow.cu
@@ -4,6 +4,8 @@
 // RUN: -emit-llvm -o - -x hip %s | FileCheck -check-prefixes=HIPDEV %s
 // RUN: %clang_cc1 -triple x86_64 -std=c++11 \
 // RUN: -emit-llvm -o - -x hip %s | FileCheck -check-prefixes=HIPHOST %s
+// RUN: %clang_cc1 -triple amdgcn -fcuda-is-device -std=c++11 -fvisibility hidden -fapply-global-visibility-to-externs \
+// RUN: -O3 -emit-llvm -o - -x hip %s | FileCheck -check-prefixes=HIPDEVUNSED %s
 
 struct textureReference {
   int a;
@@ -21,3 +23,5 @@
 // HIPDEV-NOT: declare{{.*}}void @_ZN7textureIfLi2ELi1EEC1Ev
 // HIPHOST:  define{{.*}}@_ZN7textureIfLi2ELi1EEC1Ev
 // HIPHOST:  call i32 @__hipRegisterVar{{.*}}@tex{{.*}}i32 0, i32 4, i32 0, i32 0)
+// HIPDEVUNSED: @tex = external addrspace(1) global %struct.texture
+// HIPDEVUNSED-NOT: declare{{.*}}void @_ZN7textureIfLi2ELi1EEC1Ev
Index: clang/lib/CodeGen/CodeGenModule.h
===
--- clang/lib/CodeGen/CodeGenModule.h
+++ clang/lib/CodeGen/CodeGenModule.h
@@ -1037,7 +1037,7 @@
   void MaybeHandleStaticInExternC(const SomeDecl *D, llvm::GlobalValue *GV);
 
   /// Add a global to a list to be added to the llvm.used metadata.
-  void addUsedGlobal(llvm::GlobalValue *GV);
+  void addUsedGlobal(llvm::GlobalValue *GV, bool SkipCheck = false);
 
   /// Add a global to a list to be added to the 

[PATCH] D75402: [HIP] Make sure, unused hip-pinned-shadow global var is kept within device code

2020-03-03 Thread Yaxun Liu via Phabricator via cfe-commits
yaxunl accepted this revision.
yaxunl added a comment.
This revision is now accepted and ready to land.

LGTM. Thanks!


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D75402/new/

https://reviews.llvm.org/D75402



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D75402: [HIP] Make sure, unused hip-pinned-shadow global var is kept within device code

2020-03-03 Thread Mahesha S via Phabricator via cfe-commits
hsmhsm added a comment.

In D75402#1901471 , @hsmhsm wrote:

> Take care review comments by hliao.


ping


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D75402/new/

https://reviews.llvm.org/D75402



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D75402: [HIP] Make sure, unused hip-pinned-shadow global var is kept within device code

2020-03-02 Thread Mahesha S via Phabricator via cfe-commits
hsmhsm updated this revision to Diff 247689.
hsmhsm added a comment.

Take care review comments by hliao.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D75402/new/

https://reviews.llvm.org/D75402

Files:
  clang/lib/CodeGen/CodeGenModule.cpp
  clang/lib/CodeGen/CodeGenModule.h
  clang/test/CodeGenCUDA/hip-pinned-shadow.cu


Index: clang/test/CodeGenCUDA/hip-pinned-shadow.cu
===
--- clang/test/CodeGenCUDA/hip-pinned-shadow.cu
+++ clang/test/CodeGenCUDA/hip-pinned-shadow.cu
@@ -4,6 +4,8 @@
 // RUN: -emit-llvm -o - -x hip %s | FileCheck -check-prefixes=HIPDEV %s
 // RUN: %clang_cc1 -triple x86_64 -std=c++11 \
 // RUN: -emit-llvm -o - -x hip %s | FileCheck -check-prefixes=HIPHOST %s
+// RUN: %clang_cc1 -triple amdgcn -fcuda-is-device -std=c++11 -fvisibility 
hidden -fapply-global-visibility-to-externs \
+// RUN: -O3 -emit-llvm -o - -x hip %s | FileCheck 
-check-prefixes=HIPDEVUNSED %s
 
 struct textureReference {
   int a;
@@ -21,3 +23,5 @@
 // HIPDEV-NOT: declare{{.*}}void @_ZN7textureIfLi2ELi1EEC1Ev
 // HIPHOST:  define{{.*}}@_ZN7textureIfLi2ELi1EEC1Ev
 // HIPHOST:  call i32 @__hipRegisterVar{{.*}}@tex{{.*}}i32 0, i32 4, i32 0, 
i32 0)
+// HIPDEVUNSED: @tex = external addrspace(1) global %struct.texture
+// HIPDEVUNSED-NOT: declare{{.*}}void @_ZN7textureIfLi2ELi1EEC1Ev
Index: clang/lib/CodeGen/CodeGenModule.h
===
--- clang/lib/CodeGen/CodeGenModule.h
+++ clang/lib/CodeGen/CodeGenModule.h
@@ -1037,7 +1037,7 @@
   void MaybeHandleStaticInExternC(const SomeDecl *D, llvm::GlobalValue *GV);
 
   /// Add a global to a list to be added to the llvm.used metadata.
-  void addUsedGlobal(llvm::GlobalValue *GV);
+  void addUsedGlobal(llvm::GlobalValue *GV, bool SkipCheck = false);
 
   /// Add a global to a list to be added to the llvm.compiler.used metadata.
   void addCompilerUsedGlobal(llvm::GlobalValue *GV);
Index: clang/lib/CodeGen/CodeGenModule.cpp
===
--- clang/lib/CodeGen/CodeGenModule.cpp
+++ clang/lib/CodeGen/CodeGenModule.cpp
@@ -1916,9 +1916,9 @@
   }
 }
 
-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 @@
 }
   }
 
-  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 &&


Index: clang/test/CodeGenCUDA/hip-pinned-shadow.cu
===
--- clang/test/CodeGenCUDA/hip-pinned-shadow.cu
+++ clang/test/CodeGenCUDA/hip-pinned-shadow.cu
@@ -4,6 +4,8 @@
 // RUN: -emit-llvm -o - -x hip %s | FileCheck -check-prefixes=HIPDEV %s
 // RUN: %clang_cc1 -triple x86_64 -std=c++11 \
 // RUN: -emit-llvm -o - -x hip %s | FileCheck -check-prefixes=HIPHOST %s
+// RUN: %clang_cc1 -triple amdgcn -fcuda-is-device -std=c++11 -fvisibility hidden -fapply-global-visibility-to-externs \
+// RUN: -O3 -emit-llvm -o - -x hip %s | FileCheck -check-prefixes=HIPDEVUNSED %s
 
 struct textureReference {
   int a;
@@ -21,3 +23,5 @@
 // HIPDEV-NOT: declare{{.*}}void @_ZN7textureIfLi2ELi1EEC1Ev
 // HIPHOST:  define{{.*}}@_ZN7textureIfLi2ELi1EEC1Ev
 // HIPHOST:  call i32 @__hipRegisterVar{{.*}}@tex{{.*}}i32 0, i32 4, i32 0, i32 0)
+// HIPDEVUNSED: @tex = external addrspace(1) global %struct.texture
+// HIPDEVUNSED-NOT: declare{{.*}}void @_ZN7textureIfLi2ELi1EEC1Ev
Index: clang/lib/CodeGen/CodeGenModule.h
===
--- clang/lib/CodeGen/CodeGenModule.h
+++ clang/lib/CodeGen/CodeGenModule.h
@@ -1037,7 +1037,7 @@
   void MaybeHandleStaticInExternC(const SomeDecl *D, llvm::GlobalValue *GV);
 
   /// Add a global to a list to be added to the llvm.used metadata.
-  void addUsedGlobal(llvm::GlobalValue *GV);
+  void addUsedGlobal(llvm::GlobalValue *GV, bool SkipCheck = false);
 
   /// Add a global to a list to be added to the llvm.compiler.used metadata.
   void addCompilerUsedGlobal(llvm::GlobalValue *GV);
Index: 

[PATCH] D75402: [HIP] Make sure, unused hip-pinned-shadow global var is kept within device code

2020-03-02 Thread Yaxun Liu via Phabricator via cfe-commits
yaxunl added a comment.

In D75402#1901370 , @hsmhsm wrote:

> In D75402#1901361 , @hliao wrote:
>
> > BTW, why that variable cannot have an initializer? Suppose that initializer 
> > is a trivial one, initializing to 0, would that cause any issue in the 
> > compilation?
>
>
> Initialization makes the global extern var declaration to become a global 
> definition. And. moreover, it is not a new change from my side, I just happen 
> to add a comment.


This kind of variable is supposed to be an external variable which is 
initialized by runtime at run time.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D75402/new/

https://reviews.llvm.org/D75402



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D75402: [HIP] Make sure, unused hip-pinned-shadow global var is kept within device code

2020-03-02 Thread Mahesha S via Phabricator via cfe-commits
hsmhsm added a comment.

In D75402#1901361 , @hliao wrote:

> BTW, why that variable cannot have an initializer? Suppose that initializer 
> is a trivial one, initializing to 0, would that cause any issue in the 
> compilation?


Initialization makes the global extern var declaration to become a global 
definition. And. moreover, it is not a new change from my side, I just happen 
to add a comment.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D75402/new/

https://reviews.llvm.org/D75402



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D75402: [HIP] Make sure, unused hip-pinned-shadow global var is kept within device code

2020-03-02 Thread Mahesha S via Phabricator via cfe-commits
hsmhsm marked an inline comment as done.
hsmhsm added inline comments.



Comment at: clang/lib/CodeGen/CodeGenModule.cpp:1919
 
 void CodeGenModule::addUsedGlobal(llvm::GlobalValue *GV) {
   LLVMUsed.emplace_back(GV);

hliao wrote:
> hsmhsm wrote:
> > yaxunl wrote:
> > > hliao wrote:
> > > > This check should be removed completely instead it should be revised 
> > > > for the exceptions.
> > > How about add back this assertion but make an exception for global 
> > > variables with hip_pinned_shadow attribute.
> > Yeah, that is what I was thinking of initially - to remove this assertion 
> > only to hip_pinned_shadow vars, may, be creating a clone of this function 
> > something like - CodeGenModule::addUsedGlobalForHipPinnedShadows(), without 
> > assertion statement.
> > 
> > @hliao
> > 
> > What do you say?
> how about change that function prototype to
> 
> void addUsedGlobal(llvm::GlobalValue *GV, bool SkipCheck = false)
> 
> and assert check to
> 
> assert ((SkipCheck || !GV->isDeclaration()) && "");
> 
This also looks fine to me, and I can make these changes and submit the changes.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D75402/new/

https://reviews.llvm.org/D75402



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D75402: [HIP] Make sure, unused hip-pinned-shadow global var is kept within device code

2020-03-02 Thread Michael Liao via Phabricator via cfe-commits
hliao added a comment.

BTW, why that variable cannot have an initializer? Suppose that initializer is 
a trivial one, initializing to 0, would that cause any issue in the compilation?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D75402/new/

https://reviews.llvm.org/D75402



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D75402: [HIP] Make sure, unused hip-pinned-shadow global var is kept within device code

2020-03-02 Thread Mahesha S via Phabricator via cfe-commits
hsmhsm marked an inline comment as done.
hsmhsm added inline comments.



Comment at: clang/lib/CodeGen/CodeGenModule.cpp:1919
 
 void CodeGenModule::addUsedGlobal(llvm::GlobalValue *GV) {
   LLVMUsed.emplace_back(GV);

yaxunl wrote:
> hliao wrote:
> > This check should be removed completely instead it should be revised for 
> > the exceptions.
> How about add back this assertion but make an exception for global variables 
> with hip_pinned_shadow attribute.
Yeah, that is what I was thinking of initially - to remove this assertion only 
to hip_pinned_shadow vars, may, be creating a clone of this function something 
like - CodeGenModule::addUsedGlobalForHipPinnedShadows(), without assertion 
statement.

@hliao

What do you say?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D75402/new/

https://reviews.llvm.org/D75402



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D75402: [HIP] Make sure, unused hip-pinned-shadow global var is kept within device code

2020-03-02 Thread Michael Liao via Phabricator via cfe-commits
hliao added inline comments.



Comment at: clang/lib/CodeGen/CodeGenModule.cpp:1919
 
 void CodeGenModule::addUsedGlobal(llvm::GlobalValue *GV) {
   LLVMUsed.emplace_back(GV);

hsmhsm wrote:
> yaxunl wrote:
> > hliao wrote:
> > > This check should be removed completely instead it should be revised for 
> > > the exceptions.
> > How about add back this assertion but make an exception for global 
> > variables with hip_pinned_shadow attribute.
> Yeah, that is what I was thinking of initially - to remove this assertion 
> only to hip_pinned_shadow vars, may, be creating a clone of this function 
> something like - CodeGenModule::addUsedGlobalForHipPinnedShadows(), without 
> assertion statement.
> 
> @hliao
> 
> What do you say?
how about change that function prototype to

void addUsedGlobal(llvm::GlobalValue *GV, bool SkipCheck = false)

and assert check to

assert ((SkipCheck || !GV->isDeclaration()) && "");



Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D75402/new/

https://reviews.llvm.org/D75402



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D75402: [HIP] Make sure, unused hip-pinned-shadow global var is kept within device code

2020-03-02 Thread Yaxun Liu via Phabricator via cfe-commits
yaxunl added inline comments.



Comment at: clang/lib/CodeGen/CodeGenModule.cpp:1919
 
 void CodeGenModule::addUsedGlobal(llvm::GlobalValue *GV) {
   LLVMUsed.emplace_back(GV);

hliao wrote:
> This check should be removed completely instead it should be revised for the 
> exceptions.
How about add back this assertion but make an exception for global variables 
with hip_pinned_shadow attribute.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D75402/new/

https://reviews.llvm.org/D75402



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D75402: [HIP] Make sure, unused hip-pinned-shadow global var is kept within device code

2020-03-02 Thread Michael Liao via Phabricator via cfe-commits
hliao added a comment.

the assertion in addUsedGlobal should not be removed. that will remove the 
guard for potential bugs




Comment at: clang/lib/CodeGen/CodeGenModule.cpp:1919
 
 void CodeGenModule::addUsedGlobal(llvm::GlobalValue *GV) {
   LLVMUsed.emplace_back(GV);

This check should be removed completely instead it should be revised for the 
exceptions.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D75402/new/

https://reviews.llvm.org/D75402



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D75402: [HIP] Make sure, unused hip-pinned-shadow global var is kept within device code

2020-03-01 Thread Yaxun Liu via Phabricator via cfe-commits
yaxunl added a comment.

LGTM. Thanks.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D75402/new/

https://reviews.llvm.org/D75402



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D75402: [HIP] Make sure, unused hip-pinned-shadow global var is kept within device code

2020-02-29 Thread Mahesha S via Phabricator via cfe-commits
hsmhsm created this revision.
hsmhsm added reviewers: yaxunl, tra.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D75402

Files:
  clang/lib/CodeGen/CodeGenModule.cpp
  clang/test/CodeGenCUDA/hip-pinned-shadow.cu


Index: clang/test/CodeGenCUDA/hip-pinned-shadow.cu
===
--- clang/test/CodeGenCUDA/hip-pinned-shadow.cu
+++ clang/test/CodeGenCUDA/hip-pinned-shadow.cu
@@ -4,6 +4,8 @@
 // RUN: -emit-llvm -o - -x hip %s | FileCheck -check-prefixes=HIPDEV %s
 // RUN: %clang_cc1 -triple x86_64 -std=c++11 \
 // RUN: -emit-llvm -o - -x hip %s | FileCheck -check-prefixes=HIPHOST %s
+// RUN: %clang_cc1 -triple amdgcn -fcuda-is-device -std=c++11 -fvisibility 
hidden -fapply-global-visibility-to-externs \
+// RUN: -O3 -emit-llvm -o - -x hip %s | FileCheck 
-check-prefixes=HIPDEVUNSED %s
 
 struct textureReference {
   int a;
@@ -21,3 +23,5 @@
 // HIPDEV-NOT: declare{{.*}}void @_ZN7textureIfLi2ELi1EEC1Ev
 // HIPHOST:  define{{.*}}@_ZN7textureIfLi2ELi1EEC1Ev
 // HIPHOST:  call i32 @__hipRegisterVar{{.*}}@tex{{.*}}i32 0, i32 4, i32 0, 
i32 0)
+// HIPDEVUNSED: @tex = external addrspace(1) global %struct.texture
+// HIPDEVUNSED-NOT: declare{{.*}}void @_ZN7textureIfLi2ELi1EEC1Ev
Index: clang/lib/CodeGen/CodeGenModule.cpp
===
--- clang/lib/CodeGen/CodeGenModule.cpp
+++ clang/lib/CodeGen/CodeGenModule.cpp
@@ -1917,8 +1917,6 @@
 }
 
 void CodeGenModule::addUsedGlobal(llvm::GlobalValue *GV) {
-  assert(!GV->isDeclaration() &&
- "Only globals with definition can force usage.");
   LLVMUsed.emplace_back(GV);
 }
 
@@ -4071,9 +4069,17 @@
 }
   }
 
-  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);
+  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 &&


Index: clang/test/CodeGenCUDA/hip-pinned-shadow.cu
===
--- clang/test/CodeGenCUDA/hip-pinned-shadow.cu
+++ clang/test/CodeGenCUDA/hip-pinned-shadow.cu
@@ -4,6 +4,8 @@
 // RUN: -emit-llvm -o - -x hip %s | FileCheck -check-prefixes=HIPDEV %s
 // RUN: %clang_cc1 -triple x86_64 -std=c++11 \
 // RUN: -emit-llvm -o - -x hip %s | FileCheck -check-prefixes=HIPHOST %s
+// RUN: %clang_cc1 -triple amdgcn -fcuda-is-device -std=c++11 -fvisibility hidden -fapply-global-visibility-to-externs \
+// RUN: -O3 -emit-llvm -o - -x hip %s | FileCheck -check-prefixes=HIPDEVUNSED %s
 
 struct textureReference {
   int a;
@@ -21,3 +23,5 @@
 // HIPDEV-NOT: declare{{.*}}void @_ZN7textureIfLi2ELi1EEC1Ev
 // HIPHOST:  define{{.*}}@_ZN7textureIfLi2ELi1EEC1Ev
 // HIPHOST:  call i32 @__hipRegisterVar{{.*}}@tex{{.*}}i32 0, i32 4, i32 0, i32 0)
+// HIPDEVUNSED: @tex = external addrspace(1) global %struct.texture
+// HIPDEVUNSED-NOT: declare{{.*}}void @_ZN7textureIfLi2ELi1EEC1Ev
Index: clang/lib/CodeGen/CodeGenModule.cpp
===
--- clang/lib/CodeGen/CodeGenModule.cpp
+++ clang/lib/CodeGen/CodeGenModule.cpp
@@ -1917,8 +1917,6 @@
 }
 
 void CodeGenModule::addUsedGlobal(llvm::GlobalValue *GV) {
-  assert(!GV->isDeclaration() &&
- "Only globals with definition can force usage.");
   LLVMUsed.emplace_back(GV);
 }
 
@@ -4071,9 +4069,17 @@
 }
   }
 
-  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);
+  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 &&
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits