diff options
-rw-r--r-- | src/passes/CodePushing.cpp | 21 | ||||
-rw-r--r-- | test/lit/passes/code-pushing_tnh.wast | 48 |
2 files changed, 66 insertions, 3 deletions
diff --git a/src/passes/CodePushing.cpp b/src/passes/CodePushing.cpp index 1b253c6ea..4adbb3423 100644 --- a/src/passes/CodePushing.cpp +++ b/src/passes/CodePushing.cpp @@ -124,11 +124,26 @@ private: return nullptr; } auto index = set->index; - // to be pushable, this must be SFA and the right # of gets, - // but also have no side effects, as it may not execute if pushed. + // To be pushable, this must be SFA and the right # of gets. + // + // It must also not have side effects, as it may no longer execute after it + // is pushed, since it may be behind a condition that ends up false some of + // the time. However, removable side effects are ok here. The general + // problem with removable effects is that we can only remove them, but not + // move them, because of stuff like this: + // + // if (x != 0) foo(1 / x); + // + // If we move 1 / x to execute unconditionally then it may trap, but it + // would be fine to remove it. This pass does not move code to places where + // it might execute more, but *less*: we keep the code behind any conditions + // it was already behind, and potentially put it behind further ones. In + // effect, we "partially remove" the code, making it not execute some of the + // time, which is fine. if (analyzer.isSFA(index) && numGetsSoFar[index] == analyzer.getNumGets(index) && - !EffectAnalyzer(passOptions, module, set->value).hasSideEffects()) { + !EffectAnalyzer(passOptions, module, set->value) + .hasUnremovableSideEffects()) { return set; } return nullptr; diff --git a/test/lit/passes/code-pushing_tnh.wast b/test/lit/passes/code-pushing_tnh.wast new file mode 100644 index 000000000..9993c686b --- /dev/null +++ b/test/lit/passes/code-pushing_tnh.wast @@ -0,0 +1,48 @@ +;; NOTE: Assertions have been generated by update_lit_checks.py --all-items and should not be edited. +;; NOTE: This test was ported using port_passes_tests_to_lit.py and could be cleaned up. + +;; RUN: foreach %s %t wasm-opt --code-pushing -tnh -S -o - | filecheck %s + +(module + ;; CHECK: (type $i32_i32_=>_none (func (param i32 i32))) + + ;; CHECK: (func $div (param $x i32) (param $y i32) + ;; CHECK-NEXT: (local $temp i32) + ;; CHECK-NEXT: (block $block + ;; CHECK-NEXT: (if + ;; CHECK-NEXT: (local.get $y) + ;; CHECK-NEXT: (return) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (local.set $temp + ;; CHECK-NEXT: (i32.div_u + ;; CHECK-NEXT: (i32.const 1) + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (local.get $temp) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + (func $div (param $x i32) (param $y i32) + (local $temp i32) + (block $block + ;; This division might trap (if x is 0). But with tnh we can assume that + ;; won't happen, and push it past the condition. + (local.set $temp + (i32.div_u + (i32.const 1) + (local.get $x) + ) + ) + (if + (local.get $y) + (return) + ) + (drop + (local.get $temp) + ) + ) + ) +) + |