Skip to content

Commit d2bf202

Browse files
authored
Remove incorrect assertion in IRBuilder (#8177)
IRBuilder has to handle the case where a branching instruction like br_on_null sends a value to its target label, even though we cannot directly represent such a value being sent in our IR. To do so, it adds an extra trampoline label that fetches the sent value out of scratch locals before branching to the original target. We previously had an assertion that the scratch local used to hold such extra values had the same type as the expression with the destination label. This is usually true, but it is possible that the destination label is on an If with an unreachable condition, in which case the destination has type unreachable and the scratch local has the original concrete label type. Remove the incorrect assertion. Fixes #8096.
1 parent 63bf811 commit d2bf202

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

src/wasm/wasm-ir-builder.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1166,7 +1166,6 @@ IRBuilder::fixExtraOutput(ScopeCtx& scope, Name label, Expression* curr) {
11661166

11671167
// If all the received values are in the scratch local, just fetch them out.
11681168
if (receivedType == Type::none) {
1169-
assert(extraType == labelType);
11701169
curr = builder.makeSequence(
11711170
curr, builder.makeLocalGet(extraLocal, extraType), extraType);
11721171
continue;

test/lit/wat-kitchen-sink.wast

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5139,7 +5139,7 @@
51395139
)
51405140

51415141
;; The if is unreachable except through the break; make sure this is
5142-
;; parse correctly
5142+
;; parsed correctly
51435143
;; CHECK: (func $if-else-br-return (type $27) (param $a i32) (result i32)
51445144
;; CHECK-NEXT: (block $label
51455145
;; CHECK-NEXT: (if
@@ -5168,6 +5168,66 @@
51685168
(i32.const 1)
51695169
)
51705170

5171+
;; CHECK: (func $if-else-unreachable-br-extra (type $1) (result i32)
5172+
;; CHECK-NEXT: (local $scratch nullref)
5173+
;; CHECK-NEXT: (local $scratch_1 i32)
5174+
;; CHECK-NEXT: (local $scratch_2 i32)
5175+
;; CHECK-NEXT: (local $scratch_3 (ref none))
5176+
;; CHECK-NEXT: (local $scratch_4 i32)
5177+
;; CHECK-NEXT: (block $l (result i32)
5178+
;; CHECK-NEXT: (block $l0
5179+
;; CHECK-NEXT: (br $l
5180+
;; CHECK-NEXT: (if (result i32)
5181+
;; CHECK-NEXT: (unreachable)
5182+
;; CHECK-NEXT: (then
5183+
;; CHECK-NEXT: (local.set $scratch_1
5184+
;; CHECK-NEXT: (block (result i32)
5185+
;; CHECK-NEXT: (local.set $scratch_2
5186+
;; CHECK-NEXT: (i32.const 42)
5187+
;; CHECK-NEXT: )
5188+
;; CHECK-NEXT: (local.set $scratch
5189+
;; CHECK-NEXT: (ref.null none)
5190+
;; CHECK-NEXT: )
5191+
;; CHECK-NEXT: (local.get $scratch_2)
5192+
;; CHECK-NEXT: )
5193+
;; CHECK-NEXT: )
5194+
;; CHECK-NEXT: (local.set $scratch_3
5195+
;; CHECK-NEXT: (br_on_null $l0
5196+
;; CHECK-NEXT: (local.get $scratch)
5197+
;; CHECK-NEXT: )
5198+
;; CHECK-NEXT: )
5199+
;; CHECK-NEXT: (local.set $scratch_4
5200+
;; CHECK-NEXT: (local.get $scratch_1)
5201+
;; CHECK-NEXT: )
5202+
;; CHECK-NEXT: (drop
5203+
;; CHECK-NEXT: (local.get $scratch_3)
5204+
;; CHECK-NEXT: )
5205+
;; CHECK-NEXT: (local.get $scratch_4)
5206+
;; CHECK-NEXT: )
5207+
;; CHECK-NEXT: (else
5208+
;; CHECK-NEXT: (i32.const 1337)
5209+
;; CHECK-NEXT: )
5210+
;; CHECK-NEXT: )
5211+
;; CHECK-NEXT: )
5212+
;; CHECK-NEXT: )
5213+
;; CHECK-NEXT: (local.get $scratch_1)
5214+
;; CHECK-NEXT: )
5215+
;; CHECK-NEXT: )
5216+
(func $if-else-unreachable-br-extra (result i32)
5217+
;; The condition is unreachable, making the if unreachable.
5218+
unreachable
5219+
if $l (result i32)
5220+
;; Send an extra i32. We should not get confused because the if is
5221+
;; unreachable.
5222+
i32.const 42
5223+
ref.null none
5224+
br_on_null $l
5225+
drop
5226+
else $l
5227+
i32.const 1337
5228+
end $l
5229+
)
5230+
51715231
;; CHECK: (func $try-br-catch-all-return (type $0)
51725232
;; CHECK-NEXT: (block $label
51735233
;; CHECK-NEXT: (try

0 commit comments

Comments
 (0)