clang 22.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/SmallVector.h"
24#include "llvm/TargetParser/Triple.h"
25#include <initializer_list>
26
27namespace clang {
29class IdentifierInfo;
32class ParsedAttr;
33class Scope;
34class VarDecl;
35
36namespace hlsl {
37
38// Introduce a wrapper struct around the underlying RootElement. This structure
39// will retain extra clang diagnostic information that is not available in llvm.
42 llvm::hlsl::rootsig::RootElement Element)
43 : Loc(Loc), Element(Element) {}
44
45 const llvm::hlsl::rootsig::RootElement &getElement() const { return Element; }
46 const SourceLocation &getLocation() const { return Loc; }
47
48private:
50 llvm::hlsl::rootsig::RootElement Element;
51};
52
53} // namespace hlsl
54
55using llvm::dxil::ResourceClass;
56
57// FIXME: This can be hidden (as static function in SemaHLSL.cpp) once we no
58// longer need to create builtin buffer types in HLSLExternalSemaSource.
60 Sema &S, QualType Wrapped, ArrayRef<const Attr *> AttrList,
61 QualType &ResType, HLSLAttributedResourceLocInfo *LocInfo = nullptr);
62
63enum class BindingType : uint8_t { NotAssigned, Explicit, Implicit };
64
65// DeclBindingInfo struct stores information about required/assigned resource
66// binding onon a declaration for specific resource class.
68 const VarDecl *Decl;
70 const HLSLResourceBindingAttr *Attr;
72
77
78 void setBindingAttribute(HLSLResourceBindingAttr *A, BindingType BT) {
79 assert(Attr == nullptr && BindType == BindingType::NotAssigned &&
80 "binding attribute already assigned");
81 Attr = A;
82 BindType = BT;
83 }
84};
85
86// ResourceBindings class stores information about all resource bindings
87// in a shader. It is used for binding diagnostics and implicit binding
88// assignments.
90public:
92 ResourceClass ResClass);
94 ResourceClass ResClass);
95 bool hasBindingInfoForDecl(const VarDecl *VD) const;
96
97private:
98 // List of all resource bindings required by the shader.
99 // A global declaration can have multiple bindings for different
100 // resource classes. They are all stored sequentially in this list.
101 // The DeclToBindingListIndex hashtable maps a declaration to the
102 // index of the first binding info in the list.
104 llvm::DenseMap<const VarDecl *, unsigned> DeclToBindingListIndex;
105};
106
107class SemaHLSL : public SemaBase {
108public:
109 SemaHLSL(Sema &S);
110
111 Decl *ActOnStartBuffer(Scope *BufferScope, bool CBuffer, SourceLocation KwLoc,
112 IdentifierInfo *Ident, SourceLocation IdentLoc,
113 SourceLocation LBrace);
114 void ActOnFinishBuffer(Decl *Dcl, SourceLocation RBrace);
115 HLSLNumThreadsAttr *mergeNumThreadsAttr(Decl *D,
116 const AttributeCommonInfo &AL, int X,
117 int Y, int Z);
118 HLSLWaveSizeAttr *mergeWaveSizeAttr(Decl *D, const AttributeCommonInfo &AL,
119 int Min, int Max, int Preferred,
120 int SpelledArgsCount);
121 HLSLVkConstantIdAttr *
122 mergeVkConstantIdAttr(Decl *D, const AttributeCommonInfo &AL, int Id);
123 HLSLShaderAttr *mergeShaderAttr(Decl *D, const AttributeCommonInfo &AL,
124 llvm::Triple::EnvironmentType ShaderType);
125 HLSLParamModifierAttr *
127 HLSLParamModifierAttr::Spelling Spelling);
133 bool CheckResourceBinOp(BinaryOperatorKind Opc, Expr *LHSExpr, Expr *RHSExpr,
134 SourceLocation Loc);
136 const Attr *A, llvm::Triple::EnvironmentType Stage,
137 std::initializer_list<llvm::Triple::EnvironmentType> AllowedStages);
138
140 QualType LHSType, QualType RHSType,
141 bool IsCompAssign);
143
144 /// Computes the unique Root Signature identifier from the given signature,
145 /// then lookup if there is a previousy created Root Signature decl.
146 ///
147 /// Returns the identifier and if it was found
148 std::pair<IdentifierInfo *, bool>
149 ActOnStartRootSignatureDecl(StringRef Signature);
150
151 /// Creates the Root Signature decl of the parsed Root Signature elements
152 /// onto the AST and push it onto current Scope
153 void
156
158 RootSigOverrideIdent = DeclIdent;
159 }
160
162
163 // Returns true if any RootSignatureElement is invalid and a diagnostic was
164 // produced
165 bool
167 void handleRootSignatureAttr(Decl *D, const ParsedAttr &AL);
168 void handleNumThreadsAttr(Decl *D, const ParsedAttr &AL);
169 void handleWaveSizeAttr(Decl *D, const ParsedAttr &AL);
170 void handleVkConstantIdAttr(Decl *D, const ParsedAttr &AL);
171 void handleVkBindingAttr(Decl *D, const ParsedAttr &AL);
172 void handlePackOffsetAttr(Decl *D, const ParsedAttr &AL);
173 void handleShaderAttr(Decl *D, const ParsedAttr &AL);
174 void handleResourceBindingAttr(Decl *D, const ParsedAttr &AL);
175 void handleParamModifierAttr(Decl *D, const ParsedAttr &AL);
177
178 template <typename T>
180 std::optional<unsigned> Location) {
181 T *Attr =
182 ::new (getASTContext()) T(getASTContext(), ACI, TargetDecl,
183 Location.value_or(0), Location.has_value());
184
185 if (!Attr->isSemanticIndexable() && Location.has_value()) {
186 Diag(Attr->getLocation(), diag::err_hlsl_semantic_indexing_not_supported)
187 << Attr->getAttrName()->getName();
188 return nullptr;
189 }
190 return Attr;
191 }
192
193 void diagnoseSystemSemanticAttr(Decl *D, const ParsedAttr &AL,
194 std::optional<unsigned> Index);
195 void handleSemanticAttr(Decl *D, const ParsedAttr &AL);
196
197 void handleVkExtBuiltinInputAttr(Decl *D, const ParsedAttr &AL);
198
199 bool CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall);
202 TakeLocForHLSLAttribute(const HLSLAttributedResourceType *RT);
203
204 // HLSL Type trait implementations
207
209
210 // Diagnose whether the input ID is uint/unit2/uint3 type.
211 bool diagnoseInputIDType(QualType T, const ParsedAttr &AL);
212 bool diagnosePositionType(QualType T, const ParsedAttr &AL);
213
214 bool CanPerformScalarCast(QualType SrcTy, QualType DestTy);
215 bool CanPerformElementwiseCast(Expr *Src, QualType DestType);
216 bool CanPerformAggregateSplatCast(Expr *Src, QualType DestType);
218
220
222 bool handleInitialization(VarDecl *VDecl, Expr *&Init);
224
225private:
226 // HLSL resource type attributes need to be processed all at once.
227 // This is a list to collect them.
228 llvm::SmallVector<const Attr *> HLSLResourcesTypeAttrs;
229
230 /// TypeLoc data for HLSLAttributedResourceType instances that we
231 /// have not yet populated.
232 llvm::DenseMap<const HLSLAttributedResourceType *,
234 LocsForHLSLAttributedResources;
235
236 // List of all resource bindings
238
239 // Global declaration collected for the $Globals default constant
240 // buffer which will be created at the end of the translation unit.
241 llvm::SmallVector<Decl *> DefaultCBufferDecls;
242
243 uint32_t ImplicitBindingNextOrderID = 0;
244
245 IdentifierInfo *RootSigOverrideIdent = nullptr;
246
247 struct SemanticInfo {
248 HLSLSemanticAttr *Semantic;
249 std::optional<uint32_t> Index;
250 };
251
252private:
253 void collectResourceBindingsOnVarDecl(VarDecl *D);
254 void collectResourceBindingsOnUserRecordDecl(const VarDecl *VD,
255 const RecordType *RT);
256
257 void checkSemanticAnnotation(FunctionDecl *EntryPoint, const Decl *Param,
258 const HLSLSemanticAttr *SemanticAttr);
259 HLSLSemanticAttr *createSemantic(const SemanticInfo &Semantic,
260 DeclaratorDecl *TargetDecl);
261 bool determineActiveSemanticOnScalar(FunctionDecl *FD, DeclaratorDecl *D,
262 SemanticInfo &ActiveSemantic);
263 bool determineActiveSemantic(FunctionDecl *FD, DeclaratorDecl *D,
264 SemanticInfo &ActiveSemantic);
265
266 void processExplicitBindingsOnDecl(VarDecl *D);
267
268 void diagnoseAvailabilityViolations(TranslationUnitDecl *TU);
269
270 uint32_t getNextImplicitBindingOrderID() {
271 return ImplicitBindingNextOrderID++;
272 }
273
274 bool initGlobalResourceDecl(VarDecl *VD);
275 bool initGlobalResourceArrayDecl(VarDecl *VD);
276};
277
278} // namespace clang
279
280#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:45
SourceLocation getLocation() const
Definition Attr.h:98
const IdentifierInfo * getAttrName() const
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition Expr.h:2877
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition DeclBase.h:1449
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:2000
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:5233
Describes the kind of initialization being performed, along with location information for tokens rela...
Describes an entity that is being initialized.
This represents a decl that may have a name.
Definition Decl.h:274
Represents a parameter to a function.
Definition Decl.h:1790
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:194
DeclBindingInfo * getDeclBindingInfo(const VarDecl *VD, ResourceClass ResClass)
Definition SemaHLSL.cpp:180
DeclBindingInfo * addDeclBindingInfo(const VarDecl *VD, ResourceClass ResClass)
Definition SemaHLSL.cpp:167
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
SemaDiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID)
Emit a diagnostic.
Definition SemaBase.cpp:61
ExprResult ActOnOutParamExpr(ParmVarDecl *Param, Expr *Arg)
HLSLRootSignatureDecl * lookupRootSignatureOverrideDecl(DeclContext *DC) const
bool CanPerformElementwiseCast(Expr *Src, QualType DestType)
void DiagnoseAttrStageMismatch(const Attr *A, llvm::Triple::EnvironmentType Stage, std::initializer_list< llvm::Triple::EnvironmentType > AllowedStages)
Definition SemaHLSL.cpp:955
void handleWaveSizeAttr(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)
void CheckEntryPoint(FunctionDecl *FD)
Definition SemaHLSL.cpp:852
void emitLogicalOperatorFixIt(Expr *LHS, Expr *RHS, BinaryOperatorKind Opc)
void ActOnEndOfTranslationUnit(TranslationUnitDecl *TU)
HLSLVkConstantIdAttr * mergeVkConstantIdAttr(Decl *D, const AttributeCommonInfo &AL, int Id)
Definition SemaHLSL.cpp:657
HLSLNumThreadsAttr * mergeNumThreadsAttr(Decl *D, const AttributeCommonInfo &AL, int X, int Y, int Z)
Definition SemaHLSL.cpp:623
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)
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 IsScalarizedLayoutCompatible(QualType T1, QualType T2) const
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)
void handleResourceBindingAttr(Decl *D, const ParsedAttr &AL)
bool IsTypedResourceElementCompatible(QualType T1)
void SetRootSignatureOverride(IdentifierInfo *DeclIdent)
Definition SemaHLSL.h:157
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)
T * createSemanticAttr(const AttributeCommonInfo &ACI, NamedDecl *TargetDecl, std::optional< unsigned > Location)
Definition SemaHLSL.h:179
void ActOnTopLevelFunction(FunctionDecl *FD)
Definition SemaHLSL.cpp:726
bool handleResourceTypeAttr(QualType T, const ParsedAttr &AL)
HLSLShaderAttr * mergeShaderAttr(Decl *D, const AttributeCommonInfo &AL, llvm::Triple::EnvironmentType ShaderType)
Definition SemaHLSL.cpp:693
void ActOnFinishBuffer(Decl *Dcl, SourceLocation RBrace)
Definition SemaHLSL.cpp:596
void handleVkBindingAttr(Decl *D, const ParsedAttr &AL)
HLSLParamModifierAttr * mergeParamModifierAttr(Decl *D, const AttributeCommonInfo &AL, HLSLParamModifierAttr::Spelling Spelling)
Definition SemaHLSL.cpp:706
QualType getInoutParameterType(QualType Ty)
SemaHLSL(Sema &S)
Definition SemaHLSL.cpp:198
void handleVkConstantIdAttr(Decl *D, const ParsedAttr &AL)
Decl * ActOnStartBuffer(Scope *BufferScope, bool CBuffer, SourceLocation KwLoc, IdentifierInfo *Ident, SourceLocation IdentLoc, SourceLocation LBrace)
Definition SemaHLSL.cpp:200
HLSLWaveSizeAttr * mergeWaveSizeAttr(Decl *D, const AttributeCommonInfo &AL, int Min, int Max, int Preferred, int SpelledArgsCount)
Definition SemaHLSL.cpp:637
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:854
Encodes a location in the source.
The top declaration context.
Definition Decl.h:105
Represents a variable declaration or definition.
Definition Decl.h:926
The JSON file list parser is used to communicate input to InstallAPI.
BindingType
Definition SemaHLSL.h:63
const FunctionProtoType * T
bool CreateHLSLAttributedResourceType(Sema &S, QualType Wrapped, ArrayRef< const Attr * > AttrList, QualType &ResType, HLSLAttributedResourceLocInfo *LocInfo=nullptr)
ActionResult< Expr * > ExprResult
Definition Ownership.h:249
@ Implicit
An implicit conversion.
Definition Sema.h:438
const HLSLResourceBindingAttr * Attr
Definition SemaHLSL.h:70
BindingType BindType
Definition SemaHLSL.h:71
DeclBindingInfo(const VarDecl *Decl, ResourceClass ResClass, BindingType BindType=BindingType::NotAssigned, const HLSLResourceBindingAttr *Attr=nullptr)
Definition SemaHLSL.h:73
ResourceClass ResClass
Definition SemaHLSL.h:69
void setBindingAttribute(HLSLResourceBindingAttr *A, BindingType BT)
Definition SemaHLSL.h:78
const VarDecl * Decl
Definition SemaHLSL.h:68
const SourceLocation & getLocation() const
Definition SemaHLSL.h:46
RootSignatureElement(SourceLocation Loc, llvm::hlsl::rootsig::RootElement Element)
Definition SemaHLSL.h:41
const llvm::hlsl::rootsig::RootElement & getElement() const
Definition SemaHLSL.h:45