Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shader validation tests for atomics #3452

Merged
merged 3 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/webgpu/listing_meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -1833,6 +1833,9 @@
"webgpu:shader,validation,expression,call,builtin,atan:values:*": { "subcaseMS": 0.335 },
"webgpu:shader,validation,expression,call,builtin,atanh:integer_argument:*": { "subcaseMS": 0.912 },
"webgpu:shader,validation,expression,call,builtin,atanh:values:*": { "subcaseMS": 0.231 },
"webgpu:shader,validation,expression,call,builtin,atomics:atomic_parameterization:*": { "subcaseMS": 1.346 },
"webgpu:shader,validation,expression,call,builtin,atomics:data_parameters:*": { "subcaseMS": 38.382 },
"webgpu:shader,validation,expression,call,builtin,atomics:return_types:*": { "subcaseMS": 28.021 },
"webgpu:shader,validation,expression,call,builtin,atomics:stage:*": { "subcaseMS": 1.346 },
"webgpu:shader,validation,expression,call,builtin,barriers:no_return_value:*": { "subcaseMS": 1.500 },
"webgpu:shader,validation,expression,call,builtin,barriers:only_in_compute:*": { "subcaseMS": 1.500 },
Expand Down Expand Up @@ -2138,6 +2141,9 @@
"webgpu:shader,validation,types,alias:no_indirect_recursion_via_struct_attribute:*": { "subcaseMS": 1.584 },
"webgpu:shader,validation,types,alias:no_indirect_recursion_via_struct_member:*": { "subcaseMS": 1.000 },
"webgpu:shader,validation,types,alias:no_indirect_recursion_via_vector_element:*": { "subcaseMS": 1.050 },
"webgpu:shader,validation,types,atomics:address_space:*": { "subcaseMS": 1.050 },
"webgpu:shader,validation,types,atomics:invalid_operations:*": { "subcaseMS": 1.050 },
"webgpu:shader,validation,types,atomics:type:*": { "subcaseMS": 1.050 },
"webgpu:shader,validation,types,struct:no_direct_recursion:*": { "subcaseMS": 0.951 },
"webgpu:shader,validation,types,struct:no_indirect_recursion:*": { "subcaseMS": 0.901 },
"webgpu:shader,validation,types,struct:no_indirect_recursion_via_array_element:*": { "subcaseMS": 0.901 },
Expand Down
235 changes: 222 additions & 13 deletions src/webgpu/shader/validation/expression/call/builtin/atomics.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,44 @@ import { ShaderValidationTest } from '../../../shader_validation_test.js';

export const g = makeTestGroup(ShaderValidationTest);

const kAtomicOps = {
add: { src: 'atomicAdd(&a,1)' },
sub: { src: 'atomicSub(&a,1)' },
max: { src: 'atomicMax(&a,1)' },
min: { src: 'atomicMin(&a,1)' },
and: { src: 'atomicAnd(&a,1)' },
or: { src: 'atomicOr(&a,1)' },
xor: { src: 'atomicXor(&a,1)' },
load: { src: 'atomicLoad(&a)' },
store: { src: 'atomicStore(&a,1)' },
exchange: { src: 'atomicExchange(&a,1)' },
compareexchangeweak: { src: 'atomicCompareExchangeWeak(&a,1,1)' },
interface stringToString {
(a: string): string;
}

const kAtomicOps: Record<string, stringToString> = {
add: (a: string): string => {
return `atomicAdd(${a},1)`;
},
sub: (a: string): string => {
return `atomicSub(${a},1)`;
},
max: (a: string): string => {
return `atomicMax(${a},1)`;
},
min: (a: string): string => {
return `atomicMin(${a},1)`;
},
and: (a: string): string => {
return `atomicAnd(${a},1)`;
},
or: (a: string): string => {
return `atomicOr(${a},1)`;
},
xor: (a: string): string => {
return `atomicXor(${a},1)`;
},
load: (a: string): string => {
return `atomicLoad(${a})`;
},
store: (a: string): string => {
return `atomicStore(${a},1)`;
},
exchange: (a: string): string => {
return `atomicExchange(${a},1)`;
},
compareexchangeweak: (a: string): string => {
return `atomicCompareExchangeWeak(${a},1,1)`;
},
};

g.test('stage')
Expand All @@ -35,7 +61,7 @@ Atomic built-in functions must not be used in a vertex shader stage.
.combine('atomicOp', keysOf(kAtomicOps))
)
.fn(t => {
const atomicOp = kAtomicOps[t.params.atomicOp].src;
const atomicOp = kAtomicOps[t.params.atomicOp](`&a`);
let code = `
@group(0) @binding(0) var<storage, read_write> a: atomic<i32>;
`;
Expand Down Expand Up @@ -68,3 +94,186 @@ Atomic built-in functions must not be used in a vertex shader stage.
const pass = t.params.stage !== 'vertex';
t.expectCompileResult(pass, code);
});

