summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/passes/CoalesceLocals.cpp9
-rw-r--r--src/passes/Inlining.cpp6
-rw-r--r--src/passes/RedundantSetElimination.cpp4
-rw-r--r--test/lit/passes/coalesce-locals-gc-nn.wast84
-rw-r--r--test/lit/passes/rse-gc.wast11
5 files changed, 101 insertions, 13 deletions
diff --git a/src/passes/CoalesceLocals.cpp b/src/passes/CoalesceLocals.cpp
index 42296b5bf..f58e57e72 100644
--- a/src/passes/CoalesceLocals.cpp
+++ b/src/passes/CoalesceLocals.cpp
@@ -209,10 +209,11 @@ void CoalesceLocals::calculateInterferences() {
for (Index i = func->getNumParams(); i < func->getNumLocals(); i++) {
auto type = func->getLocalType(i);
- if (type.isNonNullable()) {
- // A non-nullable value cannot be used anyhow, but we must give it
- // some value. A unique one seems least likely to result in surprise
- // during debugging.
+ if (!LiteralUtils::canMakeZero(type)) {
+ // The default value for a type for which we can't make a zero cannot
+ // be used anyhow, but we must give it some value in this analysis. A
+ // unique one seems least likely to result in surprise during
+ // debugging.
values[i] = valueNumbering.getUniqueValue();
} else {
values[i] = valueNumbering.getValue(Literal::makeZeros(type));
diff --git a/src/passes/Inlining.cpp b/src/passes/Inlining.cpp
index e81649bd2..36610a224 100644
--- a/src/passes/Inlining.cpp
+++ b/src/passes/Inlining.cpp
@@ -343,9 +343,9 @@ doInlining(Module* module, Function* into, const InliningAction& action) {
// zero-init value
for (Index i = 0; i < from->vars.size(); i++) {
auto type = from->vars[i];
- if (type.isNonNullable()) {
- // Non-nullable locals do not need to be zeroed out. They have no zero
- // value, and by definition should not be used before being written to, so
+ if (!LiteralUtils::canMakeZero(type)) {
+ // Non-zeroable locals do not need to be zeroed out. As they have no zero
+ // value they by definition should not be used before being written to, so
// any value we set here would not be observed anyhow.
continue;
}
diff --git a/src/passes/RedundantSetElimination.cpp b/src/passes/RedundantSetElimination.cpp
index 56fca4bee..90925edf2 100644
--- a/src/passes/RedundantSetElimination.cpp
+++ b/src/passes/RedundantSetElimination.cpp
@@ -175,9 +175,9 @@ struct RedundantSetElimination
std::cout << "new param value for " << i << '\n';
#endif
start[i] = getUniqueValue();
- } else if (type.isNonNullable()) {
+ } else if (!LiteralUtils::canMakeZero(type)) {
#ifdef RSE_DEBUG
- std::cout << "new unique value for non-nullable " << i << '\n';
+ std::cout << "new unique value for non-zeroable " << i << '\n';
#endif
start[i] = getUniqueValue();
} else {
diff --git a/test/lit/passes/coalesce-locals-gc-nn.wast b/test/lit/passes/coalesce-locals-gc-nn.wast
new file mode 100644
index 000000000..21b6d2a33
--- /dev/null
+++ b/test/lit/passes/coalesce-locals-gc-nn.wast
@@ -0,0 +1,84 @@
+;; NOTE: Assertions have been generated by update_lit_checks.py and should not be edited.
+;; RUN: wasm-opt %s --coalesce-locals -all --enable-gc-nn-locals -S -o - \
+;; RUN: | filecheck %s
+
+(module
+ ;; CHECK: (func $nn-locals (param $0 (ref any))
+ ;; CHECK-NEXT: (local $1 ((ref any) (ref any)))
+ ;; CHECK-NEXT: (local $2 ((ref any) (ref any)))
+ ;; CHECK-NEXT: (local.set $1
+ ;; CHECK-NEXT: (tuple.make
+ ;; CHECK-NEXT: (local.get $0)
+ ;; CHECK-NEXT: (local.get $0)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (local.set $2
+ ;; CHECK-NEXT: (tuple.make
+ ;; CHECK-NEXT: (local.get $0)
+ ;; CHECK-NEXT: (local.get $0)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (call $nn-locals
+ ;; CHECK-NEXT: (tuple.extract 0
+ ;; CHECK-NEXT: (local.get $1)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (call $nn-locals
+ ;; CHECK-NEXT: (tuple.extract 0
+ ;; CHECK-NEXT: (local.get $2)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (call $nn-locals
+ ;; CHECK-NEXT: (tuple.extract 1
+ ;; CHECK-NEXT: (local.get $1)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: (call $nn-locals
+ ;; CHECK-NEXT: (tuple.extract 1
+ ;; CHECK-NEXT: (local.get $2)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ (func $nn-locals (param $any (ref any))
+ ;; When computing interferences, coalesce locals should not error on tuples
+ ;; that contain non-nullable locals.
+ (local $x ((ref any) (ref any)))
+ (local $y ((ref any) (ref any)))
+ ;; Set values into the tuple locals and use them.
+ ;; Note that while the values are the same, we do not optimize them because
+ ;; of current limitations on tuple handling in this pass, so we are mainly
+ ;; testing for not crashing here.
+ (local.set $x
+ (tuple.make
+ (local.get $any)
+ (local.get $any)
+ )
+ )
+ (local.set $y
+ (tuple.make
+ (local.get $any)
+ (local.get $any)
+ )
+ )
+ (call $nn-locals
+ (tuple.extract 0
+ (local.get $x)
+ )
+ )
+ (call $nn-locals
+ (tuple.extract 0
+ (local.get $y)
+ )
+ )
+ (call $nn-locals
+ (tuple.extract 1
+ (local.get $x)
+ )
+ )
+ (call $nn-locals
+ (tuple.extract 1
+ (local.get $y)
+ )
+ )
+ )
+)
diff --git a/test/lit/passes/rse-gc.wast b/test/lit/passes/rse-gc.wast
index 7bea92260..ddb4aaf87 100644
--- a/test/lit/passes/rse-gc.wast
+++ b/test/lit/passes/rse-gc.wast
@@ -2,14 +2,17 @@
;; RUN: wasm-opt %s --rse --enable-gc-nn-locals -all -S -o - | filecheck %s
(module
- ;; CHECK: (func $10
- ;; CHECK-NEXT: (local $1 (ref func))
+ ;; CHECK: (func $test
+ ;; CHECK-NEXT: (local $single (ref func))
+ ;; CHECK-NEXT: (local $tuple ((ref any) (ref any)))
;; CHECK-NEXT: (nop)
;; CHECK-NEXT: )
- (func $10
+ (func $test
;; A non-nullable local. The pass should ignore it (as we cannot optimize
;; anything here anyhow: the code must assign to the local before reading from
;; it, so no sets can be redundant in that sense).
- (local $1 (ref func))
+ (local $single (ref func))
+ ;; A non-nullable tuple.
+ (local $tuple ((ref any) (ref any)))
)
)