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

Reply via email to