python3kgae created this revision.
python3kgae added reviewers: beanz, pow2clk, bogner.
Herald added a subscriber: Anastasia.
Herald added a project: All.
python3kgae requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

After generated call for ctor/dtor for entry, global variable for ctor/dtor are 
useless.
Remove them for non-lib profiles.
Lib profile still need these in case export function used the global variable 
which require ctor/dtor.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D133993

Files:
  clang/lib/CodeGen/CGHLSLRuntime.cpp
  clang/lib/CodeGen/CGHLSLRuntime.h
  clang/test/CodeGenHLSL/GlobalConstructorFunction.hlsl
  clang/test/CodeGenHLSL/GlobalConstructorLib.hlsl
  clang/test/CodeGenHLSL/GlobalConstructors.hlsl
  clang/test/CodeGenHLSL/GlobalDestructors.hlsl

Index: clang/test/CodeGenHLSL/GlobalDestructors.hlsl
===================================================================
--- clang/test/CodeGenHLSL/GlobalDestructors.hlsl
+++ clang/test/CodeGenHLSL/GlobalDestructors.hlsl
@@ -39,6 +39,9 @@
   Wag();
 }
 
+// Make sure global variable for ctors/dtors removed.
+// CHECK-NOT:@llvm.global_ctors
+// CHECK-NOT:@llvm.global_dtors
 //CHECK:      define void @main()
 //CHECK-NEXT: entry:
 //CHECK-NEXT:   call void @_GLOBAL__sub_I_GlobalDestructors.hlsl()
Index: clang/test/CodeGenHLSL/GlobalConstructors.hlsl
===================================================================
--- clang/test/CodeGenHLSL/GlobalConstructors.hlsl
+++ clang/test/CodeGenHLSL/GlobalConstructors.hlsl
@@ -5,6 +5,9 @@
 [numthreads(1,1,1)]
 void main(unsigned GI : SV_GroupIndex) {}
 
+// Make sure global variable for ctors/dtors removed.
+// CHECK-NOT:@llvm.global_ctors
+// CHECK-NOT:@llvm.global_dtors
 //CHECK:      define void @main()
 //CHECK-NEXT: entry:
 //CHECK-NEXT:   call void @_GLOBAL__sub_I_GlobalConstructors.hlsl()
Index: clang/test/CodeGenHLSL/GlobalConstructorLib.hlsl
===================================================================
--- clang/test/CodeGenHLSL/GlobalConstructorLib.hlsl
+++ clang/test/CodeGenHLSL/GlobalConstructorLib.hlsl
@@ -1,5 +1,8 @@
 // RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -S -emit-llvm -disable-llvm-passes %s -o - | FileCheck %s
 
+// Make sure global variable for ctors exist for lib profile.
+// CHECK:@llvm.global_ctors
+
 RWBuffer<float> Buffer;
 
 [shader("compute")]
Index: clang/test/CodeGenHLSL/GlobalConstructorFunction.hlsl
===================================================================
--- clang/test/CodeGenHLSL/GlobalConstructorFunction.hlsl
+++ clang/test/CodeGenHLSL/GlobalConstructorFunction.hlsl
@@ -17,6 +17,10 @@
 [numthreads(1,1,1)]
 void main(unsigned GI : SV_GroupIndex) {}
 
+// Make sure global variable for ctors/dtors removed.
+// CHECK-NOT:@llvm.global_ctors
+// CHECK-NOT:@llvm.global_dtors
+
 //CHECK: define void @main()
 //CHECK-NEXT: entry:
 //CHECK-NEXT:   call void @"?call_me_first@@YAXXZ"()
Index: clang/lib/CodeGen/CGHLSLRuntime.h
===================================================================
--- clang/lib/CodeGen/CGHLSLRuntime.h
+++ clang/lib/CodeGen/CGHLSLRuntime.h
@@ -22,6 +22,7 @@
 namespace llvm {
 class GlobalVariable;
 class Function;
+class Triple;
 } // namespace llvm
 namespace clang {
 class VarDecl;
@@ -46,7 +47,7 @@
   virtual ~CGHLSLRuntime() {}
 
   void annotateHLSLResource(const VarDecl *D, llvm::GlobalVariable *GV);
-  void generateGlobalCtorDtorCalls();
+  void generateGlobalCtorDtorCalls(llvm::Triple &T);
 
   void finishCodeGen();
 
Index: clang/lib/CodeGen/CGHLSLRuntime.cpp
===================================================================
--- clang/lib/CodeGen/CGHLSLRuntime.cpp
+++ clang/lib/CodeGen/CGHLSLRuntime.cpp
@@ -60,7 +60,7 @@
   if (T.getArch() == Triple::ArchType::dxil)
     addDxilValVersion(TargetOpts.DxilValidatorVersion, M);
 
-  generateGlobalCtorDtorCalls();
+  generateGlobalCtorDtorCalls(T);
   if (CGM.getCodeGenOpts().OptimizationLevel == 0)
     addDisableOptimizations(M);
 }
@@ -180,7 +180,7 @@
   }
 }
 
-void CGHLSLRuntime::generateGlobalCtorDtorCalls() {
+void CGHLSLRuntime::generateGlobalCtorDtorCalls(Triple &T) {
   llvm::Module &M = CGM.getModule();
   SmallVector<Function *> CtorFns;
   SmallVector<Function *> DtorFns;
@@ -202,4 +202,13 @@
     for (auto *Fn : DtorFns)
       B.CreateCall(FunctionCallee(Fn));
   }
+
+  // No need to keep global ctors/dtors for non-lib profile after call to
+  // ctors/dtors added for entry.
+  if (T.getEnvironment() != Triple::EnvironmentType::Library) {
+    if (auto *GV = M.getNamedGlobal("llvm.global_ctors"))
+      GV->eraseFromParent();
+    if (auto *GV = M.getNamedGlobal("llvm.global_dtors"))
+      GV->eraseFromParent();
+  }
 }
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to