Skip to content

Commit

Permalink
[SPIRV] Round the stride to be a multiple of the alignment.
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 committed Oct 15, 2024
1 parent 26d7dd9 commit 2b6df32
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion tools/clang/lib/SPIRV/AlignmentSizeCalculator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ 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 +419,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

0 comments on commit 2b6df32

Please sign in to comment.