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"
21#include "clang/Sema/SemaBase.h"
22#include "llvm/ADT/SmallVector.h"
23#include "llvm/TargetParser/Triple.h"
24#include <initializer_list>
25
26namespace clang {
27class AttributeCommonInfo;
28class IdentifierInfo;
29class InitializedEntity;
30class InitializationKind;
31class ParsedAttr;
32class Scope;
33class VarDecl;
34
35namespace hlsl {
36
37// Introduce a wrapper struct around the underlying RootElement. This structure
38// will retain extra clang diagnostic information that is not available in llvm.
41 llvm::hlsl::rootsig::RootElement Element)
42 : Loc(Loc), Element(Element) {}
43
44 const llvm::hlsl::rootsig::RootElement &getElement() const { return Element; }
45 const SourceLocation &getLocation() const { return Loc; }
46
47private:
49 llvm::hlsl::rootsig::RootElement Element;
50};
51
52} // namespace hlsl
53
54using llvm::dxil::ResourceClass;
55
56// FIXME: This can be hidden (as static function in SemaHLSL.cpp) once we no
57// longer need to create builtin buffer types in HLSLExternalSemaSource.
59 Sema &S, QualType Wrapped, ArrayRef<const Attr *> AttrList,
60 QualType &ResType, HLSLAttributedResourceLocInfo *LocInfo = nullptr);
61
62enum class BindingType : uint8_t { NotAssigned, Explicit, Implicit };
63
64// DeclBindingInfo struct stores information about required/assigned resource
65// binding onon a declaration for specific resource class.
67 const VarDecl *Decl;
69 const HLSLResourceBindingAttr *Attr;
71
72 DeclBindingInfo(const VarDecl *Decl, ResourceClass ResClass,
74 const HLSLResourceBindingAttr *Attr = nullptr)
76
77 void setBindingAttribute(HLSLResourceBindingAttr *A, BindingType BT) {
78 assert(Attr == nullptr && BindType == BindingType::NotAssigned &&
79 "binding attribute already assigned");
80 Attr = A;
81 BindType = BT;
82 }
83};
84
85// ResourceBindings class stores information about all resource bindings
86// in a shader. It is used for binding diagnostics and implicit binding
87// assignments.
89public:
91 ResourceClass ResClass);
93 ResourceClass ResClass);
94 bool hasBindingInfoForDecl(const VarDecl *VD) const;
95
96private:
97 // List of all resource bindings required by the shader.
98 // A global declaration can have multiple bindings for different
99 // resource classes. They are all stored sequentially in this list.
100 // The DeclToBindingListIndex hashtable maps a declaration to the
101 // index of the first binding info in the list.
103 llvm::DenseMap<const VarDecl *, unsigned> DeclToBindingListIndex;
104};
105
106class SemaHLSL : public SemaBase {
107public:
108 SemaHLSL(Sema &S);
109
110 Decl *ActOnStartBuffer(Scope *BufferScope, bool CBuffer, SourceLocation KwLoc,
111 IdentifierInfo *Ident, SourceLocation IdentLoc,
112 SourceLocation LBrace);
113 void ActOnFinishBuffer(Decl *Dcl, SourceLocation RBrace);
114 HLSLNumThreadsAttr *mergeNumThreadsAttr(Decl *D,
115 const AttributeCommonInfo &AL, int X,
116 int Y, int Z);
117 HLSLWaveSizeAttr *mergeWaveSizeAttr(Decl *D, const AttributeCommonInfo &AL,
118 int Min, int Max, int Preferred,
119 int SpelledArgsCount);
120 HLSLVkConstantIdAttr *
122 HLSLShaderAttr *mergeShaderAttr(Decl *D, const AttributeCommonInfo &AL,
123 llvm::Triple::EnvironmentType ShaderType);
124 HLSLParamModifierAttr *
126 HLSLParamModifierAttr::Spelling Spelling);
132 void CheckSemanticAnnotation(FunctionDecl *EntryPoint, const Decl *Param,
133 const HLSLAnnotationAttr *AnnotationAttr);
135 const Attr *A, llvm::Triple::EnvironmentType Stage,
136 std::initializer_list<llvm::Triple::EnvironmentType> AllowedStages);
137
139 QualType LHSType, QualType RHSType,
140 bool IsCompAssign);
142
143 /// Computes the unique Root Signature identifier from the given signature,
144 /// then lookup if there is a previousy created Root Signature decl.
145 ///
146 /// Returns the identifier and if it was found
147 std::pair<IdentifierInfo *, bool>
148 ActOnStartRootSignatureDecl(StringRef Signature);
149
150 /// Creates the Root Signature decl of the parsed Root Signature elements
151 /// onto the AST and push it onto current Scope
152 void
155
157 RootSigOverrideIdent = DeclIdent;
158 }
159
160 // Returns true if any RootSignatureElement is invalid and a diagnostic was
161 // produced
162 bool
164 void handleRootSignatureAttr(Decl *D, const ParsedAttr &AL);
165 void handleNumThreadsAttr(Decl *D, const ParsedAttr &AL);
166 void handleWaveSizeAttr(Decl *D, const ParsedAttr &AL);
167 void handleVkConstantIdAttr(Decl *D, const ParsedAttr &AL);
168 void handleVkBindingAttr(Decl *D, const ParsedAttr &AL);
171 void handleSV_GroupIDAttr(Decl *D, const ParsedAttr &AL);
172 void handleSV_PositionAttr(Decl *D, const ParsedAttr &AL);
173 void handlePackOffsetAttr(Decl *D, const ParsedAttr &AL);
174 void handleShaderAttr(Decl *D, const ParsedAttr &AL);
175 void handleResourceBindingAttr(Decl *D, const ParsedAttr &AL);
176 void handleParamModifierAttr(Decl *D, const ParsedAttr &AL);
178
180
181 bool CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall);
185
186 // HLSL Type trait implementations
189
191
192 // Diagnose whether the input ID is uint/unit2/uint3 type.
193 bool diagnoseInputIDType(QualType T, const ParsedAttr &AL);
194 bool diagnosePositionType(QualType T, const ParsedAttr &AL);
195
196 bool CanPerformScalarCast(QualType SrcTy, QualType DestTy);
197 bool ContainsBitField(QualType BaseTy);
198 bool CanPerformElementwiseCast(Expr *Src, QualType DestType);
199 bool CanPerformAggregateSplatCast(Expr *Src, QualType DestType);
201
203
205 bool handleInitialization(VarDecl *VDecl, Expr *&Init);
207
208private:
209 // HLSL resource type attributes need to be processed all at once.
210 // This is a list to collect them.
211 llvm::SmallVector<const Attr *> HLSLResourcesTypeAttrs;
212
213 /// TypeLoc data for HLSLAttributedResourceType instances that we
214 /// have not yet populated.
215 llvm::DenseMap<const HLSLAttributedResourceType *,
217 LocsForHLSLAttributedResources;
218
219 // List of all resource bindings
221
222 // Global declaration collected for the $Globals default constant
223 // buffer which will be created at the end of the translation unit.
224 llvm::SmallVector<Decl *> DefaultCBufferDecls;
225
226 uint32_t ImplicitBindingNextOrderID = 0;
227
228 IdentifierInfo *RootSigOverrideIdent = nullptr;
229
230private:
231 void collectResourceBindingsOnVarDecl(VarDecl *D);
232 void collectResourceBindingsOnUserRecordDecl(const VarDecl *VD,
233 const RecordType *RT);
234 void processExplicitBindingsOnDecl(VarDecl *D);
235
236 void diagnoseAvailabilityViolations(TranslationUnitDecl *TU);
237
238 uint32_t getNextImplicitBindingOrderID() {
239 return ImplicitBindingNextOrderID++;
240 }
241
242 bool initGlobalResourceDecl(VarDecl *VD);
243 bool initGlobalResourceArrayDecl(VarDecl *VD);
244 void createResourceRecordCtorArgs(const Type *ResourceTy, StringRef VarName,
245 HLSLResourceBindingAttr *RBA,
246 HLSLVkBindingAttr *VkBinding,
247 uint32_t ArrayIndex,
249};
250
251} // namespace clang
252
253#endif // LLVM_CLANG_SEMA_SEMAHLSL_H
Forward declaration of all AST node types.
llvm::dxil::ResourceClass ResourceClass
Definition: CGHLSLRuntime.h:50
const Decl * D
#define X(type, name)
Definition: Value.h:145
uint32_t Id
Definition: SemaARM.cpp:1179
SourceLocation Loc
Definition: SemaObjC.cpp:754
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
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition: Expr.h:2879
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:1999
One of these records is kept for each identifier that is lexed.
Describes an C or C++ initializer list.
Definition: Expr.h:5235
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
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
Definition: TypeBase.h:6502
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
ExprResult ActOnOutParamExpr(ParmVarDecl *Param, Expr *Arg)
Definition: SemaHLSL.cpp:3519
bool CanPerformElementwiseCast(Expr *Src, QualType DestType)
Definition: SemaHLSL.cpp:3475
void DiagnoseAttrStageMismatch(const Attr *A, llvm::Triple::EnvironmentType Stage, std::initializer_list< llvm::Triple::EnvironmentType > AllowedStages)
Definition: SemaHLSL.cpp:876
void handleWaveSizeAttr(Decl *D, const ParsedAttr &AL)
Definition: SemaHLSL.cpp:1440
HLSLAttributedResourceLocInfo TakeLocForHLSLAttribute(const HLSLAttributedResourceType *RT)
Definition: SemaHLSL.cpp:1851
bool CanPerformScalarCast(QualType SrcTy, QualType DestTy)
Definition: SemaHLSL.cpp:3338
QualType ProcessResourceTypeAttributes(QualType Wrapped)
Definition: SemaHLSL.cpp:1828
void handleSV_GroupThreadIDAttr(Decl *D, const ParsedAttr &AL)
Definition: SemaHLSL.cpp:1580
void handleShaderAttr(Decl *D, const ParsedAttr &AL)
Definition: SemaHLSL.cpp:1646
void handleSV_DispatchThreadIDAttr(Decl *D, const ParsedAttr &AL)
Definition: SemaHLSL.cpp:1551
void CheckEntryPoint(FunctionDecl *FD)
Definition: SemaHLSL.cpp:774
void emitLogicalOperatorFixIt(Expr *LHS, Expr *RHS, BinaryOperatorKind Opc)
Definition: SemaHLSL.cpp:1060
void ActOnEndOfTranslationUnit(TranslationUnitDecl *TU)
Definition: SemaHLSL.cpp:2464
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)
Definition: SemaHLSL.cpp:3597
std::pair< IdentifierInfo *, bool > ActOnStartRootSignatureDecl(StringRef Signature)
Computes the unique Root Signature identifier from the given signature, then lookup if there is a pre...
Definition: SemaHLSL.cpp:1079
void handlePackOffsetAttr(Decl *D, const ParsedAttr &AL)
Definition: SemaHLSL.cpp:1597
void CheckSemanticAnnotation(FunctionDecl *EntryPoint, const Decl *Param, const HLSLAnnotationAttr *AnnotationAttr)
Definition: SemaHLSL.cpp:848
bool diagnosePositionType(QualType T, const ParsedAttr &AL)
Definition: SemaHLSL.cpp:1560
bool ContainsBitField(QualType BaseTy)
Definition: SemaHLSL.cpp:3405
bool handleInitialization(VarDecl *VDecl, Expr *&Init)
Definition: SemaHLSL.cpp:4146
bool diagnoseInputIDType(QualType T, const ParsedAttr &AL)
Definition: SemaHLSL.cpp:1538
void handleParamModifierAttr(Decl *D, const ParsedAttr &AL)
Definition: SemaHLSL.cpp:2114
bool CanPerformAggregateSplatCast(Expr *Src, QualType DestType)
Definition: SemaHLSL.cpp:3440
void handleSV_PositionAttr(Decl *D, const ParsedAttr &AL)
Definition: SemaHLSL.cpp:1572
bool IsScalarizedLayoutCompatible(QualType T1, QualType T2) const
Definition: SemaHLSL.cpp:3281
void handleRootSignatureAttr(Decl *D, const ParsedAttr &AL)
Definition: SemaHLSL.cpp:1356
bool CheckCompatibleParameterABI(FunctionDecl *New, FunctionDecl *Old)
Definition: SemaHLSL.cpp:3304
QualType handleVectorBinOpConversion(ExprResult &LHS, ExprResult &RHS, QualType LHSType, QualType RHSType, bool IsCompAssign)
Definition: SemaHLSL.cpp:993
void handleResourceBindingAttr(Decl *D, const ParsedAttr &AL)
Definition: SemaHLSL.cpp:2024
bool IsTypedResourceElementCompatible(QualType T1)
Definition: SemaHLSL.cpp:3244
void SetRootSignatureOverride(IdentifierInfo *DeclIdent)
Definition: SemaHLSL.h:156
bool transformInitList(const InitializedEntity &Entity, InitListExpr *Init)
Definition: SemaHLSL.cpp:4093
void handleNumThreadsAttr(Decl *D, const ParsedAttr &AL)
Definition: SemaHLSL.cpp:1382
bool ActOnUninitializedVarDecl(VarDecl *D)
Definition: SemaHLSL.cpp:3800
void handleVkExtBuiltinInputAttr(Decl *D, const ParsedAttr &AL)
Definition: SemaHLSL.cpp:1504
void ActOnTopLevelFunction(FunctionDecl *FD)
Definition: SemaHLSL.cpp:724
bool handleResourceTypeAttr(QualType T, const ParsedAttr &AL)
Definition: SemaHLSL.cpp:1750
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)
Definition: SemaHLSL.cpp:1521
HLSLParamModifierAttr * mergeParamModifierAttr(Decl *D, const AttributeCommonInfo &AL, HLSLParamModifierAttr::Spelling Spelling)
Definition: SemaHLSL.cpp:704
QualType getInoutParameterType(QualType Ty)
Definition: SemaHLSL.cpp:3577
void handleVkConstantIdAttr(Decl *D, const ParsedAttr &AL)
Definition: SemaHLSL.cpp:1512
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)
Definition: SemaHLSL.cpp:1205
void handleSV_GroupIDAttr(Decl *D, const ParsedAttr &AL)
Definition: SemaHLSL.cpp:1589
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...
Definition: SemaHLSL.cpp:1091
void ActOnVariableDeclarator(VarDecl *VD)
Definition: SemaHLSL.cpp:3631
bool CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall)
Definition: SemaHLSL.cpp:2766
Sema - This implements semantic analysis and AST building for C.
Definition: Sema.h:850
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.
BinaryOperatorKind
BindingType
Definition: SemaHLSL.h:62
bool CreateHLSLAttributedResourceType(Sema &S, QualType Wrapped, ArrayRef< const Attr * > AttrList, QualType &ResType, HLSLAttributedResourceLocInfo *LocInfo=nullptr)
Definition: SemaHLSL.cpp:1666
const FunctionProtoType * T
@ Implicit
An implicit conversion.
const HLSLResourceBindingAttr * Attr
Definition: SemaHLSL.h:69
BindingType BindType
Definition: SemaHLSL.h:70
DeclBindingInfo(const VarDecl *Decl, ResourceClass ResClass, BindingType BindType=BindingType::NotAssigned, const HLSLResourceBindingAttr *Attr=nullptr)
Definition: SemaHLSL.h:72
ResourceClass ResClass
Definition: SemaHLSL.h:68
void setBindingAttribute(HLSLResourceBindingAttr *A, BindingType BT)
Definition: SemaHLSL.h:77
const VarDecl * Decl
Definition: SemaHLSL.h:67
const SourceLocation & getLocation() const
Definition: SemaHLSL.h:45
RootSignatureElement(SourceLocation Loc, llvm::hlsl::rootsig::RootElement Element)
Definition: SemaHLSL.h:40
const llvm::hlsl::rootsig::RootElement & getElement() const
Definition: SemaHLSL.h:44