Skip to content

Commit 8923b6d

Browse files
authored
[NNNN] Design default templates for textures. (#386)
Add a design for handling `Texture2D` and `Texture2D<>` notation for declaring textured types. See llvm/llvm-project#184207 for an implementation.
1 parent 732cfef commit 8923b6d

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed

proposals/0037-texture-and-sampler-types.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ been designed:
6767
[0015 - Mapping Resource Attributes to DXIL and SPIR-V](0015-resource-attributes-in-dxil-and-spirv.md).
6868
- GetDimensions: See
6969
[0033 - Resource GetDimensions](0033-resources-get-dimensions.md).
70+
- Default Template Arguments and Shorthand: See
71+
[NNNN - Texture Default Templates](NNNN-texture-default-templates.md).
7072

7173
The detailed design in this proposal will fill in the details of the
7274
`HLSLAttributedResourceType` for these types. It should follow the design in
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
---
2+
title: "[NNNN] - Texture Default Templates"
3+
params:
4+
authors:
5+
- s-perron: Steven Perron
6+
status: Under Consideration
7+
sponsors:
8+
---
9+
10+
- PRs: [#184207](https://github.com/llvm/llvm-project/pull/184207)
11+
12+
## Introduction
13+
14+
This proposal describes the design and implementation for supporting default
15+
template arguments and shorthand notation for HLSL texture types (e.g.,
16+
`Texture2D`) in HLSL. This allows these types to be used without template
17+
arguments or with an empty template list (e.g., `Texture2D` or `Texture2D<>`),
18+
defaulting to an element type of `float4`, matching the behavior of the DirectX
19+
Shader Compiler (DXC) in HLSL202x.
20+
21+
## Motivation
22+
23+
In HLSL, texture types are frequently used with a default element type of
24+
`float4`. DXC allows developers to omit the template arguments entirely (e.g.,
25+
`Texture2D`) or provide an empty template list (e.g., `Texture2D<>`).
26+
27+
Clang's current implementation of HLSL resources requires explicit template
28+
arguments. Implementing this in Clang using standard C++ features like Class
29+
Template Argument Deduction (CTAD) is problematic because:
30+
31+
1. HLSL202x does not support user-defined CTAD, and enabling it globally would
32+
diverge from DXC's implementation.
33+
2. Resource handling in Clang (such as determining if a type is a resource via
34+
its handle and attributes) happens early in the compilation flow. Standard
35+
C++ template deduction occurs later, and many parts of the resource-specific
36+
semantic analysis expect a concrete type. Supporting "yet-to-be-deduced"
37+
resource types would require a significant and pervasive rewrite of the
38+
resource handling code in Clang.
39+
40+
The goal is to provide a surgical solution that matches DXC behavior while
41+
maintaining the integrity of Clang's existing resource representation.
42+
43+
## Proposed solution
44+
45+
The proposed solution involves two complementary mechanisms:
46+
47+
1. **Default Template Parameter**: Update the class template declarations for
48+
HLSL texture types in the HLSL External Sema Source to include a default
49+
template argument of `float4` (represented as `vector<float, 4>`). This
50+
natively enables the `Texture2D<>` syntax in C++.
51+
52+
2. **Shorthand Notation Support**: Introduce a special case in Clang's semantic
53+
analysis to intercept the use of a template name without any template
54+
arguments. When an identifier for an HLSL texture type is encountered in a
55+
context where a type is expected, and it is identified as an HLSL resource
56+
template, Clang will automatically instantiate the template with its default
57+
parameters.
58+
59+
## Detailed design
60+
61+
### BuiltinTypeDeclBuilder Enhancements
62+
63+
The `BuiltinTypeDeclBuilder` utility, used to generate HLSL resource types in
64+
the External Sema Source, is updated to support default template parameters:
65+
66+
```cpp
67+
BuiltinTypeDeclBuilder &
68+
BuiltinTypeDeclBuilder::addSimpleTemplateParams(ArrayRef<StringRef> Names,
69+
ArrayRef<QualType> DefaultValues,
70+
ConceptDecl *CD);
71+
```
72+
73+
This allows the registration code to specify `float4` as the default for the
74+
`element_type` parameter of texture types.
75+
76+
### HLSLExternalSemaSource Integration
77+
78+
The initialization of texture types in `HLSLExternalSemaSource.cpp` is modified
79+
to provide the default type. For `Texture2D`, it would look like:
80+
81+
```cpp
82+
QualType Float4Ty = AST.getExtVectorType(AST.FloatTy, 4);
83+
Decl = BuiltinTypeDeclBuilder(*SemaPtr, HLSLNamespace, "Texture2D")
84+
.addSimpleTemplateParams({"element_type"}, {Float4Ty},
85+
TypedBufferConcept)
86+
.finalizeForwardDeclaration();
87+
```
88+
89+
### Semantic Analysis (Sema)
90+
91+
A new method, `SemaHLSL::ActOnTemplateShorthand`, is added to handle the
92+
conversion of a template name to a concrete type:
93+
94+
1. **Intercepting Template Names**: In `Sema::getTypeName`, when an identifier is
95+
successfully resolved but identifies a template, and the language is HLSL,
96+
`ActOnTemplateShorthand` is called before falling back to Class Template
97+
Argument Deduction (CTAD) or erroring.
98+
2. **Validation**: The function verifies that the template belongs to the
99+
`hlsl` namespace and has default arguments for all its template parameters.
100+
3. **Synthesis**: It iterates through the template parameters and retrieves the
101+
default arguments from the declaration to construct a complete
102+
`TemplateArgumentListInfo`. It then calls `Sema::CheckTemplateIdType` to
103+
produce a fully qualified `QualType`.
104+
105+
### Testing Strategy
106+
107+
1. **AST Verification**: Use `-ast-dump` to ensure that `Texture2D`,
108+
`Texture2D<>`, and `Texture2D<float4>` (and other texture types) all result
109+
in a `ClassTemplateSpecializationDecl` with `float4` as the template
110+
argument when shorthand or empty templates are used.
111+
2. **CodeGen Verification**: Verify that variables declared with shorthand
112+
notation correctly lower to LLVM IR with the expected target extension types
113+
(e.g., `target("dx.Texture", <4 x float>, ...)`).
114+
3. **Binding and Semantics**: Ensure that the shorthand types work correctly
115+
with HLSL-specific features like `register` bindings and member function
116+
calls (e.g., `Sample`). This tests that the texture can be identified as a
117+
resource early in the compilation.
118+
119+
## Alternatives considered
120+
121+
### Class Template Argument Deduction (CTAD)
122+
123+
As mentioned in the motivation, CTAD was considered but rejected because it is
124+
not officially supported in HLSL202x and would require significant architectural
125+
changes to handle undeduced resources in early Sema passes.
126+
127+
### Type Aliases
128+
129+
We considered using `typedef Texture2D<float4> Texture2D;` or declaring a
130+
non-template type alongside the template type. This was quickly rejected because
131+
it would break the usual C++ lookup. Creating these types of special cases is
132+
not a good solution.

0 commit comments

Comments
 (0)