Skip to content

Commit f08e213

Browse files
committed
code review feedback, add test for dynamic indexing, typedef and identical index
1 parent 8690223 commit f08e213

File tree

4 files changed

+47
-13
lines changed

4 files changed

+47
-13
lines changed

clang/lib/CodeGen/CGHLSLRuntime.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,16 @@ static int getTotalArraySize(const clang::Type *Ty) {
113113
if (Ty->isIncompleteArrayType())
114114
return -1;
115115
int Size = 1;
116-
while (const auto *CAT = dyn_cast<ConstantArrayType>(Ty)) {
116+
while (const auto *CAT =
117+
dyn_cast<ConstantArrayType>(Ty->getUnqualifiedDesugaredType())) {
117118
Size *= CAT->getSExtSize();
118119
Ty = CAT->getArrayElementTypeNoTypeQual();
119120
}
120121
return Size;
121122
}
122123

123124
// Find constructor decl for a specific resource record type and binding
124-
// (implicit vs. explicit). The constructor has 6 parameters.
125+
// (implicit vs. explicit). The constructor has 5 parameters.
125126
// For explicit binding the signature is:
126127
// void(unsigned, unsigned, int, unsigned, const char *).
127128
// For implicit binding the signature is:
@@ -171,7 +172,7 @@ static void createResourceCtorArgs(CodeGenModule &CGM, CXXConstructorDecl *CD,
171172
if (VkBinding) {
172173
RegisterSlot = VkBinding->getBinding();
173174
SpaceNo = VkBinding->getSet();
174-
} else if (RBA) {
175+
} else {
175176
if (RBA->hasRegisterSlot())
176177
RegisterSlot = RBA->getSlotNumber();
177178
SpaceNo = RBA->getSpaceNumber();
@@ -815,7 +816,7 @@ std::optional<LValue> CGHLSLRuntime::emitResourceArraySubscriptExpr(
815816
ArraySubsExpr->getType()->isHLSLResourceRecordArray() &&
816817
"expected resource array subscript expression");
817818

818-
// let clang codegen handle local resource array subscrips
819+
// let clang codegen handle local resource array subscripts
819820
const VarDecl *ArrayDecl = dyn_cast<VarDecl>(getArrayDecl(ArraySubsExpr));
820821
if (!ArrayDecl || !ArrayDecl->hasGlobalStorage())
821822
return std::nullopt;
@@ -824,12 +825,10 @@ std::optional<LValue> CGHLSLRuntime::emitResourceArraySubscriptExpr(
824825
assert(!ArraySubsExpr->getType()->isArrayType() &&
825826
"indexing of array subsets it not supported yet");
826827

827-
// get total array size (= range size)
828+
// get the resource array type
828829
const Type *ResArrayTy = ArrayDecl->getType().getTypePtr();
829830
assert(ResArrayTy->isHLSLResourceRecordArray() &&
830831
"expected array of resource classes");
831-
llvm::Value *Range =
832-
llvm::ConstantInt::get(CGM.IntTy, getTotalArraySize(ResArrayTy));
833832

834833
// Iterate through all nested array subscript expressions to calculate
835834
// the index in the flattened resource array (if this is a multi-
@@ -886,6 +885,10 @@ std::optional<LValue> CGHLSLRuntime::emitResourceArraySubscriptExpr(
886885
llvm::Value *ThisPtr = CGF.getAsNaturalPointerTo(
887886
ThisAddress, CD->getThisType()->getPointeeType());
888887

888+
// get total array size (= range size)
889+
llvm::Value *Range =
890+
llvm::ConstantInt::get(CGM.IntTy, getTotalArraySize(ResArrayTy));
891+
889892
// assemble the constructor parameters
890893
CallArgList Args;
891894
createResourceCtorArgs(CGM, CD, ThisPtr, Range, Index, ArrayDecl->getName(),
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.6-compute -finclude-default-header \
2+
// RUN: -emit-llvm -disable-llvm-passes -o - %s | FileCheck %s
3+
4+
// CHECK: @[[BufA:.*]] = private unnamed_addr constant [2 x i8] c"A\00", align 1
5+
6+
RWBuffer<float> A[4][3] : register(u2);
7+
RWStructuredBuffer<float> Out;
8+
9+
// Make sure A[GI.x][GI.y] is translated to a RWBuffer<float> constructor call with range 12 and dynamically calculated index
10+
11+
// CHECK: define internal void @_Z4mainDv3_j(<3 x i32> noundef %GI)
12+
// CHECK: %[[GI_alloca:.*]] = alloca <3 x i32>, align 16
13+
// CHECK: %[[Tmp0:.*]] = alloca %"class.hlsl::RWBuffer
14+
// CHECK: store <3 x i32> %GI, ptr %[[GI_alloca]]
15+
16+
// CHECK: %[[GI:.*]] = load <3 x i32>, ptr %[[GI_alloca]], align 16
17+
// CHECK: %[[GI_y:.*]] = extractelement <3 x i32> %[[GI]], i32 1
18+
// CHECK: %[[GI:.*]] = load <3 x i32>, ptr %[[GI_alloca]], align 16
19+
// CHECK: %[[GI_x:.*]] = extractelement <3 x i32> %[[GI]], i32 0
20+
// CHECK: %[[Tmp1:.*]] = mul i32 %[[GI_x]], 3
21+
// CHECK: %[[Index:.*]] = add i32 %[[GI_y]], %[[Tmp1]]
22+
// CHECK: call void @_ZN4hlsl8RWBufferIfEC1EjjijPKc(ptr {{.*}} %[[Tmp0]], i32 noundef 2, i32 noundef 0, i32 noundef 12, i32 noundef %[[Index]], ptr noundef @A.str)
23+
[numthreads(4,1,1)]
24+
void main(uint3 GI : SV_GroupThreadID) {
25+
Out[0] = A[GI.x][GI.y][0];
26+
}

clang/test/CodeGenHLSL/resources/res-array-global-multi-dim.hlsl

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99

1010
RWBuffer<float> B[4][4] : register(u2);
1111
RWBuffer<int> C[2][2][5] : register(u10, space1);
12-
RWBuffer<uint> D[10][5]; // implicit binding -> u18, space0
12+
13+
typedef RWBuffer<uint> RWBufferArrayTenByFive[10][5]; // test typedef for the resource array type
14+
RWBufferArrayTenByFive D; // implicit binding -> u18, space0
1315

1416
RWStructuredBuffer<float> Out;
1517

@@ -19,14 +21,18 @@ void main() {
1921
// CHECK: %[[Tmp0:.*]] = alloca %"class.hlsl::RWBuffer
2022
// CHECK: %[[Tmp1:.*]] = alloca %"class.hlsl::RWBuffer
2123
// CHECK: %[[Tmp2:.*]] = alloca %"class.hlsl::RWBuffer
24+
// CHECK: %[[Tmp3:.*]] = alloca %"class.hlsl::RWBuffer
2225

23-
// Make sure that B[2][3] is translated to a RWBuffer<float> constructor call for explicit binding (u2, space0) with range 16 and index 14
26+
// Make sure that B[3][2] is translated to a RWBuffer<float> constructor call for explicit binding (u2, space0) with range 16 and index 14
2427
// CHECK: call void @_ZN4hlsl8RWBufferIfEC1EjjijPKc(ptr {{.*}} %[[Tmp0]], i32 noundef 2, i32 noundef 0, i32 noundef 16, i32 noundef 14, ptr noundef @[[BufB]])
2528

2629
// Make sure that C[1][0][3] is translated to a RWBuffer<int> constructor call for explicit binding (u10, space1) with range 20 and index 13
2730
// CHECK: call void @_ZN4hlsl8RWBufferIiEC1EjjijPKc(ptr {{.*}} %[[Tmp1]], i32 noundef 10, i32 noundef 1, i32 noundef 20, i32 noundef 13, ptr noundef @[[BufC]])
2831

2932
// Make sure that D[9][2] is translated to a RWBuffer<uint> constructor call for implicit binding (u18, space0) with range 50 and index 47
3033
// CHECK: call void @_ZN4hlsl8RWBufferIjEC1EjijjPKc(ptr {{.*}} %[[Tmp2]], i32 noundef 0, i32 noundef 50, i32 noundef 47, i32 noundef 0, ptr noundef @[[BufD]])
31-
Out[0] = B[3][2][0] + (float)C[1][0][3][0] + (float)D[9][2][0];
34+
35+
// Make sure that the second B[3][2] is translated to the same a RWBuffer<float> constructor call as the first B[3][2] subscript
36+
// CHECK: call void @_ZN4hlsl8RWBufferIfEC1EjjijPKc(ptr {{.*}} %[[Tmp3]], i32 noundef 2, i32 noundef 0, i32 noundef 16, i32 noundef 14, ptr noundef @[[BufB]])
37+
Out[0] = B[3][2][0] + (float)C[1][0][3][0] + (float)D[9][2][0] + B[3][2][1];
3238
}

clang/test/CodeGenHLSL/resources/res-array-global.hlsl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,8 @@ void main() {
5050
// DXIL: call void @_ZN4hlsl8RWBufferIiEC1EjjijPKc(ptr {{.*}} %[[Tmp2]], i32 noundef 2, i32 noundef 0, i32 noundef 3, i32 noundef 1, ptr noundef @[[BufC]])
5151
// SPV: call void @_ZN4hlsl8RWBufferIiEC1EjjijPKc(ptr {{.*}} %[[Tmp2]], i32 noundef 2, i32 noundef 0, i32 noundef 3, i32 noundef 1, ptr noundef @[[BufC]])
5252

53-
// Make sure D[7] is translated to a RWBuffer<doublet> constructor call with range 10 and index 7
54-
// and DXIL for implicit binding in space0, order id 1
55-
// and SPIR-V explicit binding (binding 13, set 0), order id 0
53+
// Make sure D[7] is translated to a RWBuffer<double> constructor call with implicit binding
54+
// for both DXIL and SPIR-V
5655
// DXIL: call void @_ZN4hlsl8RWBufferIdEC1EjijjPKc(ptr {{.*}} %[[Tmp3]], i32 noundef 0, i32 noundef 10, i32 noundef 7, i32 noundef 1, ptr noundef @[[BufD]])
5756
// SPV: call void @_ZN4hlsl8RWBufferIdEC1EjijjPKc(ptr {{.*}} %[[Tmp3]], i32 noundef 0, i32 noundef 10, i32 noundef 7, i32 noundef 0, ptr noundef @[[BufD]])
5857
Out[0] = A[2][0] + (float)B[3][0] + (float)C[1][0] + (float)D[7][0];

0 commit comments

Comments
 (0)