summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <azakai@google.com>2022-08-09 09:49:04 -0700
committerGitHub <noreply@github.com>2022-08-09 09:49:04 -0700
commit5df39e3bbddaefcb0d0c4d1f10412c508b120aca (patch)
tree3b08eb5f119517dec137a077daecc4f2b5348c11
parent94edb07713c607373c6cae088039e9a1802e1164 (diff)
downloadbinaryen-5df39e3bbddaefcb0d0c4d1f10412c508b120aca.tar.gz
binaryen-5df39e3bbddaefcb0d0c4d1f10412c508b120aca.tar.bz2
binaryen-5df39e3bbddaefcb0d0c4d1f10412c508b120aca.zip
RedundantSetElimination: ReFinalize when needed (#4877)
-rw-r--r--src/passes/RedundantSetElimination.cpp21
-rw-r--r--test/lit/passes/rse-gc.wast34
2 files changed, 55 insertions, 0 deletions
diff --git a/src/passes/RedundantSetElimination.cpp b/src/passes/RedundantSetElimination.cpp
index 90925edf2..0b4cad056 100644
--- a/src/passes/RedundantSetElimination.cpp
+++ b/src/passes/RedundantSetElimination.cpp
@@ -67,6 +67,9 @@ struct RedundantSetElimination
Index numLocals;
+ // In rare cases we make a change to a type that requires a refinalize.
+ bool refinalize = false;
+
// cfg traversal work
static void doVisitLocalSet(RedundantSetElimination* self,
@@ -94,6 +97,10 @@ struct RedundantSetElimination
flowValues(func);
// remove redundant sets
optimize();
+
+ if (refinalize) {
+ ReFinalize().walkFunctionInModule(func, this->getModule());
+ }
}
// Use a value numbering for the values of expressions.
@@ -346,6 +353,20 @@ struct RedundantSetElimination
drop->value = value;
drop->finalize();
} else {
+ // If we are replacing the set with something of a more specific type,
+ // then we need to refinalize, for example:
+ //
+ // (struct.get $X 0
+ // (local.tee $x
+ // (..something of type $Y, a subtype of $X..)
+ // )
+ // )
+ //
+ // After the replacement the struct.get will read from $Y, whose field may
+ // have a more refined type.
+ if (value->type != set->type) {
+ refinalize = true;
+ }
*setp = value;
}
}
diff --git a/test/lit/passes/rse-gc.wast b/test/lit/passes/rse-gc.wast
index ddb4aaf87..e96b06a45 100644
--- a/test/lit/passes/rse-gc.wast
+++ b/test/lit/passes/rse-gc.wast
@@ -2,6 +2,15 @@
;; RUN: wasm-opt %s --rse --enable-gc-nn-locals -all -S -o - | filecheck %s
(module
+ ;; CHECK: (type $B (struct (field dataref)))
+
+ ;; CHECK: (type $A (struct (field (ref null data))))
+ (type $A (struct_subtype (field (ref null data)) data))
+
+ ;; $B is a subtype of $A, and its field has a more refined type (it is non-
+ ;; nullable).
+ (type $B (struct_subtype (field (ref data)) $A))
+
;; CHECK: (func $test
;; CHECK-NEXT: (local $single (ref func))
;; CHECK-NEXT: (local $tuple ((ref any) (ref any)))
@@ -15,4 +24,29 @@
;; A non-nullable tuple.
(local $tuple ((ref any) (ref any)))
)
+
+ ;; CHECK: (func $needs-refinalize (param $b (ref $B)) (result anyref)
+ ;; CHECK-NEXT: (local $a (ref null $A))
+ ;; CHECK-NEXT: (local.set $a
+ ;; CHECK-NEXT: (local.get $b)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (struct.get $B 0
+ ;; CHECK-NEXT: (local.get $b)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ (func $needs-refinalize (param $b (ref $B)) (result anyref)
+ (local $a (ref null $A))
+ ;; Make $a contain $b.
+ (local.set $a
+ (local.get $b)
+ )
+ (struct.get $A 0
+ ;; Once more, make $a contain $b. This set is redundant. After removing it,
+ ;; the struct.get will be reading from type $B, which has a more refined
+ ;; field, so we must refinalize to get the right type for the instruction.
+ (local.tee $a
+ (local.get $b)
+ )
+ )
+ )
)