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);
134 void CheckSemanticAnnotation(FunctionDecl *EntryPoint, const Decl *Param,
135 const HLSLAnnotationAttr *AnnotationAttr);
136 bool CheckResourceBinOp(BinaryOperatorKind Opc, Expr *LHSExpr, Expr *RHSExpr,
137 SourceLocation Loc);
139 const Attr *A, llvm::Triple::EnvironmentType Stage,
140 std::initializer_list<llvm::Triple::EnvironmentType> AllowedStages);
141
143 QualType LHSType, QualType RHSType,
144 bool IsCompAssign);
146
147 /// Computes the unique Root Signature identifier from the given signature,
148 /// then lookup if there is a previousy created Root Signature decl.
149 ///
150 /// Returns the identifier and if it was found
151 std::pair<IdentifierInfo *, bool>
152 ActOnStartRootSignatureDecl(StringRef Signature);
153
154 /// Creates the Root Signature decl of the parsed Root Signature elements
155 /// onto the AST and push it onto current Scope
156 void
159
161 RootSigOverrideIdent = DeclIdent;
162 }
163
165
166 // Returns true if any RootSignatureElement is invalid and a diagnostic was
167 // produced
168 bool
170 void handleRootSignatureAttr(Decl *D, const ParsedAttr &AL);
171 void handleNumThreadsAttr(Decl *D, const ParsedAttr &AL);
172 void handleWaveSizeAttr(Decl *D, const ParsedAttr &AL);
173 void handleVkConstantIdAttr(Decl *D, const ParsedAttr &AL);
174 void handleVkBindingAttr(Decl *D, const ParsedAttr &AL);
175 void handlePackOffsetAttr(Decl *D, const ParsedAttr &AL);
176 void handleShaderAttr(Decl *D, const ParsedAttr &AL);
177 void handleResourceBindingAttr(Decl *D, const ParsedAttr &AL);
178 void handleParamModifierAttr(Decl *D, const ParsedAttr &AL);
180
181 template <typename T>
183 std::optional<unsigned> Location) {
184 T *Attr = ::new (getASTContext()) T(getASTContext(), AL);
185 if (Attr->isSemanticIndexable())
186 Attr->setSemanticIndex(Location ? *Location : 0);
187 else if (Location.has_value()) {
188 Diag(Attr->getLocation(), diag::err_hlsl_semantic_indexing_not_supported)
189 << Attr->getAttrName()->getName();
190 return nullptr;
191 }
192
193 return Attr;
194 }
195
196 void diagnoseSystemSemanticAttr(Decl *D, const ParsedAttr &AL,
197 std::optional<unsigned> Index);
198 void handleSemanticAttr(Decl *D, const ParsedAttr &AL);
199
200 void handleVkExtBuiltinInputAttr(Decl *D, const ParsedAttr &AL);
201
202 bool CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall);
205 TakeLocForHLSLAttribute(const HLSLAttributedResourceType *RT);
206
207 // HLSL Type trait implementations
210
212
213 // Diagnose whether the input ID is uint/unit2/uint3 type.
214 bool diagnoseInputIDType(QualType T, const ParsedAttr &AL);
215 bool diagnosePositionType(QualType T, const ParsedAttr &AL);
216
217 bool CanPerformScalarCast(QualType SrcTy, QualType DestTy);
218 bool ContainsBitField(QualType BaseTy);
219 bool CanPerformElementwiseCast(Expr *Src, QualType DestType);
220 bool CanPerformAggregateSplatCast(Expr *Src, QualType DestType);
222
224
226 bool handleInitialization(VarDecl *VDecl, Expr *&Init);
228
229private:
230 // HLSL resource type attributes need to be processed all at once.
231 // This is a list to collect them.
232 llvm::SmallVector<const Attr *> HLSLResourcesTypeAttrs;
233
234 /// TypeLoc data for HLSLAttributedResourceType instances that we
235 /// have not yet populated.
236 llvm::DenseMap<const HLSLAttributedResourceType *,
238 LocsForHLSLAttributedResources;
239
240 // List of all resource bindings
242
243 // Global declaration collected for the $Globals default constant
244 // buffer which will be created at the end of the translation unit.
245 llvm::SmallVector<Decl *> DefaultCBufferDecls;
246
247 uint32_t ImplicitBindingNextOrderID = 0;
248
249 IdentifierInfo *RootSigOverrideIdent = nullptr;
250
251private:
252 void collectResourceBindingsOnVarDecl(VarDecl *D);
253 void collectResourceBindingsOnUserRecordDecl(const VarDecl *VD,
254 const RecordType *RT);
255 void processExplicitBindingsOnDecl(VarDecl *D);
256
257 void diagnoseAvailabilityViolations(TranslationUnitDecl *TU);
258
259 uint32_t getNextImplicitBindingOrderID() {
260 return ImplicitBindingNextOrderID++;
261 }
262
263 bool initGlobalResourceDecl(VarDecl *VD);
264 bool initGlobalResourceArrayDecl(VarDecl *VD);
265};
266
267} // namespace clang
268
269#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:44
SourceLocation getLocation() const
Definition Attr.h:97
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
Represents a ValueDecl that came out of a declarator.
Definition Decl.h:779
This represents one expression.
Definition Expr.h:112
Represents a function declaration or definition.
Definition Decl.h:1999
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.
Represents a parameter to a function.
Definition Decl.h:1789
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:191
DeclBindingInfo * getDeclBindingInfo(const VarDecl *VD, ResourceClass ResClass)
Definition SemaHLSL.cpp:177
DeclBindingInfo * addDeclBindingInfo(const VarDecl *VD, ResourceClass ResClass)
Definition SemaHLSL.cpp:164
Scope - A scope is a transient data structure that is used while parsing the program.
Definition Scope.h:41
SemaDiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID, bool DeferHint=false)
Emit a diagnostic.
Definition SemaBase.cpp:61
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 DiagnoseAttrStageMismatch(const Attr *A, llvm::Triple::EnvironmentType Stage, std::initializer_list< llvm::Triple::EnvironmentType > AllowedStages)
Definition SemaHLSL.cpp:892
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)
bool isSemanticValid(FunctionDecl *FD, DeclaratorDecl *D)
Definition SemaHLSL.cpp:772
QualType ProcessResourceTypeAttributes(QualType Wrapped)
void handleShaderAttr(Decl *D, const ParsedAttr &AL)
void CheckEntryPoint(FunctionDecl *FD)
Definition SemaHLSL.cpp:792
void emitLogicalOperatorFixIt(Expr *LHS, Expr *RHS, BinaryOperatorKind Opc)
void ActOnEndOfTranslationUnit(TranslationUnitDecl *TU)
HLSLVkConstantIdAttr * mergeVkConstantIdAttr(Decl *D, const AttributeCommonInfo &AL, int Id)
Definition SemaHLSL.cpp:655
HLSLNumThreadsAttr * mergeNumThreadsAttr(Decl *D, const AttributeCommonInfo &AL, int X, int Y, int Z)
Definition SemaHLSL.cpp:621
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)
void CheckSemanticAnnotation(FunctionDecl *EntryPoint, const Decl *Param, const HLSLAnnotationAttr *AnnotationAttr)
Definition SemaHLSL.cpp:864
bool diagnosePositionType(QualType T, const ParsedAttr &AL)
bool ContainsBitField(QualType BaseTy)
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
T * createSemanticAttr(const ParsedAttr &AL, std::optional< unsigned > Location)
Definition SemaHLSL.h:182
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:160
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)
void ActOnTopLevelFunction(FunctionDecl *FD)
Definition SemaHLSL.cpp:724
bool handleResourceTypeAttr(QualType T, const ParsedAttr &AL)
HLSLShaderAttr * mergeShaderAttr(Decl *D, const AttributeCommonInfo &AL, llvm::Triple::EnvironmentType ShaderType)
Definition SemaHLSL.cpp:691
void ActOnFinishBuffer(Decl *Dcl, SourceLocation RBrace)
Definition SemaHLSL.cpp:593
void handleVkBindingAttr(Decl *D, const ParsedAttr &AL)
HLSLParamModifierAttr * mergeParamModifierAttr(Decl *D, const AttributeCommonInfo &AL, HLSLParamModifierAttr::Spelling Spelling)
Definition SemaHLSL.cpp:704
QualType getInoutParameterType(QualType Ty)
SemaHLSL(Sema &S)
Definition SemaHLSL.cpp:195
void handleVkConstantIdAttr(Decl *D, const ParsedAttr &AL)
Decl * ActOnStartBuffer(Scope *BufferScope, bool CBuffer, SourceLocation KwLoc, IdentifierInfo *Ident, SourceLocation IdentLoc, SourceLocation LBrace)
Definition SemaHLSL.cpp:197
HLSLWaveSizeAttr * mergeWaveSizeAttr(Decl *D, const AttributeCommonInfo &AL, int Min, int Max, int Preferred, int SpelledArgsCount)
Definition SemaHLSL.cpp:635
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:104
Represents a variable declaration or definition.
Definition Decl.h:925
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:437
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