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 @@ -212,4 +212,14 @@ 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. + Triple T(M.getTargetTriple()); + 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 @@ -1,5 +1,10 @@ -// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-compute -std=hlsl202x -S -emit-llvm -disable-llvm-passes %s -o - | FileCheck %s -// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -std=hlsl202x -S -emit-llvm -disable-llvm-passes %s -o - | FileCheck %s --check-prefixes=CHECK +// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-compute -std=hlsl202x -S -emit-llvm -disable-llvm-passes %s -o - | FileCheck %s --check-prefixes=CS,CHECK +// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -std=hlsl202x -S -emit-llvm -disable-llvm-passes %s -o - | FileCheck %s --check-prefixes=LIB,CHECK + +// Make sure global variable for dtors exist for lib profile. +// LIB:@llvm.global_dtors +// Make sure global variable for dtors removed for compute profile. +// CS-NOT:llvm.global_dtors struct Tail { Tail() { @@ -40,6 +45,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()