2121#include " clang/AST/ASTContext.h"
2222#include " clang/AST/Attrs.inc"
2323#include " clang/AST/Decl.h"
24+ #include " clang/AST/HLSLResource.h"
2425#include " clang/AST/RecursiveASTVisitor.h"
2526#include " clang/AST/Type.h"
2627#include " clang/Basic/TargetOptions.h"
@@ -131,35 +132,24 @@ static CXXMethodDecl *lookupMethod(CXXRecordDecl *Record, StringRef Name,
131132
132133static CXXMethodDecl *lookupResourceInitMethodAndSetupArgs (
133134 CodeGenModule &CGM, CXXRecordDecl *ResourceDecl, llvm::Value *Range,
134- llvm::Value *Index, StringRef Name, HLSLResourceBindingAttr *RBA ,
135- HLSLVkBindingAttr *VkBinding, CallArgList &Args) {
136- assert ((VkBinding || RBA ) && " at least one a binding attribute expected" );
135+ llvm::Value *Index, StringRef Name, ResourceBindingAttrs &Binding ,
136+ CallArgList &Args) {
137+ assert (Binding. hasBinding ( ) && " at least one binding attribute expected" );
137138
138139 ASTContext &AST = CGM.getContext ();
139- std::optional<uint32_t > RegisterSlot;
140- uint32_t SpaceNo = 0 ;
141- if (VkBinding) {
142- RegisterSlot = VkBinding->getBinding ();
143- SpaceNo = VkBinding->getSet ();
144- } else {
145- if (RBA->hasRegisterSlot ())
146- RegisterSlot = RBA->getSlotNumber ();
147- SpaceNo = RBA->getSpaceNumber ();
148- }
149-
150140 CXXMethodDecl *CreateMethod = nullptr ;
151141 Value *NameStr = buildNameForResource (Name, CGM);
152- Value *Space = llvm::ConstantInt::get (CGM.IntTy , SpaceNo );
142+ Value *Space = llvm::ConstantInt::get (CGM.IntTy , Binding. getSpace () );
153143
154- if (RegisterSlot. has_value ()) {
144+ if (Binding. isExplicit ()) {
155145 // explicit binding
156- auto *RegSlot = llvm::ConstantInt::get (CGM.IntTy , RegisterSlot. value ());
146+ auto *RegSlot = llvm::ConstantInt::get (CGM.IntTy , Binding. getSlot ());
157147 Args.add (RValue::get (RegSlot), AST.UnsignedIntTy );
158148 CreateMethod = lookupMethod (ResourceDecl, " __createFromBinding" , SC_Static);
159149 } else {
160150 // implicit binding
161151 auto *OrderID =
162- llvm::ConstantInt::get (CGM.IntTy , RBA-> getImplicitBindingOrderID ());
152+ llvm::ConstantInt::get (CGM.IntTy , Binding. getImplicitOrderID ());
163153 Args.add (RValue::get (OrderID), AST.UnsignedIntTy );
164154 CreateMethod =
165155 lookupMethod (ResourceDecl, " __createFromImplicitBinding" , SC_Static);
@@ -194,8 +184,8 @@ static std::optional<llvm::Value *> initializeLocalResourceArray(
194184 CodeGenFunction &CGF, CXXRecordDecl *ResourceDecl,
195185 const ConstantArrayType *ArrayTy, AggValueSlot &ValueSlot,
196186 llvm::Value *Range, llvm::Value *StartIndex, StringRef ResourceName,
197- HLSLResourceBindingAttr *RBA, HLSLVkBindingAttr *VkBinding ,
198- ArrayRef<llvm::Value *> PrevGEPIndices, SourceLocation ArraySubsExprLoc) {
187+ ResourceBindingAttrs &Binding, ArrayRef<llvm::Value *> PrevGEPIndices ,
188+ SourceLocation ArraySubsExprLoc) {
199189
200190 ASTContext &AST = CGF.getContext ();
201191 llvm::IntegerType *IntTy = CGF.CGM .IntTy ;
@@ -220,7 +210,7 @@ static std::optional<llvm::Value *> initializeLocalResourceArray(
220210 }
221211 std::optional<llvm::Value *> MaybeIndex = initializeLocalResourceArray (
222212 CGF, ResourceDecl, SubArrayTy, ValueSlot, Range, Index, ResourceName,
223- RBA, VkBinding , GEPIndices, ArraySubsExprLoc);
213+ Binding , GEPIndices, ArraySubsExprLoc);
224214 if (!MaybeIndex)
225215 return std::nullopt ;
226216 Index = *MaybeIndex;
@@ -244,8 +234,7 @@ static std::optional<llvm::Value *> initializeLocalResourceArray(
244234
245235 CallArgList Args;
246236 CXXMethodDecl *CreateMethod = lookupResourceInitMethodAndSetupArgs (
247- CGF.CGM , ResourceDecl, Range, Index, ResourceName, RBA, VkBinding,
248- Args);
237+ CGF.CGM , ResourceDecl, Range, Index, ResourceName, Binding, Args);
249238
250239 if (!CreateMethod)
251240 // This can happen if someone creates an array of structs that looks like
@@ -439,14 +428,7 @@ void CGHLSLRuntime::addBuffer(const HLSLBufferDecl *BufDecl) {
439428 emitBufferGlobalsAndMetadata (BufDecl, BufGV);
440429
441430 // Initialize cbuffer from binding (implicit or explicit)
442- if (HLSLVkBindingAttr *VkBinding = BufDecl->getAttr <HLSLVkBindingAttr>()) {
443- initializeBufferFromBinding (BufDecl, BufGV, VkBinding);
444- } else {
445- HLSLResourceBindingAttr *RBA = BufDecl->getAttr <HLSLResourceBindingAttr>();
446- assert (RBA &&
447- " cbuffer/tbuffer should always have resource binding attribute" );
448- initializeBufferFromBinding (BufDecl, BufGV, RBA);
449- }
431+ initializeBufferFromBinding (BufDecl, BufGV);
450432}
451433
452434void CGHLSLRuntime::addRootSignature (
@@ -810,44 +792,29 @@ static void initializeBuffer(CodeGenModule &CGM, llvm::GlobalVariable *GV,
810792}
811793
812794void CGHLSLRuntime::initializeBufferFromBinding (const HLSLBufferDecl *BufDecl,
813- llvm::GlobalVariable *GV,
814- HLSLVkBindingAttr *VkBinding) {
815- assert (VkBinding && " expect a nonnull binding attribute" );
816- auto *Index = llvm::ConstantInt::get (CGM.IntTy , 0 );
817- auto *RangeSize = llvm::ConstantInt::get (CGM.IntTy , 1 );
818- auto *Set = llvm::ConstantInt::get (CGM.IntTy , VkBinding->getSet ());
819- auto *Binding = llvm::ConstantInt::get (CGM.IntTy , VkBinding->getBinding ());
820- Value *Name = buildNameForResource (BufDecl->getName (), CGM);
821- llvm::Intrinsic::ID IntrinsicID =
822- CGM.getHLSLRuntime ().getCreateHandleFromBindingIntrinsic ();
823-
824- SmallVector<Value *> Args{Set, Binding, RangeSize, Index, Name};
825- initializeBuffer (CGM, GV, IntrinsicID, Args);
826- }
795+ llvm::GlobalVariable *GV) {
796+ ResourceBindingAttrs Binding (BufDecl);
797+ assert (Binding.hasBinding () &&
798+ " cbuffer/tbuffer should always have resource binding attribute" );
827799
828- void CGHLSLRuntime::initializeBufferFromBinding (const HLSLBufferDecl *BufDecl,
829- llvm::GlobalVariable *GV,
830- HLSLResourceBindingAttr *RBA) {
831- assert (RBA && " expect a nonnull binding attribute" );
832800 auto *Index = llvm::ConstantInt::get (CGM.IntTy , 0 );
833801 auto *RangeSize = llvm::ConstantInt::get (CGM.IntTy , 1 );
834- auto *Space = llvm::ConstantInt::get (CGM.IntTy , RBA-> getSpaceNumber ());
802+ auto *Space = llvm::ConstantInt::get (CGM.IntTy , Binding. getSpace ());
835803 Value *Name = buildNameForResource (BufDecl->getName (), CGM);
836804
837- llvm::Intrinsic::ID IntrinsicID =
838- RBA->hasRegisterSlot ()
839- ? CGM.getHLSLRuntime ().getCreateHandleFromBindingIntrinsic ()
840- : CGM.getHLSLRuntime ().getCreateHandleFromImplicitBindingIntrinsic ();
841-
842805 // buffer with explicit binding
843- if (RBA->hasRegisterSlot ()) {
844- auto *RegSlot = llvm::ConstantInt::get (CGM.IntTy , RBA->getSlotNumber ());
806+ if (Binding.isExplicit ()) {
807+ llvm::Intrinsic::ID IntrinsicID =
808+ CGM.getHLSLRuntime ().getCreateHandleFromBindingIntrinsic ();
809+ auto *RegSlot = llvm::ConstantInt::get (CGM.IntTy , Binding.getSlot ());
845810 SmallVector<Value *> Args{Space, RegSlot, RangeSize, Index, Name};
846811 initializeBuffer (CGM, GV, IntrinsicID, Args);
847812 } else {
848813 // buffer with implicit binding
814+ llvm::Intrinsic::ID IntrinsicID =
815+ CGM.getHLSLRuntime ().getCreateHandleFromImplicitBindingIntrinsic ();
849816 auto *OrderID =
850- llvm::ConstantInt::get (CGM.IntTy , RBA-> getImplicitBindingOrderID ());
817+ llvm::ConstantInt::get (CGM.IntTy , Binding. getImplicitOrderID ());
851818 SmallVector<Value *> Args{OrderID, Space, RangeSize, Index, Name};
852819 initializeBuffer (CGM, GV, IntrinsicID, Args);
853820 }
@@ -960,9 +927,9 @@ std::optional<LValue> CGHLSLRuntime::emitResourceArraySubscriptExpr(
960927
961928 // Find binding info for the resource array. For implicit binding
962929 // an HLSLResourceBindingAttr should have been added by SemaHLSL.
963- HLSLVkBindingAttr *VkBinding = ArrayDecl-> getAttr <HLSLVkBindingAttr>( );
964- HLSLResourceBindingAttr *RBA = ArrayDecl-> getAttr <HLSLResourceBindingAttr>();
965- assert ((VkBinding || RBA) && " resource array must have a binding attribute" );
930+ ResourceBindingAttrs Binding (ArrayDecl );
931+ assert ((Binding. hasBinding ()) &&
932+ " resource array must have a binding attribute" );
966933
967934 // Find the individual resource type.
968935 QualType ResultTy = ArraySubsExpr->getType ();
@@ -992,7 +959,7 @@ std::optional<LValue> CGHLSLRuntime::emitResourceArraySubscriptExpr(
992959 CallArgList Args;
993960 CXXMethodDecl *CreateMethod = lookupResourceInitMethodAndSetupArgs (
994961 CGF.CGM , ResourceTy->getAsCXXRecordDecl (), Range, Index,
995- ArrayDecl->getName (), RBA, VkBinding , Args);
962+ ArrayDecl->getName (), Binding , Args);
996963
997964 if (!CreateMethod)
998965 // This can happen if someone creates an array of structs that looks like
@@ -1009,8 +976,8 @@ std::optional<LValue> CGHLSLRuntime::emitResourceArraySubscriptExpr(
1009976 cast<ConstantArrayType>(ResultTy.getTypePtr ());
1010977 std::optional<llvm::Value *> EndIndex = initializeLocalResourceArray (
1011978 CGF, ResourceTy->getAsCXXRecordDecl (), ArrayTy, ValueSlot, Range, Index,
1012- ArrayDecl->getName (), RBA, VkBinding ,
1013- { llvm::ConstantInt::get (CGM. IntTy , 0 )}, ArraySubsExpr->getExprLoc ());
979+ ArrayDecl->getName (), Binding, { llvm::ConstantInt::get (CGM. IntTy , 0 )} ,
980+ ArraySubsExpr->getExprLoc ());
1014981 if (!EndIndex)
1015982 return std::nullopt ;
1016983 }
0 commit comments