Skip to content

Commit fa25cd6

Browse files
authored
Do not recollect types in ReorderTypes (#8195)
ReorderTypes previously collected the module types and their counts, even though it already extends GlobalTypeRewriter, which also collects the module types in its constructor. Not only did this duplicate work, it was also a subtle source of bugs because ReorderTypes and GlobalTypeRewriter collected types slightly differently. ReorderTypes collected the binary types and GlobalTypeRewriter collected the used IR types. This could result in different types being collected for the same multivalue control flow signatures, causing an assertion failure when a type was seemingly missing from the collected info. Fix the problem and make the pass more efficient by simply not collecting the types two separate times.
1 parent 1a51861 commit fa25cd6

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

src/passes/ReorderTypes.cpp

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ namespace wasm {
3131
namespace {
3232

3333
struct ReorderingTypeRewriter : GlobalTypeRewriter {
34-
using InfoMap = InsertOrderedMap<HeapType, ModuleUtils::HeapTypeInfo>;
35-
36-
InfoMap& typeInfo;
37-
3834
// Use a simpler cost calculation so the effects can be seen with smaller test
3935
// cases.
4036
bool forTesting;
@@ -45,8 +41,8 @@ struct ReorderingTypeRewriter : GlobalTypeRewriter {
4541
static constexpr float maxFactor = 1.0;
4642
static constexpr Index numFactors = 21;
4743

48-
ReorderingTypeRewriter(Module& wasm, InfoMap& typeInfo, bool forTesting)
49-
: GlobalTypeRewriter(wasm), typeInfo(typeInfo), forTesting(forTesting) {}
44+
ReorderingTypeRewriter(Module& wasm, bool forTesting)
45+
: GlobalTypeRewriter(wasm), forTesting(forTesting) {}
5046

5147
std::vector<HeapType> getSortedTypes(PredecessorGraph preds) override {
5248
auto numTypes = preds.size();
@@ -150,11 +146,7 @@ struct ReorderTypes : Pass {
150146
Fatal() << "ReorderTypes requires --closed-world";
151147
}
152148

153-
// Collect the use counts for each type.
154-
auto typeInfo = ModuleUtils::collectHeapTypeInfo(
155-
*module, ModuleUtils::TypeInclusion::BinaryTypes);
156-
157-
ReorderingTypeRewriter(*module, typeInfo, forTesting).update();
149+
ReorderingTypeRewriter(*module, forTesting).update();
158150
}
159151
};
160152

test/lit/passes/reorder-types.wast

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,3 +391,28 @@
391391
(local (ref $Y))
392392
)
393393
)
394+
395+
(module
396+
;; Regression test. ReorderTypes used to collect the types used in the binary
397+
;; for their counts, which in this case includes $multi because it is part of
398+
;; the rec group. However, GlobalTypeRewriter separately collected only the
399+
;; used IR types, which includes a standalone function type instead of $multi.
400+
;; The sort then tried to lookup the count for the standalone function type
401+
;; and crashed when it couldn't find it.
402+
(rec
403+
(type $multi (func (result (ref $A) (ref $B))))
404+
;; CHECK: (rec
405+
;; CHECK-NEXT: (type $A (sub (struct)))
406+
(type $A (sub (struct)))
407+
;; CHECK: (type $B (sub $A (struct)))
408+
(type $B (sub $A (struct)))
409+
)
410+
;; CHECK: (func $test (type $3) (param $0 i32) (result (ref $A) (ref $B))
411+
;; CHECK-NEXT: (unreachable)
412+
;; CHECK-NEXT: )
413+
(func $test (param i32) (result (ref $A) (ref $B))
414+
(block (type $multi) (result (ref $A) (ref $B))
415+
(unreachable)
416+
)
417+
)
418+
)

0 commit comments

Comments
 (0)