Skip to content

Commit 494e318

Browse files
authored
wgsl: test uniformity analysis of break-if condition (#4472)
Use a code gadget designed to carry the uniformity of the break-if condition value into the execution uniformity of the second iteration of the enclosing loop. Fixed: #4471
1 parent 61eae08 commit 494e318

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

src/webgpu/shader/validation/uniformity/uniformity.spec.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,14 @@ function generateOp(op: string): string {
185185
}
186186
}
187187

188-
function generateConditionalStatement(statement: string, condition: string, op: string): string {
188+
const kStatementKinds = ['if', 'for', 'while', 'switch', 'break-if'] as const;
189+
type kStatementType = (typeof kStatementKinds)[number];
190+
191+
function generateConditionalStatement(
192+
statement: kStatementType,
193+
condition: string,
194+
op: string
195+
): string {
189196
const code = ``;
190197
switch (statement) {
191198
case 'if': {
@@ -215,8 +222,19 @@ function generateConditionalStatement(statement: string, condition: string, op:
215222
}
216223
`;
217224
}
218-
default: {
219-
unreachable(`Unhandled statement`);
225+
case 'break-if': {
226+
// The initial 'if' prevents the loop from being infinite. Its condition
227+
// is uniform, to ensure the first iteration of the the body executes
228+
// uniformly. The uniformity of the second iteration depends entirely on
229+
// the uniformity of the break-if condition.
230+
return `loop {
231+
if ${generateCondition('uniform_storage_ro')} { break; }
232+
${generateOp(op)}
233+
continuing {
234+
break if ${generateCondition(condition)};
235+
}
236+
}
237+
`;
220238
}
221239
}
222240

@@ -227,7 +245,7 @@ g.test('basics')
227245
.desc(`Test collective operations in simple uniform or non-uniform control flow.`)
228246
.params(u =>
229247
u
230-
.combine('statement', ['if', 'for', 'while', 'switch'] as const)
248+
.combine('statement', kStatementKinds)
231249
.beginSubcases()
232250
.combineWithParams(kConditions)
233251
.combineWithParams(kCollectiveOps)
@@ -315,7 +333,7 @@ g.test('basics,subgroups')
315333
.desc(`Test subgroup operations in simple uniform or non-uniform control flow.`)
316334
.params(u =>
317335
u
318-
.combine('statement', ['if', 'for', 'while', 'switch'] as const)
336+
.combine('statement', kStatementKinds)
319337
.beginSubcases()
320338
.combineWithParams(kConditions)
321339
.combine('op', kSubgroupOps)

0 commit comments

Comments
 (0)