Skip to content

Commit

Permalink
Tests for scoping diagnostics (#3450)
Browse files Browse the repository at this point in the history
  • Loading branch information
alan-baker authored Mar 4, 2024
1 parent ea0efb1 commit 1f4b242
Show file tree
Hide file tree
Showing 2 changed files with 240 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/webgpu/listing_meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down
239 changes: 239 additions & 0 deletions src/webgpu/shader/validation/parse/diagnostic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<f32>;
@group(0) @binding(1) var s : sampler;
var<private> non_uniform_cond : bool;
var<private> non_uniform_coord : f32;
var<private> non_uniform_val : u32;
@fragment fn main() {
${body}
}
`;
}

const kScopeCases: Record<string, ScopeCase> = {
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);
}
});

0 comments on commit 1f4b242

Please sign in to comment.