Skip to content

Commit

Permalink
[SPIRV] Round the stride to be a multiple of the alignment. (#6967)
Browse files Browse the repository at this point in the history
We currently pick the size of a struct to be the stride of the array
elements when doing scalar layout. This is not correct because this
could cause the struct to not be correctly aligned.

This is fixed by rounding the size of the struct up to a mutliple of the
alignment.

Fixes #6947
  • Loading branch information
s-perron authored Oct 21, 2024
1 parent 16e6727 commit c72be81
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
11 changes: 10 additions & 1 deletion tools/clang/lib/SPIRV/AlignmentSizeCalculator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ inline uint32_t roundToPow2(uint32_t val, uint32_t pow2) {
return (val + pow2 - 1) & ~(pow2 - 1);
}

/// Returns the smallest value greater than or equal to |val| that is a multiple
/// of |multiple|.
inline uint32_t roundToMultiple(uint32_t val, uint32_t multiple) {
if (val == 0)
return 0;
uint32_t t = (val - 1) / multiple;
return (multiple * (t + 1));
}

/// Returns true if the given vector type (of the given size) crosses the
/// 4-component vector boundary if placed at the given offset.
bool improperStraddle(clang::QualType type, int size, int offset) {
Expand Down Expand Up @@ -411,7 +420,7 @@ std::pair<uint32_t, uint32_t> AlignmentSizeCalculator::getAlignmentAndSize(

if (rule == SpirvLayoutRule::FxcSBuffer ||
rule == SpirvLayoutRule::Scalar) {
*stride = size;
*stride = roundToMultiple(size, alignment);
// Use element alignment for fxc structured buffers and
// VK_EXT_scalar_block_layout
return {alignment, size * elemCount};
Expand Down
21 changes: 21 additions & 0 deletions tools/clang/test/CodeGenSPIRV/array.scalar.layout.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// RUN: %dxc -T cs_6_2 -E main %s -fvk-use-scalar-layout -spirv | FileCheck %s

// Check that the array stride and offsets are corrects. The uint64_t has alignment
// 8 and the struct has size 12. So the stride should be the smallest multiple of 8
// greater than or equal to 12, which is 16.

// CHECK-DAG: OpMemberDecorate %Data 0 Offset 0
// CHECK-DAG: OpMemberDecorate %Data 1 Offset 8
// CHECK-DAG: OpDecorate %_runtimearr_Data ArrayStride 16
// CHECK-DAG: OpMemberDecorate %type_RWStructuredBuffer_Data 0 Offset 0
struct Data {
uint64_t y;
uint x;
};
RWStructuredBuffer<Data> buffer;

[numthreads(1, 1, 1)]
void main()
{
buffer[0].x = 5;
}

0 comments on commit c72be81

Please sign in to comment.