diff --git a/clang/lib/CodeGen/CGHLSLRuntime.h b/clang/lib/CodeGen/CGHLSLRuntime.h --- a/clang/lib/CodeGen/CGHLSLRuntime.h +++ b/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(); diff --git a/clang/lib/CodeGen/CGHLSLRuntime.cpp b/clang/lib/CodeGen/CGHLSLRuntime.cpp --- a/clang/lib/CodeGen/CGHLSLRuntime.cpp +++ b/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 CtorFns; SmallVector 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(); + } } diff --git a/clang/test/CodeGenHLSL/GlobalConstructorFunction.hlsl b/clang/test/CodeGenHLSL/GlobalConstructorFunction.hlsl --- a/clang/test/CodeGenHLSL/GlobalConstructorFunction.hlsl +++ b/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"() diff --git a/clang/test/CodeGenHLSL/GlobalConstructorLib.hlsl b/clang/test/CodeGenHLSL/GlobalConstructorLib.hlsl --- a/clang/test/CodeGenHLSL/GlobalConstructorLib.hlsl +++ b/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 Buffer; [shader("compute")] diff --git a/clang/test/CodeGenHLSL/GlobalConstructors.hlsl b/clang/test/CodeGenHLSL/GlobalConstructors.hlsl --- a/clang/test/CodeGenHLSL/GlobalConstructors.hlsl +++ b/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() diff --git a/clang/test/CodeGenHLSL/GlobalDestructors.hlsl b/clang/test/CodeGenHLSL/GlobalDestructors.hlsl --- a/clang/test/CodeGenHLSL/GlobalDestructors.hlsl +++ b/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()