clang 20.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 ParsedAttr;
30class Scope;
31class VarDecl;
32
33using llvm::dxil::ResourceClass;
34
35// FIXME: This can be hidden (as static function in SemaHLSL.cpp) once we no
36// longer need to create builtin buffer types in HLSLExternalSemaSource.
38 Sema &S, QualType Wrapped, ArrayRef<const Attr *> AttrList,
39 QualType &ResType, HLSLAttributedResourceLocInfo *LocInfo = nullptr);
40
41enum class BindingType : uint8_t { NotAssigned, Explicit, Implicit };
42
43// DeclBindingInfo struct stores information about required/assigned resource
44// binding onon a declaration for specific resource class.
46 const VarDecl *Decl;
47 ResourceClass ResClass;
48 const HLSLResourceBindingAttr *Attr;
50
51 DeclBindingInfo(const VarDecl *Decl, ResourceClass ResClass,
53 const HLSLResourceBindingAttr *Attr = nullptr)
55
56 void setBindingAttribute(HLSLResourceBindingAttr *A, BindingType BT) {
57 assert(Attr == nullptr && BindType == BindingType::NotAssigned &&
58 "binding attribute already assigned");
59 Attr = A;
60 BindType = BT;
61 }
62};
63
64// ResourceBindings class stores information about all resource bindings
65// in a shader. It is used for binding diagnostics and implicit binding
66// assigments.
68public:
70 ResourceClass ResClass);
72 ResourceClass ResClass);
73 bool hasBindingInfoForDecl(const VarDecl *VD) const;
74
75private:
76 // List of all resource bindings required by the shader.
77 // A global declaration can have multiple bindings for different
78 // resource classes. They are all stored sequentially in this list.
79 // The DeclToBindingListIndex hashtable maps a declaration to the
80 // index of the first binding info in the list.
82 llvm::DenseMap<const VarDecl *, unsigned> DeclToBindingListIndex;
83};
84
85class SemaHLSL : public SemaBase {
86public:
87 SemaHLSL(Sema &S);
88
89 Decl *ActOnStartBuffer(Scope *BufferScope, bool CBuffer, SourceLocation KwLoc,
90 IdentifierInfo *Ident, SourceLocation IdentLoc,
91 SourceLocation LBrace);
92 void ActOnFinishBuffer(Decl *Dcl, SourceLocation RBrace);
93 HLSLNumThreadsAttr *mergeNumThreadsAttr(Decl *D,
94 const AttributeCommonInfo &AL, int X,
95 int Y, int Z);
96 HLSLWaveSizeAttr *mergeWaveSizeAttr(Decl *D, const AttributeCommonInfo &AL,
97 int Min, int Max, int Preferred,
98 int SpelledArgsCount);
99 HLSLShaderAttr *mergeShaderAttr(Decl *D, const AttributeCommonInfo &AL,
100 llvm::Triple::EnvironmentType ShaderType);
101 HLSLParamModifierAttr *
103 HLSLParamModifierAttr::Spelling Spelling);
107 void CheckSemanticAnnotation(FunctionDecl *EntryPoint, const Decl *Param,
108 const HLSLAnnotationAttr *AnnotationAttr);
110 const Attr *A, llvm::Triple::EnvironmentType Stage,
111 std::initializer_list<llvm::Triple::EnvironmentType> AllowedStages);
113
115 QualType LHSType, QualType RHSType,
116 bool IsCompAssign);
118
119 void handleNumThreadsAttr(Decl *D, const ParsedAttr &AL);
120 void handleWaveSizeAttr(Decl *D, const ParsedAttr &AL);
123 void handleSV_GroupIDAttr(Decl *D, const ParsedAttr &AL);
124 void handlePackOffsetAttr(Decl *D, const ParsedAttr &AL);
125 void handleShaderAttr(Decl *D, const ParsedAttr &AL);
126 void handleResourceBindingAttr(Decl *D, const ParsedAttr &AL);
127 void handleParamModifierAttr(Decl *D, const ParsedAttr &AL);
129
130 bool CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall);
134
135 // HLSL Type trait implementations
138
140
141 // Diagnose whether the input ID is uint/unit2/uint3 type.
142 bool diagnoseInputIDType(QualType T, const ParsedAttr &AL);
143
145
147
148private:
149 // HLSL resource type attributes need to be processed all at once.
150 // This is a list to collect them.
151 llvm::SmallVector<const Attr *> HLSLResourcesTypeAttrs;
152
153 /// TypeLoc data for HLSLAttributedResourceType instances that we
154 /// have not yet populated.
155 llvm::DenseMap<const HLSLAttributedResourceType *,
157 LocsForHLSLAttributedResources;
158
159 // List of all resource bindings
161
162private:
163 void collectResourcesOnVarDecl(VarDecl *D);
164 void collectResourcesOnUserRecordDecl(const VarDecl *VD,
165 const RecordType *RT);
166 void processExplicitBindingsOnDecl(VarDecl *D);
167};
168
169} // namespace clang
170
171#endif // LLVM_CLANG_SEMA_SEMAHLSL_H
Forward declaration of all AST node types.
const Decl * D
#define X(type, name)
Definition: Value.h:144
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:43
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition: Expr.h:2874
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
This represents one expression.
Definition: Expr.h:110
Represents a function declaration or definition.
Definition: Decl.h:1935
One of these records is kept for each identifier that is lexed.
Represents a parameter to a function.
Definition: Decl.h:1725
ParsedAttr - Represents a syntactic attribute.
Definition: ParsedAttr.h:129
A (possibly-)qualified type.
Definition: Type.h:929
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
Definition: Type.h:6072
bool hasBindingInfoForDecl(const VarDecl *VD) const
Definition: SemaHLSL.cpp:138
DeclBindingInfo * getDeclBindingInfo(const VarDecl *VD, ResourceClass ResClass)
Definition: SemaHLSL.cpp:124
DeclBindingInfo * addDeclBindingInfo(const VarDecl *VD, ResourceClass ResClass)
Definition: SemaHLSL.cpp:111
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:2425
void DiagnoseAttrStageMismatch(const Attr *A, llvm::Triple::EnvironmentType Stage, std::initializer_list< llvm::Triple::EnvironmentType > AllowedStages)
Definition: SemaHLSL.cpp:448
void handleWaveSizeAttr(Decl *D, const ParsedAttr &AL)
Definition: SemaHLSL.cpp:705
HLSLAttributedResourceLocInfo TakeLocForHLSLAttribute(const HLSLAttributedResourceType *RT)
Definition: SemaHLSL.cpp:1053
QualType ProcessResourceTypeAttributes(QualType Wrapped)
Definition: SemaHLSL.cpp:1030
void handleSV_GroupThreadIDAttr(Decl *D, const ParsedAttr &AL)
Definition: SemaHLSL.cpp:791
void handleShaderAttr(Decl *D, const ParsedAttr &AL)
Definition: SemaHLSL.cpp:857
void handleSV_DispatchThreadIDAttr(Decl *D, const ParsedAttr &AL)
Definition: SemaHLSL.cpp:782
void CheckEntryPoint(FunctionDecl *FD)
Definition: SemaHLSL.cpp:353
void emitLogicalOperatorFixIt(Expr *LHS, Expr *RHS, BinaryOperatorKind Opc)
Definition: SemaHLSL.cpp:632
HLSLNumThreadsAttr * mergeNumThreadsAttr(Decl *D, const AttributeCommonInfo &AL, int X, int Y, int Z)
Definition: SemaHLSL.cpp:253
void handlePackOffsetAttr(Decl *D, const ParsedAttr &AL)
Definition: SemaHLSL.cpp:808
void CheckSemanticAnnotation(FunctionDecl *EntryPoint, const Decl *Param, const HLSLAnnotationAttr *AnnotationAttr)
Definition: SemaHLSL.cpp:427
bool diagnoseInputIDType(QualType T, const ParsedAttr &AL)
Definition: SemaHLSL.cpp:769
void handleParamModifierAttr(Decl *D, const ParsedAttr &AL)
Definition: SemaHLSL.cpp:1306
bool IsScalarizedLayoutCompatible(QualType T1, QualType T2) const
Definition: SemaHLSL.cpp:2370
bool CheckCompatibleParameterABI(FunctionDecl *New, FunctionDecl *Old)
Definition: SemaHLSL.cpp:2393
QualType handleVectorBinOpConversion(ExprResult &LHS, ExprResult &RHS, QualType LHSType, QualType RHSType, bool IsCompAssign)
Definition: SemaHLSL.cpp:565
void handleResourceBindingAttr(Decl *D, const ParsedAttr &AL)
Definition: SemaHLSL.cpp:1224
bool IsTypedResourceElementCompatible(QualType T1)
Definition: SemaHLSL.cpp:2333
void handleNumThreadsAttr(Decl *D, const ParsedAttr &AL)
Definition: SemaHLSL.cpp:650
void DiagnoseAvailabilityViolations(TranslationUnitDecl *TU)
Definition: SemaHLSL.cpp:1656
void ActOnTopLevelFunction(FunctionDecl *FD)
Definition: SemaHLSL.cpp:320
bool handleResourceTypeAttr(QualType T, const ParsedAttr &AL)
Definition: SemaHLSL.cpp:961
HLSLShaderAttr * mergeShaderAttr(Decl *D, const AttributeCommonInfo &AL, llvm::Triple::EnvironmentType ShaderType)
Definition: SemaHLSL.cpp:287
void ActOnFinishBuffer(Decl *Dcl, SourceLocation RBrace)
Definition: SemaHLSL.cpp:202
HLSLParamModifierAttr * mergeParamModifierAttr(Decl *D, const AttributeCommonInfo &AL, HLSLParamModifierAttr::Spelling Spelling)
Definition: SemaHLSL.cpp:300
QualType getInoutParameterType(QualType Ty)
Definition: SemaHLSL.cpp:2483
Decl * ActOnStartBuffer(Scope *BufferScope, bool CBuffer, SourceLocation KwLoc, IdentifierInfo *Ident, SourceLocation IdentLoc, SourceLocation LBrace)
Definition: SemaHLSL.cpp:144
HLSLWaveSizeAttr * mergeWaveSizeAttr(Decl *D, const AttributeCommonInfo &AL, int Min, int Max, int Preferred, int SpelledArgsCount)
Definition: SemaHLSL.cpp:267
void handleSV_GroupIDAttr(Decl *D, const ParsedAttr &AL)
Definition: SemaHLSL.cpp:800
void ActOnVariableDeclarator(VarDecl *VD)
Definition: SemaHLSL.cpp:2494
bool CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall)
Definition: SemaHLSL.cpp:1937
Sema - This implements semantic analysis and AST building for C.
Definition: Sema.h:463
Encodes a location in the source.
The top declaration context.
Definition: Decl.h:84
Represents a variable declaration or definition.
Definition: Decl.h:882
The JSON file list parser is used to communicate input to InstallAPI.
BinaryOperatorKind
BindingType
Definition: SemaHLSL.h:41
bool CreateHLSLAttributedResourceType(Sema &S, QualType Wrapped, ArrayRef< const Attr * > AttrList, QualType &ResType, HLSLAttributedResourceLocInfo *LocInfo=nullptr)
Definition: SemaHLSL.cpp:877
const FunctionProtoType * T
@ Implicit
An implicit conversion.
const HLSLResourceBindingAttr * Attr
Definition: SemaHLSL.h:48
BindingType BindType
Definition: SemaHLSL.h:49
DeclBindingInfo(const VarDecl *Decl, ResourceClass ResClass, BindingType BindType=BindingType::NotAssigned, const HLSLResourceBindingAttr *Attr=nullptr)
Definition: SemaHLSL.h:51
ResourceClass ResClass
Definition: SemaHLSL.h:47
void setBindingAttribute(HLSLResourceBindingAttr *A, BindingType BT)
Definition: SemaHLSL.h:56
const VarDecl * Decl
Definition: SemaHLSL.h:46