From 3f4d36db0250ff08dc52cc0769bc6e63b88b76c4 Mon Sep 17 00:00:00 2001 From: alan-baker Date: Fri, 22 Mar 2024 18:18:48 -0400 Subject: [PATCH] Remaining validation tests for const declarations (#3546) * Tests for: * scopes * initializers * types * Override test that checks it is disallowed at function scope. --- src/webgpu/listing_meta.json | 4 + .../shader/validation/decl/const.spec.ts | 158 ++++++++++++++++++ .../shader/validation/decl/override.spec.ts | 7 + 3 files changed, 169 insertions(+) diff --git a/src/webgpu/listing_meta.json b/src/webgpu/listing_meta.json index a04228611d62..00df55474105 100644 --- a/src/webgpu/listing_meta.json +++ b/src/webgpu/listing_meta.json @@ -1823,10 +1823,14 @@ "webgpu:shader,validation,const_assert,const_assert:evaluation_stage:*": { "subcaseMS": 3.367 }, "webgpu:shader,validation,decl,compound_statement:decl_conflict:*": { "subcaseMS": 5.225 }, "webgpu:shader,validation,decl,compound_statement:decl_use:*": { "subcaseMS": 0.625 }, + "webgpu:shader,validation,decl,const:function_scope:*": { "subcaseMS": 2.088 }, + "webgpu:shader,validation,decl,const:initializer:*": { "subcaseMS": 0.768 }, "webgpu:shader,validation,decl,const:no_direct_recursion:*": { "subcaseMS": 0.951 }, "webgpu:shader,validation,decl,const:no_indirect_recursion:*": { "subcaseMS": 0.950 }, "webgpu:shader,validation,decl,const:no_indirect_recursion_via_array_size:*": { "subcaseMS": 2.601 }, "webgpu:shader,validation,decl,const:no_indirect_recursion_via_struct_attribute:*": { "subcaseMS": 1.034 }, + "webgpu:shader,validation,decl,const:type:*": { "subcaseMS": 10.651 }, + "webgpu:shader,validation,decl,override:function_scope:*": { "subcaseMS": 1.003 }, "webgpu:shader,validation,decl,override:id:*": { "subcaseMS": 69.432 }, "webgpu:shader,validation,decl,override:initializer:*": { "subcaseMS": 4.810 }, "webgpu:shader,validation,decl,override:no_direct_recursion:*": { "subcaseMS": 1.000 }, diff --git a/src/webgpu/shader/validation/decl/const.spec.ts b/src/webgpu/shader/validation/decl/const.spec.ts index 6ded2480c739..38ac76fa045c 100644 --- a/src/webgpu/shader/validation/decl/const.spec.ts +++ b/src/webgpu/shader/validation/decl/const.spec.ts @@ -3,6 +3,7 @@ Validation tests for const declarations `; import { makeTestGroup } from '../../../../common/framework/test_group.js'; +import { keysOf } from '../../../../common/util/data_tables.js'; import { ShaderValidationTest } from '../shader_validation_test.js'; export const g = makeTestGroup(ShaderValidationTest); @@ -59,3 +60,160 @@ const b = S(4).a; `; t.expectCompileResult(t.params.target === 'a', wgsl); }); + +const kTypeCases = { + bool: { + code: `const x : bool = true;`, + valid: true, + }, + i32: { + code: `const x : i32 = 1i;`, + valid: true, + }, + u32: { + code: `const x : u32 = 1u;`, + valid: true, + }, + f32: { + code: `const x : f32 = 1f;`, + valid: true, + }, + f16: { + code: `enable f16;\nconst x : f16 = 1h;`, + valid: true, + }, + abstract_int: { + code: ` + const x = 0xffffffffff; + const_assert x == 0xffffffffff;`, + valid: true, + }, + abstract_float: { + code: ` + const x = 3937509.87755102; + const_assert x != 3937510.0; + const_assert x != 3937509.75;`, + valid: true, + }, + vec2i: { + code: `const x : vec2i = vec2i();`, + valid: true, + }, + vec3u: { + code: `const x : vec3u = vec3u();`, + valid: true, + }, + vec4f: { + code: `const x : vec4f = vec4f();`, + valid: true, + }, + mat2x2: { + code: `const x : mat2x2f = mat2x2f();`, + valid: true, + }, + mat4x3f: { + code: `const x : mat4x3 = mat4x3();`, + valid: true, + }, + array_sized: { + code: `const x : array = array(1,2,3,4);`, + valid: true, + }, + array_runtime: { + code: `const x : array = array(1,2,3);`, + valid: false, + }, + struct: { + code: `struct S { x : u32 }\nconst x : S = S(0);`, + valid: true, + }, + atomic: { + code: `const x : atomic = 0;`, + valid: false, + }, + vec_abstract_int: { + code: ` + const x = vec2(0xffffffffff,0xfffffffff0); + const_assert x.x == 0xffffffffff; + const_assert x.y == 0xfffffffff0;`, + valid: true, + }, + array_abstract_int: { + code: ` + const x = array(0xffffffffff,0xfffffffff0); + const_assert x[0] == 0xffffffffff; + const_assert x[1] == 0xfffffffff0;`, + valid: true, + }, +}; + +g.test('type') + .desc('Test const types') + .params(u => u.combine('case', keysOf(kTypeCases))) + .beforeAllSubcases(t => { + if (t.params.case === 'f16') { + t.selectDeviceOrSkipTestCase('shader-f16'); + } + }) + .fn(t => { + const testcase = kTypeCases[t.params.case]; + const code = testcase.code; + const expect = testcase.valid; + t.expectCompileResult(expect, code); + }); + +const kInitCases = { + no_init: { + code: `const x : u32;`, + valid: false, + }, + no_type: { + code: `const x = 0;`, + valid: true, + }, + init_matching_type: { + code: `const x : i32 = 1i;`, + valid: true, + }, + init_mismatch_type: { + code: `const x : u32 = 1i;`, + valid: false, + }, + abs_int_init_convert: { + code: `const x : u32 = 1;`, + valid: true, + }, + abs_float_init_convert: { + code: `const x : f32 = 1.0;`, + valid: true, + }, + init_const_expr: { + code: `const x = 0;\nconst y = x + 2;`, + valid: true, + }, + init_override_expr: { + code: `override x : u32;\nconst y = x * 2;`, + valid: false, + }, + init_runtime_expr: { + code: `var x = 1i;\nconst y = x - 1;`, + valid: false, + }, +}; + +g.test('initializer') + .desc('Test const initializers') + .params(u => u.combine('case', keysOf(kInitCases))) + .fn(t => { + const testcase = kInitCases[t.params.case]; + const code = testcase.code; + const expect = testcase.valid; + t.expectCompileResult(expect, code); + }); + +g.test('function_scope') + .desc('Test that const declarations are allowed in functions') + .fn(t => { + const code = `fn foo() { const x = 0; }`; + t.expectCompileResult(true, code); + }); diff --git a/src/webgpu/shader/validation/decl/override.spec.ts b/src/webgpu/shader/validation/decl/override.spec.ts index 66d1c092a32f..41dd1391b27a 100644 --- a/src/webgpu/shader/validation/decl/override.spec.ts +++ b/src/webgpu/shader/validation/decl/override.spec.ts @@ -200,3 +200,10 @@ g.test('initializer') const expect = testcase.valid; t.expectCompileResult(expect, code); }); + +g.test('function_scope') + .desc('Test that override declarations are disallowed in functions') + .fn(t => { + const code = `fn foo() { override x : u32; }`; + t.expectCompileResult(false, code); + });