function generateAtomicCode(
type: string,
access: string,
aspace: string,
style: string,
op: string
): string {
let moduleVar = ``;
let functionVar = ``;
let param = ``;
let aParam = ``;
if (style === 'var') {
aParam = `&a`;
switch (aspace) {
case 'storage':
moduleVar = `@group(0) @binding(0) var<storage, ${access}> a : atomic<${type}>;\n`;
break;
case 'workgroup':
moduleVar = `var<workgroup> a : atomic<${type}>;\n`;
break;
case 'uniform':
moduleVar = `@group(0) @binding(0) var<uniform> a : atomic<${type}>;\n`;
break;
case 'private':
moduleVar = `var<private> a : atomic<${type}>;\n`;
break;
case 'function':
functionVar = `var a : atomic<${type}>;\n`;
break;
default:
break;
}
} else {
const aspaceParam = aspace === 'storage' ? `, ${access}` : ``;
param = `p : ptr<${aspace}, atomic<${type}>${aspaceParam}>`;
aParam = `p`;
}

return `
${moduleVar}
fn foo(${param}) {
${functionVar}
${kAtomicOps[op](aParam)};
}
`;
}

g.test('atomic_parameterization')
.desc('Tests the valid atomic parameters')
.params(u =>
u
.combine('op', keysOf(kAtomicOps))
.beginSubcases()
.combine('aspace', ['storage', 'workgroup', 'private', 'uniform', 'function'] as const)
.combine('access', ['read', 'read_write'] as const)
.combine('type', ['i32', 'u32'] as const)
.combine('style', ['param', 'var'] as const)
.filter(t => {
switch (t.aspace) {
case 'uniform':
return t.style === 'param' && t.access === 'read';
case 'workgroup':
return t.access === 'read_write';
case 'function':
case 'private':
return t.style === 'param' && t.access === 'read_write';
default:
return true;
}
})
)
.fn(t => {
if (
t.params.style === 'param' &&
!(t.params.aspace === 'function' || t.params.aspace === 'private')
) {
t.skipIfLanguageFeatureNotSupported('unrestricted_pointer_parameters');
}

const aspaceOK = t.params.aspace === 'storage' || t.params.aspace === 'workgroup';
const accessOK = t.params.access === 'read_write';
t.expectCompileResult(
aspaceOK && accessOK,
generateAtomicCode(
t.params.type,
t.params.access,
t.params.aspace,
t.params.style,
t.params.op
)
);
});

g.test('data_parameters')
.desc('Validates that data parameters must match atomic type (or be implicitly convertible)')
.params(u =>
u
.combine('op', [
'atomicStore',
'atomicAdd',
'atomicSub',
'atomicMax',
'atomicMin',
'atomicAnd',
'atomicOr',
'atomicXor',
'atomicExchange',
'atomicCompareExchangeWeak1',
'atomicCompareExchangeWeak2',
] as const)
.beginSubcases()
.combine('atomicType', ['i32', 'u32'] as const)
.combine('dataType', ['i32', 'u32', 'f32', 'AbstractInt'] as const)
)
.fn(t => {
let dataValue = '';
switch (t.params.dataType) {
case 'i32':
dataValue = '1i';
break;
case 'u32':
dataValue = '1u';
break;
case 'f32':
dataValue = '1f';
break;
case 'AbstractInt':
dataValue = '1';
break;
}
let op = '';
switch (t.params.op) {
case 'atomicCompareExchangeWeak1':
op = `atomicCompareExchangeWeak(&a, ${dataValue}, 1)`;
break;
case 'atomicCompareExchangeWeak2':
op = `atomicCompareExchangeWeak(&a, 1, ${dataValue})`;
break;
default:
op = `${t.params.op}(&a, ${dataValue})`;
break;
}
const code = `
var<workgroup> a : atomic<${t.params.atomicType}>;
fn foo() {
${op};
}
`;

const expect = t.params.atomicType === t.params.dataType || t.params.dataType === 'AbstractInt';
t.expectCompileResult(expect, code);
});

g.test('return_types')
.desc('Validates return types of atomics')
.params(u =>
u
.combine('op', keysOf(kAtomicOps))
.beginSubcases()
.combine('atomicType', ['i32', 'u32'] as const)
.combine('returnType', ['i32', 'u32', 'f32'] as const)
)
.fn(t => {
let op = `${kAtomicOps[t.params.op]('&a')}`;
switch (t.params.op) {
case 'compareexchangeweak':
op = `let tmp : ${t.params.returnType} = ${op}.old_value`;
break;
default:
op = `let tmp : ${t.params.returnType} = ${op}`;
break;
}
const code = `
var<workgroup> a : atomic<${t.params.atomicType}>;
fn foo() {
${op};
}
`;

const expect = t.params.atomicType === t.params.returnType && t.params.op !== 'store';
t.expectCompileResult(expect, code);
});
Loading
Loading