diff options
-rw-r--r-- | src/ir/localize.h | 42 | ||||
-rw-r--r-- | src/passes/GlobalTypeOptimization.cpp | 43 | ||||
-rw-r--r-- | test/lit/passes/gto-removals.wast | 157 |
3 files changed, 234 insertions, 8 deletions
diff --git a/src/ir/localize.h b/src/ir/localize.h index c8e85822a..44ec5af32 100644 --- a/src/ir/localize.h +++ b/src/ir/localize.h @@ -17,7 +17,8 @@ #ifndef wasm_ir_localizer_h #define wasm_ir_localizer_h -#include <wasm-builder.h> +#include "ir/iteration.h" +#include "wasm-builder.h" namespace wasm { @@ -26,7 +27,6 @@ namespace wasm { // // Note that if the local is reused, this assumes it is not modified in between // the set and the get, which the caller must ensure. - struct Localizer { Index index; Expression* expr; @@ -44,6 +44,44 @@ struct Localizer { } }; +// Replaces all children with gets of locals, if they have any effects. After +// this, the original input has only local.gets as inputs, or other things that +// have no interacting effects, and so those children can be reordered. +// The sets of the locals are emitted on a |sets| property on the class. Those +// must be emitted right before the input. +// This stops at the first unreachable child, as there is no code executing +// after that point anyhow. +// TODO: use in more places +struct ChildLocalizer { + std::vector<LocalSet*> sets; + + ChildLocalizer(Expression* input, + Function* func, + Module* wasm, + const PassOptions& options) { + Builder builder(*wasm); + ChildIterator iterator(input); + auto& children = iterator.children; + // The children are in reverse order, so allocate the output first and + // apply items as we go. + auto num = children.size(); + for (Index i = 0; i < num; i++) { + auto** childp = children[num - 1 - i]; + auto* child = *childp; + if (child->type == Type::unreachable) { + break; + } + // If there are effects, use a local for this. + // TODO: Compare interactions between their side effects. + if (EffectAnalyzer(options, *wasm, child).hasAnything()) { + auto local = builder.addVar(func, child->type); + sets.push_back(builder.makeLocalSet(local, child)); + *childp = builder.makeLocalGet(local, child->type); + } + } + } +}; + } // namespace wasm #endif // wasm_ir_localizer_h diff --git a/src/passes/GlobalTypeOptimization.cpp b/src/passes/GlobalTypeOptimization.cpp index 442b7570a..7ca7c4f85 100644 --- a/src/passes/GlobalTypeOptimization.cpp +++ b/src/passes/GlobalTypeOptimization.cpp @@ -22,10 +22,10 @@ // * Fields that are never read from can be removed entirely. // // TODO: Specialize field types. -// TODO: Remove unused fields. // #include "ir/effects.h" +#include "ir/localize.h" #include "ir/struct-utils.h" #include "ir/subtypes.h" #include "ir/type-updating.h" @@ -300,6 +300,33 @@ struct GlobalTypeOptimization : public Pass { auto& operands = curr->operands; assert(indexesAfterRemoval.size() == operands.size()); + // Check for side effects in removed fields. If there are any, we must + // use locals to save the values (while keeping them in order). + bool useLocals = false; + for (Index i = 0; i < operands.size(); i++) { + auto newIndex = indexesAfterRemoval[i]; + if (newIndex == RemovedField && + EffectAnalyzer(getPassOptions(), *getModule(), operands[i]) + .hasUnremovableSideEffects()) { + useLocals = true; + break; + } + } + if (useLocals) { + auto* func = getFunction(); + if (!func) { + Fatal() << "TODO: side effects in removed fields in globals\n"; + } + auto* block = Builder(*getModule()).makeBlock(); + auto sets = + ChildLocalizer(curr, func, getModule(), getPassOptions()).sets; + block->list.set(sets); + block->list.push_back(curr); + block->finalize(curr->type); + replaceCurrent(block); + addedLocals = true; + } + // Remove the unneeded operands. Index removed = 0; for (Index i = 0; i < operands.size(); i++) { @@ -308,11 +335,6 @@ struct GlobalTypeOptimization : public Pass { assert(newIndex < operands.size()); operands[newIndex] = operands[i]; } else { - if (EffectAnalyzer(getPassOptions(), *getModule(), operands[i]) - .hasUnremovableSideEffects()) { - Fatal() << "TODO: handle side effects in field removal " - "(impossible in global locations?)"; - } removed++; } } @@ -347,6 +369,15 @@ struct GlobalTypeOptimization : public Pass { curr->index = newIndex; } + void visitFunction(Function* curr) { + if (addedLocals) { + TypeUpdating::handleNonDefaultableLocals(curr, *getModule()); + } + } + + private: + bool addedLocals = false; + Index getNewIndex(HeapType type, Index index) { auto iter = parent.indexesAfterRemovals.find(type); if (iter == parent.indexesAfterRemovals.end()) { diff --git a/test/lit/passes/gto-removals.wast b/test/lit/passes/gto-removals.wast index 2247285ca..4f7402e11 100644 --- a/test/lit/passes/gto-removals.wast +++ b/test/lit/passes/gto-removals.wast @@ -387,3 +387,160 @@ ) ) ) + +(module + ;; A new with side effects + + ;; CHECK: (type $struct (struct_subtype (field i32) (field (rtt $struct)) data)) + (type $struct (struct i32 f64 (ref any) (rtt $struct))) + + ;; CHECK: (type $ref|any|_=>_none (func_subtype (param (ref any)) func)) + + ;; CHECK: (type $none_=>_none (func_subtype func)) + + ;; CHECK: (type $i32_=>_i32 (func_subtype (param i32) (result i32) func)) + + ;; CHECK: (type $i32_=>_f64 (func_subtype (param i32) (result f64) func)) + + ;; CHECK: (type $i32_=>_ref|any| (func_subtype (param i32) (result (ref any)) func)) + + ;; CHECK: (func $gets (param $x (ref any)) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (struct.get $struct 0 + ;; CHECK-NEXT: (ref.null $struct) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (struct.get $struct 1 + ;; CHECK-NEXT: (ref.null $struct) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + (func $gets (param $x (ref any)) + ;; Gets to keep certain fields alive. + (drop + (struct.get $struct 0 + (ref.null $struct) + ) + ) + (drop + (struct.get $struct 3 + (ref.null $struct) + ) + ) + ) + + ;; CHECK: (func $new-side-effect + ;; CHECK-NEXT: (local $0 i32) + ;; CHECK-NEXT: (local $1 f64) + ;; CHECK-NEXT: (local $2 anyref) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (block (result (ref $struct)) + ;; CHECK-NEXT: (local.set $0 + ;; CHECK-NEXT: (call $helper0 + ;; CHECK-NEXT: (i32.const 0) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (local.set $1 + ;; CHECK-NEXT: (call $helper1 + ;; CHECK-NEXT: (i32.const 1) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (local.set $2 + ;; CHECK-NEXT: (call $helper2 + ;; CHECK-NEXT: (i32.const 2) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (struct.new $struct + ;; CHECK-NEXT: (local.get $0) + ;; CHECK-NEXT: (rtt.canon $struct) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + (func $new-side-effect + ;; The 2nd&3rd fields here will be removed, since those fields have no + ;; reads. They has side effects, though, so the operands will be saved in + ;; locals. Note that we can't save the rtt.canon in locals, but it has + ;; no effects, and we leave such arguments as they are. + ;; Note also that one of the fields is non-nullable, and we need to use a + ;; nullable local for it. + (drop + (struct.new $struct + (call $helper0 (i32.const 0)) + (call $helper1 (i32.const 1)) + (call $helper2 (i32.const 2)) + (rtt.canon $struct) + ) + ) + ) + + ;; CHECK: (func $new-unreachable + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (block + ;; CHECK-NEXT: (i32.const 2) + ;; CHECK-NEXT: (unreachable) + ;; CHECK-NEXT: (call $helper2 + ;; CHECK-NEXT: (i32.const 3) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (rtt.canon $struct) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + (func $new-unreachable + ;; Another case with side effects. We stop at the unreachable param before + ;; it, however. + (drop + (struct.new $struct + (i32.const 2) + (unreachable) + (call $helper2 (i32.const 3)) + (rtt.canon $struct) + ) + ) + ) + + ;; CHECK: (func $new-side-effect-in-kept (param $any (ref any)) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (struct.new $struct + ;; CHECK-NEXT: (call $helper0 + ;; CHECK-NEXT: (i32.const 0) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (rtt.canon $struct) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + (func $new-side-effect-in-kept (param $any (ref any)) + ;; Side effects appear in fields that we do *not* remove. In that case, + ;; we do not need to use locals. + (drop + (struct.new $struct + (call $helper0 (i32.const 0)) + (f64.const 3.14159) + (local.get $any) + (rtt.canon $struct) + ) + ) + ) + + ;; CHECK: (func $helper0 (param $x i32) (result i32) + ;; CHECK-NEXT: (unreachable) + ;; CHECK-NEXT: ) + (func $helper0 (param $x i32) (result i32) + (unreachable) + ) + + ;; CHECK: (func $helper1 (param $x i32) (result f64) + ;; CHECK-NEXT: (unreachable) + ;; CHECK-NEXT: ) + (func $helper1 (param $x i32) (result f64) + (unreachable) + ) + + ;; CHECK: (func $helper2 (param $x i32) (result (ref any)) + ;; CHECK-NEXT: (unreachable) + ;; CHECK-NEXT: ) + (func $helper2 (param $x i32) (result (ref any)) + (unreachable) + ) +) |