Skip to content

Commit

Permalink
generateTypes: Avoid generating test case with invalid alignment (gpu…
Browse files Browse the repository at this point in the history
…web#3870)

This is the last step in the code cleanup for adding f16
to generateTypes

Fixed: gpuweb#3405
  • Loading branch information
dneto0 authored Jul 22, 2024
1 parent 50b6e7a commit 198d177
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
7 changes: 0 additions & 7 deletions src/webgpu/shader/execution/robust_access.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,6 @@ g.test('linear_memory')
assert(_kTypeInfo !== undefined, 'not an indexable type');
assert('arrayLength' in _kTypeInfo);

if (baseType === 'f16' && addressSpace === 'uniform' && containerType === 'array') {
// Array elements must be aligned to 16 bytes, but the logic in generateTypes
// creates an array of vec4 of the baseType. But for f16 that's only 8 bytes.
// We would need to write more complex logic for that.
t.skip('Test logic does not handle array of f16 in the uniform address space');
}

let usesCanary = false;
let globalSource = '';
let testFunctionSource = '';
Expand Down
17 changes: 11 additions & 6 deletions src/webgpu/shader/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ export function* generateTypes({
let supportsAtomics = scalarInfo.supportsAtomics;
let layout: undefined | AlignmentAndSize = undefined;
let accessSuffixes: undefined | string[] = undefined;
let validLayoutForAddressSpace = true;
if (scalarInfo.layout) {
// Compute the layout of the array type.
// Adjust the array element count or element type as needed.
Expand All @@ -326,7 +327,9 @@ export function* generateTypes({
supportsAtomics = false;
accessSuffixes = ['.x', '.y', '.z', '.w'];
const arrayElemLayout = vectorLayout('vec4', baseType) as AlignmentAndSize;
// assert(arrayElemLayout.alignment % 16 === 0); // Callers responsibility to avoid
// Arrays in uniform address space have to be 16 byte-aligned.
// An array of vec4<f16> is only 8byte aligned.
validLayoutForAddressSpace = arrayElemLayout.alignment % 16 === 0;
arrayElementCount = align(arrayElementCount, 4) / 4;
const arrayByteSize = arrayElementCount * arrayElemLayout.size;
layout = { alignment: arrayElemLayout.alignment, size: arrayByteSize };
Expand Down Expand Up @@ -354,11 +357,13 @@ export function* generateTypes({
accessSuffixes,
};

// Sized
yield { type: `array<${arrayElemType},${arrayElementCount}>`, _kTypeInfo: arrayTypeInfo };
// Unsized
if (addressSpace === 'storage') {
yield { type: `array<${arrayElemType}>`, _kTypeInfo: arrayTypeInfo };
if (validLayoutForAddressSpace) {
// Sized
yield { type: `array<${arrayElemType},${arrayElementCount}>`, _kTypeInfo: arrayTypeInfo };
// Unsized
if (addressSpace === 'storage') {
yield { type: `array<${arrayElemType}>`, _kTypeInfo: arrayTypeInfo };
}
}
}

Expand Down

0 comments on commit 198d177

Please sign in to comment.