summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <azakai@google.com>2021-10-11 17:37:23 -0700
committerGitHub <noreply@github.com>2021-10-11 17:37:23 -0700
commitecda340b791be6c0a31c7dd290a682244aff3e89 (patch)
tree93d97df579919bb6fef6e796d949ebd9f763cc6f
parent072c60c9314ca37f0f4a9393bb81dabd1be5a6b5 (diff)
downloadbinaryen-ecda340b791be6c0a31c7dd290a682244aff3e89.tar.gz
binaryen-ecda340b791be6c0a31c7dd290a682244aff3e89.tar.bz2
binaryen-ecda340b791be6c0a31c7dd290a682244aff3e89.zip
Fix tee/as-non-null reordering when writing to a non-nullable param (#4232)
-rw-r--r--src/passes/OptimizeInstructions.cpp6
-rw-r--r--test/lit/passes/optimize-instructions-gc.wast31
2 files changed, 36 insertions, 1 deletions
diff --git a/src/passes/OptimizeInstructions.cpp b/src/passes/OptimizeInstructions.cpp
index 97f1cb756..ed3d474d9 100644
--- a/src/passes/OptimizeInstructions.cpp
+++ b/src/passes/OptimizeInstructions.cpp
@@ -1054,8 +1054,12 @@ struct OptimizeInstructions
// can't remove or move a ref.as_non_null flowing into a local.set/tee, and
// (2) even if the local were nullable, if we change things we might prevent
// the LocalSubtyping pass from turning it into a non-nullable local later.
+ // Note that we must also check if this local is nullable regardless, as a
+ // parameter might be non-nullable even if nullable locals are disallowed
+ // (as that just affects vars, and not params).
if (auto* as = curr->value->dynCast<RefAs>()) {
- if (as->op == RefAsNonNull && !getModule()->features.hasGCNNLocals()) {
+ if (as->op == RefAsNonNull && !getModule()->features.hasGCNNLocals() &&
+ getFunction()->getLocalType(curr->index).isNullable()) {
// (local.tee (ref.as_non_null ..))
// =>
// (ref.as_non_null (local.tee ..))
diff --git a/test/lit/passes/optimize-instructions-gc.wast b/test/lit/passes/optimize-instructions-gc.wast
index 37db14f9e..ab973ffae 100644
--- a/test/lit/passes/optimize-instructions-gc.wast
+++ b/test/lit/passes/optimize-instructions-gc.wast
@@ -1254,6 +1254,37 @@
)
)
)
+
+ ;; CHECK: (func $flip-tee-of-as-non-null-non-nullable (param $x (ref any))
+ ;; CHECK-NEXT: (drop
+ ;; CHECK-NEXT: (local.tee $x
+ ;; CHECK-NEXT: (ref.as_non_null
+ ;; CHECK-NEXT: (ref.null any)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; NOMNL: (func $flip-tee-of-as-non-null-non-nullable (param $x (ref any))
+ ;; NOMNL-NEXT: (drop
+ ;; NOMNL-NEXT: (local.tee $x
+ ;; NOMNL-NEXT: (ref.as_non_null
+ ;; NOMNL-NEXT: (ref.null any)
+ ;; NOMNL-NEXT: )
+ ;; NOMNL-NEXT: )
+ ;; NOMNL-NEXT: )
+ ;; NOMNL-NEXT: )
+ (func $flip-tee-of-as-non-null-non-nullable (param $x (ref any))
+ (drop
+ (local.tee $x
+ ;; this *cannnot* be moved through the tee outward, as the param is in
+ ;; fact non-nullable, and we depend on the ref.as_non_null in order to
+ ;; get a valid type to assign to it
+ (ref.as_non_null
+ (ref.null any)
+ )
+ )
+ )
+ )
;; CHECK: (func $ternary-identical-arms (param $x i32) (param $y (ref null $struct)) (param $z (ref null $struct))
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (ref.is_null