diff options
-rw-r--r-- | src/passes/GlobalStructInference.cpp | 22 | ||||
-rw-r--r-- | test/lit/passes/gsi.wast | 159 |
2 files changed, 177 insertions, 4 deletions
diff --git a/src/passes/GlobalStructInference.cpp b/src/passes/GlobalStructInference.cpp index 50eddfd98..1444f8ad8 100644 --- a/src/passes/GlobalStructInference.cpp +++ b/src/passes/GlobalStructInference.cpp @@ -64,7 +64,10 @@ struct GlobalStructInference : public Pass { bool requiresNonNullableLocalFixups() override { return false; } // Maps optimizable struct types to the globals whose init is a struct.new of - // them. If a global is not present here, it cannot be optimized. + // them. + // + // We will remove unoptimizable types from here, so in practice, if a type is + // optimizable it will have an entry here, and not if not. std::unordered_map<HeapType, std::vector<Name>> typeGlobals; void run(Module* module) override { @@ -152,7 +155,13 @@ struct GlobalStructInference : public Pass { // fields) for (auto type : unoptimizable) { while (1) { + unoptimizable.insert(type); + + // Also erase the globals, as we will never read them anyhow. This can + // allow us to skip unneeded work, when we check if typeGlobals is + // empty, below. typeGlobals.erase(type); + auto super = type.getSuperType(); if (!super) { break; @@ -172,8 +181,12 @@ struct GlobalStructInference : public Pass { break; } curr = *super; - for (auto global : globals) { - typeGlobals[curr].push_back(global); + + // As above, avoid adding pointless data for anything unoptimizable. + if (!unoptimizable.count(curr)) { + for (auto global : globals) { + typeGlobals[curr].push_back(global); + } } } } @@ -209,7 +222,8 @@ struct GlobalStructInference : public Pass { // We must ignore the case of a non-struct heap type, that is, a bottom // type (which is all that is left after we've already ruled out - // unreachable). + // unreachable). Such things will not be in typeGlobals, which we are + // checking now anyhow. auto heapType = type.getHeapType(); auto iter = parent.typeGlobals.find(heapType); if (iter == parent.typeGlobals.end()) { diff --git a/test/lit/passes/gsi.wast b/test/lit/passes/gsi.wast index 4cec6aa3c..6fe4da95a 100644 --- a/test/lit/passes/gsi.wast +++ b/test/lit/passes/gsi.wast @@ -1268,3 +1268,162 @@ ) ) ) + +;; Two subtypes, each with a global. A get of the parent can be optimized into +;; a select, as it must read one of the children. +(module + ;; CHECK: (type $struct (struct (field i32))) + (type $struct (struct_subtype i32 data)) + + ;; CHECK: (type $sub-struct1 (struct_subtype (field i32) $struct)) + (type $sub-struct1 (struct_subtype i32 $struct)) + + ;; CHECK: (type $sub-struct2 (struct_subtype (field i32) $struct)) + (type $sub-struct2 (struct_subtype i32 $struct)) + + ;; CHECK: (type $ref?|$struct|_=>_none (func (param (ref null $struct)))) + + ;; CHECK: (global $global1 (ref $sub-struct1) (struct.new $sub-struct1 + ;; CHECK-NEXT: (i32.const 42) + ;; CHECK-NEXT: )) + (global $global1 (ref $sub-struct1) (struct.new $sub-struct1 + (i32.const 42) + )) + + ;; CHECK: (global $global2 (ref $sub-struct2) (struct.new $sub-struct2 + ;; CHECK-NEXT: (i32.const 1337) + ;; CHECK-NEXT: )) + (global $global2 (ref $sub-struct2) (struct.new $sub-struct2 + (i32.const 1337) + )) + + ;; CHECK: (func $test (type $ref?|$struct|_=>_none) (param $struct (ref null $struct)) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (select + ;; CHECK-NEXT: (i32.const 42) + ;; CHECK-NEXT: (i32.const 1337) + ;; CHECK-NEXT: (ref.eq + ;; CHECK-NEXT: (ref.as_non_null + ;; CHECK-NEXT: (local.get $struct) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (global.get $global1) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + (func $test (param $struct (ref null $struct)) + (drop + (struct.get $struct 0 + (local.get $struct) + ) + ) + ) +) + +;; As above, but now the parent is unoptimizable due to a struct.new in a +;; function. We must not optimize here to a select. +(module + ;; CHECK: (type $struct (struct (field i32))) + (type $struct (struct_subtype i32 data)) + + ;; CHECK: (type $sub-struct1 (struct_subtype (field i32) $struct)) + (type $sub-struct1 (struct_subtype i32 $struct)) + + ;; CHECK: (type $sub-struct2 (struct_subtype (field i32) $struct)) + (type $sub-struct2 (struct_subtype i32 $struct)) + + ;; CHECK: (type $ref?|$struct|_=>_none (func (param (ref null $struct)))) + + ;; CHECK: (global $global1 (ref $sub-struct1) (struct.new $sub-struct1 + ;; CHECK-NEXT: (i32.const 42) + ;; CHECK-NEXT: )) + (global $global1 (ref $sub-struct1) (struct.new $sub-struct1 + (i32.const 42) + )) + + ;; CHECK: (global $global2 (ref $sub-struct2) (struct.new $sub-struct2 + ;; CHECK-NEXT: (i32.const 1337) + ;; CHECK-NEXT: )) + (global $global2 (ref $sub-struct2) (struct.new $sub-struct2 + (i32.const 1337) + )) + + ;; CHECK: (func $test (type $ref?|$struct|_=>_none) (param $struct (ref null $struct)) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (struct.new $struct + ;; CHECK-NEXT: (i32.const 999999) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (struct.get $struct 0 + ;; CHECK-NEXT: (local.get $struct) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + (func $test (param $struct (ref null $struct)) + (drop + (struct.new $struct + (i32.const 999999) + ) + ) + (drop + (struct.get $struct 0 + (local.get $struct) + ) + ) + ) +) + +;; As above, the struct.new in a function is of a subtype. Again, we cannot +;; optimize, as unoptimizability spreads to supertypes. +(module + ;; CHECK: (type $struct (struct (field i32))) + (type $struct (struct_subtype i32 data)) + + ;; CHECK: (type $sub-struct1 (struct_subtype (field i32) $struct)) + (type $sub-struct1 (struct_subtype i32 $struct)) + + ;; CHECK: (type $sub-struct2 (struct_subtype (field i32) $struct)) + (type $sub-struct2 (struct_subtype i32 $struct)) + + ;; CHECK: (type $ref?|$struct|_=>_none (func (param (ref null $struct)))) + + ;; CHECK: (global $global1 (ref $sub-struct1) (struct.new $sub-struct1 + ;; CHECK-NEXT: (i32.const 42) + ;; CHECK-NEXT: )) + (global $global1 (ref $sub-struct1) (struct.new $sub-struct1 + (i32.const 42) + )) + + ;; CHECK: (global $global2 (ref $sub-struct2) (struct.new $sub-struct2 + ;; CHECK-NEXT: (i32.const 1337) + ;; CHECK-NEXT: )) + (global $global2 (ref $sub-struct2) (struct.new $sub-struct2 + (i32.const 1337) + )) + + ;; CHECK: (func $test (type $ref?|$struct|_=>_none) (param $struct (ref null $struct)) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (struct.new $sub-struct1 + ;; CHECK-NEXT: (i32.const 999999) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (struct.get $struct 0 + ;; CHECK-NEXT: (local.get $struct) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + (func $test (param $struct (ref null $struct)) + (drop + (struct.new $sub-struct1 + (i32.const 999999) + ) + ) + (drop + (struct.get $struct 0 + (local.get $struct) + ) + ) + ) +) |