Skip to content

Commit 62e73db

Browse files
committed
Refactor: Address review comments for implicit typed buffer counters
This commit addresses review comments on the initial implementation of implicit typed buffer counters. The changes include: - Refactored `CGHLSLBuiltins.cpp` to move `HandleTy` declaration into the SPIR-V specific block and add an early exit for the DXIL case. - Updated `CGHLSLRuntime.cpp` to use a `const char *` for `CreateMethodName` to avoid string allocation. - In `HLSLBuiltinTypeDeclBuilder.cpp`, replaced `if (Record->isCompleteDefinition())` with an assert and added descriptive comments to `addCreateFromBindingWithImplicitCounter` and `addCreateFromImplicitBindingWithImplicitCounter`. - In `StructuredBuffers-constructors.hlsl`, updated the alignment check to be more specific and re-inserted a removed comment block. - In `SemaHLSL.cpp`, changed `CreateMethodName` to `const char *`, removed a debug print, moved the re-creation of the binding object into the `else` block, and used `AST.getHLSLAttributedResourceType` for consistency.
1 parent 35d1352 commit 62e73db

File tree

5 files changed

+68
-38
lines changed

5 files changed

+68
-38
lines changed

clang/lib/CodeGen/CGHLSLBuiltins.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -353,17 +353,17 @@ Value *CodeGenFunction::EmitHLSLBuiltinExpr(unsigned BuiltinID,
353353
return Builder.CreateIntrinsic(HandleTy, IntrinsicID, Args);
354354
}
355355
case Builtin::BI__builtin_hlsl_resource_counterhandlefromimplicitbinding: {
356-
llvm::Type *HandleTy = CGM.getTypes().ConvertType(E->getType());
357356
Value *MainHandle = EmitScalarExpr(E->getArg(0));
358-
if (CGM.getTriple().isSPIRV()) {
359-
Value *OrderID = EmitScalarExpr(E->getArg(1));
360-
Value *SpaceOp = EmitScalarExpr(E->getArg(2));
361-
llvm::Intrinsic::ID IntrinsicID =
362-
llvm::Intrinsic::spv_resource_counterhandlefromimplicitbinding;
363-
SmallVector<Value *> Args{MainHandle, OrderID, SpaceOp};
364-
return Builder.CreateIntrinsic(HandleTy, IntrinsicID, Args);
365-
}
366-
return MainHandle;
357+
if (!CGM.getTriple().isSPIRV())
358+
return MainHandle;
359+
360+
llvm::Type *HandleTy = CGM.getTypes().ConvertType(E->getType());
361+
Value *OrderID = EmitScalarExpr(E->getArg(1));
362+
Value *SpaceOp = EmitScalarExpr(E->getArg(2));
363+
llvm::Intrinsic::ID IntrinsicID =
364+
llvm::Intrinsic::spv_resource_counterhandlefromimplicitbinding;
365+
SmallVector<Value *> Args{MainHandle, OrderID, SpaceOp};
366+
return Builder.CreateIntrinsic(HandleTy, IntrinsicID, Args);
367367
}
368368
case Builtin::BI__builtin_hlsl_resource_nonuniformindex: {
369369
Value *IndexOp = EmitScalarExpr(E->getArg(0));

clang/lib/CodeGen/CGHLSLRuntime.cpp

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -145,24 +145,19 @@ static CXXMethodDecl *lookupResourceInitMethodAndSetupArgs(
145145
// explicit binding
146146
auto *RegSlot = llvm::ConstantInt::get(CGM.IntTy, Binding.getSlot());
147147
Args.add(RValue::get(RegSlot), AST.UnsignedIntTy);
148-
if (Binding.hasCounterImplicitOrderID())
149-
CreateMethod = lookupMethod(
150-
ResourceDecl, "__createFromBindingWithImplicitCounter", SC_Static);
151-
else
152-
CreateMethod =
153-
lookupMethod(ResourceDecl, "__createFromBinding", SC_Static);
148+
const char *Name = Binding.hasCounterImplicitOrderID()
149+
? "__createFromBindingWithImplicitCounter"
150+
: "__createFromBinding";
151+
CreateMethod = lookupMethod(ResourceDecl, Name, SC_Static);
154152
} else {
155153
// implicit binding
156154
auto *OrderID =
157155
llvm::ConstantInt::get(CGM.IntTy, Binding.getImplicitOrderID());
158156
Args.add(RValue::get(OrderID), AST.UnsignedIntTy);
159-
if (Binding.hasCounterImplicitOrderID())
160-
CreateMethod = lookupMethod(
161-
ResourceDecl, "__createFromImplicitBindingWithImplicitCounter",
162-
SC_Static);
163-
else
164-
CreateMethod =
165-
lookupMethod(ResourceDecl, "__createFromImplicitBinding", SC_Static);
157+
const char *Name = Binding.hasCounterImplicitOrderID()
158+
? "__createFromImplicitBindingWithImplicitCounter"
159+
: "__createFromImplicitBinding";
160+
CreateMethod = lookupMethod(ResourceDecl, Name, SC_Static);
166161
}
167162
Args.add(RValue::get(Space), AST.UnsignedIntTy);
168163
Args.add(RValue::get(Range), AST.IntTy);

clang/lib/Sema/HLSLBuiltinTypeDeclBuilder.cpp

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -938,10 +938,24 @@ BuiltinTypeDeclBuilder &BuiltinTypeDeclBuilder::addCreateFromImplicitBinding() {
938938
.finalize();
939939
}
940940

941+
// Adds static method that initializes resource from binding:
942+
//
943+
// static Resource<T>
944+
// __createFromBindingWithImplicitCounter(unsigned registerNo,
945+
// unsigned spaceNo, int range,
946+
// unsigned index, const char *name,
947+
// unsigned counterOrderId) {
948+
// Resource<T> tmp;
949+
// tmp.__handle = __builtin_hlsl_resource_handlefrombinding(
950+
// tmp.__handle, registerNo, spaceNo, range, index, name);
951+
// tmp.__counter_handle =
952+
// __builtin_hlsl_resource_counterhandlefromimplicitbinding(
953+
// tmp.__handle, counterOrderId, spaceNo);
954+
// return tmp;
955+
// }
941956
BuiltinTypeDeclBuilder &
942957
BuiltinTypeDeclBuilder::addCreateFromBindingWithImplicitCounter() {
943-
if (Record->isCompleteDefinition())
944-
return *this;
958+
assert(!Record->isCompleteDefinition() && "record is already complete");
945959

946960
using PH = BuiltinTypeMethodBuilder::PlaceHolder;
947961
ASTContext &AST = SemaRef.getASTContext();
@@ -971,10 +985,25 @@ BuiltinTypeDeclBuilder::addCreateFromBindingWithImplicitCounter() {
971985
.finalize();
972986
}
973987

988+
// Adds static method that initializes resource from binding:
989+
//
990+
// static Resource<T>
991+
// __createFromImplicitBindingWithImplicitCounter(unsigned orderId,
992+
// unsigned spaceNo, int range,
993+
// unsigned index,
994+
// const char *name,
995+
// unsigned counterOrderId) {
996+
// Resource<T> tmp;
997+
// tmp.__handle = __builtin_hlsl_resource_handlefromimplicitbinding(
998+
// tmp.__handle, orderId, spaceNo, range, index, name);
999+
// tmp.__counter_handle =
1000+
// __builtin_hlsl_resource_counterhandlefromimplicitbinding(
1001+
// tmp.__handle, counterOrderId, spaceNo);
1002+
// return tmp;
1003+
// }
9741004
BuiltinTypeDeclBuilder &
9751005
BuiltinTypeDeclBuilder::addCreateFromImplicitBindingWithImplicitCounter() {
976-
if (Record->isCompleteDefinition())
977-
return *this;
1006+
assert(!Record->isCompleteDefinition() && "record is already complete");
9781007

9791008
using PH = BuiltinTypeMethodBuilder::PlaceHolder;
9801009
ASTContext &AST = SemaRef.getASTContext();

clang/lib/Sema/SemaHLSL.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3000,7 +3000,7 @@ bool SemaHLSL::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
30003000
auto MainAttrs = MainResType->getAttrs();
30013001
assert(!MainAttrs.IsCounter && "cannot create a counter from a counter");
30023002
MainAttrs.IsCounter = true;
3003-
QualType CounterHandleTy = getASTContext().getHLSLAttributedResourceType(
3003+
QualType CounterHandleTy = AST.getHLSLAttributedResourceType(
30043004
MainResType->getWrappedType(), MainResType->getContainedType(),
30053005
MainAttrs);
30063006
TheCall->setType(CounterHandleTy);
@@ -3813,12 +3813,13 @@ void SemaHLSL::ActOnVariableDeclarator(VarDecl *VD) {
38133813
uint32_t OrderID = getNextImplicitBindingOrderID();
38143814
if (Binding.hasBinding())
38153815
Binding.setImplicitOrderID(OrderID);
3816-
else
3816+
else {
38173817
addImplicitBindingAttrToDecl(
38183818
SemaRef, VD, getRegisterType(getResourceArrayHandleType(VD)),
38193819
OrderID);
3820-
// Re-create the binding object to pick up the new attribute.
3821-
Binding = ResourceBindingAttrs(VD);
3820+
// Re-create the binding object to pick up the new attribute.
3821+
Binding = ResourceBindingAttrs(VD);
3822+
}
38223823
}
38233824

38243825
// Get to the base type of a potentially multi-dimensional array.
@@ -3855,7 +3856,7 @@ bool SemaHLSL::initGlobalResourceDecl(VarDecl *VD) {
38553856
llvm::SmallVector<Expr *> Args;
38563857

38573858
bool HasCounter = hasCounterHandle(ResourceDecl);
3858-
std::string CreateMethodName;
3859+
const char *CreateMethodName;
38593860
if (Binding.isExplicit())
38603861
CreateMethodName = HasCounter ? "__createFromBindingWithImplicitCounter"
38613862
: "__createFromBinding";
@@ -3866,10 +3867,6 @@ bool SemaHLSL::initGlobalResourceDecl(VarDecl *VD) {
38663867

38673868
CreateMethod =
38683869
lookupMethod(SemaRef, ResourceDecl, CreateMethodName, VD->getLocation());
3869-
if (!CreateMethod) {
3870-
llvm::dbgs() << "STEVEN: failed to get method " << CreateMethodName << "\n";
3871-
VD->dumpColor();
3872-
}
38733870

38743871
if (!CreateMethod)
38753872
// This can happen if someone creates a struct that looks like an HLSL

clang/test/CodeGenHLSL/resources/StructuredBuffers-constructors.hlsl

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export void foo() {
3838
// Buf1 initialization part 2 - body of StructuredBuffer<float>::::__createFromBinding
3939

4040
// CHECK: define {{.*}} void @hlsl::StructuredBuffer<float>::__createFromBinding(unsigned int, unsigned int, int, unsigned int, char const*)
41-
// CHECK-SAME: ptr {{.*}} sret(%"class.hlsl::StructuredBuffer") align {{[0-9]+}} %[[RetValue1:.*]], i32 noundef %registerNo,
41+
// CHECK-SAME: ptr {{.*}} sret(%"class.hlsl::StructuredBuffer") align {{(4|8)}} %[[RetValue1:.*]], i32 noundef %registerNo,
4242
// CHECK-SAME: i32 noundef %spaceNo, i32 noundef %range, i32 noundef %index, ptr noundef %name)
4343
// CHECK: %[[Tmp1:.*]] = alloca %"class.hlsl::StructuredBuffer"
4444
// CHECK-DXIL: %[[Handle1:.*]] = call target("dx.RawBuffer", float, 0, 0)
@@ -55,7 +55,7 @@ export void foo() {
5555

5656
// Buf2 initialization part 2 - body of RWStructuredBuffer<float>::__createFromImplicitBindingWithImplicitCounter
5757
// CHECK: define linkonce_odr hidden void @hlsl::RWStructuredBuffer<float>::__createFromImplicitBindingWithImplicitCounter(unsigned int, unsigned int, int, unsigned int, char const*, unsigned int)
58-
// CHECK-SAME: (ptr {{.*}} sret(%"class.hlsl::RWStructuredBuffer") align {{[0-9]+}} %[[RetValue2:.*]], i32 noundef %orderId,
58+
// CHECK-SAME: (ptr {{.*}} sret(%"class.hlsl::RWStructuredBuffer") align {{(4|8)}} %[[RetValue2:.*]], i32 noundef %orderId,
5959
// CHECK-SAME: i32 noundef %spaceNo, i32 noundef %range, i32 noundef %index, ptr noundef %name, i32 noundef %counterOrderId)
6060
// CHECK: %[[Tmp2:.*]] = alloca %"class.hlsl::RWStructuredBuffer"
6161
// CHECK-DXIL: %[[Handle2:.*]] = call target("dx.RawBuffer", float, 1, 0)
@@ -77,6 +77,15 @@ export void foo() {
7777
// CHECK-SPV-NEXT: store target("spirv.VulkanBuffer", i32, 12, 1) %[[CounterHandle]], ptr %[[CounterHandlePtr]], align 8
7878
// CHECK: call void @hlsl::RWStructuredBuffer<float>::RWStructuredBuffer(hlsl::RWStructuredBuffer<float> const&)(ptr {{.*}} %[[RetValue2]], ptr {{.*}} %[[Tmp2]])
7979

80+
// Buf3 initialization part 1 - local variable declared in function foo() is initialized by
81+
// AppendStructuredBuffer<float> C1 default constructor
82+
// CHECK: define {{.*}}void @foo()
83+
// CHECK-NEXT: entry:
84+
// CHECK: %Buf3 = alloca %"class.hlsl::AppendStructuredBuffer", align {{4|8}}
85+
// CHECK-NEXT: call void @hlsl::AppendStructuredBuffer<float>::AppendStructuredBuffer()(ptr {{.*}} %Buf3)
86+
87+
// Buf3 initialization part 2 - body of AppendStructuredBuffer<float> default C1 constructor that calls
88+
// the default C2 constructor
8089
// CHECK: define linkonce_odr hidden void @hlsl::StructuredBuffer<float>::StructuredBuffer()(ptr {{.*}} %this)
8190
// CHECK: call void @hlsl::StructuredBuffer<float>::StructuredBuffer()(ptr {{.*}} %this1)
8291

0 commit comments

Comments
 (0)