Skip to content

Commit a62ee0d

Browse files
authored
NameTypes and TypeSSA : Prefer _ over $ in names, and lint away _N suffixes (#5968)
Apparently $N (e.g. FooClass$5) is a convention in Java for anonymous classes, so our $N that we use to disambiguate could be confusing. As the way we disambiguate does not matter, switch to using _N. This PR does that in both TypeSSA and NameTypes. Also make NameTypes "lint" names as it goes. That pass tries to give types nice names, leaving existing ones that seem ok, and renaming long or unnamed ones. This PR makes it aware of the _N notation and it tries to remove it, if removing it does not cause a collision. An example of how that helps is if TypeSSA creates a subtype $Foo_0 and then we manage to remove $Foo, then we can use the shorter name for the subtype.
1 parent 7d4d502 commit a62ee0d

File tree

6 files changed

+100
-52
lines changed

6 files changed

+100
-52
lines changed

src/passes/NameTypes.cpp

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,36 @@ struct NameTypes : public Pass {
3434
// Find all the types.
3535
std::vector<HeapType> types = ModuleUtils::collectHeapTypes(*module);
3636

37+
std::unordered_set<Name> used;
38+
3739
// Ensure simple names. If a name already exists, and is short enough, keep
3840
// it.
3941
size_t i = 0;
4042
for (auto& type : types) {
4143
if (module->typeNames.count(type) == 0 ||
4244
module->typeNames[type].name.size() >= NameLenLimit) {
43-
module->typeNames[type].name = "type$" + std::to_string(i++);
45+
module->typeNames[type].name = "type_" + std::to_string(i++);
46+
}
47+
used.insert(module->typeNames[type].name);
48+
}
49+
50+
// "Lint" the names a little. In particular a name with a "_7" or such
51+
// suffix, as TypeSSA creates, can be removed if it does not cause a
52+
// collision. This keeps the names unique while removing 'noise.'
53+
//
54+
// Note we must iterate in a deterministic order here, so do it on |types|.
55+
for (auto& type : types) {
56+
auto& names = module->typeNames[type];
57+
std::string name = names.name.toString();
58+
while (name.size() > 1 && isdigit(name.back())) {
59+
name.pop_back();
60+
}
61+
if (name.size() > 1 && name.back() == '_') {
62+
name.pop_back();
63+
if (!used.count(name)) {
64+
names.name = name;
65+
used.insert(name);
66+
}
4467
}
4568
}
4669
}

src/passes/TypeSSA.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ struct TypeSSA : public Pass {
285285

286286
// If the old type has a nice name, make a nice name for the new one.
287287
if (typeNames.count(oldType)) {
288-
auto intendedName = typeNames[oldType].name.toString() + '$' +
288+
auto intendedName = typeNames[oldType].name.toString() + '_' +
289289
std::to_string(++nameCounter);
290290
auto newName =
291291
Names::getValidNameGivenExisting(intendedName, existingTypeNames);

test/lit/passes/name-types.wast

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,44 @@
66
(type $obnoxious-super-long-type-name_____________________________1 (struct))
77

88
;; A reasonable name that will be kept.
9-
;; CHECK: (type $type$1 (struct ))
9+
;; CHECK: (type $type_1 (struct ))
1010

1111
;; CHECK: (type $reasonable-name (struct (field i32)))
1212
(type $reasonable-name (struct (field i32)))
1313

14-
;; CHECK: (type $type$0 (func (param (ref $type$1) (ref $reasonable-name))))
14+
;; lintable is a name that can be polished up a bit. We'll remove the _7. But
15+
;; unlintable cannot be shortened because a conflict would occur. onelintable
16+
;; allows one of the two longer names to be shortened, but the second would
17+
;; conflict.
18+
(type $lintable-name_7 (struct (field i64)))
1519

16-
;; CHECK: (func $foo (type $type$0) (param $x (ref $type$1)) (param $y (ref $reasonable-name))
20+
;; CHECK: (type $lintable-name (struct (field i64)))
21+
22+
;; CHECK: (type $unlintable-name_7 (struct (field f32)))
23+
(type $unlintable-name_7 (struct (field f32)))
24+
25+
;; CHECK: (type $unlintable-name (struct (field f64)))
26+
(type $unlintable-name (struct (field f64)))
27+
28+
(type $onelintable-name_6 (struct (field i32) (field i32) (field i32)))
29+
;; CHECK: (type $onelintable-name (struct (field i32) (field i32) (field i32)))
30+
31+
;; CHECK: (type $onelintable-name_8 (struct (field i32) (field i32) (field i32) (field i32)))
32+
(type $onelintable-name_8 (struct (field i32) (field i32) (field i32) (field i32)))
33+
34+
;; CHECK: (type $type (func (param (ref $type_1) (ref $reasonable-name) (ref $lintable-name) (ref $unlintable-name_7) (ref $unlintable-name) (ref $onelintable-name) (ref $onelintable-name_8))))
35+
36+
;; CHECK: (func $foo (type $type) (param $x (ref $type_1)) (param $y (ref $reasonable-name)) (param $z (ref $lintable-name)) (param $w (ref $unlintable-name_7)) (param $t (ref $unlintable-name)) (param $a (ref $onelintable-name)) (param $b (ref $onelintable-name_8))
1737
;; CHECK-NEXT: (nop)
1838
;; CHECK-NEXT: )
1939
(func $foo
2040
;; Use the types to keep them alive.
2141
(param $x (ref $obnoxious-super-long-type-name_____________________________1))
2242
(param $y (ref $reasonable-name))
43+
(param $z (ref $lintable-name_7))
44+
(param $w (ref $unlintable-name_7))
45+
(param $t (ref $unlintable-name))
46+
(param $a (ref $onelintable-name_6))
47+
(param $b (ref $onelintable-name_8))
2348
)
2449
)

test/lit/passes/type-ssa.wast

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,24 @@
1010
;; CHECK: (type $1 (func))
1111

1212
;; CHECK: (rec
13-
;; CHECK-NEXT: (type $struct$1 (sub $struct (struct (field i32))))
13+
;; CHECK-NEXT: (type $struct_1 (sub $struct (struct (field i32))))
1414

15-
;; CHECK: (type $struct$2 (sub $struct (struct (field i32))))
15+
;; CHECK: (type $struct_2 (sub $struct (struct (field i32))))
1616

17-
;; CHECK: (type $struct$3 (sub $struct (struct (field i32))))
17+
;; CHECK: (type $struct_3 (sub $struct (struct (field i32))))
1818

19-
;; CHECK: (type $struct$4 (sub $struct (struct (field i32))))
19+
;; CHECK: (type $struct_4 (sub $struct (struct (field i32))))
2020

21-
;; CHECK: (type $struct$5 (sub $struct (struct (field i32))))
21+
;; CHECK: (type $struct_5 (sub $struct (struct (field i32))))
2222

23-
;; CHECK: (global $g (ref $struct) (struct.new $struct$4
23+
;; CHECK: (global $g (ref $struct) (struct.new $struct_4
2424
;; CHECK-NEXT: (i32.const 42)
2525
;; CHECK-NEXT: ))
2626
(global $g (ref $struct) (struct.new $struct
2727
(i32.const 42)
2828
))
2929

30-
;; CHECK: (global $h (ref $struct) (struct.new $struct$5
30+
;; CHECK: (global $h (ref $struct) (struct.new $struct_5
3131
;; CHECK-NEXT: (i32.const 42)
3232
;; CHECK-NEXT: ))
3333
(global $h (ref $struct) (struct.new $struct
@@ -36,10 +36,10 @@
3636

3737
;; CHECK: (func $foo (type $1)
3838
;; CHECK-NEXT: (drop
39-
;; CHECK-NEXT: (struct.new_default $struct$1)
39+
;; CHECK-NEXT: (struct.new_default $struct_1)
4040
;; CHECK-NEXT: )
4141
;; CHECK-NEXT: (drop
42-
;; CHECK-NEXT: (struct.new $struct$2
42+
;; CHECK-NEXT: (struct.new $struct_2
4343
;; CHECK-NEXT: (i32.const 10)
4444
;; CHECK-NEXT: )
4545
;; CHECK-NEXT: )
@@ -57,7 +57,7 @@
5757

5858
;; CHECK: (func $another-func (type $1)
5959
;; CHECK-NEXT: (drop
60-
;; CHECK-NEXT: (struct.new $struct$3
60+
;; CHECK-NEXT: (struct.new $struct_3
6161
;; CHECK-NEXT: (i32.const 100)
6262
;; CHECK-NEXT: )
6363
;; CHECK-NEXT: )
@@ -139,18 +139,18 @@
139139
(type $struct (sub (struct (field (ref null any)))))
140140

141141
;; CHECK: (rec
142-
;; CHECK-NEXT: (type $struct$1 (sub $struct (struct (field anyref))))
142+
;; CHECK-NEXT: (type $struct_1 (sub $struct (struct (field anyref))))
143143

144-
;; CHECK: (type $struct$2 (sub $struct (struct (field anyref))))
144+
;; CHECK: (type $struct_2 (sub $struct (struct (field anyref))))
145145

146-
;; CHECK: (type $struct$3 (sub $struct (struct (field anyref))))
146+
;; CHECK: (type $struct_3 (sub $struct (struct (field anyref))))
147147

148148
;; CHECK: (func $foo (type $0) (param $any anyref) (param $array arrayref)
149149
;; CHECK-NEXT: (drop
150-
;; CHECK-NEXT: (struct.new_default $struct$1)
150+
;; CHECK-NEXT: (struct.new_default $struct_1)
151151
;; CHECK-NEXT: )
152152
;; CHECK-NEXT: (drop
153-
;; CHECK-NEXT: (struct.new $struct$2
153+
;; CHECK-NEXT: (struct.new $struct_2
154154
;; CHECK-NEXT: (ref.null none)
155155
;; CHECK-NEXT: )
156156
;; CHECK-NEXT: )
@@ -160,7 +160,7 @@
160160
;; CHECK-NEXT: )
161161
;; CHECK-NEXT: )
162162
;; CHECK-NEXT: (drop
163-
;; CHECK-NEXT: (struct.new $struct$3
163+
;; CHECK-NEXT: (struct.new $struct_3
164164
;; CHECK-NEXT: (local.get $array)
165165
;; CHECK-NEXT: )
166166
;; CHECK-NEXT: )
@@ -216,36 +216,36 @@
216216
(elem func $array.new)
217217

218218
;; CHECK: (rec
219-
;; CHECK-NEXT: (type $array$1 (sub $array (array (mut anyref))))
219+
;; CHECK-NEXT: (type $array_1 (sub $array (array (mut anyref))))
220220

221-
;; CHECK: (type $array$2 (sub $array (array (mut anyref))))
221+
;; CHECK: (type $array_2 (sub $array (array (mut anyref))))
222222

223-
;; CHECK: (type $array$3 (sub $array (array (mut anyref))))
223+
;; CHECK: (type $array_3 (sub $array (array (mut anyref))))
224224

225-
;; CHECK: (type $array-func$4 (sub $array-func (array (mut funcref))))
225+
;; CHECK: (type $array-func_4 (sub $array-func (array (mut funcref))))
226226

227-
;; CHECK: (type $array$5 (sub $array (array (mut anyref))))
227+
;; CHECK: (type $array_5 (sub $array (array (mut anyref))))
228228

229-
;; CHECK: (type $array$6 (sub $array (array (mut anyref))))
229+
;; CHECK: (type $array_6 (sub $array (array (mut anyref))))
230230

231231
;; CHECK: (type $9 (func))
232232

233233
;; CHECK: (elem $0 func $array.new)
234234

235235
;; CHECK: (func $array.new (type $1) (param $refined (ref i31)) (param $null-any anyref)
236236
;; CHECK-NEXT: (drop
237-
;; CHECK-NEXT: (array.new_default $array$1
237+
;; CHECK-NEXT: (array.new_default $array_1
238238
;; CHECK-NEXT: (i32.const 5)
239239
;; CHECK-NEXT: )
240240
;; CHECK-NEXT: )
241241
;; CHECK-NEXT: (drop
242-
;; CHECK-NEXT: (array.new $array$2
242+
;; CHECK-NEXT: (array.new $array_2
243243
;; CHECK-NEXT: (ref.null none)
244244
;; CHECK-NEXT: (i32.const 5)
245245
;; CHECK-NEXT: )
246246
;; CHECK-NEXT: )
247247
;; CHECK-NEXT: (drop
248-
;; CHECK-NEXT: (array.new $array$3
248+
;; CHECK-NEXT: (array.new $array_3
249249
;; CHECK-NEXT: (local.get $refined)
250250
;; CHECK-NEXT: (i32.const 5)
251251
;; CHECK-NEXT: )
@@ -289,7 +289,7 @@
289289

290290
;; CHECK: (func $array.new_seg (type $9)
291291
;; CHECK-NEXT: (drop
292-
;; CHECK-NEXT: (array.new_elem $array-func$4 $0
292+
;; CHECK-NEXT: (array.new_elem $array-func_4 $0
293293
;; CHECK-NEXT: (i32.const 0)
294294
;; CHECK-NEXT: (i32.const 3)
295295
;; CHECK-NEXT: )
@@ -308,12 +308,12 @@
308308

309309
;; CHECK: (func $array.new_fixed (type $1) (param $refined (ref i31)) (param $null-any anyref)
310310
;; CHECK-NEXT: (drop
311-
;; CHECK-NEXT: (array.new_fixed $array$5 1
311+
;; CHECK-NEXT: (array.new_fixed $array_5 1
312312
;; CHECK-NEXT: (ref.null none)
313313
;; CHECK-NEXT: )
314314
;; CHECK-NEXT: )
315315
;; CHECK-NEXT: (drop
316-
;; CHECK-NEXT: (array.new_fixed $array$6 1
316+
;; CHECK-NEXT: (array.new_fixed $array_6 1
317317
;; CHECK-NEXT: (local.get $refined)
318318
;; CHECK-NEXT: )
319319
;; CHECK-NEXT: )
@@ -369,14 +369,14 @@
369369
;; CHECK: (type $empty (sub (struct )))
370370
(type $empty (sub (struct)))
371371

372-
;; CHECK: (type $empty$1 (sub $empty (struct )))
372+
;; CHECK: (type $empty_1 (sub $empty (struct )))
373373

374374
;; CHECK: (type $2 (func (param anyref)))
375375

376376
;; CHECK: (type $struct (sub (struct (field externref) (field anyref) (field externref))))
377377
(type $struct (sub (struct externref anyref externref)))
378378

379-
;; CHECK: (global $g (mut anyref) (struct.new_default $empty$1))
379+
;; CHECK: (global $g (mut anyref) (struct.new_default $empty_1))
380380
(global $g (mut anyref) (struct.new $empty))
381381

382382
;; CHECK: (func $0 (type $2) (param $param anyref)
@@ -428,13 +428,13 @@
428428
;; CHECK: (type $2 (func (param (ref $subarray))))
429429

430430
;; CHECK: (rec
431-
;; CHECK-NEXT: (type $array$1 (sub $array (array (mut f32))))
431+
;; CHECK-NEXT: (type $array_1 (sub $array (array (mut f32))))
432432

433433
;; CHECK: (type $4 (struct (field (mut i32)) (field (mut i32)) (field (mut f64)) (field (mut f64)) (field (mut i32)) (field (mut f64)) (field (mut f64)) (field (mut i32)) (field (mut i32)) (field (mut i32)) (field (mut i32))))
434434

435435
;; CHECK: (func $1 (type $2) (param $ref (ref $subarray))
436436
;; CHECK-NEXT: (drop
437-
;; CHECK-NEXT: (array.new_default $array$1
437+
;; CHECK-NEXT: (array.new_default $array_1
438438
;; CHECK-NEXT: (i32.const 64)
439439
;; CHECK-NEXT: )
440440
;; CHECK-NEXT: )
@@ -455,15 +455,15 @@
455455
;; CHECK: (type $A (sub (struct )))
456456
(type $A (sub (struct)))
457457

458-
;; CHECK: (type $A$1 (sub $A (struct )))
458+
;; CHECK: (type $A_1 (sub $A (struct )))
459459

460460
;; CHECK: (type $2 (func (result (ref $A))))
461461

462462
;; CHECK: (func $0 (type $2) (result (ref $A))
463-
;; CHECK-NEXT: (block $label (result (ref $A$1))
463+
;; CHECK-NEXT: (block $label (result (ref $A_1))
464464
;; CHECK-NEXT: (drop
465-
;; CHECK-NEXT: (br_on_cast $label (ref $A$1) (ref $A$1)
466-
;; CHECK-NEXT: (struct.new_default $A$1)
465+
;; CHECK-NEXT: (br_on_cast $label (ref $A_1) (ref $A_1)
466+
;; CHECK-NEXT: (struct.new_default $A_1)
467467
;; CHECK-NEXT: )
468468
;; CHECK-NEXT: )
469469
;; CHECK-NEXT: (unreachable)

test/lit/passes/type-ssa_and_merging.wast

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
;; NOP: (type $2 (func (result i32)))
2323

2424
;; NOP: (import "a" "b" (func $import (type $2) (result i32)))
25-
;; YES: (type $A$2 (sub $A (struct )))
25+
;; YES: (type $A_2 (sub $A (struct )))
2626

27-
;; YES: (type $A$1 (sub $A (struct )))
27+
;; YES: (type $A_1 (sub $A (struct )))
2828

2929
;; YES: (import "a" "b" (func $import (type $0) (result i32)))
3030
(import "a" "b" (func $import (result i32)))

test/lit/recursive-types.wast

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,47 +8,47 @@
88
(type (func (param (ref null 1)) (result (ref null 1))))
99
(type (func (param (ref null 0)) (result (ref null 1))))
1010
(rec
11-
;; CHECK: (type $type$0 (func (param (ref null $type$0)) (result (ref null $type$0))))
11+
;; CHECK: (type $type (func (param (ref null $type)) (result (ref null $type))))
1212

1313
;; CHECK: (rec
14-
;; CHECK-NEXT: (type $f3 (sub (func (param (ref null $type$2)) (result (ref null $f3)))))
14+
;; CHECK-NEXT: (type $f3 (sub (func (param (ref null $type_2)) (result (ref null $f3)))))
1515
(type $f3 (sub (func (param (ref null 4)) (result (ref null 3)))))
1616
(type (sub $f3 (func (param (ref null 3)) (result (ref null 4)))))
1717
)
1818

19-
;; CHECK: (type $type$2 (sub $f3 (func (param (ref null $f3)) (result (ref null $type$2)))))
19+
;; CHECK: (type $type_2 (sub $f3 (func (param (ref null $f3)) (result (ref null $type_2)))))
2020

21-
;; CHECK: (type $type$1 (func (param (ref null $type$0)) (result (ref null $type$0))))
21+
;; CHECK: (type $type_1 (func (param (ref null $type)) (result (ref null $type))))
2222

23-
;; CHECK: (func $foo (type $type$0) (param $0 (ref null $type$0)) (result (ref null $type$0))
23+
;; CHECK: (func $foo (type $type) (param $0 (ref null $type)) (result (ref null $type))
2424
;; CHECK-NEXT: (unreachable)
2525
;; CHECK-NEXT: )
2626
(func $foo (type 0)
2727
(unreachable)
2828
)
2929

30-
;; CHECK: (func $bar (type $type$0) (param $0 (ref null $type$0)) (result (ref null $type$0))
30+
;; CHECK: (func $bar (type $type) (param $0 (ref null $type)) (result (ref null $type))
3131
;; CHECK-NEXT: (unreachable)
3232
;; CHECK-NEXT: )
3333
(func $bar (type 1)
3434
(unreachable)
3535
)
3636

37-
;; CHECK: (func $baz (type $type$1) (param $0 (ref null $type$0)) (result (ref null $type$0))
37+
;; CHECK: (func $baz (type $type_1) (param $0 (ref null $type)) (result (ref null $type))
3838
;; CHECK-NEXT: (unreachable)
3939
;; CHECK-NEXT: )
4040
(func $baz (type 2)
4141
(unreachable)
4242
)
4343

44-
;; CHECK: (func $qux (type $f3) (param $0 (ref null $type$2)) (result (ref null $f3))
44+
;; CHECK: (func $qux (type $f3) (param $0 (ref null $type_2)) (result (ref null $f3))
4545
;; CHECK-NEXT: (unreachable)
4646
;; CHECK-NEXT: )
4747
(func $qux (type 3)
4848
(unreachable)
4949
)
5050

51-
;; CHECK: (func $quux (type $type$2) (param $0 (ref null $f3)) (result (ref null $type$2))
51+
;; CHECK: (func $quux (type $type_2) (param $0 (ref null $f3)) (result (ref null $type_2))
5252
;; CHECK-NEXT: (unreachable)
5353
;; CHECK-NEXT: )
5454
(func $quux (type 4)

0 commit comments

Comments
 (0)