diff options
author | Thomas Lively <tlively@google.com> | 2024-11-26 14:26:57 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-11-26 22:26:57 +0000 |
commit | 4ffe27255ce99d452d05d4b352e3f6e1e9ca7d83 (patch) | |
tree | 45609182512408ac238411d0fd44e22745d0f585 | |
parent | ffc3f2219b18c2a2ddb160c0d81518234faa2cd1 (diff) | |
download | binaryen-4ffe27255ce99d452d05d4b352e3f6e1e9ca7d83.tar.gz binaryen-4ffe27255ce99d452d05d4b352e3f6e1e9ca7d83.tar.bz2 binaryen-4ffe27255ce99d452d05d4b352e3f6e1e9ca7d83.zip |
ReFinalize after merging siblings in TypeMerging (#7121)
The LUB of sibling types is their common supertype, but after the
sibling types are merged, their LUB is the merged type, which is a
strict subtype of the previous LUB. This means that merging sibling
types causes `selects` to have stale types when the two select arms
previously had the two merged sibling types. To fix any potential stale
types, ReFinalize after merging sibling types.
-rw-r--r-- | src/passes/TypeMerging.cpp | 11 | ||||
-rw-r--r-- | test/lit/passes/type-merging.wast | 62 |
2 files changed, 72 insertions, 1 deletions
diff --git a/src/passes/TypeMerging.cpp b/src/passes/TypeMerging.cpp index 22c2352f2..e7a25cf37 100644 --- a/src/passes/TypeMerging.cpp +++ b/src/passes/TypeMerging.cpp @@ -38,6 +38,7 @@ #include "ir/module-utils.h" #include "ir/type-updating.h" +#include "ir/utils.h" #include "pass.h" #include "support/dfa_minimization.h" #include "support/small_set.h" @@ -229,14 +230,24 @@ void TypeMerging::run(Module* module_) { // Merging can unlock more sibling merging opportunities because two identical // types cannot be merged until their respective identical parents have been // merged in a previous step, making them siblings. + // + // If we merge siblings, we also need to refinalize because the LUB of merged + // siblings is the merged type rather than their common supertype after the + // merge. + bool refinalize = false; merge(Supertypes); for (int i = 0; i < MAX_ITERATIONS; ++i) { if (!merge(Siblings)) { break; } + refinalize = true; } applyMerges(); + + if (refinalize) { + ReFinalize().run(getPassRunner(), module); + } } bool TypeMerging::merge(MergeKind kind) { diff --git a/test/lit/passes/type-merging.wast b/test/lit/passes/type-merging.wast index 942e15b02..b216cede0 100644 --- a/test/lit/passes/type-merging.wast +++ b/test/lit/passes/type-merging.wast @@ -877,7 +877,6 @@ ;; leading to a failure to build new types. ;; TODO: Store a heap type on control flow structures to avoid creating ;; standalone function types for them. -;; TODO: Investigate why the rec group contains two of the same type below. (module (rec ;; CHECK: (rec @@ -987,6 +986,67 @@ (export "public" (global $public)) ) +;; Regression test that produces invalid IR if we do not refinalize after +;; merging siblings. +(module + (rec + ;; CHECK: (rec + ;; CHECK-NEXT: (type $super (sub (struct))) + (type $super (sub (struct))) + ;; CHECK: (type $subA (sub $super (struct (field i32)))) + (type $subA (sub $super (struct (field i32)))) + (type $subB (sub $super (struct (field i32)))) + ) + + ;; CHECK: (type $2 (func)) + + ;; CHECK: (func $test (type $2) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (select (result (ref $subA)) + ;; CHECK-NEXT: (struct.new_default $subA) + ;; CHECK-NEXT: (struct.new_default $subA) + ;; CHECK-NEXT: (i32.const 0) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + (func $test + (drop + (select (result (ref $super)) + (struct.new_default $subA) + (struct.new_default $subB) + (i32.const 0) + ) + ) + ) +) + +;; TODO: We should be able to merge $sub into $public here, but we currently do +;; not encode the successors of public types in their DFA states, so we +;; currently fail to do this merge. See issue #7120. +(module + ;; CHECK: (type $public (sub (struct (field (ref null $public))))) + (type $public (sub (struct (field (ref null $public))))) + ;; CHECK: (rec + ;; CHECK-NEXT: (type $sub (sub $public (struct (field (ref null $public))))) + (type $sub (sub $public (struct (field (ref null $public))))) + + ;; CHECK: (type $2 (func)) + + ;; CHECK: (global $g (ref null $public) (ref.null none)) + (global $g (export "g") (ref null $public) (ref.null none)) + + ;; CHECK: (export "g" (global $g)) + + ;; CHECK: (func $test (type $2) + ;; CHECK-NEXT: (local $0 (ref $public)) + ;; CHECK-NEXT: (local $1 (ref $sub)) + ;; CHECK-NEXT: ) + (func $test + (local (ref $public)) + (local (ref $sub)) + ) +) + ;; Check that a ref.test inhibits merging (ref.cast is already checked above). (module ;; CHECK: (rec |