Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:37:04

0001 //===----- SemaHLSL.h ----- Semantic Analysis for HLSL constructs ---------===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 /// \file
0009 /// This file declares semantic analysis for HLSL constructs.
0010 ///
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_CLANG_SEMA_SEMAHLSL_H
0014 #define LLVM_CLANG_SEMA_SEMAHLSL_H
0015 
0016 #include "clang/AST/ASTFwd.h"
0017 #include "clang/AST/Attr.h"
0018 #include "clang/AST/Type.h"
0019 #include "clang/AST/TypeLoc.h"
0020 #include "clang/Basic/SourceLocation.h"
0021 #include "clang/Sema/SemaBase.h"
0022 #include "llvm/ADT/SmallVector.h"
0023 #include "llvm/TargetParser/Triple.h"
0024 #include <initializer_list>
0025 
0026 namespace clang {
0027 class AttributeCommonInfo;
0028 class IdentifierInfo;
0029 class ParsedAttr;
0030 class Scope;
0031 class VarDecl;
0032 
0033 using llvm::dxil::ResourceClass;
0034 
0035 // FIXME: This can be hidden (as static function in SemaHLSL.cpp) once we no
0036 // longer need to create builtin buffer types in HLSLExternalSemaSource.
0037 bool CreateHLSLAttributedResourceType(
0038     Sema &S, QualType Wrapped, ArrayRef<const Attr *> AttrList,
0039     QualType &ResType, HLSLAttributedResourceLocInfo *LocInfo = nullptr);
0040 
0041 enum class BindingType : uint8_t { NotAssigned, Explicit, Implicit };
0042 
0043 // DeclBindingInfo struct stores information about required/assigned resource
0044 // binding onon a declaration for specific resource class.
0045 struct DeclBindingInfo {
0046   const VarDecl *Decl;
0047   ResourceClass ResClass;
0048   const HLSLResourceBindingAttr *Attr;
0049   BindingType BindType;
0050 
0051   DeclBindingInfo(const VarDecl *Decl, ResourceClass ResClass,
0052                   BindingType BindType = BindingType::NotAssigned,
0053                   const HLSLResourceBindingAttr *Attr = nullptr)
0054       : Decl(Decl), ResClass(ResClass), Attr(Attr), BindType(BindType) {}
0055 
0056   void setBindingAttribute(HLSLResourceBindingAttr *A, BindingType BT) {
0057     assert(Attr == nullptr && BindType == BindingType::NotAssigned &&
0058            "binding attribute already assigned");
0059     Attr = A;
0060     BindType = BT;
0061   }
0062 };
0063 
0064 // ResourceBindings class stores information about all resource bindings
0065 // in a shader. It is used for binding diagnostics and implicit binding
0066 // assigments.
0067 class ResourceBindings {
0068 public:
0069   DeclBindingInfo *addDeclBindingInfo(const VarDecl *VD,
0070                                       ResourceClass ResClass);
0071   DeclBindingInfo *getDeclBindingInfo(const VarDecl *VD,
0072                                       ResourceClass ResClass);
0073   bool hasBindingInfoForDecl(const VarDecl *VD) const;
0074 
0075 private:
0076   // List of all resource bindings required by the shader.
0077   // A global declaration can have multiple bindings for different
0078   // resource classes. They are all stored sequentially in this list.
0079   // The DeclToBindingListIndex hashtable maps a declaration to the
0080   // index of the first binding info in the list.
0081   llvm::SmallVector<DeclBindingInfo> BindingsList;
0082   llvm::DenseMap<const VarDecl *, unsigned> DeclToBindingListIndex;
0083 };
0084 
0085 class SemaHLSL : public SemaBase {
0086 public:
0087   SemaHLSL(Sema &S);
0088 
0089   Decl *ActOnStartBuffer(Scope *BufferScope, bool CBuffer, SourceLocation KwLoc,
0090                          IdentifierInfo *Ident, SourceLocation IdentLoc,
0091                          SourceLocation LBrace);
0092   void ActOnFinishBuffer(Decl *Dcl, SourceLocation RBrace);
0093   HLSLNumThreadsAttr *mergeNumThreadsAttr(Decl *D,
0094                                           const AttributeCommonInfo &AL, int X,
0095                                           int Y, int Z);
0096   HLSLWaveSizeAttr *mergeWaveSizeAttr(Decl *D, const AttributeCommonInfo &AL,
0097                                       int Min, int Max, int Preferred,
0098                                       int SpelledArgsCount);
0099   HLSLShaderAttr *mergeShaderAttr(Decl *D, const AttributeCommonInfo &AL,
0100                                   llvm::Triple::EnvironmentType ShaderType);
0101   HLSLParamModifierAttr *
0102   mergeParamModifierAttr(Decl *D, const AttributeCommonInfo &AL,
0103                          HLSLParamModifierAttr::Spelling Spelling);
0104   void ActOnTopLevelFunction(FunctionDecl *FD);
0105   void ActOnVariableDeclarator(VarDecl *VD);
0106   void CheckEntryPoint(FunctionDecl *FD);
0107   void CheckSemanticAnnotation(FunctionDecl *EntryPoint, const Decl *Param,
0108                                const HLSLAnnotationAttr *AnnotationAttr);
0109   void DiagnoseAttrStageMismatch(
0110       const Attr *A, llvm::Triple::EnvironmentType Stage,
0111       std::initializer_list<llvm::Triple::EnvironmentType> AllowedStages);
0112   void DiagnoseAvailabilityViolations(TranslationUnitDecl *TU);
0113 
0114   QualType handleVectorBinOpConversion(ExprResult &LHS, ExprResult &RHS,
0115                                        QualType LHSType, QualType RHSType,
0116                                        bool IsCompAssign);
0117   void emitLogicalOperatorFixIt(Expr *LHS, Expr *RHS, BinaryOperatorKind Opc);
0118 
0119   void handleNumThreadsAttr(Decl *D, const ParsedAttr &AL);
0120   void handleWaveSizeAttr(Decl *D, const ParsedAttr &AL);
0121   void handleSV_DispatchThreadIDAttr(Decl *D, const ParsedAttr &AL);
0122   void handleSV_GroupThreadIDAttr(Decl *D, const ParsedAttr &AL);
0123   void handleSV_GroupIDAttr(Decl *D, const ParsedAttr &AL);
0124   void handlePackOffsetAttr(Decl *D, const ParsedAttr &AL);
0125   void handleShaderAttr(Decl *D, const ParsedAttr &AL);
0126   void handleResourceBindingAttr(Decl *D, const ParsedAttr &AL);
0127   void handleParamModifierAttr(Decl *D, const ParsedAttr &AL);
0128   bool handleResourceTypeAttr(QualType T, const ParsedAttr &AL);
0129 
0130   bool CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall);
0131   QualType ProcessResourceTypeAttributes(QualType Wrapped);
0132   HLSLAttributedResourceLocInfo
0133   TakeLocForHLSLAttribute(const HLSLAttributedResourceType *RT);
0134 
0135   // HLSL Type trait implementations
0136   bool IsScalarizedLayoutCompatible(QualType T1, QualType T2) const;
0137   bool IsTypedResourceElementCompatible(QualType T1);
0138 
0139   bool CheckCompatibleParameterABI(FunctionDecl *New, FunctionDecl *Old);
0140 
0141   // Diagnose whether the input ID is uint/unit2/uint3 type.
0142   bool diagnoseInputIDType(QualType T, const ParsedAttr &AL);
0143 
0144   ExprResult ActOnOutParamExpr(ParmVarDecl *Param, Expr *Arg);
0145 
0146   QualType getInoutParameterType(QualType Ty);
0147 
0148 private:
0149   // HLSL resource type attributes need to be processed all at once.
0150   // This is a list to collect them.
0151   llvm::SmallVector<const Attr *> HLSLResourcesTypeAttrs;
0152 
0153   /// TypeLoc data for HLSLAttributedResourceType instances that we
0154   /// have not yet populated.
0155   llvm::DenseMap<const HLSLAttributedResourceType *,
0156                  HLSLAttributedResourceLocInfo>
0157       LocsForHLSLAttributedResources;
0158 
0159   // List of all resource bindings
0160   ResourceBindings Bindings;
0161 
0162 private:
0163   void collectResourcesOnVarDecl(VarDecl *D);
0164   void collectResourcesOnUserRecordDecl(const VarDecl *VD,
0165                                         const RecordType *RT);
0166   void processExplicitBindingsOnDecl(VarDecl *D);
0167 };
0168 
0169 } // namespace clang
0170 
0171 #endif // LLVM_CLANG_SEMA_SEMAHLSL_H