From f3b0babe4264557f3cfef9f4878b97f4ec5a82ef Mon Sep 17 00:00:00 2001 From: Chris Bieneman Date: Sun, 20 Sep 2026 22:18:35 -0500 Subject: [PATCH 1/3] [202x][0005] Implement cbuffer contexts This restricts the ability to nest namespaces and cbuffer/tbuffer declarations within cbuffers. This implements the language adopted by TC57 for the draft specification. Fixes #8484 ../tools/clang/include/clang/Basic/DiagnosticSemaKinds.td ../tools/clang/test/SemaHLSL/hlsl/202x-cbuffer-contexts.hlsl --- .../clang/Basic/DiagnosticSemaKinds.td | 2 + tools/clang/lib/Sema/SemaHLSL.cpp | 13 +++++ .../SemaHLSL/hlsl/202x-cbuffer-contexts.hlsl | 57 +++++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 tools/clang/test/SemaHLSL/hlsl/202x-cbuffer-contexts.hlsl diff --git a/tools/clang/include/clang/Basic/DiagnosticSemaKinds.td b/tools/clang/include/clang/Basic/DiagnosticSemaKinds.td index cc7a4662f1..650bce5874 100644 --- a/tools/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/tools/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -7926,6 +7926,8 @@ def err_hlsl_unsupported_object_context "entry function parameters|entry function return type|" "patch constant function parameters|patch constant function return type|" "payload parameters|attributes|builtin template parameters|structured buffers|global variables|groupshared variables}1">; +def err_hlsl_unsupported_declaration_in_buffer : Error< + "unsupported declaration %0 in %select{tbuffer|cbuffer}1 declaration">; def err_hlsl_logical_binop_scalar : Error< "operands for short-circuiting logical binary operator must be scalar, for non-scalar types use '%select{and|or}0'">; def err_hlsl_ternary_scalar : Error< diff --git a/tools/clang/lib/Sema/SemaHLSL.cpp b/tools/clang/lib/Sema/SemaHLSL.cpp index da6f7f8f90..2e47e27efc 100644 --- a/tools/clang/lib/Sema/SemaHLSL.cpp +++ b/tools/clang/lib/Sema/SemaHLSL.cpp @@ -15387,6 +15387,19 @@ void Sema::ActOnFinishHLSLBuffer(Decl *Dcl, SourceLocation RBrace) { bool HasPackOffset = false; bool HasNonPackOffset = false; for (auto *Field : BufDecl->decls()) { + // HLSL 202x 0005 Cbuffer Contexts proposal resetricts the contents of a + // cbuffer to classes (records), functions, variables, and empty + // declarations (see: https://hlsl-tc57.github.io/tc57/proposal/0005/) + if (getLangOpts().HLSLVersion >= hlsl::LangStd::v202x && + (isa(Field) || isa(Field))) { + NamedDecl *ND = cast(Field); + Diag(Field->getLocation(), + diag::err_hlsl_unsupported_declaration_in_buffer) + << ND << BufDecl->isCBuffer(); + Diag(Dcl->getLocation(), diag::note_declared_at); + Dcl->setInvalidDecl(); + } + VarDecl *Var = dyn_cast(Field); if (!Var) continue; diff --git a/tools/clang/test/SemaHLSL/hlsl/202x-cbuffer-contexts.hlsl b/tools/clang/test/SemaHLSL/hlsl/202x-cbuffer-contexts.hlsl new file mode 100644 index 0000000000..212e505152 --- /dev/null +++ b/tools/clang/test/SemaHLSL/hlsl/202x-cbuffer-contexts.hlsl @@ -0,0 +1,57 @@ +// RUN: %dxc -T lib_6_9 -verify -HV 202x %s + +cbuffer A { // expected-note{{declared here}} + int Y; + // expected-error@+1 {{unsupported declaration 'N' in cbuffer declaration}} + namespace N { + } + float4 F5; +} + +cbuffer A { // expected-note{{declared here}} + // expected-error@+1 {{unsupported declaration 'Nested' in cbuffer declaration}} + cbuffer Nested { + } +} + +tbuffer TB { // expected-note{{declared here}} + // expected-error@+1 {{unsupported declaration 'NS' in tbuffer declaration}} + namespace NS {} +} + +tbuffer TB2 { // expected-note{{declared here}} + // expected-error@+1{{unsupported declaration 'CB2' in tbuffer declaration}} + cbuffer CB2 { + int X; + } +} + +namespace Valid { + cbuffer CBValid { + int CompletelyFine; + } + + tbuffer TBValid { + float StillFine; + } + + cbuffer GoingOffTheRails { // expected-note{{declared here}} + // expected-error@+1{{unsupported declaration 'NotCool' in cbuffer declaration}} + tbuffer NotCool { + ; // even if it is empty... + } + } + + cbuffer GoingOffTheRailsAgain { // expected-note{{declared here}} + // expected-error@+1{{unsupported declaration 'StillNotCool' in cbuffer declaration}} + tbuffer StillNotCool { // expected-note{{declared here}} + // expected-error@+1{{unsupported declaration 'Turtles' in tbuffer declaration}} + cbuffer Turtles { // expected-note{{declared here}} + // expected-error@+1{{unsupported declaration 'AllTheWayDown' in cbuffer declaration}} + tbuffer AllTheWayDown { + ; + } + } + } + } +} From e9a25e9372bcdb18ed39a0f1249b302480f88ecb Mon Sep 17 00:00:00 2001 From: Chris B Date: Mon, 21 Sep 2026 10:16:56 -0500 Subject: [PATCH 2/3] Fix typo in HLSL 202x proposal comment Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- tools/clang/lib/Sema/SemaHLSL.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/clang/lib/Sema/SemaHLSL.cpp b/tools/clang/lib/Sema/SemaHLSL.cpp index 2e47e27efc..fa5869966c 100644 --- a/tools/clang/lib/Sema/SemaHLSL.cpp +++ b/tools/clang/lib/Sema/SemaHLSL.cpp @@ -15387,7 +15387,7 @@ void Sema::ActOnFinishHLSLBuffer(Decl *Dcl, SourceLocation RBrace) { bool HasPackOffset = false; bool HasNonPackOffset = false; for (auto *Field : BufDecl->decls()) { - // HLSL 202x 0005 Cbuffer Contexts proposal resetricts the contents of a + // HLSL 202x 0005 Cbuffer Contexts proposal restricts the contents of a // cbuffer to classes (records), functions, variables, and empty // declarations (see: https://hlsl-tc57.github.io/tc57/proposal/0005/) if (getLangOpts().HLSLVersion >= hlsl::LangStd::v202x && From 19e9faabdf8ba1c259f3eb49f78ae41bed9eef7f Mon Sep 17 00:00:00 2001 From: Chris Bieneman Date: Tue, 22 Sep 2026 11:19:34 -0500 Subject: [PATCH 3/3] Update release notes --- docs/ReleaseNotes.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/ReleaseNotes.md b/docs/ReleaseNotes.md index d09a8bef25..3b66b9a68c 100644 --- a/docs/ReleaseNotes.md +++ b/docs/ReleaseNotes.md @@ -60,6 +60,9 @@ line upon naming the release. Refer to previous for appropriate section names. with language changes introduced in HLSL 2026. - The legacy effects syntax support is removed in HLSL 202x [#8480](https://github.com/microsoft/DirectXShaderCompiler/issues/8480). +- HLSL 202x disallows putting cbuffer, tbuffer, or namespace declarations inside + a cbuffer or tbuffer + [#8484](https://github.com/microsoft/DirectXShaderCompiler/issues/8484). #### SPIR-V