[clang] 8e4f0ce - [HLSL] Remove support for user-defined constructors and destructors (#193375)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Apr 24 11:10:23 PDT 2026
Author: Helena Kotas
Date: 2026-04-24T11:10:18-07:00
New Revision: 8e4f0ce6985354e90caa4deed80331e34cd7fb1c
URL: https://github.com/llvm/llvm-project/commit/8e4f0ce6985354e90caa4deed80331e34cd7fb1c
DIFF: https://github.com/llvm/llvm-project/commit/8e4f0ce6985354e90caa4deed80331e34cd7fb1c.diff
LOG: [HLSL] Remove support for user-defined constructors and destructors (#193375)
This change removes the support for user-defined constructors and
destructors.
Authored-by: Sarah Spall <sarahspall at microsoft.com>
Related to #185466
Added:
clang/test/SemaHLSL/Constructors.hlsl
Modified:
clang/include/clang/Basic/DiagnosticSemaKinds.td
clang/lib/Sema/SemaDeclCXX.cpp
clang/test/SemaHLSL/GlobalConstructors.hlsl
Removed:
clang/test/CodeGenHLSL/GlobalDestructors.hlsl
clang/test/CodeGenHLSL/inline-constructors.hlsl
################################################################################
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 43d3737bf3f3e..7ed4684c8359e 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -13565,6 +13565,7 @@ def err_hlsl_builtin_resource_coordinate_dimension_mismatch : Error<
"builtin %0 resource coordinate dimension mismatch: expected %1, found %2">;
// HLSL Diagnostics
+def err_hlsl_cstor_dstor : Error<"HLSL does not support constructors or destructors">;
def err_hlsl_langstd_unimplemented : Error<"support for HLSL language version %0 is incomplete">;
def err_hlsl_attr_unsupported_in_stage : Error<"attribute %0 is unsupported in '%1' shaders, requires %select{|one of the following: }2%3">;
def err_hlsl_attr_invalid_type : Error<
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 1c605a06b2250..90919d48f0d15 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -3526,6 +3526,18 @@ Sema::ActOnCXXMemberDeclarator(Scope *S, AccessSpecifier AS, Declarator &D,
}
}
+ // HLSL prohibits user defined constructors and destructors.
+ if (getLangOpts().HLSL) {
+ switch (Name.getNameKind()) {
+ case DeclarationName::CXXConstructorName:
+ case DeclarationName::CXXDestructorName:
+ Diag(Loc, diag::err_hlsl_cstor_dstor);
+ return nullptr;
+ default:
+ break;
+ }
+ }
+
// C++ 9.2p6: A member shall not be declared to have automatic storage
// duration (auto, register) or with the extern storage-class-specifier.
// C++ 7.1.1p8: The mutable specifier can be applied only to names of class
diff --git a/clang/test/CodeGenHLSL/GlobalDestructors.hlsl b/clang/test/CodeGenHLSL/GlobalDestructors.hlsl
deleted file mode 100644
index 9d8c2e65a9598..0000000000000
--- a/clang/test/CodeGenHLSL/GlobalDestructors.hlsl
+++ /dev/null
@@ -1,94 +0,0 @@
-// RUN: %clang_cc1 -triple spirv-unknown-vulkan1.3-compute -emit-llvm -disable-llvm-passes %s -o - | FileCheck %s --check-prefixes=CS,NOINLINE-SPIRV,CHECK
-// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-compute -emit-llvm -disable-llvm-passes %s -o - | FileCheck %s --check-prefixes=CS,NOINLINE-DXIL,CHECK
-// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -emit-llvm -disable-llvm-passes %s -o - | FileCheck %s --check-prefixes=LIB,NOINLINE-DXIL,CHECK
-// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-compute -emit-llvm -O0 %s -o - | FileCheck %s --check-prefixes=INLINE,CHECK
-// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -emit-llvm -O0 %s -o - | FileCheck %s --check-prefixes=INLINE,CHECK
-
-// Tests that constructors and destructors are appropriately generated for globals
-// and that their calls are inlined when AlwaysInline is run
-// but global variables are retained for the library profiles
-
-// Make sure global variable for ctors/dtors exist for lib profile.
-// LIB:@llvm.global_ctors
-// LIB:@llvm.global_dtors
-// Make sure global variable for ctors/dtors removed for compute profile.
-// CS-NOT:@llvm.global_ctors
-// CS-NOT:@llvm.global_dtors
-
-struct Tail {
- Tail() {
- add(1);
- }
-
- ~Tail() {
- add(-1);
- }
-
- void add(int V) {
- static int Count = 0;
- Count += V;
- }
-};
-
-struct Pupper {
- static int Count;
-
- Pupper() {
- Count += 1; // :)
- }
-
- ~Pupper() {
- Count -= 1; // :(
- }
-} GlobalPup;
-
-void Wag() {
- static Tail T;
- T.add(0);
-}
-
-int Pupper::Count = 0;
-
-[numthreads(1,1,1)]
-[shader("compute")]
-void main(unsigned GI : SV_GroupIndex) {
- Wag();
-}
-
-// CHECK: define void @main()
-// CHECK-NEXT: entry:
-// Verify destructor is emitted
-// NOINLINE-DXIL-NEXT: call void @_GLOBAL__sub_I_GlobalDestructors.hlsl()
-// NOINLINE-DXIL-NEXT: %0 = call i32 @llvm.dx.flattened.thread.id.in.group()
-// NOINLINE-DXIL-NEXT: call void @_Z4mainj(i32 %0)
-// NOINLINE-DXIL-NEXT: call void @_GLOBAL__D_a()
-// NOINLINE-DXIL-NEXT: ret void
-
-// NOINLINE-SPIRV-NEXT: %0 = call token @llvm.experimental.convergence.entry()
-// NOINLINE-SPIRV-NEXT: call spir_func void @_GLOBAL__sub_I_GlobalDestructors.hlsl() [ "convergencectrl"(token %0) ]
-// NOINLINE-SPIRV-NEXT: %1 = call i32 @llvm.spv.flattened.thread.id.in.group()
-// NOINLINE-SPIRV-NEXT: call spir_func void @_Z4mainj(i32 %1) [ "convergencectrl"(token %0) ]
-// NOINLINE-SPIRV-NEXT: call spir_func void @_GLOBAL__D_a() [ "convergencectrl"(token %0) ]
-// NOINLINE-SPIRV-NEXT: ret void
-
-// Verify inlining leaves only calls to "llvm." intrinsics
-// INLINE-NOT: call {{[^@]*}} @{{[^l][^l][^v][^m][^\.]}}
-// INLINE: ret void
-
-// This is really just a sanity check I needed for myself to verify that
-// function scope static variables also get destroyed properly.
-
-// NOINLINE-DXIL: define internal void @_GLOBAL__D_a() [[IntAttr:\#[0-9]+]]
-// NOINLINE-DXIL-NEXT: entry:
-// NOINLINE-DXIL-NEXT: call void @_ZN4TailD1Ev(ptr @_ZZ3WagvE1T)
-// NOINLINE-DXIL-NEXT: call void @_ZN6PupperD1Ev(ptr @GlobalPup)
-// NOINLINE-DXIL-NEXT: ret void
-
-// NOINLINE-SPIRV: define internal spir_func void @_GLOBAL__D_a() [[IntAttr:\#[0-9]+]]
-// NOINLINE-SPIRV-NEXT: entry:
-// NOINLINE-SPIRV-NEXT: %0 = call token @llvm.experimental.convergence.entry()
-// NOINLINE-SPIRV-NEXT: call spir_func void @_ZN4TailD1Ev(ptr addrspacecast (ptr addrspace(10) @_ZZ3WagvE1T to ptr)) [ "convergencectrl"(token %0) ]
-// NOINLINE-SPIRV-NEXT: call spir_func void @_ZN6PupperD1Ev(ptr @GlobalPup) [ "convergencectrl"(token %0) ]
-// NOINLINE-SPIRV-NEXT: ret void
-
-// NOINLINE: attributes [[IntAttr]] = {{.*}} alwaysinline
diff --git a/clang/test/CodeGenHLSL/inline-constructors.hlsl b/clang/test/CodeGenHLSL/inline-constructors.hlsl
deleted file mode 100644
index b0d5a783fb372..0000000000000
--- a/clang/test/CodeGenHLSL/inline-constructors.hlsl
+++ /dev/null
@@ -1,76 +0,0 @@
-// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-compute -std=hlsl202x -emit-llvm -o - -disable-llvm-passes %s | FileCheck %s --check-prefixes=CHECK,NOINLINE
-// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -std=hlsl202x -emit-llvm -o - -disable-llvm-passes %s | FileCheck %s --check-prefixes=CHECK,NOINLINE
-// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-compute -std=hlsl202x -emit-llvm -o - -O0 %s | FileCheck %s --check-prefixes=CHECK,INLINE
-// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -std=hlsl202x -emit-llvm -o - -O0 %s | FileCheck %s --check-prefixes=CHECK,INLINE
-// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-compute -std=hlsl202x -emit-llvm -o - -O1 %s | FileCheck %s --check-prefixes=CHECK,INLINE
-// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -std=hlsl202x -emit-llvm -o - -O1 %s | FileCheck %s --check-prefixes=CHECK,INLINE
-
-// Tests that implicit constructor calls for user classes will always be inlined.
-
-struct Weed {
- Weed() {Count += 1;}
- [[maybe_unused]] void pull() {Count--;}
- static int weedCount() { return Count; }
-private:
- static int Count;
-
-} YardWeeds;
-
-int Weed::Count = 1; // It begins. . .
-
-struct Kitty {
- unsigned burrsInFur;
-
- Kitty() {
- burrsInFur = 0;
- }
-
- void wanderInYard(int hours) {
- burrsInFur = hours*Weed::weedCount()/8;
- }
-
- void lick() {
- if(burrsInFur) {
- burrsInFur--;
- Weed w;
- }
- }
-
-} Nion;
-
-void NionsDay(int hours) {
- static Kitty Nion;
- Nion.wanderInYard(hours);
- while(Nion.burrsInFur) Nion.lick();
-}
-
-// CHECK: define void @main()
-// CHECK-NEXT: entry:
-// Verify constructor is emitted
-// NOINLINE-NEXT: call void @_GLOBAL__sub_I_inline_constructors.hlsl()
-// NOINLINE-NEXT: %0 = call i32 @llvm.dx.flattened.thread.id.in.group()
-// NOINLINE-NEXT: call void @_Z4mainj(i32 %0)
-// Verify inlining leaves only calls to "llvm." intrinsics
-// INLINE-NOT: call {{[^@]*}} @{{[^l][^l][^v][^m][^\.]}}
-// CHECK: ret void
-[shader("compute")]
-[numthreads(1,1,1)]
-void main(unsigned GI : SV_GroupIndex) {
- NionsDay(10);
-}
-
-
-// CHECK: define void @rainyMain()
-// CHECK-NEXT: entry:
-// Verify constructor is emitted
-// NOINLINE-NEXT: call void @_GLOBAL__sub_I_inline_constructors.hlsl()
-// NOINLINE-NEXT: call void @_Z9rainyMainv()
-// Verify inlining leaves only calls to "llvm." intrinsics
-// INLINE-NOT: call {{[^@]*}} @{{[^l][^l][^v][^m][^\.]}}
-// CHECK: ret void
-[shader("compute")]
-[numthreads(1,1,1)]
-void rainyMain() {
- NionsDay(1);
-}
-
diff --git a/clang/test/SemaHLSL/Constructors.hlsl b/clang/test/SemaHLSL/Constructors.hlsl
new file mode 100644
index 0000000000000..fb6f70b20a0de
--- /dev/null
+++ b/clang/test/SemaHLSL/Constructors.hlsl
@@ -0,0 +1,52 @@
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -fsyntax-only %s -verify
+
+struct Pair {
+ int First;
+ int Second;
+
+ //expected-error at +1 {{HLSL does not support constructors or destructors}}
+ Pair() {
+ First = 0;
+ Second = 0;
+ }
+
+ //expected-error at +1 {{HLSL does not support constructors or destructors}}
+ Pair(int F, int S) {
+ First = F;
+ Second = S;
+ }
+
+ //expected-error at +2 {{references are unsupported in HLSL}}
+ //expected-error at +1 {{HLSL does not support constructors or destructors}}
+ Pair(const Pair& P) {
+ this.First = P.First;
+ this.Second = P.Second;
+ }
+
+ //expected-error at +2 {{references are unsupported in HLSL}}
+ //expected-error at +1 {{HLSL does not support constructors or destructors}}
+ Pair(Pair&& P) = default;
+
+ //expected-error at +1 {{HLSL does not support constructors or destructors}}
+ ~Pair();
+
+ //expected-error at +2 {{references are unsupported in HLSL}}
+ //expected-error at +1 {{references are unsupported in HLSL}}
+ Pair& operator=(const Pair& P)
+ {
+ this.First = P.First;
+ this.Second = P.Second;
+ //expected-error at +1 {{the '*' operator is unsupported in HLSL}}
+ return *this;
+ }
+
+ //expected-error at +2 {{references are unsupported in HLSL}}
+ //expected-error at +1 {{references are unsupported in HLSL}}
+ Pair& operator=(Pair&& P)
+ {
+ First = move(P.First);
+ Second = move(P.Second);
+ //expected-error at +1 {{the '*' operator is unsupported in HLSL}}
+ return *this;
+ }
+};
diff --git a/clang/test/SemaHLSL/GlobalConstructors.hlsl b/clang/test/SemaHLSL/GlobalConstructors.hlsl
index ddd09422d7485..a8aa0a4ea908c 100644
--- a/clang/test/SemaHLSL/GlobalConstructors.hlsl
+++ b/clang/test/SemaHLSL/GlobalConstructors.hlsl
@@ -3,6 +3,7 @@
int i;
struct Pup {
+ // expected-error at +1 {{HLSL does not support constructors or destructors}}
Pup() {
i++;
}
More information about the cfe-commits
mailing list