21
21
#include " clang/AST/ASTContext.h"
22
22
#include " clang/AST/Attrs.inc"
23
23
#include " clang/AST/Decl.h"
24
+ #include " clang/AST/HLSLResource.h"
24
25
#include " clang/AST/RecursiveASTVisitor.h"
25
26
#include " clang/AST/Type.h"
26
27
#include " clang/Basic/TargetOptions.h"
@@ -131,35 +132,24 @@ static CXXMethodDecl *lookupMethod(CXXRecordDecl *Record, StringRef Name,
131
132
132
133
static CXXMethodDecl *lookupResourceInitMethodAndSetupArgs (
133
134
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" );
137
138
138
139
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
-
150
140
CXXMethodDecl *CreateMethod = nullptr ;
151
141
Value *NameStr = buildNameForResource (Name, CGM);
152
- Value *Space = llvm::ConstantInt::get (CGM.IntTy , SpaceNo );
142
+ Value *Space = llvm::ConstantInt::get (CGM.IntTy , Binding. getSpace () );
153
143
154
- if (RegisterSlot. has_value ()) {
144
+ if (Binding. isExplicit ()) {
155
145
// explicit binding
156
- auto *RegSlot = llvm::ConstantInt::get (CGM.IntTy , RegisterSlot. value ());
146
+ auto *RegSlot = llvm::ConstantInt::get (CGM.IntTy , Binding. getSlot ());
157
147
Args.add (RValue::get (RegSlot), AST.UnsignedIntTy );
158
148
CreateMethod = lookupMethod (ResourceDecl, " __createFromBinding" , SC_Static);
159
149
} else {
160
150
// implicit binding
161
151
auto *OrderID =
162
- llvm::ConstantInt::get (CGM.IntTy , RBA-> getImplicitBindingOrderID ());
152
+ llvm::ConstantInt::get (CGM.IntTy , Binding. getImplicitOrderID ());
163
153
Args.add (RValue::get (OrderID), AST.UnsignedIntTy );
164
154
CreateMethod =
165
155
lookupMethod (ResourceDecl, " __createFromImplicitBinding" , SC_Static);
@@ -194,8 +184,8 @@ static std::optional<llvm::Value *> initializeLocalResourceArray(
194
184
CodeGenFunction &CGF, CXXRecordDecl *ResourceDecl,
195
185
const ConstantArrayType *ArrayTy, AggValueSlot &ValueSlot,
196
186
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) {
199
189
200
190
ASTContext &AST = CGF.getContext ();
201
191
llvm::IntegerType *IntTy = CGF.CGM .IntTy ;
@@ -220,7 +210,7 @@ static std::optional<llvm::Value *> initializeLocalResourceArray(
220
210
}
221
211
std::optional<llvm::Value *> MaybeIndex = initializeLocalResourceArray (
222
212
CGF, ResourceDecl, SubArrayTy, ValueSlot, Range, Index, ResourceName,
223
- RBA, VkBinding , GEPIndices, ArraySubsExprLoc);
213
+ Binding , GEPIndices, ArraySubsExprLoc);
224
214
if (!MaybeIndex)
225
215
return std::nullopt ;
226
216
Index = *MaybeIndex;
@@ -244,8 +234,7 @@ static std::optional<llvm::Value *> initializeLocalResourceArray(
244
234
245
235
CallArgList Args;
246
236
CXXMethodDecl *CreateMethod = lookupResourceInitMethodAndSetupArgs (
247
- CGF.CGM , ResourceDecl, Range, Index, ResourceName, RBA, VkBinding,
248
- Args);
237
+ CGF.CGM , ResourceDecl, Range, Index, ResourceName, Binding, Args);
249
238
250
239
if (!CreateMethod)
251
240
// This can happen if someone creates an array of structs that looks like
@@ -439,14 +428,7 @@ void CGHLSLRuntime::addBuffer(const HLSLBufferDecl *BufDecl) {
439
428
emitBufferGlobalsAndMetadata (BufDecl, BufGV);
440
429
441
430
// 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);
450
432
}
451
433
452
434
void CGHLSLRuntime::addRootSignature (
@@ -810,44 +792,29 @@ static void initializeBuffer(CodeGenModule &CGM, llvm::GlobalVariable *GV,
810
792
}
811
793
812
794
void 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" );
827
799
828
- void CGHLSLRuntime::initializeBufferFromBinding (const HLSLBufferDecl *BufDecl,
829
- llvm::GlobalVariable *GV,
830
- HLSLResourceBindingAttr *RBA) {
831
- assert (RBA && " expect a nonnull binding attribute" );
832
800
auto *Index = llvm::ConstantInt::get (CGM.IntTy , 0 );
833
801
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 ());
835
803
Value *Name = buildNameForResource (BufDecl->getName (), CGM);
836
804
837
- llvm::Intrinsic::ID IntrinsicID =
838
- RBA->hasRegisterSlot ()
839
- ? CGM.getHLSLRuntime ().getCreateHandleFromBindingIntrinsic ()
840
- : CGM.getHLSLRuntime ().getCreateHandleFromImplicitBindingIntrinsic ();
841
-
842
805
// 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 ());
845
810
SmallVector<Value *> Args{Space, RegSlot, RangeSize, Index, Name};
846
811
initializeBuffer (CGM, GV, IntrinsicID, Args);
847
812
} else {
848
813
// buffer with implicit binding
814
+ llvm::Intrinsic::ID IntrinsicID =
815
+ CGM.getHLSLRuntime ().getCreateHandleFromImplicitBindingIntrinsic ();
849
816
auto *OrderID =
850
- llvm::ConstantInt::get (CGM.IntTy , RBA-> getImplicitBindingOrderID ());
817
+ llvm::ConstantInt::get (CGM.IntTy , Binding. getImplicitOrderID ());
851
818
SmallVector<Value *> Args{OrderID, Space, RangeSize, Index, Name};
852
819
initializeBuffer (CGM, GV, IntrinsicID, Args);
853
820
}
@@ -960,9 +927,9 @@ std::optional<LValue> CGHLSLRuntime::emitResourceArraySubscriptExpr(
960
927
961
928
// Find binding info for the resource array. For implicit binding
962
929
// 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" );
966
933
967
934
// Find the individual resource type.
968
935
QualType ResultTy = ArraySubsExpr->getType ();
@@ -992,7 +959,7 @@ std::optional<LValue> CGHLSLRuntime::emitResourceArraySubscriptExpr(
992
959
CallArgList Args;
993
960
CXXMethodDecl *CreateMethod = lookupResourceInitMethodAndSetupArgs (
994
961
CGF.CGM , ResourceTy->getAsCXXRecordDecl (), Range, Index,
995
- ArrayDecl->getName (), RBA, VkBinding , Args);
962
+ ArrayDecl->getName (), Binding , Args);
996
963
997
964
if (!CreateMethod)
998
965
// This can happen if someone creates an array of structs that looks like
@@ -1009,8 +976,8 @@ std::optional<LValue> CGHLSLRuntime::emitResourceArraySubscriptExpr(
1009
976
cast<ConstantArrayType>(ResultTy.getTypePtr ());
1010
977
std::optional<llvm::Value *> EndIndex = initializeLocalResourceArray (
1011
978
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 ());
1014
981
if (!EndIndex)
1015
982
return std::nullopt ;
1016
983
}
0 commit comments