Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/ir/type-updating.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "find_all.h"
#include "ir/local-structural-dominance.h"
#include "ir/module-utils.h"
#include "ir/names.h"
#include "ir/utils.h"
#include "support/topological_sort.h"
#include "wasm-type-ordering.h"
Expand Down Expand Up @@ -153,10 +154,23 @@ GlobalTypeRewriter::TypeMap GlobalTypeRewriter::rebuildTypes(
}

// Update type names (doing it before mapTypes can help debugging there, but
// has no other effect; mapTypes does not look at type names).
// has no other effect; mapTypes does not look at type names). While doing so
// avoid creating duplicate names.
std::unordered_set<Name> typeNames;
for (auto& [type, info] : wasm.typeNames) {
typeNames.insert(info.name);
}
for (auto& [old, new_] : oldToNewTypes) {
if (auto it = wasm.typeNames.find(old); it != wasm.typeNames.end()) {
wasm.typeNames[new_] = it->second;
wasm.typeNames[new_] = wasm.typeNames[old];
// Use the existing name in the new type, as usually it completely
// replaces the old. Rename the old name in a unique way to avoid
// confusion in the case that it remains used.
auto deduped = Names::getValidName(wasm.typeNames[old].name, [&](Name test) {
return !typeNames.count(test);
});
wasm.typeNames[old].name = deduped;
typeNames.insert(deduped);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/passes/RemoveUnusedTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ struct RemoveUnusedTypes : Pass {
}

// We're not changing the contents of any of the types, so we just round
// trip them throgh GlobalTypeRewriter, which will put all the private types
// trip them through GlobalTypeRewriter which will put all the private types
// in a single new rec group and leave out all the unused types.
GlobalTypeRewriter(*module).update();
}
Expand Down
3 changes: 2 additions & 1 deletion test/lit/passes/signature-pruning.wast
Original file line number Diff line number Diff line change
Expand Up @@ -1168,10 +1168,11 @@
;; CHECK: (rec
;; CHECK-NEXT: (type $none (func))
(type $none (func))
;; CHECK: (type $much (func (param i32)))
(type $much (func (param i32)))
)

;; CHECK: (type $much_0 (func (param i32)))

;; CHECK: (export "exported" (func $exported))

;; CHECK: (func $exported (type $none)
Expand Down
42 changes: 22 additions & 20 deletions test/lit/passes/type-ssa_and_merging.wast
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,26 @@
;; can.

(module
(type $A (sub (struct (field (mut i32)))))

;; NOP: (rec
;; NOP-NEXT: (type $0 (func (param (ref $A)) (result i32)))
;; NOP-NEXT: (type $0 (func (param (ref $A_1)) (result i32)))

;; NOP: (type $A_1 (sub (struct (field i32))))

;; NOP: (type $2 (func (result i32)))

;; NOP: (type $A (sub (struct (field i32))))
;; NOP: (import "a" "b" (func $import (type $2) (result i32)))
;; YES: (type $0 (func (result i32)))

;; YES: (rec
;; YES-NEXT: (type $1 (func (param (ref $A))))
;; YES-NEXT: (type $1 (func (param (ref $A_3))))

;; YES: (type $A (sub (struct )))
(type $A (sub (struct (field (mut i32)))))
;; YES: (type $A_3 (sub (struct )))

;; NOP: (type $2 (func (result i32)))

;; NOP: (import "a" "b" (func $import (type $2) (result i32)))
;; YES: (type $A_2 (sub $A (struct )))
;; YES: (type $A_2_1 (sub $A_3 (struct )))

;; YES: (type $A_1 (sub $A (struct )))
;; YES: (type $A_1_1 (sub $A_3 (struct )))

;; YES: (import "a" "b" (func $import (type $0) (result i32)))
(import "a" "b" (func $import (result i32)))
Expand All @@ -35,7 +37,7 @@

;; NOP: (func $main1 (type $2) (result i32)
;; NOP-NEXT: (call $get-a-1
;; NOP-NEXT: (struct.new $A
;; NOP-NEXT: (struct.new $A_1
;; NOP-NEXT: (i32.const 42)
;; NOP-NEXT: )
;; NOP-NEXT: )
Expand All @@ -46,7 +48,7 @@

;; YES: (func $main1 (type $0) (result i32)
;; YES-NEXT: (call $get-a-1
;; YES-NEXT: (struct.new_default $A)
;; YES-NEXT: (struct.new_default $A_3)
;; YES-NEXT: )
;; YES-NEXT: (i32.const 42)
;; YES-NEXT: )
Expand All @@ -59,14 +61,14 @@

;; NOP: (func $main2 (type $2) (result i32)
;; NOP-NEXT: (call $get-a-2
;; NOP-NEXT: (struct.new $A
;; NOP-NEXT: (struct.new $A_1
;; NOP-NEXT: (i32.const 1337)
;; NOP-NEXT: )
;; NOP-NEXT: )
;; NOP-NEXT: )
;; YES: (func $main2 (type $0) (result i32)
;; YES-NEXT: (call $get-a-2
;; YES-NEXT: (struct.new_default $A)
;; YES-NEXT: (struct.new_default $A_3)
;; YES-NEXT: )
;; YES-NEXT: (i32.const 1337)
;; YES-NEXT: )
Expand All @@ -77,7 +79,7 @@
)
)

;; NOP: (func $get-a-1 (type $0) (param $0 (ref $A)) (result i32)
;; NOP: (func $get-a-1 (type $0) (param $0 (ref $A_1)) (result i32)
;; NOP-NEXT: (if
;; NOP-NEXT: (call $import)
;; NOP-NEXT: (then
Expand All @@ -88,11 +90,11 @@
;; NOP-NEXT: )
;; NOP-NEXT: )
;; NOP-NEXT: )
;; NOP-NEXT: (struct.get $A 0
;; NOP-NEXT: (struct.get $A_1 0
;; NOP-NEXT: (local.get $0)
;; NOP-NEXT: )
;; NOP-NEXT: )
;; YES: (func $get-a-1 (type $1) (param $0 (ref $A))
;; YES: (func $get-a-1 (type $1) (param $0 (ref $A_3))
;; YES-NEXT: (if
;; YES-NEXT: (call $import)
;; YES-NEXT: (then
Expand All @@ -119,7 +121,7 @@
(struct.get $A 0 (local.get 0))
)

;; NOP: (func $get-a-2 (type $0) (param $0 (ref $A)) (result i32)
;; NOP: (func $get-a-2 (type $0) (param $0 (ref $A_1)) (result i32)
;; NOP-NEXT: (if
;; NOP-NEXT: (call $import)
;; NOP-NEXT: (then
Expand All @@ -130,11 +132,11 @@
;; NOP-NEXT: )
;; NOP-NEXT: )
;; NOP-NEXT: )
;; NOP-NEXT: (struct.get $A 0
;; NOP-NEXT: (struct.get $A_1 0
;; NOP-NEXT: (local.get $0)
;; NOP-NEXT: )
;; NOP-NEXT: )
;; YES: (func $get-a-2 (type $1) (param $0 (ref $A))
;; YES: (func $get-a-2 (type $1) (param $0 (ref $A_3))
;; YES-NEXT: (if
;; YES-NEXT: (call $import)
;; YES-NEXT: (then
Expand Down
Loading