Skip to content

Commit

Permalink
Adds validation tests for count* builtin functions. (#3498)
Browse files Browse the repository at this point in the history
* Adds validation tests for count* builtin functions.

* Address comments

* Add listing_meta.json updates

* Address comments and update tests
  • Loading branch information
lokokung authored Apr 3, 2024
1 parent 8216d76 commit a2b7c58
Show file tree
Hide file tree
Showing 4 changed files with 606 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/webgpu/listing_meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -1945,6 +1945,18 @@
"webgpu:shader,validation,expression,call,builtin,cos:values:*": { "subcaseMS": 0.338 },
"webgpu:shader,validation,expression,call,builtin,cosh:integer_argument:*": { "subcaseMS": 0.889 },
"webgpu:shader,validation,expression,call,builtin,cosh:values:*": { "subcaseMS": 0.272 },
"webgpu:shader,validation,expression,call,builtin,countLeadingZeros:arguments:*": { "subcaseMS": 77.173 },
"webgpu:shader,validation,expression,call,builtin,countLeadingZeros:float_argument:*": { "subcaseMS": 64.191 },
"webgpu:shader,validation,expression,call,builtin,countLeadingZeros:must_use:*": { "subcaseMS": 4.120 },
"webgpu:shader,validation,expression,call,builtin,countLeadingZeros:values:*": { "subcaseMS": 3153.457 },
"webgpu:shader,validation,expression,call,builtin,countOneBits:arguments:*": { "subcaseMS": 66.449 },
"webgpu:shader,validation,expression,call,builtin,countOneBits:float_argument:*": { "subcaseMS": 44.219 },
"webgpu:shader,validation,expression,call,builtin,countOneBits:must_use:*": { "subcaseMS": 3.284 },
"webgpu:shader,validation,expression,call,builtin,countOneBits:values:*": { "subcaseMS": 3771.859 },
"webgpu:shader,validation,expression,call,builtin,countTrailingZeros:arguments:*": { "subcaseMS": 70.424 },
"webgpu:shader,validation,expression,call,builtin,countTrailingZeros:float_argument:*": { "subcaseMS": 46.181 },
"webgpu:shader,validation,expression,call,builtin,countTrailingZeros:must_use:*": { "subcaseMS": 3.934 },
"webgpu:shader,validation,expression,call,builtin,countTrailingZeros:values:*": { "subcaseMS": 3125.847 },
"webgpu:shader,validation,expression,call,builtin,cross:args:*": { "subcaseMS": 1.000 },
"webgpu:shader,validation,expression,call,builtin,cross:must_use:*": { "subcaseMS": 1.000 },
"webgpu:shader,validation,expression,call,builtin,cross:values:*": { "subcaseMS": 1.000 },
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
const builtin = 'countLeadingZeros';
export const description = `
Validation tests for the ${builtin}() builtin.
`;

import { makeTestGroup } from '../../../../../../common/framework/test_group.js';
import { keysOf, objectsToRecord } from '../../../../../../common/util/data_tables.js';
import {
Type,
kConcreteIntegerScalarsAndVectors,
kFloatScalarsAndVectors,
} from '../../../../../util/conversion.js';
import { ShaderValidationTest } from '../../../shader_validation_test.js';

import {
fullRangeForType,
kConstantAndOverrideStages,
stageSupportsType,
validateConstOrOverrideBuiltinEval,
} from './const_override_validation.js';

export const g = makeTestGroup(ShaderValidationTest);

const kValuesTypes = objectsToRecord(kConcreteIntegerScalarsAndVectors);

g.test('values')
.desc(
`
Validates that constant evaluation and override evaluation of ${builtin}() never errors
`
)
.params(u =>
u
.combine('stage', kConstantAndOverrideStages)
.combine('type', keysOf(kValuesTypes))
.filter(u => stageSupportsType(u.stage, kValuesTypes[u.type]))
.beginSubcases()
.expand('value', u => fullRangeForType(kValuesTypes[u.type]))
)
.fn(t => {
const expectedResult = true; // countLeadingZeros() should never error
validateConstOrOverrideBuiltinEval(
t,
builtin,
expectedResult,
[kValuesTypes[t.params.type].create(t.params.value)],
t.params.stage
);
});

// u32 is included here to confirm that validation is failing due to a type issue and not something else.
const kFloatTypes = objectsToRecord([Type.u32, ...kFloatScalarsAndVectors]);

g.test('float_argument')
.desc(
`
Validates that float arguments are rejected by ${builtin}()
`
)
.params(u => u.combine('type', keysOf(kFloatTypes)))
.fn(t => {
const type = kFloatTypes[t.params.type];
validateConstOrOverrideBuiltinEval(
t,
builtin,
/* expectedResult */ type === Type.u32,
[type.create(0)],
'constant'
);
});

const kTests: {
readonly [name: string]: {
/** Arguments to pass to the builtin with parentheses. */
readonly args: string;
/** Should the test case pass. */
readonly pass: boolean;
/** Additional setup code in the function scope. */
readonly preamble?: string;
};
} = {
valid: {
args: '(1u)',
pass: true,
},
// Number of arguments.
no_parens: {
args: '',
pass: false,
},
too_few_args: {
args: '()',
pass: false,
},
too_many_args: {
args: '(1u,2u)',
pass: false,
},
// Arguments types (only 1 argument for this builtin).
alias: {
args: '(u32_alias(1))',
pass: true,
},
bool: {
args: '(false)',
pass: false,
},
vec_bool: {
args: '(vec2<bool>(false,true))',
pass: false,
},
matrix: {
args: '(mat2x2(1,1,1,1))',
pass: false,
},
atomic: {
args: '(a)',
pass: false,
},
array: {
preamble: 'var arry: array<u32, 5>;',
args: '(arry)',
pass: false,
},
array_runtime: {
args: '(k.arry)',
pass: false,
},
struct: {
preamble: 'var x: A;',
args: '(x)',
pass: false,
},
enumerant: {
args: '(read_write)',
pass: false,
},
ptr: {
preamble: `var<function> f = 1u;
let p: ptr<function, u32> = &f;`,
args: '(p)',
pass: false,
},
ptr_deref: {
preamble: `var<function> f = 1u;
let p: ptr<function, u32> = &f;`,
args: '(*p)',
pass: true,
},
sampler: {
args: '(s)',
pass: false,
},
texture: {
args: '(t)',
pass: false,
},
};

g.test('arguments')
.desc(`Test compilation validation of ${builtin} with variously shaped and typed arguments`)
.params(u => u.combine('test', keysOf(kTests)))
.fn(t => {
const test = kTests[t.params.test];
t.expectCompileResult(
test.pass,
`alias u32_alias = u32;
@group(0) @binding(0) var s: sampler;
@group(0) @binding(1) var t: texture_2d<f32>;
var<workgroup> a: atomic<u32>;
struct A {
i: u32,
}
struct B {
arry: array<u32>,
}
@group(0) @binding(3) var<storage> k: B;
@vertex
fn main() -> @builtin(position) vec4<f32> {
${test.preamble ? test.preamble : ''}
_ = ${builtin}${test.args};
return vec4<f32>(.4, .2, .3, .1);
}`
);
});

g.test('must_use')
.desc(`Result of ${builtin} must be used`)
.params(u => u.combine('use', [true, false]))
.fn(t => {
const use_it = t.params.use ? '_ = ' : '';
t.expectCompileResult(t.params.use, `fn f() { ${use_it}${builtin}(1u); }`);
});
Loading

0 comments on commit a2b7c58

Please sign in to comment.