Skip to content

Commit

Permalink
fixup! add an expression constness tracker
Browse files Browse the repository at this point in the history
  • Loading branch information
jimblandy committed Oct 10, 2023
1 parent df4ec39 commit 7449bbf
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
27 changes: 17 additions & 10 deletions src/front/glsl/parser/declarations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,19 +246,26 @@ impl<'source> ParsingContext<'source> {
})
.transpose()?;

let (decl_initializer, late_initializer) = if is_global_const {
(init, None)
let decl_initializer;
let late_initializer;
if is_global_const {
decl_initializer = init;
late_initializer = None;
} else if ctx.external {
let decl_initializer =
decl_initializer =
init.and_then(|expr| ctx.ctx.lift_up_const_expression(expr).ok());
(decl_initializer, None)
late_initializer = None;
} else if let Some(init) = init {
if ctx.is_inside_loop || !ctx.ctx.expression_constness.is_const(init) {
decl_initializer = None;
late_initializer = Some(init);
} else {
decl_initializer = Some(init);
late_initializer = None;
}
} else {
let decl_initializer =
init.filter(|expr| ctx.ctx.expression_constness.is_const(*expr));
let late_initializer = (decl_initializer.is_none() || ctx.is_inside_loop)
.then_some(init)
.flatten();
(decl_initializer, late_initializer)
decl_initializer = None;
late_initializer = None;
};

let pointer = ctx.add_var(frontend, ty, name, decl_initializer, meta)?;
Expand Down
5 changes: 2 additions & 3 deletions tests/out/wgsl/local-var-init-in-loop.comp.wgsl
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
fn main_1() {
var sum: vec4<f32> = vec4(0.0);
var i: i32 = 0;
var a: vec4<f32> = vec4(1.0);
var a: vec4<f32>;

loop {
let _e6 = i;
if !((_e6 < 4)) {
break;
}
{
let _e15 = vec4(1.0);
a = _e15;
a = vec4(1.0);
let _e17 = sum;
let _e18 = a;
sum = (_e17 + _e18);
Expand Down

0 comments on commit 7449bbf

Please sign in to comment.