Skip to content

Commit e460877

Browse files
authored
Do not repeat types names in text output (#6499)
For types that do not have explicit names, we generate index-based names in the printer. However, we did not previously ensure that the generated types were not already used as explicit names, so it was possible to print the same name for multiple types, which is not valid. Fix the problem by skipping indices that are already used as type names. Fixes #6492.
1 parent 75993de commit e460877

17 files changed

+154
-121
lines changed

src/passes/Print.cpp

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,17 +152,50 @@ struct PrintSExpression : public UnifiedExpressionVisitor<PrintSExpression> {
152152
// user-provided names and the fallback indexed names.
153153
struct TypePrinter : TypeNameGeneratorBase<TypePrinter> {
154154
PrintSExpression& parent;
155-
IndexedTypeNameGenerator<> fallback;
155+
DefaultTypeNameGenerator fallback;
156+
std::unordered_map<HeapType, TypeNames> fallbackNames;
156157

157158
TypePrinter(PrintSExpression& parent, const std::vector<HeapType>& types)
158-
: parent(parent), fallback(types) {}
159+
: parent(parent) {
160+
if (!parent.currModule) {
161+
return;
162+
}
163+
std::unordered_set<Name> usedNames;
164+
for (auto& [_, names] : parent.currModule->typeNames) {
165+
usedNames.insert(names.name);
166+
}
167+
size_t i = 0;
168+
// Use indices for any remaining type names, skipping any that are already
169+
// used.
170+
for (auto type : types) {
171+
if (parent.currModule->typeNames.count(type)) {
172+
++i;
173+
continue;
174+
}
175+
Name name;
176+
do {
177+
name = std::to_string(i++);
178+
} while (usedNames.count(name));
179+
fallbackNames[type] = {name, {}};
180+
}
181+
}
159182

160183
TypeNames getNames(HeapType type) {
161184
if (parent.currModule) {
162185
if (auto it = parent.currModule->typeNames.find(type);
163186
it != parent.currModule->typeNames.end()) {
164187
return it->second;
165188
}
189+
// In principle we should always have at least a fallback name for every
190+
// type in the module, so this lookup should never fail. In practice,
191+
// though, the `printExpression` variants deliberately avoid walking the
192+
// module to find unnamed types so they can be safely used in a
193+
// function-parallel context. That means we can have a module but not
194+
// have generated the fallback names, so this lookup can fail, in which
195+
// case we generate a name on demand.
196+
if (auto it = fallbackNames.find(type); it != fallbackNames.end()) {
197+
return it->second;
198+
}
166199
}
167200
return fallback.getNames(type);
168201
}

test/lit/passes/O4_disable-bulk-memory.wast

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
(type $7 (func (param i32 i32 i32)))
5151
;; CHECK: (type $11 (func (param i32)))
5252

53-
;; CHECK: (type $3 (func (param f64 f64 f64 f64 f64 f64 f64) (result i32)))
53+
;; CHECK: (type $12 (func (param f64 f64 f64 f64 f64 f64 f64) (result i32)))
5454

5555
;; CHECK: (type $8 (func (result f64)))
5656
(type $8 (func (result f64)))

test/lit/passes/coalesce-locals.wast

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
;; CHECK: (type $FUNCSIG$iii (func (param i32 i32) (result i32)))
1414

15-
;; CHECK: (type $4 (func (param f64 i32) (result i64)))
15+
;; CHECK: (type $5 (func (param f64 i32) (result i64)))
1616

1717
;; CHECK: (type $3 (func (param i32 f32)))
1818

@@ -22,11 +22,11 @@
2222
(type $2 (func))
2323
(type $3 (func (param i32 f32)))
2424
(type $4 (func (param i32)))
25-
;; CHECK: (type $7 (func (param i32) (result i32)))
25+
;; CHECK: (type $8 (func (param i32) (result i32)))
2626

27-
;; CHECK: (type $8 (func (param i32 i32)))
27+
;; CHECK: (type $9 (func (param i32 i32)))
2828

29-
;; CHECK: (type $9 (func (result f64)))
29+
;; CHECK: (type $10 (func (result f64)))
3030

3131
;; CHECK: (import "env" "_emscripten_autodebug_i32" (func $_emscripten_autodebug_i32 (param i32 i32) (result i32)))
3232
(import "env" "_emscripten_autodebug_i32" (func $_emscripten_autodebug_i32 (param i32 i32) (result i32)))

test/lit/passes/dae-optimizing.wast

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
;; RUN: foreach %s %t wasm-opt --dae-optimizing -S -o - | filecheck %s
55

66
(module
7-
;; CHECK: (type $0 (func (result i32)))
87
(type $0 (func (param f32) (result f32)))
9-
;; CHECK: (type $1 (func (result f32)))
108
(type $1 (func (param f64 f32 f32 f64 f32 i64 f64) (result i32)))
9+
;; CHECK: (type $3 (func (result i32)))
10+
11+
;; CHECK: (type $4 (func (result f32)))
12+
1113
;; CHECK: (type $2 (func (param f64 f32 f32 f64 f32 i32 i32 f64) (result i32)))
1214
(type $2 (func (param f64 f32 f32 f64 f32 i32 i32 f64) (result i32)))
1315
;; CHECK: (global $global$0 (mut i32) (i32.const 10))

test/lit/passes/flatten_all-features.wast

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
(type $2 (func (result i32)))
1515
;; CHECK: (type $3 (func (param i32) (result i32)))
1616
(type $3 (func (param i32) (result i32)))
17-
;; CHECK: (type $4 (func (result f32)))
18-
(type $4 (func (param i64 i64) (result i64)))
19-
;; CHECK: (type $4 (func (param i64 i64) (result i64)))
17+
;; CHECK: (type $5 (func (result f32)))
2018

21-
;; CHECK: (type $6 (func (result anyref)))
19+
;; CHECK: (type $4 (func (param i64 i64) (result i64)))
20+
(type $4 (func (param i64 i64) (result i64)))
21+
;; CHECK: (type $7 (func (result anyref)))
2222

2323
;; CHECK: (global $x (mut i32) (i32.const 0))
2424
(global $x (mut i32) (i32.const 0))
@@ -833,7 +833,7 @@
833833
(i32.const 0)
834834
)
835835
)
836-
;; CHECK: (func $a17 (type $4) (result f32)
836+
;; CHECK: (func $a17 (type $5) (result f32)
837837
;; CHECK-NEXT: (local $var$0 f32)
838838
;; CHECK-NEXT: (local $1 f32)
839839
;; CHECK-NEXT: (local $2 f32)
@@ -921,7 +921,7 @@
921921
)
922922
)
923923
)
924-
;; CHECK: (func $a19 (type $4) (result f32)
924+
;; CHECK: (func $a19 (type $5) (result f32)
925925
;; CHECK-NEXT: (block $label$0
926926
;; CHECK-NEXT: (block $label$1
927927
;; CHECK-NEXT: (unreachable)
@@ -3585,7 +3585,7 @@
35853585
;; targets an outer branch whose return type is a supertype of the br_if's
35863586
;; value type, we need the value to be set into two locals: one with the outer
35873587
;; block's type, and one with its value type.
3588-
;; CHECK: (func $subtype (type $6) (result anyref)
3588+
;; CHECK: (func $subtype (type $7) (result anyref)
35893589
;; CHECK-NEXT: (local $0 eqref)
35903590
;; CHECK-NEXT: (local $1 anyref)
35913591
;; CHECK-NEXT: (local $2 nullref)

test/lit/passes/fpcast-emu.wast

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@
297297
(type $0 (func (param i64)))
298298
;; CHECK: (type $1 (func (param f32) (result i64)))
299299
(type $1 (func (param f32) (result i64)))
300-
;; CHECK: (type $1 (func (param i64 i64 i64 i64 i64 i64 i64 i64 i64 i64 i64 i64 i64 i64 i64 i64) (result i64)))
300+
;; CHECK: (type $2 (func (param i64 i64 i64 i64 i64 i64 i64 i64 i64 i64 i64 i64 i64 i64 i64 i64) (result i64)))
301301

302302
;; CHECK: (global $global$0 (mut i32) (i32.const 10))
303303
(global $global$0 (mut i32) (i32.const 10))
@@ -312,7 +312,7 @@
312312
;; CHECK-NEXT: (global.set $global$0
313313
;; CHECK-NEXT: (i32.const 0)
314314
;; CHECK-NEXT: )
315-
;; CHECK-NEXT: (call_indirect (type $1)
315+
;; CHECK-NEXT: (call_indirect (type $2)
316316
;; CHECK-NEXT: (br $label$1
317317
;; CHECK-NEXT: (i64.const 4294967295)
318318
;; CHECK-NEXT: )

test/lit/passes/instrument-memory.wast

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
(memory 256 256)
88
;; CHECK: (type $1 (func))
99
(type $1 (func))
10-
;; CHECK: (type $1 (func (param i32 i32 i32 i32) (result i32)))
10+
;; CHECK: (type $2 (func (param i32 i32 i32 i32) (result i32)))
1111

12-
;; CHECK: (type $2 (func (param i32 i32) (result i32)))
12+
;; CHECK: (type $3 (func (param i32 i32) (result i32)))
1313

14-
;; CHECK: (type $3 (func (param i32 i64) (result i64)))
14+
;; CHECK: (type $4 (func (param i32 i64) (result i64)))
1515

16-
;; CHECK: (type $4 (func (param i32 f32) (result f32)))
16+
;; CHECK: (type $5 (func (param i32 f32) (result f32)))
1717

18-
;; CHECK: (type $5 (func (param i32 f64) (result f64)))
18+
;; CHECK: (type $6 (func (param i32 f64) (result f64)))
1919

2020
;; CHECK: (import "env" "load_ptr" (func $load_ptr (param i32 i32 i32 i32) (result i32)))
2121

test/lit/passes/instrument-memory64.wast

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
(memory i64 256 256)
88
;; CHECK: (type $1 (func))
99
(type $1 (func))
10-
;; CHECK: (type $1 (func (param i32 i32 i64 i64) (result i64)))
10+
;; CHECK: (type $2 (func (param i32 i32 i64 i64) (result i64)))
1111

12-
;; CHECK: (type $2 (func (param i32 i32) (result i32)))
12+
;; CHECK: (type $3 (func (param i32 i32) (result i32)))
1313

14-
;; CHECK: (type $3 (func (param i32 i64) (result i64)))
14+
;; CHECK: (type $4 (func (param i32 i64) (result i64)))
1515

16-
;; CHECK: (type $4 (func (param i32 f32) (result f32)))
16+
;; CHECK: (type $5 (func (param i32 f32) (result f32)))
1717

18-
;; CHECK: (type $5 (func (param i32 f64) (result f64)))
18+
;; CHECK: (type $6 (func (param i32 f64) (result f64)))
1919

2020
;; CHECK: (import "env" "load_ptr" (func $load_ptr (param i32 i32 i64 i64) (result i64)))
2121

test/lit/passes/optimize-instructions-mvp.wast

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22
;; RUN: wasm-opt %s --optimize-instructions --mvp-features -S -o - | filecheck %s
33

44
(module
5-
;; CHECK: (type $0 (func (result i32)))
6-
(type $0 (func (param i32 i64)))
7-
85
;; CHECK: (type $0 (func (param i32 i64)))
6+
(type $0 (func (param i32 i64)))
97

108
;; CHECK: (import "a" "b" (func $get-f64 (result f64)))
119
(import "a" "b" (func $get-f64 (result f64)))

test/lld/duplicate_imports.wat.out

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
(module
2-
(type $0 (func (param i32 f32 f64) (result f32)))
3-
(type $1 (func (param i32 f64 f64) (result f32)))
2+
(type $3 (func (param i32 f32 f64) (result f32)))
3+
(type $4 (func (param i32 f64 f64) (result f32)))
44
(type $0 (func (param i32) (result i32)))
55
(type $1 (func (result i32)))
66
(type $2 (func))
7-
(type $5 (func (param f32 f64) (result f32)))
8-
(type $6 (func (param f64 f64) (result f32)))
9-
(type $7 (func (param i64) (result i32)))
10-
(type $8 (func (param i32 i32) (result i32)))
7+
(type $8 (func (param f32 f64) (result f32)))
8+
(type $9 (func (param f64 f64) (result f32)))
9+
(type $10 (func (param i64) (result i32)))
10+
(type $11 (func (param i32 i32) (result i32)))
1111
(import "env" "puts" (func $puts1 (param i32) (result i32)))
1212
(import "env" "invoke_ffd" (func $invoke_ffd (param i32 f32 f64) (result f32)))
1313
(import "env" "invoke_ffd" (func $invoke_ffd2 (param i32 f64 f64) (result f32)))
@@ -37,14 +37,14 @@
3737
(nop)
3838
)
3939
(func $dynCall_ffd (param $fptr i32) (param $0 f32) (param $1 f64) (result f32)
40-
(call_indirect (type $5)
40+
(call_indirect (type $8)
4141
(local.get $0)
4242
(local.get $1)
4343
(local.get $fptr)
4444
)
4545
)
4646
(func $dynCall_fdd (param $fptr i32) (param $0 f64) (param $1 f64) (result f32)
47-
(call_indirect (type $6)
47+
(call_indirect (type $9)
4848
(local.get $0)
4949
(local.get $1)
5050
(local.get $fptr)

0 commit comments

Comments
 (0)