From 1f4b242a9c2baabf6eab2df6449d0c125097f070 Mon Sep 17 00:00:00 2001 From: alan-baker Date: Mon, 4 Mar 2024 17:51:08 -0500 Subject: [PATCH] Tests for scoping diagnostics (#3450) --- src/webgpu/listing_meta.json | 1 + .../validation/parse/diagnostic.spec.ts | 239 ++++++++++++++++++ 2 files changed, 240 insertions(+) diff --git a/src/webgpu/listing_meta.json b/src/webgpu/listing_meta.json index a8ecd45ed3de..d779fa7b05ee 100644 --- a/src/webgpu/listing_meta.json +++ b/src/webgpu/listing_meta.json @@ -1976,6 +1976,7 @@ "webgpu:shader,validation,parse,diagnostic:conflicting_attribute_different_location:*": { "subcaseMS": 2.257 }, "webgpu:shader,validation,parse,diagnostic:conflicting_attribute_same_location:*": { "subcaseMS": 1.400 }, "webgpu:shader,validation,parse,diagnostic:conflicting_directive:*": { "subcaseMS": 1.244 }, + "webgpu:shader,validation,parse,diagnostic:diagnostic_scoping:*": { "subcaseMS": 1.244 }, "webgpu:shader,validation,parse,diagnostic:invalid_locations:*": { "subcaseMS": 1.930 }, "webgpu:shader,validation,parse,diagnostic:invalid_severity:*": { "subcaseMS": 1.361 }, "webgpu:shader,validation,parse,diagnostic:valid_locations:*": { "subcaseMS": 1.368 }, diff --git a/src/webgpu/shader/validation/parse/diagnostic.spec.ts b/src/webgpu/shader/validation/parse/diagnostic.spec.ts index 757cf50cbba4..701ae46f3a0d 100644 --- a/src/webgpu/shader/validation/parse/diagnostic.spec.ts +++ b/src/webgpu/shader/validation/parse/diagnostic.spec.ts @@ -220,3 +220,242 @@ g.test('after_other_directives') code += generateDiagnostic('directive', 'info', 'derivative_uniformity') + ';'; t.expectCompileResult(true, code); }); + +interface ScopeCase { + code: string; + result: boolean | 'warn'; +} + +function scopeCode(body: string): string { + return ` +@group(0) @binding(0) var t : texture_1d; +@group(0) @binding(1) var s : sampler; +var non_uniform_cond : bool; +var non_uniform_coord : f32; +var non_uniform_val : u32; +@fragment fn main() { + ${body} +} +`; +} + +const kScopeCases: Record = { + override_global_off: { + code: ` + ${generateDiagnostic('directive', 'error', 'derivative_uniformity')}; + ${scopeCode(` + ${generateDiagnostic('', 'off', 'derivative_uniformity')} + if non_uniform_cond { + _ = textureSample(t,s,0.0); + }`)}; + `, + result: true, + }, + override_global_on: { + code: ` + ${generateDiagnostic('directive', 'off', 'derivative_uniformity')}; + ${scopeCode(` + ${generateDiagnostic('', 'error', 'derivative_uniformity')} + if non_uniform_cond { + _ = textureSample(t,s,0.0); + }`)} + `, + result: false, + }, + override_global_warn: { + code: ` + ${generateDiagnostic('directive', 'error', 'derivative_uniformity')}; + ${scopeCode(` + ${generateDiagnostic('', 'warning', 'derivative_uniformity')} + if non_uniform_cond { + _ = textureSample(t,s,0.0); + }`)} + `, + result: 'warn', + }, + global_if_nothing_else_warn: { + code: ` + ${generateDiagnostic('directive', 'warning', 'derivative_uniformity')}; + ${scopeCode(` + if non_uniform_cond { + _ = textureSample(t,s,0.0); + }`)} + `, + result: 'warn', + }, + deepest_nesting_warn: { + code: scopeCode(` + ${generateDiagnostic('', 'error', 'derivative_uniformity')} + if non_uniform_cond { + ${generateDiagnostic('', 'warning', 'derivative_uniformity')} + if non_uniform_cond { + _ = textureSample(t,s,0.0); + } + }`), + result: 'warn', + }, + deepest_nesting_off: { + code: scopeCode(` + ${generateDiagnostic('', 'error', 'derivative_uniformity')} + if non_uniform_cond { + ${generateDiagnostic('', 'off', 'derivative_uniformity')} + if non_uniform_cond { + _ = textureSample(t,s,0.0); + } + }`), + result: true, + }, + deepest_nesting_error: { + code: scopeCode(` + ${generateDiagnostic('', 'off', 'derivative_uniformity')} + if non_uniform_cond { + ${generateDiagnostic('', 'error', 'derivative_uniformity')} + if non_uniform_cond { + _ = textureSample(t,s,0.0); + } + }`), + result: false, + }, + other_nest_unaffected: { + code: ` + ${generateDiagnostic('directive', 'warning', 'derivative_uniformity')}; + ${scopeCode(` + ${generateDiagnostic('', 'off', 'derivative_uniformity')} + if non_uniform_cond { + _ = textureSample(t,s,0.0); + } + if non_uniform_cond { + _ = textureSample(t,s,0.0); + }`)} + `, + result: 'warn', + }, + deeper_nest_no_effect: { + code: ` + ${generateDiagnostic('directive', 'error', 'derivative_uniformity')}; + ${scopeCode(` + if non_uniform_cond { + ${generateDiagnostic('', 'off', 'derivative_uniformity')} + if non_uniform_cond { + } + _ = textureSample(t,s,0.0); + }`)} + `, + result: false, + }, + call_unaffected_error: { + code: ` + ${generateDiagnostic('directive', 'error', 'derivative_uniformity')}; + fn foo() { _ = textureSample(t,s,0.0); } + ${scopeCode(` + ${generateDiagnostic('', 'off', 'derivative_uniformity')} + if non_uniform_cond { + foo(); + }`)} + `, + result: false, + }, + call_unaffected_warn: { + code: ` + ${generateDiagnostic('directive', 'warning', 'derivative_uniformity')}; + fn foo() { _ = textureSample(t,s,0.0); } + ${scopeCode(` + ${generateDiagnostic('', 'off', 'derivative_uniformity')} + if non_uniform_cond { + foo(); + }`)} + `, + result: 'warn', + }, + call_unaffected_off: { + code: ` + ${generateDiagnostic('directive', 'off', 'derivative_uniformity')}; + fn foo() { _ = textureSample(t,s,0.0); } + ${scopeCode(` + ${generateDiagnostic('', 'error', 'derivative_uniformity')} + if non_uniform_cond { + foo(); + }`)} + `, + result: true, + }, + if_condition_error: { + code: scopeCode(` + if (non_uniform_cond) { + ${generateDiagnostic('', 'error', 'derivative_uniformity')} + if textureSample(t,s,non_uniform_coord).x > 0.0 + ${generateDiagnostic('', 'off', 'derivative_uniformity')} { + } + }`), + result: false, + }, + if_condition_warn: { + code: scopeCode(` + if non_uniform_cond { + ${generateDiagnostic('', 'warning', 'derivative_uniformity')} + if textureSample(t,s,non_uniform_coord).x > 0.0 + ${generateDiagnostic('', 'error', 'derivative_uniformity')} { + } + }`), + result: 'warn', + }, + if_condition_off: { + code: scopeCode(` + if non_uniform_cond { + ${generateDiagnostic('', 'off', 'derivative_uniformity')} + if textureSample(t,s,non_uniform_coord).x > 0.0 + ${generateDiagnostic('', 'error', 'derivative_uniformity')} { + } + }`), + result: true, + }, + switch_error: { + code: scopeCode(` + ${generateDiagnostic('', 'error', 'derivative_uniformity')} + switch non_uniform_val { + case 0 ${generateDiagnostic('', 'off', 'derivative_uniformity')} { + } + default { + _ = textureSample(t,s,0.0); + } + }`), + result: false, + }, + switch_warn: { + code: scopeCode(` + ${generateDiagnostic('', 'warning', 'derivative_uniformity')} + switch non_uniform_val { + case 0 ${generateDiagnostic('', 'off', 'derivative_uniformity')} { + } + default { + _ = textureSample(t,s,0.0); + } + }`), + result: 'warn', + }, + switch_off: { + code: scopeCode(` + ${generateDiagnostic('', 'off', 'derivative_uniformity')} + switch non_uniform_val { + case 0 ${generateDiagnostic('', 'error', 'derivative_uniformity')}{ + } + default { + _ = textureSample(t,s,0.0); + } + }`), + result: true, + }, +}; + +g.test('diagnostic_scoping') + .specURL('https://gpuweb.github.io/gpuweb/wgsl/#diagnostics') + .desc('Tests that innermost scope controls the diagnostic') + .params(u => u.combine('case', keysOf(kScopeCases))) + .fn(t => { + const testcase = kScopeCases[t.params.case]; + if (testcase.result === 'warn') { + t.expectCompileWarning(true, testcase.code); + } else { + t.expectCompileResult(testcase.result as boolean, testcase.code); + } + });