clang 23.0.0git
SemaHLSL.h
Go to the documentation of this file.
1//===----- SemaHLSL.h ----- Semantic Analysis for HLSL constructs ---------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8/// \file
9/// This file declares semantic analysis for HLSL constructs.
10///
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_CLANG_SEMA_SEMAHLSL_H
14#define LLVM_CLANG_SEMA_SEMAHLSL_H
15
16#include "clang/AST/ASTFwd.h"
17#include "clang/AST/Attr.h"
18#include "clang/AST/Type.h"
19#include "clang/AST/TypeLoc.h"
22#include "clang/Sema/SemaBase.h"
23#include "llvm/ADT/DenseMap.h"
24#include "llvm/ADT/SmallVector.h"
25#include "llvm/ADT/StringSet.h"
26#include "llvm/TargetParser/Triple.h"
27#include <initializer_list>
28
29namespace clang {
31class IdentifierInfo;
34class ParsedAttr;
35class Scope;
36class VarDecl;
37
38namespace hlsl {
39
40// Introduce a wrapper struct around the underlying RootElement. This structure
41// will retain extra clang diagnostic information that is not available in llvm.
44 llvm::hlsl::rootsig::RootElement Element)
45 : Loc(Loc), Element(Element) {}
46
47 const llvm::hlsl::rootsig::RootElement &getElement() const { return Element; }
48 const SourceLocation &getLocation() const { return Loc; }
49
50private:
52 llvm::hlsl::rootsig::RootElement Element;
53};
54
55} // namespace hlsl
56
57using llvm::dxil::ResourceClass;
58
59// FIXME: This can be hidden (as static function in SemaHLSL.cpp) once we no
60// longer need to create builtin buffer types in HLSLExternalSemaSource.
62 Sema &S, QualType Wrapped, ArrayRef<const Attr *> AttrList,
63 QualType &ResType, HLSLAttributedResourceLocInfo *LocInfo = nullptr);
64
66
67// DeclBindingInfo struct stores information about required/assigned resource
68// binding onon a declaration for specific resource class.
70 const VarDecl *Decl;
72 const HLSLResourceBindingAttr *Attr;
74
79
80 void setBindingAttribute(HLSLResourceBindingAttr *A, BindingType BT) {
81 assert(Attr == nullptr && BindType == BindingType::NotAssigned &&
82 "binding attribute already assigned");
83 Attr = A;
84 BindType = BT;
85 }
86};
87
88// ResourceBindings class stores information about all resource bindings
89// in a shader. It is used for binding diagnostics and implicit binding
90// assignments.
92public:
94 ResourceClass ResClass);
96 ResourceClass ResClass);
97 bool hasBindingInfoForDecl(const VarDecl *VD) const;
98
99private:
100 // List of all resource bindings required by the shader.
101 // A global declaration can have multiple bindings for different
102 // resource classes. They are all stored sequentially in this list.
103 // The DeclToBindingListIndex hashtable maps a declaration to the
104 // index of the first binding info in the list.
106 llvm::DenseMap<const VarDecl *, unsigned> DeclToBindingListIndex;
107};
108
109class SemaHLSL : public SemaBase {
110public:
111 SemaHLSL(Sema &S);
112
113 Decl *ActOnStartBuffer(Scope *BufferScope, bool CBuffer, SourceLocation KwLoc,
114 IdentifierInfo *Ident, SourceLocation IdentLoc,
115 SourceLocation LBrace);
116 void ActOnFinishBuffer(Decl *Dcl, SourceLocation RBrace);
117 HLSLNumThreadsAttr *mergeNumThreadsAttr(Decl *D,
118 const AttributeCommonInfo &AL, int X,
119 int Y, int Z);
120 HLSLWaveSizeAttr *mergeWaveSizeAttr(Decl *D, const AttributeCommonInfo &AL,
121 int Min, int Max, int Preferred,
122 int SpelledArgsCount);
123 HLSLVkConstantIdAttr *
124 mergeVkConstantIdAttr(Decl *D, const AttributeCommonInfo &AL, int Id);
125 HLSLShaderAttr *mergeShaderAttr(Decl *D, const AttributeCommonInfo &AL,
126 llvm::Triple::EnvironmentType ShaderType);
127 HLSLParamModifierAttr *
129 HLSLParamModifierAttr::Spelling Spelling);
136
137 // Return true if everything is ok; returns false if there was an error.
138 bool CheckResourceBinOp(BinaryOperatorKind Opc, Expr *LHSExpr, Expr *RHSExpr,
139 SourceLocation Loc);
140
142
144 QualType LHSType, QualType RHSType,
145 bool IsCompAssign);
147
148 // Returns the result of converting ConstantBuffer<T> to
149 // `const hlsl_constant T&`. If `BaseExpr`'s type is not ConstantBuffer<T>
150 // then the return value is `std::nullopt`.
151 std::optional<ExprResult>
153
154 // Returns the conversion operator to convert `RD` to `const hlsl_constant
155 // Type&`. Returns `nullptr` if it could not be found.
157 CXXRecordDecl *RD);
158
159 /// Computes the unique Root Signature identifier from the given signature,
160 /// then lookup if there is a previousy created Root Signature decl.
161 ///
162 /// Returns the identifier and if it was found
163 std::pair<IdentifierInfo *, bool>
164 ActOnStartRootSignatureDecl(StringRef Signature);
165
166 /// Creates the Root Signature decl of the parsed Root Signature elements
167 /// onto the AST and push it onto current Scope
168 void
171
173 RootSigOverrideIdent = DeclIdent;
174 }
175
177
178 // Returns true if any RootSignatureElement is invalid and a diagnostic was
179 // produced
180 bool
182 void handleRootSignatureAttr(Decl *D, const ParsedAttr &AL);
183 void handleNumThreadsAttr(Decl *D, const ParsedAttr &AL);
184 void handleWaveSizeAttr(Decl *D, const ParsedAttr &AL);
185 void handleVkConstantIdAttr(Decl *D, const ParsedAttr &AL);
186 void handleVkBindingAttr(Decl *D, const ParsedAttr &AL);
187 void handleVkLocationAttr(Decl *D, const ParsedAttr &AL);
188 void handlePackOffsetAttr(Decl *D, const ParsedAttr &AL);
189 void handleShaderAttr(Decl *D, const ParsedAttr &AL);
190 void handleResourceBindingAttr(Decl *D, const ParsedAttr &AL);
191 void handleParamModifierAttr(Decl *D, const ParsedAttr &AL);
194 SourceLocation Loc);
195 // Re-type a layout-adapting matrix builtin call \p E with \p DestType's
196 // row_major/column_major sugar so CodeGen lowers it into that layout.
198 bool handleResourceTypeAttr(QualType T, const ParsedAttr &AL);
199
200 template <typename T>
202 std::optional<unsigned> Location) {
203 return ::new (getASTContext())
204 T(getASTContext(), ACI, ACI.getAttrName()->getName(),
205 Location.value_or(0));
206 }
207
208 void diagnoseSystemSemanticAttr(Decl *D, const ParsedAttr &AL,
209 std::optional<unsigned> Index);
210 void handleSemanticAttr(Decl *D, const ParsedAttr &AL);
211
212 void handleVkExtBuiltinInputAttr(Decl *D, const ParsedAttr &AL);
213 void handleVkExtBuiltinOutputAttr(Decl *D, const ParsedAttr &AL);
214 void handleVkPushConstantAttr(Decl *D, const ParsedAttr &AL);
215
216 bool CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall);
219 TakeLocForHLSLAttribute(const HLSLAttributedResourceType *RT);
220
221 // HLSL Type trait implementations
225
227
229 SourceLocation NameLoc);
230
231 // Diagnose whether the input ID is uint/unit2/uint3 type.
232 bool diagnoseInputIDType(QualType T, const ParsedAttr &AL);
233 bool diagnosePositionType(QualType T, const ParsedAttr &AL);
234
235 bool CanPerformScalarCast(QualType SrcTy, QualType DestTy);
236 bool CanPerformElementwiseCast(Expr *Src, QualType DestType);
237 bool CanPerformAggregateSplatCast(Expr *Src, QualType DestType);
239
241
243 bool handleInitialization(VarDecl *VDecl, Expr *&Init);
246 SourceLocation OpLoc,
247 const IdentifierInfo *CompName,
248 SourceLocation CompLoc);
249
251 return ImplicitBindingNextOrderID++;
252 }
253
256
257private:
258 // HLSL resource type attributes need to be processed all at once.
259 // This is a list to collect them.
260 llvm::SmallVector<const Attr *> HLSLResourcesTypeAttrs;
261
262 /// TypeLoc data for HLSLAttributedResourceType instances that we
263 /// have not yet populated.
264 llvm::DenseMap<const HLSLAttributedResourceType *,
266 LocsForHLSLAttributedResources;
267
268 // List of all resource bindings
270
271 // Map of local resource variables to their assigned global resources.
272 //
273 // The binding can be a nullptr, in which case, the variable has yet to be
274 // initialized or assigned to.
275 llvm::DenseMap<const VarDecl *, const DeclBindingInfo *> Assigns;
276
277 // Global declaration collected for the $Globals default constant
278 // buffer which will be created at the end of the translation unit.
279 llvm::SmallVector<Decl *> DefaultCBufferDecls;
280
281 uint32_t ImplicitBindingNextOrderID = 0;
282
283 IdentifierInfo *RootSigOverrideIdent = nullptr;
284
285 bool HasDeclaredAPushConstant = false;
286
287 // Information about the current subtree being flattened.
288 struct SemanticInfo {
289 HLSLParsedSemanticAttr *Semantic;
290 std::optional<uint32_t> Index = std::nullopt;
291 };
292
293 // Bitmask used to recall if the current semantic subtree is
294 // input, output or inout.
295 enum IOType {
296 In = 0b01,
297 Out = 0b10,
298 InOut = 0b11,
299 };
300
301 // The context shared by all semantics with the same IOType during
302 // flattening.
303 struct SemanticContext {
304 // Present if any semantic sharing the same IO type has an explicit or
305 // implicit SPIR-V location index assigned.
306 std::optional<bool> UsesExplicitVkLocations = std::nullopt;
307 // The set of semantics found to be active during flattening. Used to detect
308 // index collisions.
309 llvm::StringSet<> ActiveSemantics = {};
310 // The IOType of this semantic set.
311 IOType CurrentIOType;
312 };
313
314 struct SemanticStageInfo {
315 llvm::Triple::EnvironmentType Stage;
316 IOType AllowedIOTypesMask;
317 };
318
319private:
320 void collectResourceBindingsOnVarDecl(VarDecl *D);
321 void collectResourceBindingsOnUserRecordDecl(const VarDecl *VD,
322 const RecordType *RT);
323
324 void checkSemanticAnnotation(FunctionDecl *EntryPoint, const Decl *Param,
325 const HLSLAppliedSemanticAttr *SemanticAttr,
326 const SemanticContext &SC);
327
328 bool determineActiveSemanticOnScalar(FunctionDecl *FD,
329 DeclaratorDecl *OutputDecl,
330 DeclaratorDecl *D,
331 SemanticInfo &ActiveSemantic,
332 SemanticContext &SC);
333
334 bool determineActiveSemantic(FunctionDecl *FD, DeclaratorDecl *OutputDecl,
335 DeclaratorDecl *D, SemanticInfo &ActiveSemantic,
336 SemanticContext &SC);
337
338 void processExplicitBindingsOnDecl(VarDecl *D);
339
340 void diagnoseAvailabilityViolations(TranslationUnitDecl *TU);
341
342 void diagnoseAttrStageMismatch(
343 const Attr *A, llvm::Triple::EnvironmentType Stage,
344 std::initializer_list<llvm::Triple::EnvironmentType> AllowedStages);
345
346 void diagnoseSemanticStageMismatch(
347 const Attr *A, llvm::Triple::EnvironmentType Stage, IOType CurrentIOType,
348 std::initializer_list<SemanticStageInfo> AllowedStages);
349
350 void handleGlobalStructOrArrayOfWithResources(VarDecl *VD);
351
352 // Infer a common global binding info for an Expr
353 //
354 // Returns std::nullopt if the expr refers to non-unique global bindings.
355 // Returns nullptr if it refer to any global binding, otherwise it returns
356 // a reference to the global binding info.
357 std::optional<const DeclBindingInfo *> inferGlobalBinding(Expr *E);
358
359 void trackLocalResource(VarDecl *VDecl, Expr *E);
360};
361
362} // namespace clang
363
364#endif // LLVM_CLANG_SEMA_SEMAHLSL_H
Forward declaration of all AST node types.
llvm::dxil::ResourceClass ResourceClass
#define X(type, name)
Definition Value.h:97
llvm::SmallVector< std::pair< const MemRegion *, SVal >, 4 > Bindings
Defines the clang::SourceLocation class and associated facilities.
Defines the clang::TypeLoc interface and its subclasses.
C Language Family Type Representation.
Attr - This represents one attribute.
Definition Attr.h:46
const IdentifierInfo * getAttrName() const
Represents a C++ struct/union/class.
Definition DeclCXX.h:258
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition Expr.h:2949
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition DeclBase.h:1462
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
This represents one expression.
Definition Expr.h:112
Represents a function declaration or definition.
Definition Decl.h:2018
One of these records is kept for each identifier that is lexed.
StringRef getName() const
Return the actual identifier string.
Describes an C or C++ initializer list.
Definition Expr.h:5305
Describes the kind of initialization being performed, along with location information for tokens rela...
Describes an entity that is being initialized.
MemberExpr - [C99 6.5.2.3] Structure and Union Members.
Definition Expr.h:3370
This represents a decl that may have a name.
Definition Decl.h:274
Represents a parameter to a function.
Definition Decl.h:1808
ParsedAttr - Represents a syntactic attribute.
Definition ParsedAttr.h:119
A (possibly-)qualified type.
Definition TypeBase.h:937
bool hasBindingInfoForDecl(const VarDecl *VD) const
Definition SemaHLSL.cpp:233
DeclBindingInfo * getDeclBindingInfo(const VarDecl *VD, ResourceClass ResClass)
Definition SemaHLSL.cpp:219
DeclBindingInfo * addDeclBindingInfo(const VarDecl *VD, ResourceClass ResClass)
Definition SemaHLSL.cpp:206
Scope - A scope is a transient data structure that is used while parsing the program.
Definition Scope.h:41
SemaBase(Sema &S)
Definition SemaBase.cpp:7
ASTContext & getASTContext() const
Definition SemaBase.cpp:9
ExprResult ActOnOutParamExpr(ParmVarDecl *Param, Expr *Arg)
HLSLRootSignatureDecl * lookupRootSignatureOverrideDecl(DeclContext *DC) const
bool CanPerformElementwiseCast(Expr *Src, QualType DestType)
void handleWaveSizeAttr(Decl *D, const ParsedAttr &AL)
void handleVkLocationAttr(Decl *D, const ParsedAttr &AL)
HLSLAttributedResourceLocInfo TakeLocForHLSLAttribute(const HLSLAttributedResourceType *RT)
void handleSemanticAttr(Decl *D, const ParsedAttr &AL)
bool CanPerformScalarCast(QualType SrcTy, QualType DestTy)
QualType ProcessResourceTypeAttributes(QualType Wrapped)
void handleShaderAttr(Decl *D, const ParsedAttr &AL)
uint32_t getNextImplicitBindingOrderID()
Definition SemaHLSL.h:250
void CheckEntryPoint(FunctionDecl *FD)
Definition SemaHLSL.cpp:984
void handleVkExtBuiltinOutputAttr(Decl *D, const ParsedAttr &AL)
void emitLogicalOperatorFixIt(Expr *LHS, Expr *RHS, BinaryOperatorKind Opc)
void propagateContextualMatrixLayout(Expr *E, QualType DestType)
T * createSemanticAttr(const AttributeCommonInfo &ACI, std::optional< unsigned > Location)
Definition SemaHLSL.h:201
bool initGlobalResourceDecl(VarDecl *VD)
void ActOnEndOfTranslationUnit(TranslationUnitDecl *TU)
bool initGlobalResourceArrayDecl(VarDecl *VD)
HLSLVkConstantIdAttr * mergeVkConstantIdAttr(Decl *D, const AttributeCommonInfo &AL, int Id)
Definition SemaHLSL.cpp:748
HLSLNumThreadsAttr * mergeNumThreadsAttr(Decl *D, const AttributeCommonInfo &AL, int X, int Y, int Z)
Definition SemaHLSL.cpp:714
void deduceAddressSpace(VarDecl *Decl)
std::pair< IdentifierInfo *, bool > ActOnStartRootSignatureDecl(StringRef Signature)
Computes the unique Root Signature identifier from the given signature, then lookup if there is a pre...
void handlePackOffsetAttr(Decl *D, const ParsedAttr &AL)
Attr * buildMatrixLayoutTypeAttr(QualType T, const ParsedAttr &AL)
std::optional< ExprResult > tryPerformConstantBufferConversion(ExprResult &BaseExpr)
bool diagnosePositionType(QualType T, const ParsedAttr &AL)
bool handleInitialization(VarDecl *VDecl, Expr *&Init)
bool diagnoseInputIDType(QualType T, const ParsedAttr &AL)
void handleParamModifierAttr(Decl *D, const ParsedAttr &AL)
bool CheckResourceBinOp(BinaryOperatorKind Opc, Expr *LHSExpr, Expr *RHSExpr, SourceLocation Loc)
bool CanPerformAggregateSplatCast(Expr *Src, QualType DestType)
bool ActOnResourceMemberAccessExpr(MemberExpr *ME)
bool IsScalarizedLayoutCompatible(QualType T1, QualType T2) const
QualType ActOnTemplateShorthand(TemplateDecl *Template, SourceLocation NameLoc)
void diagnoseSystemSemanticAttr(Decl *D, const ParsedAttr &AL, std::optional< unsigned > Index)
void handleRootSignatureAttr(Decl *D, const ParsedAttr &AL)
bool CheckCompatibleParameterABI(FunctionDecl *New, FunctionDecl *Old)
QualType handleVectorBinOpConversion(ExprResult &LHS, ExprResult &RHS, QualType LHSType, QualType RHSType, bool IsCompAssign)
QualType checkMatrixComponent(Sema &S, QualType baseType, ExprValueKind &VK, SourceLocation OpLoc, const IdentifierInfo *CompName, SourceLocation CompLoc)
bool IsConstantBufferElementCompatible(QualType T1)
void handleResourceBindingAttr(Decl *D, const ParsedAttr &AL)
bool IsTypedResourceElementCompatible(QualType T1)
void SetRootSignatureOverride(IdentifierInfo *DeclIdent)
Definition SemaHLSL.h:172
bool transformInitList(const InitializedEntity &Entity, InitListExpr *Init)
void handleNumThreadsAttr(Decl *D, const ParsedAttr &AL)
bool ActOnUninitializedVarDecl(VarDecl *D)
void handleVkExtBuiltinInputAttr(Decl *D, const ParsedAttr &AL)
bool canHaveOverloadedBinOp(QualType Ty, BinaryOperatorKind Opc)
void ActOnTopLevelFunction(FunctionDecl *FD)
Definition SemaHLSL.cpp:817
bool handleResourceTypeAttr(QualType T, const ParsedAttr &AL)
void handleVkPushConstantAttr(Decl *D, const ParsedAttr &AL)
HLSLShaderAttr * mergeShaderAttr(Decl *D, const AttributeCommonInfo &AL, llvm::Triple::EnvironmentType ShaderType)
Definition SemaHLSL.cpp:784
NamedDecl * getConstantBufferConversionFunction(QualType Type, CXXRecordDecl *RD)
void ActOnFinishBuffer(Decl *Dcl, SourceLocation RBrace)
Definition SemaHLSL.cpp:687
void handleVkBindingAttr(Decl *D, const ParsedAttr &AL)
HLSLParamModifierAttr * mergeParamModifierAttr(Decl *D, const AttributeCommonInfo &AL, HLSLParamModifierAttr::Spelling Spelling)
Definition SemaHLSL.cpp:797
QualType getInoutParameterType(QualType Ty)
SemaHLSL(Sema &S)
Definition SemaHLSL.cpp:237
void handleVkConstantIdAttr(Decl *D, const ParsedAttr &AL)
Decl * ActOnStartBuffer(Scope *BufferScope, bool CBuffer, SourceLocation KwLoc, IdentifierInfo *Ident, SourceLocation IdentLoc, SourceLocation LBrace)
Definition SemaHLSL.cpp:239
bool diagnoseMatrixLayoutInstantiation(attr::Kind K, QualType T, SourceLocation Loc)
HLSLWaveSizeAttr * mergeWaveSizeAttr(Decl *D, const AttributeCommonInfo &AL, int Min, int Max, int Preferred, int SpelledArgsCount)
Definition SemaHLSL.cpp:728
bool handleRootSignatureElements(ArrayRef< hlsl::RootSignatureElement > Elements)
void ActOnFinishRootSignatureDecl(SourceLocation Loc, IdentifierInfo *DeclIdent, ArrayRef< hlsl::RootSignatureElement > Elements)
Creates the Root Signature decl of the parsed Root Signature elements onto the AST and push it onto c...
void ActOnVariableDeclarator(VarDecl *VD)
bool CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall)
Sema - This implements semantic analysis and AST building for C.
Definition Sema.h:868
Encodes a location in the source.
The base class of all kinds of template declarations (e.g., class, function, etc.).
The top declaration context.
Definition Decl.h:105
The base class of the type hierarchy.
Definition TypeBase.h:1875
Represents a variable declaration or definition.
Definition Decl.h:924
Definition SPIR.cpp:47
The JSON file list parser is used to communicate input to InstallAPI.
BindingType
Definition SemaHLSL.h:65
@ Template
We are parsing a template declaration.
Definition Parser.h:81
bool CreateHLSLAttributedResourceType(Sema &S, QualType Wrapped, ArrayRef< const Attr * > AttrList, QualType &ResType, HLSLAttributedResourceLocInfo *LocInfo=nullptr)
ExprValueKind
The categorization of expression values, currently following the C++11 scheme.
Definition Specifiers.h:133
ActionResult< Expr * > ExprResult
Definition Ownership.h:249
@ Implicit
An implicit conversion.
Definition Sema.h:440
__packed_splat4 __packed_splat2 __packed_splat8 __packed_splat4 __packed_splat2 uint8_t
__packed_splat4 __packed_splat2 __packed_splat8 __packed_splat4 __packed_splat2 __packed_splat4 __packed_splat2 __packed_splat8 __packed_splat4 uint32_t
const HLSLResourceBindingAttr * Attr
Definition SemaHLSL.h:72
BindingType BindType
Definition SemaHLSL.h:73
DeclBindingInfo(const VarDecl *Decl, ResourceClass ResClass, BindingType BindType=BindingType::NotAssigned, const HLSLResourceBindingAttr *Attr=nullptr)
Definition SemaHLSL.h:75
ResourceClass ResClass
Definition SemaHLSL.h:71
void setBindingAttribute(HLSLResourceBindingAttr *A, BindingType BT)
Definition SemaHLSL.h:80
const VarDecl * Decl
Definition SemaHLSL.h:70
const SourceLocation & getLocation() const
Definition SemaHLSL.h:48
RootSignatureElement(SourceLocation Loc, llvm::hlsl::rootsig::RootElement Element)
Definition SemaHLSL.h:43
const llvm::hlsl::rootsig::RootElement & getElement() const
Definition SemaHLSL.h:47