Skip to content

Fix invalid flow assumption in 'if' compilation #1244

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2581,11 +2581,7 @@ export class Compiler extends DiagnosticEmitter {
}
elseFlow.freeScopedLocals();
this.currentFlow = flow;
if (elseTerminates && !thenTerminates) {
flow.inherit(thenFlow);
} else {
flow.inheritMutual(thenFlow, elseFlow);
}
flow.inheritMutual(thenFlow, elseFlow);
return module.if(condExpr,
module.flatten(thenStmts),
module.flatten(elseStmts)
Expand Down
30 changes: 30 additions & 0 deletions tests/compiler/while.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -1415,6 +1415,36 @@
call $~lib/builtins/abort
unreachable
end
i32.const 0
global.set $while/ran
i32.const 0
local.set $3
loop $while-continue|067
local.get $3
i32.const 1
i32.add
local.tee $3
i32.const 1
i32.lt_s
br_if $while-continue|067
end
i32.const 1
global.set $while/ran
i32.const 0
global.set $while/ran
i32.const 0
local.set $3
loop $while-continue|08
local.get $3
i32.const 1
i32.add
local.tee $3
i32.const 1
i32.lt_s
br_if $while-continue|08
end
i32.const 1
global.set $while/ran
)
(func $~start
global.get $~started
Expand Down
32 changes: 32 additions & 0 deletions tests/compiler/while.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,35 @@ function testRefAutorelease(): void {
ran = false;
testRefAutorelease();
assert(ran);

function testIfImplicitContinueThen(): void {
var i = 0;
while (true) {
i++;
if (i < 1) {
// continue
} else {
break;
}
}
ran = true;
}
ran = false;
testIfImplicitContinueThen();
assert(ran);

function testIfImplicitContinueElse(): void {
var i = 0;
while (true) {
i++;
if (i >= 1) {
break;
} else {
// continue
}
}
ran = true;
}
ran = false;
testIfImplicitContinueElse();
assert(ran);
86 changes: 86 additions & 0 deletions tests/compiler/while.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -2154,6 +2154,66 @@
local.get $1
call $~lib/rt/pure/__release
)
(func $while/testIfImplicitContinueThen
(local $0 i32)
(local $1 i32)
i32.const 0
local.set $0
block $while-break|0
loop $while-continue|0
i32.const 1
local.set $1
local.get $1
if
local.get $0
i32.const 1
i32.add
local.set $0
local.get $0
i32.const 1
i32.lt_s
if
nop
else
br $while-break|0
end
br $while-continue|0
end
end
end
i32.const 1
global.set $while/ran
)
(func $while/testIfImplicitContinueElse
(local $0 i32)
(local $1 i32)
i32.const 0
local.set $0
block $while-break|0
loop $while-continue|0
i32.const 1
local.set $1
local.get $1
if
local.get $0
i32.const 1
i32.add
local.set $0
local.get $0
i32.const 1
i32.ge_s
if
br $while-break|0
else
nop
end
br $while-continue|0
end
end
end
i32.const 1
global.set $while/ran
)
(func $start:while
i32.const 0
global.set $while/ran
Expand Down Expand Up @@ -2310,6 +2370,32 @@
call $~lib/builtins/abort
unreachable
end
i32.const 0
global.set $while/ran
call $while/testIfImplicitContinueThen
global.get $while/ran
i32.eqz
if
i32.const 0
i32.const 32
i32.const 193
i32.const 1
call $~lib/builtins/abort
unreachable
end
i32.const 0
global.set $while/ran
call $while/testIfImplicitContinueElse
global.get $while/ran
i32.eqz
if
i32.const 0
i32.const 32
i32.const 209
i32.const 1
call $~lib/builtins/abort
unreachable
end
)
(func $~start
global.get $~started
Expand Down