diff options
author | Alon Zakai <azakai@google.com> | 2021-11-08 16:23:09 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-09 00:23:09 +0000 |
commit | ed4d22e22414a561e5b5af0064a3a47fc8db1645 (patch) | |
tree | 9c870d4aad45345773936fb61270f19889b539da | |
parent | b604ad935d1adc8498872e8afdb18df2f9b3b572 (diff) | |
download | binaryen-ed4d22e22414a561e5b5af0064a3a47fc8db1645.tar.gz binaryen-ed4d22e22414a561e5b5af0064a3a47fc8db1645.tar.bz2 binaryen-ed4d22e22414a561e5b5af0064a3a47fc8db1645.zip |
Effects.h: Fix RefAs (#4312)
We marked that as only trapping if the input as nullable. But ref.as_func
will trap if it isn't a func, for example.
We could in theory try to check if a trap is possible, like checking if the
input is already non-nullable or already a function, etc., but we have
optimization passes to get rid of RefAs when they are not needed
anyhow, so there is no point to duplicate that here.
-rw-r--r-- | src/ir/effects.h | 12 | ||||
-rw-r--r-- | test/lit/passes/vacuum-gc.wast | 51 | ||||
-rw-r--r-- | test/lit/passes/vacuum-tnh.wast | 17 |
3 files changed, 77 insertions, 3 deletions
diff --git a/src/ir/effects.h b/src/ir/effects.h index f24f0ac3f..ced8e63cb 100644 --- a/src/ir/effects.h +++ b/src/ir/effects.h @@ -725,9 +725,15 @@ private: } void visitRefAs(RefAs* curr) { // traps when the arg is not valid - if (curr->value->type.isNullable()) { - parent.implicitTrap = true; - } + parent.implicitTrap = true; + // Note: We could be more precise here and report the lack of a possible + // trap if the input is non-nullable (and also of the right kind for + // RefAsFunc etc.). However, we have optimization passes that will + // remove a RefAs in such a case (in OptimizeInstructions, and also + // Vacuum in trapsNeverHappen mode), so duplicating that code here would + // only help until the next time those optimizations run. As a tradeoff, + // we keep the code here simpler, but it does mean another optimization + // cycle may be needed in some cases. } }; diff --git a/test/lit/passes/vacuum-gc.wast b/test/lit/passes/vacuum-gc.wast new file mode 100644 index 000000000..8ca40bee8 --- /dev/null +++ b/test/lit/passes/vacuum-gc.wast @@ -0,0 +1,51 @@ +;; NOTE: Assertions have been generated by update_lit_checks.py and should not be edited. +;; RUN: wasm-opt %s --vacuum -all -S -o - | filecheck %s + +(module + ;; CHECK: (func $drop-ref-as (param $x anyref) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (ref.as_non_null + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (ref.as_func + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (ref.as_data + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (ref.as_i31 + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + (func $drop-ref-as (param $x anyref) + ;; Without -tnh, we must assume all ref_as* can have a trap effect, and so + ;; we cannot remove anything here. + (drop + (ref.as_non_null + (local.get $x) + ) + ) + (drop + (ref.as_func + (local.get $x) + ) + ) + (drop + (ref.as_data + (local.get $x) + ) + ) + (drop + (ref.as_i31 + (local.get $x) + ) + ) + ) +) diff --git a/test/lit/passes/vacuum-tnh.wast b/test/lit/passes/vacuum-tnh.wast index 4820a55bc..83cb298c2 100644 --- a/test/lit/passes/vacuum-tnh.wast +++ b/test/lit/passes/vacuum-tnh.wast @@ -24,6 +24,23 @@ ) ) + ;; Other ref.as* as well. + (drop + (ref.as_func + (local.get $y) + ) + ) + (drop + (ref.as_data + (local.get $y) + ) + ) + (drop + (ref.as_i31 + (local.get $y) + ) + ) + ;; Ignore unreachable code. (drop (unreachable) |