Skip to content

Commit 9434c08

Browse files
authoredApr 1, 2024··
[HLSL] Implement array temporary support (#79382)
HLSL constant sized array function parameters do not decay to pointers. Instead constant sized array types are preserved as unique types for overload resolution, template instantiation and name mangling. This implements the change by adding a new `ArrayParameterType` which represents a non-decaying `ConstantArrayType`. The new type behaves the same as `ConstantArrayType` except that it does not decay to a pointer. Values of `ConstantArrayType` in HLSL decay during overload resolution via a new `HLSLArrayRValue` cast to `ArrayParameterType`. `ArrayParamterType` values are passed indirectly by-value to functions in IR generation resulting in callee generated memcpy instructions. The behavior of HLSL function calls is documented in the [draft language specification](https://microsoft.github.io/hlsl-specs/specs/hlsl.pdf) under the Expr.Post.Call heading. Additionally the design of this implementation approach is documented in [Clang's documentation](https://clang.llvm.org/docs/HLSL/FunctionCalls.html) Resolves #70123
1 parent 92d0d6f commit 9434c08

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+606
-31
lines changed
 

‎clang/docs/HLSL/FunctionCalls.rst

+16-15
Original file line numberDiff line numberDiff line change
@@ -157,22 +157,23 @@ Clang Implementation
157157
of the changes in the prototype implementation are restoring Clang-3.7 code
158158
that was previously modified to its original state.
159159

160-
The implementation in clang depends on two new AST nodes and minor extensions to
161-
Clang's existing support for Objective-C write-back arguments. The goal of this
162-
design is to capture the semantic details of HLSL function calls in the AST, and
163-
minimize the amount of magic that needs to occur during IR generation.
164-
165-
The two new AST nodes are ``HLSLArrayTemporaryExpr`` and ``HLSLOutParamExpr``,
166-
which respectively represent the temporaries used for passing arrays by value
167-
and the temporaries created for function outputs.
160+
The implementation in clang adds a new non-decaying array type, a new AST node
161+
to represent output parameters, and minor extensions to Clang's existing support
162+
for Objective-C write-back arguments. The goal of this design is to capture the
163+
semantic details of HLSL function calls in the AST, and minimize the amount of
164+
magic that needs to occur during IR generation.
168165

169166
Array Temporaries
170167
-----------------
171168

172-
The ``HLSLArrayTemporaryExpr`` represents temporary values for input
173-
constant-sized array arguments. This applies for all constant-sized array
174-
arguments regardless of whether or not the parameter is constant-sized or
175-
unsized.
169+
The new ``ArrayParameterType`` is a sub-class of ``ConstantArrayType``
170+
inheriting all the behaviors and methods of the parent except that it does not
171+
decay to a pointer during overload resolution or template type deduction.
172+
173+
An argument of ``ConstantArrayType`` can be implicitly converted to an
174+
equivalent non-decayed ``ArrayParameterType`` if the underlying canonical
175+
``ConstantArrayType`` is the same. This occurs during overload resolution
176+
instead of array to pointer decay.
176177

177178
.. code-block:: c++
178179

@@ -193,7 +194,7 @@ In the example above, the following AST is generated for the call to
193194
CallExpr 'void'
194195
|-ImplicitCastExpr 'void (*)(float [4])' <FunctionToPointerDecay>
195196
| `-DeclRefExpr 'void (float [4])' lvalue Function 'SizedArray' 'void (float [4])'
196-
`-HLSLArrayTemporaryExpr 'float [4]'
197+
`-ImplicitCastExpr 'float [4]' <HLSLArrayRValue>
197198
`-DeclRefExpr 'float [4]' lvalue Var 'arr' 'float [4]'
198199
199200
In the example above, the following AST is generated for the call to
@@ -204,7 +205,7 @@ In the example above, the following AST is generated for the call to
204205
CallExpr 'void'
205206
|-ImplicitCastExpr 'void (*)(float [])' <FunctionToPointerDecay>
206207
| `-DeclRefExpr 'void (float [])' lvalue Function 'UnsizedArray' 'void (float [])'
207-
`-HLSLArrayTemporaryExpr 'float [4]'
208+
`-ImplicitCastExpr 'float [4]' <HLSLArrayRValue>
208209
`-DeclRefExpr 'float [4]' lvalue Var 'arr' 'float [4]'
209210
210211
In both of these cases the argument expression is of known array size so we can
@@ -236,7 +237,7 @@ An expected AST should be something like:
236237
CallExpr 'void'
237238
|-ImplicitCastExpr 'void (*)(float [])' <FunctionToPointerDecay>
238239
| `-DeclRefExpr 'void (float [])' lvalue Function 'UnsizedArray' 'void (float [])'
239-
`-HLSLArrayTemporaryExpr 'float [4]'
240+
`-ImplicitCastExpr 'float [4]' <HLSLArrayRValue>
240241
`-DeclRefExpr 'float [4]' lvalue Var 'arr' 'float [4]'
241242
242243
Out Parameter Temporaries

‎clang/include/clang/AST/ASTContext.h

+7
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,9 @@ class ASTContext : public RefCountedBase<ASTContext> {
260260
ASTContext&>
261261
SubstTemplateTemplateParmPacks;
262262

263+
mutable llvm::ContextualFoldingSet<ArrayParameterType, ASTContext &>
264+
ArrayParameterTypes;
265+
263266
/// The set of nested name specifiers.
264267
///
265268
/// This set is managed by the NestedNameSpecifier class.
@@ -1367,6 +1370,10 @@ class ASTContext : public RefCountedBase<ASTContext> {
13671370
/// type to the decayed type.
13681371
QualType getDecayedType(QualType Orig, QualType Decayed) const;
13691372

1373+
/// Return the uniqued reference to a specified array parameter type from the
1374+
/// original array type.
1375+
QualType getArrayParameterType(QualType Ty) const;
1376+
13701377
/// Return the uniqued reference to the atomic type for the specified
13711378
/// type.
13721379
QualType getAtomicType(QualType T) const;

0 commit comments

Comments
 (0)