diff options
-rw-r--r-- | src/ir/effects.h | 51 | ||||
-rw-r--r-- | test/lit/passes/optimize-casts-tnh.wast | 45 | ||||
-rw-r--r-- | test/lit/passes/simplify-locals-tnh.wast | 100 |
3 files changed, 190 insertions, 6 deletions
diff --git a/src/ir/effects.h b/src/ir/effects.h index 29a6375ad..5670e3ef6 100644 --- a/src/ir/effects.h +++ b/src/ir/effects.h @@ -217,8 +217,39 @@ public: // check if we break to anything external from ourselves bool hasExternalBreakTargets() const { return !breakTargets.empty(); } - // checks if these effects would invalidate another set (e.g., if we write, we - // invalidate someone that reads, they can't be moved past us) + // Checks if these effects would invalidate another set of effects (e.g., if + // we write, we invalidate someone that reads). + // + // This assumes the things whose effects we are comparing will both execute, + // at least if neither of them transfers control flow away. That is, we assume + // that there is no transfer of control flow *between* them: we are comparing + // things appear after each other, perhaps with some other code in the middle, + // but that code does not transfer control flow. It is not valid to call this + // method in other situations, like this: + // + // A + // (br_if 0 (local.get 0)) ;; this may transfer control flow away + // B + // + // Calling this method in that situation is invalid because only A may + // execute and not B. The following are examples of situations where it is + // valid to call this method: + // + // A + // ;; nothing in between them at all + // B + // + // A + // (local.set 0 (i32.const 0)) ;; something in between without a possible + // ;; control flow transfer + // B + // + // That the things being compared both execute only matters in the case of + // traps-never-happen: in that mode we can move traps but only if doing so + // would not make them start to appear when they did not. In the second + // example we can't reorder A and B if B traps, but in the first example we + // can reorder them even if B traps (even if A has a global effect like a + // global.set, since we assume B does not trap in traps-never-happen). bool invalidates(const EffectAnalyzer& other) { if ((transfersControlFlow() && other.hasSideEffects()) || (other.transfersControlFlow() && hasSideEffects()) || @@ -271,10 +302,17 @@ public: // anything. assert(!((trap && other.throws()) || (throws() && other.trap))); // We can't reorder an implicit trap in a way that could alter what global - // state is modified. - if ((trap && other.writesGlobalState()) || - (other.trap && writesGlobalState())) { - return true; + // state is modified. However, in trapsNeverHappen mode we assume traps do + // not occur in practice, which lets us ignore this, at least in the case + // that the code executes. As mentioned above, we assume that there is no + // transfer of control flow between the things we are comparing, so all we + // need to do is check for such transfers in them. + if (!trapsNeverHappen || transfersControlFlow() || + other.transfersControlFlow()) { + if ((trap && other.writesGlobalState()) || + (other.trap && writesGlobalState())) { + return true; + } } return false; } @@ -921,6 +959,7 @@ private: public: // Helpers + // See comment on invalidate() for the assumptions on the inputs here. static bool canReorder(const PassOptions& passOptions, Module& module, Expression* a, diff --git a/test/lit/passes/optimize-casts-tnh.wast b/test/lit/passes/optimize-casts-tnh.wast new file mode 100644 index 000000000..ced59bd0c --- /dev/null +++ b/test/lit/passes/optimize-casts-tnh.wast @@ -0,0 +1,45 @@ +;; NOTE: Assertions have been generated by update_lit_checks.py and should not be edited. +;; RUN: wasm-opt %s --optimize-casts -all -tnh -S -o - | filecheck %s + +(module + ;; CHECK: (type $A (struct )) + (type $A (struct_subtype data)) + + ;; CHECK: (global $a (mut i32) (i32.const 0)) + (global $a (mut i32) (i32.const 0)) + + ;; CHECK: (func $best (type $ref|struct|_=>_none) (param $x (ref struct)) + ;; CHECK-NEXT: (local $1 (ref $A)) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (local.tee $1 + ;; CHECK-NEXT: (ref.cast $A + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (global.set $a + ;; CHECK-NEXT: (i32.const 10) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (ref.cast $A + ;; CHECK-NEXT: (local.get $1) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + (func $best (param $x (ref struct)) + (drop + (local.get $x) + ) + ;; global.sets normally prevent casts from being moved before them, but + ;; if traps are assumed to never happen then we can still optimize. + (global.set $a + (i32.const 10) + ) + (drop + (ref.cast $A + (local.get $x) + ) + ) + ) +) + diff --git a/test/lit/passes/simplify-locals-tnh.wast b/test/lit/passes/simplify-locals-tnh.wast new file mode 100644 index 000000000..51fda82c1 --- /dev/null +++ b/test/lit/passes/simplify-locals-tnh.wast @@ -0,0 +1,100 @@ +;; NOTE: Assertions have been generated by update_lit_checks.py and should not be edited. +;; RUN: wasm-opt %s --simplify-locals --traps-never-happen -all -S -o - | filecheck %s --check-prefix TNH +;; RUN: wasm-opt %s --simplify-locals -all -S -o - | filecheck %s --check-prefix NO_TNH + + +(module + (memory 1 1) + + ;; TNH: (global $glob (mut i32) (i32.const 0)) + ;; NO_TNH: (global $glob (mut i32) (i32.const 0)) + (global $glob (mut i32) (i32.const 0)) + + ;; TNH: (func $optimize-past-global.set (type $none_=>_i32) (result i32) + ;; TNH-NEXT: (local $temp i32) + ;; TNH-NEXT: (nop) + ;; TNH-NEXT: (global.set $glob + ;; TNH-NEXT: (i32.const 1) + ;; TNH-NEXT: ) + ;; TNH-NEXT: (i32.load + ;; TNH-NEXT: (i32.const 0) + ;; TNH-NEXT: ) + ;; TNH-NEXT: ) + ;; NO_TNH: (func $optimize-past-global.set (type $none_=>_i32) (result i32) + ;; NO_TNH-NEXT: (local $temp i32) + ;; NO_TNH-NEXT: (local.set $temp + ;; NO_TNH-NEXT: (i32.load + ;; NO_TNH-NEXT: (i32.const 0) + ;; NO_TNH-NEXT: ) + ;; NO_TNH-NEXT: ) + ;; NO_TNH-NEXT: (global.set $glob + ;; NO_TNH-NEXT: (i32.const 1) + ;; NO_TNH-NEXT: ) + ;; NO_TNH-NEXT: (local.get $temp) + ;; NO_TNH-NEXT: ) + (func $optimize-past-global.set (result i32) + (local $temp i32) + ;; This load might trap, and so normally we can't move it past a global.set, + ;; which has a global effect that could be noticed. However, in TNH mode we + ;; can assume it doesn't trap and push it forward. + (local.set $temp + (i32.load + (i32.const 0) + ) + ) + (global.set $glob + (i32.const 1) + ) + (local.get $temp) + ) + + ;; TNH: (func $no-optimize-past-return (type $none_=>_i32) (result i32) + ;; TNH-NEXT: (local $temp i32) + ;; TNH-NEXT: (local.set $temp + ;; TNH-NEXT: (i32.load + ;; TNH-NEXT: (i32.const 0) + ;; TNH-NEXT: ) + ;; TNH-NEXT: ) + ;; TNH-NEXT: (if + ;; TNH-NEXT: (i32.const 0) + ;; TNH-NEXT: (return + ;; TNH-NEXT: (i32.const 1) + ;; TNH-NEXT: ) + ;; TNH-NEXT: ) + ;; TNH-NEXT: (local.get $temp) + ;; TNH-NEXT: ) + ;; NO_TNH: (func $no-optimize-past-return (type $none_=>_i32) (result i32) + ;; NO_TNH-NEXT: (local $temp i32) + ;; NO_TNH-NEXT: (local.set $temp + ;; NO_TNH-NEXT: (i32.load + ;; NO_TNH-NEXT: (i32.const 0) + ;; NO_TNH-NEXT: ) + ;; NO_TNH-NEXT: ) + ;; NO_TNH-NEXT: (if + ;; NO_TNH-NEXT: (i32.const 0) + ;; NO_TNH-NEXT: (return + ;; NO_TNH-NEXT: (i32.const 1) + ;; NO_TNH-NEXT: ) + ;; NO_TNH-NEXT: ) + ;; NO_TNH-NEXT: (local.get $temp) + ;; NO_TNH-NEXT: ) + (func $no-optimize-past-return (result i32) + (local $temp i32) + ;; As above, but now the thing in the middle may transfer control flow. We + ;; can in principle optimize here (moving a trap to possibly not happen + ;; later is fine, in TNH mode), but SimplifyLocals only works in a single + ;; basic block so we don't atm. + (local.set $temp + (i32.load + (i32.const 0) + ) + ) + (if + (i32.const 0) + (return + (i32.const 1) + ) + ) + (local.get $temp) + ) +) |