Skip to content

Commit e5183a0

Browse files
authored
Set the layout rule explicitly for raw buffer operations (microsoft#6701)
The first first fix in microsoft#5392 was not correct. It relied on the layout rule for the address to be the correct layout rule, but that is not always the case. The address is just an integer that could exist in any storage class. The correct solution is to explicitly set the layout rule for the BitCast operation when expanding the RawBuffer* functions. We know that the result of the BitCast is a pointer to the physical storage buffer storage class, so we know the layout need to be the storage buffer layout. Fixes microsoft#6554
1 parent 74205f8 commit e5183a0

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

tools/clang/lib/SPIRV/SpirvEmitter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14263,6 +14263,7 @@ SpirvEmitter::loadDataFromRawAddress(SpirvInstruction *addressInUInt64,
1426314263
SpirvUnaryOp *address = spvBuilder.createUnaryOp(
1426414264
spv::Op::OpBitcast, bufferPtrType, addressInUInt64, loc);
1426514265
address->setStorageClass(spv::StorageClass::PhysicalStorageBuffer);
14266+
address->setLayoutRule(spirvOptions.sBufferLayoutRule);
1426614267

1426714268
SpirvLoad *loadInst =
1426814269
dyn_cast<SpirvLoad>(spvBuilder.createLoad(bufferType, address, loc));
@@ -14291,6 +14292,7 @@ SpirvEmitter::storeDataToRawAddress(SpirvInstruction *addressInUInt64,
1429114292
if (!address)
1429214293
return nullptr;
1429314294
address->setStorageClass(spv::StorageClass::PhysicalStorageBuffer);
14295+
address->setLayoutRule(spirvOptions.sBufferLayoutRule);
1429414296

1429514297
// If the source value has a different layout, it is not safe to directly
1429614298
// store it. It needs to be component-wise reconstructed to the new layout.

tools/clang/test/CodeGenSPIRV/intrinsics.vkrawbufferload.hlsl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44
// CHECK: OpExtension "SPV_KHR_physical_storage_buffer"
55
// CHECK: OpMemoryModel PhysicalStorageBuffer64 GLSL450
66

7+
// CHECK: OpMemberDecorate %BufferData_0 0 Offset 0
8+
// CHECK: OpMemberDecorate %BufferData_0 1 Offset 4
9+
// CHECK: %BufferData_0 = OpTypeStruct %float %v3float
10+
struct BufferData {
11+
float f;
12+
float3 v;
13+
};
14+
715
uint64_t Address;
816
float4 main() : SV_Target0 {
917
// CHECK: [[addr:%[0-9]+]] = OpLoad %ulong
@@ -34,5 +42,13 @@ float4 main() : SV_Target0 {
3442
// CHECK-NEXT: OpStore %v [[load_3]]
3543
uint v = vk::RawBufferLoad(Address);
3644

45+
// CHECK: [[buf:%[0-9]+]] = OpBitcast %_ptr_PhysicalStorageBuffer_BufferData_0
46+
// CHECK-NEXT: [[load:%[0-9]+]] = OpLoad %BufferData_0 [[buf]] Aligned 4
47+
BufferData d = vk::RawBufferLoad<BufferData>(Address);
48+
49+
// CHECK: [[buf:%[0-9]+]] = OpBitcast %_ptr_PhysicalStorageBuffer_BufferData_0 %ulong_0
50+
// CHECK-NEXT: [[load:%[0-9]+]] = OpLoad %BufferData_0 [[buf]] Aligned 4
51+
d = vk::RawBufferLoad<BufferData>(0);
52+
3753
return float4(w.x, x, y, z);
3854
}

tools/clang/test/CodeGenSPIRV/intrinsics.vkrawbufferstore.hlsl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,14 @@ void main(uint3 tid : SV_DispatchThreadID) {
7575
xyzw.z = 84;
7676
xyzw.w = 69;
7777
vk::RawBufferStore<XYZW>(Address, xyzw);
78+
79+
// CHECK: [[xyzwval:%[0-9]+]] = OpLoad %XYZW %xyzw
80+
// CHECK-NEXT: [[buf_3:%[0-9]+]] = OpBitcast %_ptr_PhysicalStorageBuffer_XYZW_0 %ulong_0
81+
// CHECK-NEXT: [[member1:%[0-9]+]] = OpCompositeExtract %int [[xyzwval]] 0
82+
// CHECK-NEXT: [[member2:%[0-9]+]] = OpCompositeExtract %int [[xyzwval]] 1
83+
// CHECK-NEXT: [[member3:%[0-9]+]] = OpCompositeExtract %int [[xyzwval]] 2
84+
// CHECK-NEXT: [[member4:%[0-9]+]] = OpCompositeExtract %int [[xyzwval]] 3
85+
// CHECK-NEXT: [[p_xyzwval:%[0-9]+]] = OpCompositeConstruct %XYZW_0 [[member1]] [[member2]] [[member3]] [[member4]]
86+
// CHECK-NEXT: OpStore [[buf_3]] [[p_xyzwval]] Aligned 4
87+
vk::RawBufferStore<XYZW>(0, xyzw);
7888
}

0 commit comments

Comments
 (0)