Skip to content

Commit 3b61caf

Browse files
llvm-beanzhekota
andauthored
[0037] Only restrict nesting cbuffer and namespace decls (#685)
This slightly scales back the proposal to restricting namespaces in cbuffers and restricting nesting of cbuffers. This is likely a safe change for HLSL 202x, and we'll consider more significant changes in subsequent language updates. This conclusion came out of the language design meeting discussion from 10/13/2025. The language spec updates are posted in a draft as #684. --------- Co-authored-by: Helena Kotas <[email protected]>
1 parent 6affb57 commit 3b61caf

File tree

2 files changed

+47
-26
lines changed

2 files changed

+47
-26
lines changed
2.1 KB
Loading

proposals/0037-cbuffer-contexts.md

Lines changed: 47 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ params:
77
status: Under Consideration
88
---
99

10-
11-
1210
* Planned Version: 202x
1311
* Issues: [DXC #4514](https://github.com/microsoft/DirectXShaderCompiler/issues/4514)
1412

@@ -69,39 +67,62 @@ To simplify HLSL's language semantics and the compiler implementation a new
6967
grammar formation is adopted for cbuffers:
7068

7169
```latex
72-
7370
\begin{grammar}
74-
\define{cbuffer-declaration-group}\br
75-
\terminal{cbuffer} identifier \opt{resource-binding} \terminal{\{}
76-
\opt{cbuffer-declaration-seq} \terminal {\}}\br
77-
78-
\define{cbuffer-declaration-seq}\br
79-
cbuffer-declaration\br
80-
cbuffer-declaration-seq cbuffer-declaration\br
81-
8271
\define{cbuffer-declaration}\br
83-
variable-declaration\br
84-
empty-declaration\br
72+
\terminal{cbuffer} name \opt{resource-binding} \terminal{\{}
73+
\opt{cbuffer-member-seq} \terminal {\}}\br
74+
75+
\define{cbuffer-member-seq}\br
76+
cbuffer-member-declaration\br
77+
cbuffer-member-seq cbuffer-member-declaration\br
78+
79+
\define{cbuffer-member-declaration}\br
80+
block-declaration\br
81+
function-definition\br
82+
template-declaration\br
83+
empty-declaration
8584
\end{grammar}
8685
```
8786
![Latex Rendering](0037-assets/cbuffer-grammar.png)
8887

89-
This simplified grammar disallows members of `cbuffer` declarations that do not
90-
have semantic meaning, and allows a simplification of `cbuffer` scoping rules.
88+
This simplified grammar restricts the declarations that are valid within a
89+
cbuffer to declarations that are broadly valid inside block scopes as well as
90+
function and template declarations. This allows a cbuffer to contain
91+
declarations of classes and data types as well as functions and variables.
9192

9293
A `cbuffer` may only be declared at translation unit or namespace scope. A
93-
`cbuffer` may only contain non-static non-constexpr variable (or empty)
94-
declarations. All declarations within a `cbuffer` declare names in the immediate
95-
enclosing scope.
94+
`cbuffer` may not contain a namespace declaration and may only contain a subset
95+
of valid global declarations.
96+
97+
## Detailed Design
98+
99+
> The following text will be included in the [Decl.cbuffer] section in the
100+
> language specification.
101+
102+
A _cbuffer declaration_ is declared with the \texttt{cbuffer} keyword. The name
103+
of the cbuffer declaration does not declare a name, and cannot be referenced
104+
from within the translation unit, nor is it required to be unique. Each cbuffer
105+
declaration refers to a unique constant buffer resource.
106+
107+
Declarations within a cbuffer declaration that declare names, declare their
108+
names in the scope containing the cbuffer declaration. The cbuffer declaration
109+
itself does not declare a declaration scope. A cbuffer declaration may not
110+
contain a _namespace-declaration_ or _cbuffer-declaration_.
111+
112+
Variable declarations with program storage duration in the cbuffer declaration
113+
are called _shader constants_. Shader constants are implicitly `const`
114+
and cannot be modified in program code.
96115

97-
A `cbuffer` is a colection of constant values provided to a shader at runtime.
98-
As such, it should only contain constant variable declarations backed by
99-
read-only storage in device memory that persists across the life of the
100-
dispatch.
116+
## Long-term Considerations
101117

102-
Notably a `cbuffer` cannot contain function declarations, type declarations
103-
(classes, enumerations, typedefs), namespace declarations, constexpr or static
104-
variable declarations, `cbuffer` declarations, or any other declaration type not
105-
explicitly listed as allowed.
118+
During our discussion in the language design meeting additional restrictions to
119+
cbuffer declarations were considered. The general consensus was that additional
120+
restrictions are desirable and will remove possible programmer errors, however
121+
there were concerns about HLSL 202x changing too much at once.
106122

123+
In a future language version we may consider further restricting cbuffers to
124+
allow only variable declarations of _program storage duration_.
107125

126+
In implementing cbuffer support in Clang we should consider implementing
127+
a diagnostic for all variable declarations that are not of program storage
128+
duration to identify potential programmer errors.

0 commit comments

Comments
 (0)