Skip to content

Commit

Permalink
[wgsl-in] Don't double-initialize variables local to loops.
Browse files Browse the repository at this point in the history
  • Loading branch information
jimblandy committed Oct 12, 2023
1 parent 61d91eb commit 402ed5e
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/front/wgsl/lower/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1181,10 +1181,20 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {

let (const_initializer, initializer) = {
match initializer {
Some(init) if ctx.expression_constness.is_const(init) => {
(Some(init), is_inside_loop.then_some(init))
Some(init) => {
// It's not correct to hoist the initializer up
// to the top of the function if:
// - the initialization is inside a loop, and should
// take place on every iteration, or
// - the initialization is not a constant
// expression, so its value depends on the
// state at the point of initialization.
if is_inside_loop || !ctx.expression_constness.is_const(init) {
(None, Some(init))
} else {
(Some(init), None)
}
}
Some(init) => (None, Some(init)),
None => (None, None),
}
};
Expand Down

0 comments on commit 402ed5e

Please sign in to comment.