diff options
author | Alon Zakai <azakai@google.com> | 2023-08-08 16:03:50 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-08-08 16:03:50 -0700 |
commit | 6537640f95a08aaf5f4829e34adc1f9755892af8 (patch) | |
tree | 8922b0ee12b01fd1be84e4dda6a2e7f7a0124d75 | |
parent | f1e92f99867b646aebd8a4f9b35c4972301e6469 (diff) | |
download | binaryen-6537640f95a08aaf5f4829e34adc1f9755892af8.tar.gz binaryen-6537640f95a08aaf5f4829e34adc1f9755892af8.tar.bz2 binaryen-6537640f95a08aaf5f4829e34adc1f9755892af8.zip |
SimplifyGlobals: Connect adjacent blocks in LinearExecutionWalker (#5865)
Followup to #5860, this does the same for SimplifyGlobals as for SimplifyLocals.
As there, this is valid because it's ok if we branch away. This part of the pass
applies a global value to a global.get based on a dominating global.set, so any
dominance is good enough for us.
-rw-r--r-- | src/passes/SimplifyGlobals.cpp | 5 | ||||
-rw-r--r-- | test/lit/passes/simplify-globals-dominance.wast | 55 |
2 files changed, 60 insertions, 0 deletions
diff --git a/src/passes/SimplifyGlobals.cpp b/src/passes/SimplifyGlobals.cpp index 97e91bbab..398a6c3ca 100644 --- a/src/passes/SimplifyGlobals.cpp +++ b/src/passes/SimplifyGlobals.cpp @@ -341,6 +341,11 @@ struct ConstantGlobalApplier return std::make_unique<ConstantGlobalApplier>(constantGlobals, optimize); } + // It is ok to look at adjacent blocks together, as if a later part of a block + // is not reached that is fine - changes we make there would not be reached in + // that case. + bool connectAdjacentBlocks = true; + bool refinalize = false; void replaceCurrent(Expression* rep) { diff --git a/test/lit/passes/simplify-globals-dominance.wast b/test/lit/passes/simplify-globals-dominance.wast new file mode 100644 index 000000000..948e8c5ec --- /dev/null +++ b/test/lit/passes/simplify-globals-dominance.wast @@ -0,0 +1,55 @@ +;; NOTE: Assertions have been generated by update_lit_checks.py --all-items and should not be edited. + +;; RUN: foreach %s %t wasm-opt --simplify-globals -all -S -o - | filecheck %s + +(module + ;; CHECK: (type $none_=>_none (func)) + + ;; CHECK: (global $global (mut i32) (i32.const 0)) + (global $global (mut i32) (i32.const 0)) + + ;; CHECK: (func $test (type $none_=>_none) + ;; CHECK-NEXT: (global.set $global + ;; CHECK-NEXT: (i32.const 10) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (if + ;; CHECK-NEXT: (i32.const 0) + ;; CHECK-NEXT: (block + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (i32.const 10) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (call $test) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (global.get $global) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (global.get $global) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + (func $test + (global.set $global + (i32.const 10) + ) + (if + (i32.const 0) + (block + ;; This is dominated by the set, so we can apply 10 here. + (drop + (global.get $global) + ) + (call $test) + ;; This is after a call, so we do nothing (we are still dominated by the + ;; global.set, but the call might set the global to another value). + (drop + (global.get $global) + ) + ) + ;; This is dominated by the set, but we do not optimize it yet. TODO + (drop + (global.get $global) + ) + ) + ) +) |