summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <azakai@google.com>2022-11-21 15:54:21 -0800
committerGitHub <noreply@github.com>2022-11-21 15:54:21 -0800
commitc449f41678d5e1699d634855743f5874ae6ec6e4 (patch)
tree029016b74efbf0608efc9c18c6fcaf94212f590c
parentea0c800b90713edef9566feb30ccb6d276beb857 (diff)
downloadbinaryen-c449f41678d5e1699d634855743f5874ae6ec6e4.tar.gz
binaryen-c449f41678d5e1699d634855743f5874ae6ec6e4.tar.bz2
binaryen-c449f41678d5e1699d634855743f5874ae6ec6e4.zip
Code Pushing: Ignore unreachable sets (#5284)
Normally we ignore them anyhow (unreachability is an effect, either a trap or a control flow switch), but in traps-never-happen mode we can ignore a trap, so we need to check this manually.
-rw-r--r--src/passes/CodePushing.cpp20
-rw-r--r--test/lit/passes/code-pushing_tnh.wast30
2 files changed, 49 insertions, 1 deletions
diff --git a/src/passes/CodePushing.cpp b/src/passes/CodePushing.cpp
index 20f16b145..2b45dda16 100644
--- a/src/passes/CodePushing.cpp
+++ b/src/passes/CodePushing.cpp
@@ -323,6 +323,26 @@ private:
assert(i > 0);
i--;
auto* pushable = isPushable(list[i]);
+ if (pushable && pushable->type == Type::unreachable) {
+ // Don't try to push something unreachable. If we did, then we'd need to
+ // refinalize the block we are moving it from:
+ //
+ // (block $unreachable
+ // (local.set $x (unreachable))
+ // (if (..)
+ // (.. (local.get $x))
+ // )
+ //
+ // The block should not be unreachable if the local.set is moved into
+ // the if arm (the if arm may not execute, so the if itself will not
+ // change type). It is simpler to avoid this complexity and leave this
+ // to DCE to simplify first.
+ //
+ // (Note that the side effect of trapping will normally prevent us from
+ // trying to push something unreachable, but in traps-never-happen mode
+ // we are allowed to ignore that, and so we need this check.)
+ pushable = nullptr;
+ }
if (!pushable) {
// Something that is staying where it is, so anything we push later must
// move past it. Note the effects and continue.
diff --git a/test/lit/passes/code-pushing_tnh.wast b/test/lit/passes/code-pushing_tnh.wast
index 9993c686b..17f69a466 100644
--- a/test/lit/passes/code-pushing_tnh.wast
+++ b/test/lit/passes/code-pushing_tnh.wast
@@ -6,6 +6,8 @@
(module
;; CHECK: (type $i32_i32_=>_none (func (param i32 i32)))
+ ;; CHECK: (type $none_=>_none (func))
+
;; CHECK: (func $div (param $x i32) (param $y i32)
;; CHECK-NEXT: (local $temp i32)
;; CHECK-NEXT: (block $block
@@ -44,5 +46,31 @@
)
)
)
-)
+ ;; CHECK: (func $unreachable-value
+ ;; CHECK-NEXT: (local $x i32)
+ ;; CHECK-NEXT: (local.tee $x
+ ;; CHECK-NEXT: (unreachable)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (if
+ ;; CHECK-NEXT: (i32.const 0)
+ ;; CHECK-NEXT: (drop
+ ;; CHECK-NEXT: (local.get $x)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ (func $unreachable-value
+ (local $x i32)
+ ;; We should not push this into the if. (If we did, we'd need to refinalize
+ ;; the block, or we'd error; instead, leave this to DCE.)
+ (local.set $x
+ (unreachable)
+ )
+ (if
+ (i32.const 0)
+ (drop
+ (local.get $x)
+ )
+ )
+ )
+)