Skip to content

Commit a640374

Browse files
authored
Fix supertype counts when collecting heap types (#6905)
We previous incremented the use count for a declared supertype only if it was also a type we had never seen before. Fix the count by treating the supertype the same as any other type used in a type definition. Update tests accordingly, including by manually moving input types around to better match the output.
1 parent fbbdc64 commit a640374

File tree

10 files changed

+75
-95
lines changed

10 files changed

+75
-95
lines changed

src/ir/module-utils.cpp

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -506,9 +506,7 @@ InsertOrderedMap<HeapType, HeapTypeInfo> collectHeapTypeInfo(
506506
while (!newTypes.empty()) {
507507
while (!newTypes.empty()) {
508508
auto ht = newTypes.pop();
509-
// TODO: Use getReferencedHeapTypes instead and remove separate
510-
// consideration of supertypes below.
511-
for (HeapType child : ht.getHeapTypeChildren()) {
509+
for (HeapType child : ht.getReferencedHeapTypes()) {
512510
if (!child.isBasic()) {
513511
if (!info.contains(child)) {
514512
noteNewType(child);
@@ -517,15 +515,6 @@ InsertOrderedMap<HeapType, HeapTypeInfo> collectHeapTypeInfo(
517515
}
518516
}
519517

520-
if (auto super = ht.getDeclaredSuperType()) {
521-
if (!info.contains(*super)) {
522-
noteNewType(*super);
523-
// TODO: This should be unconditional for the count to be correct, but
524-
// this will be moot once we use getReferencedHeapTypes above.
525-
info.note(*super);
526-
}
527-
}
528-
529518
// Make sure we've noted the complete recursion group of each type as
530519
// well.
531520
if (inclusion != TypeInclusion::UsedIRTypes) {

test/example/c-api-kitchen-sink.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3037,9 +3037,9 @@ TypeBuilderErrorReasonForwardSupertypeReference: 2
30373037
TypeBuilderErrorReasonForwardChildReference: 3
30383038
module with recursive GC types:
30393039
(module
3040+
(type $SomeStruct (sub (struct (field $SomeField (mut (ref null $SomeStruct))))))
30403041
(type $SomeArray (sub (array (mut (ref null $SomeArray)))))
30413042
(type $SomeSignature (sub (func (param (ref null $SomeSignature) (ref null $SomeArray)) (result (ref null $SomeSignature)))))
3042-
(type $SomeStruct (sub (struct (field $SomeField (mut (ref null $SomeStruct))))))
30433043
(type $3 (func))
30443044
(type $SomeSubStruct (sub $SomeStruct (struct (field $SomeField (mut (ref null $SomeStruct))) (field $SomePackedField i8))))
30453045
(func $test (type $3)

test/lit/basic/subtypes.wast

Lines changed: 38 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -11,57 +11,51 @@
1111

1212
;; Test that we can roundtrip struct and array types
1313
(module
14-
;; Arrays
14+
;; Recursive structs
1515
;; CHECK-TEXT: (type $struct-rec-one (sub (struct (field (ref $struct-rec-one)))))
16-
17-
;; CHECK-TEXT: (type $struct-rec-two (sub $struct-rec-one (struct (field (ref $struct-rec-two)) (field (ref $struct-rec-two)))))
18-
19-
;; CHECK-TEXT: (type $vector-i32 (array i32))
2016
;; CHECK-BIN: (type $struct-rec-one (sub (struct (field (ref $struct-rec-one)))))
21-
17+
(type $struct-rec-one (sub (struct
18+
(field (ref $struct-rec-one))
19+
)))
20+
;; CHECK-TEXT: (type $struct-rec-two (sub $struct-rec-one (struct (field (ref $struct-rec-two)) (field (ref $struct-rec-two)))))
2221
;; CHECK-BIN: (type $struct-rec-two (sub $struct-rec-one (struct (field (ref $struct-rec-two)) (field (ref $struct-rec-two)))))
22+
(type $struct-rec-two (sub $struct-rec-one (struct
23+
(field (ref $struct-rec-two))
24+
(field (ref $struct-rec-two))
25+
)))
2326

24-
;; CHECK-BIN: (type $vector-i32 (array i32))
25-
(type $vector-i32 (array i32))
26-
27+
;; Non-recursive structs
2728
;; CHECK-TEXT: (type $struct-any (sub (struct (field (ref any)))))
28-
29-
;; CHECK-TEXT: (type $struct-i31 (sub $struct-any (struct (field (ref i31)))))
30-
31-
;; CHECK-TEXT: (type $vector-any (sub (array (ref any))))
3229
;; CHECK-BIN: (type $struct-any (sub (struct (field (ref any)))))
33-
30+
(type $struct-any (sub (struct
31+
(field (ref any))
32+
)))
33+
;; CHECK-TEXT: (type $struct-i31 (sub $struct-any (struct (field (ref i31)))))
3434
;; CHECK-BIN: (type $struct-i31 (sub $struct-any (struct (field (ref i31)))))
35+
(type $struct-i31 (sub $struct-any (struct
36+
(field (ref i31))
37+
)))
3538

39+
;; Arrays
40+
;; CHECK-TEXT: (type $vector-any (sub (array (ref any))))
3641
;; CHECK-BIN: (type $vector-any (sub (array (ref any))))
3742
(type $vector-any (sub (array (ref any))))
43+
44+
;; CHECK-TEXT: (type $vector-i32 (array i32))
45+
;; CHECK-BIN: (type $vector-i32 (array i32))
46+
(type $vector-i32 (array i32))
47+
3848
;; CHECK-TEXT: (type $vector-i31 (sub $vector-any (array (ref i31))))
3949
;; CHECK-BIN: (type $vector-i31 (sub $vector-any (array (ref i31))))
4050
(type $vector-i31 (sub $vector-any (array (ref i31))))
4151

42-
;; Structs
43-
(type $struct-any (sub (struct
44-
(field (ref any))
45-
)))
46-
(type $struct-i31 (sub $struct-any (struct
47-
(field (ref i31))
48-
)))
4952
;; CHECK-TEXT: (type $struct-i31_any (sub $struct-i31 (struct (field (ref i31)) (field (ref any)))))
5053
;; CHECK-BIN: (type $struct-i31_any (sub $struct-i31 (struct (field (ref i31)) (field (ref any)))))
5154
(type $struct-i31_any (sub $struct-i31(struct
5255
(field (ref i31))
5356
(field (ref any))
5457
)))
5558

56-
;; Recursive structs
57-
(type $struct-rec-one (sub (struct
58-
(field (ref $struct-rec-one))
59-
)))
60-
(type $struct-rec-two (sub $struct-rec-one (struct
61-
(field (ref $struct-rec-two))
62-
(field (ref $struct-rec-two))
63-
)))
64-
6559
;; CHECK-TEXT: (type $8 (func (param (ref $vector-i32) (ref null $vector-i32))))
6660

6761
;; CHECK-TEXT: (type $9 (func (param (ref $vector-i31) (ref $vector-any))))
@@ -166,47 +160,47 @@
166160

167161
;; CHECK-BIN-NODEBUG: (type $1 (sub $0 (struct (field (ref $1)) (field (ref $1)))))
168162

169-
;; CHECK-BIN-NODEBUG: (type $2 (array i32))
163+
;; CHECK-BIN-NODEBUG: (type $2 (sub (struct (field (ref any)))))
170164

171-
;; CHECK-BIN-NODEBUG: (type $3 (sub (struct (field (ref any)))))
165+
;; CHECK-BIN-NODEBUG: (type $3 (sub $2 (struct (field (ref i31)))))
172166

173-
;; CHECK-BIN-NODEBUG: (type $4 (sub $3 (struct (field (ref i31)))))
167+
;; CHECK-BIN-NODEBUG: (type $4 (sub (array (ref any))))
174168

175-
;; CHECK-BIN-NODEBUG: (type $5 (sub (array (ref any))))
169+
;; CHECK-BIN-NODEBUG: (type $5 (array i32))
176170

177-
;; CHECK-BIN-NODEBUG: (type $6 (sub $5 (array (ref i31))))
171+
;; CHECK-BIN-NODEBUG: (type $6 (sub $4 (array (ref i31))))
178172

179-
;; CHECK-BIN-NODEBUG: (type $7 (sub $4 (struct (field (ref i31)) (field (ref any)))))
173+
;; CHECK-BIN-NODEBUG: (type $7 (sub $3 (struct (field (ref i31)) (field (ref any)))))
180174

181-
;; CHECK-BIN-NODEBUG: (type $8 (func (param (ref $2) (ref null $2))))
175+
;; CHECK-BIN-NODEBUG: (type $8 (func (param (ref $5) (ref null $5))))
182176

183-
;; CHECK-BIN-NODEBUG: (type $9 (func (param (ref $6) (ref $5))))
177+
;; CHECK-BIN-NODEBUG: (type $9 (func (param (ref $6) (ref $4))))
184178

185-
;; CHECK-BIN-NODEBUG: (type $10 (func (param (ref $4) (ref $3))))
179+
;; CHECK-BIN-NODEBUG: (type $10 (func (param (ref $3) (ref $2))))
186180

187-
;; CHECK-BIN-NODEBUG: (type $11 (func (param (ref $4) (ref $7))))
181+
;; CHECK-BIN-NODEBUG: (type $11 (func (param (ref $3) (ref $7))))
188182

189183
;; CHECK-BIN-NODEBUG: (type $12 (func (param (ref $0) (ref $1))))
190184

191-
;; CHECK-BIN-NODEBUG: (func $0 (type $8) (param $0 (ref $2)) (param $1 (ref null $2))
185+
;; CHECK-BIN-NODEBUG: (func $0 (type $8) (param $0 (ref $5)) (param $1 (ref null $5))
192186
;; CHECK-BIN-NODEBUG-NEXT: (local.set $1
193187
;; CHECK-BIN-NODEBUG-NEXT: (local.get $0)
194188
;; CHECK-BIN-NODEBUG-NEXT: )
195189
;; CHECK-BIN-NODEBUG-NEXT: )
196190

197-
;; CHECK-BIN-NODEBUG: (func $1 (type $9) (param $0 (ref $6)) (param $1 (ref $5))
191+
;; CHECK-BIN-NODEBUG: (func $1 (type $9) (param $0 (ref $6)) (param $1 (ref $4))
198192
;; CHECK-BIN-NODEBUG-NEXT: (local.set $1
199193
;; CHECK-BIN-NODEBUG-NEXT: (local.get $0)
200194
;; CHECK-BIN-NODEBUG-NEXT: )
201195
;; CHECK-BIN-NODEBUG-NEXT: )
202196

203-
;; CHECK-BIN-NODEBUG: (func $2 (type $10) (param $0 (ref $4)) (param $1 (ref $3))
197+
;; CHECK-BIN-NODEBUG: (func $2 (type $10) (param $0 (ref $3)) (param $1 (ref $2))
204198
;; CHECK-BIN-NODEBUG-NEXT: (local.set $1
205199
;; CHECK-BIN-NODEBUG-NEXT: (local.get $0)
206200
;; CHECK-BIN-NODEBUG-NEXT: )
207201
;; CHECK-BIN-NODEBUG-NEXT: )
208202

209-
;; CHECK-BIN-NODEBUG: (func $3 (type $11) (param $0 (ref $4)) (param $1 (ref $7))
203+
;; CHECK-BIN-NODEBUG: (func $3 (type $11) (param $0 (ref $3)) (param $1 (ref $7))
210204
;; CHECK-BIN-NODEBUG-NEXT: (local.set $0
211205
;; CHECK-BIN-NODEBUG-NEXT: (local.get $1)
212206
;; CHECK-BIN-NODEBUG-NEXT: )

test/lit/isorecursive-good.wast

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525
)
2626

2727
(rec
28-
;; CHECK: (type $final-root (struct))
29-
3028
;; CHECK: (rec
3129
;; CHECK-NEXT: (type $super-func (sub (func (param (ref $sub-array)) (result (ref $super-array)))))
3230
(type $super-func (sub (func (param (ref $sub-array)) (result (ref $super-array)))))
@@ -36,6 +34,7 @@
3634
(type $final-func (sub final $sub-func (func (param (ref $super-array)) (result (ref $final-array)))))
3735
)
3836

37+
;; CHECK: (type $final-root (struct))
3938
(type $final-root (sub final (struct)))
4039

4140
;; CHECK: (func $make-super-struct (type $10) (result (ref $super-struct))

test/lit/passes/dae-gc-refine-return.wast

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
)
5656

5757
;; Refine the return type based on the value flowing out.
58-
;; CHECK: (func $refine-return-flow (type $3) (result i31ref)
58+
;; CHECK: (func $refine-return-flow (type $4) (result i31ref)
5959
;; CHECK-NEXT: (local $temp anyref)
6060
;; CHECK-NEXT: (local $i31 i31ref)
6161
;; CHECK-NEXT: (local.set $temp
@@ -71,7 +71,7 @@
7171

7272
(local.get $i31)
7373
)
74-
;; CHECK: (func $call-refine-return-flow (type $3) (result i31ref)
74+
;; CHECK: (func $call-refine-return-flow (type $4) (result i31ref)
7575
;; CHECK-NEXT: (local $temp anyref)
7676
;; CHECK-NEXT: (local.set $temp
7777
;; CHECK-NEXT: (call $call-refine-return-flow)
@@ -105,7 +105,7 @@
105105
)
106106

107107
;; Refine the return type based on a return.
108-
;; CHECK: (func $refine-return-return (type $3) (result i31ref)
108+
;; CHECK: (func $refine-return-return (type $4) (result i31ref)
109109
;; CHECK-NEXT: (local $temp anyref)
110110
;; CHECK-NEXT: (local $i31 i31ref)
111111
;; CHECK-NEXT: (local.set $temp
@@ -125,7 +125,7 @@
125125
)
126126

127127
;; Refine the return type based on multiple values.
128-
;; CHECK: (func $refine-return-many (type $3) (result i31ref)
128+
;; CHECK: (func $refine-return-many (type $4) (result i31ref)
129129
;; CHECK-NEXT: (local $temp anyref)
130130
;; CHECK-NEXT: (local $i31 i31ref)
131131
;; CHECK-NEXT: (local.set $temp
@@ -353,7 +353,7 @@
353353
)
354354
(return_call $tail-callee)
355355
)
356-
;; CHECK: (func $tail-call-caller (type $4)
356+
;; CHECK: (func $tail-call-caller (type $5)
357357
;; CHECK-NEXT: (drop
358358
;; CHECK-NEXT: (call $tail-caller-yes)
359359
;; CHECK-NEXT: )
@@ -410,7 +410,7 @@
410410
)
411411
(return_call_indirect (type $"return_{}") (i32.const 0))
412412
)
413-
;; CHECK: (func $tail-call-caller-indirect (type $4)
413+
;; CHECK: (func $tail-call-caller-indirect (type $5)
414414
;; CHECK-NEXT: (drop
415415
;; CHECK-NEXT: (call $tail-caller-indirect-yes)
416416
;; CHECK-NEXT: )
@@ -484,7 +484,7 @@
484484
;; should not hit an assertion on such things.
485485
(return_call_ref $"return_{}" (unreachable))
486486
)
487-
;; CHECK: (func $tail-call-caller-call_ref (type $4)
487+
;; CHECK: (func $tail-call-caller-call_ref (type $5)
488488
;; CHECK-NEXT: (drop
489489
;; CHECK-NEXT: (call $tail-caller-call_ref-yes)
490490
;; CHECK-NEXT: )

test/lit/passes/gufa-tnh.wast

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,8 +1346,6 @@
13461346
(type $A (sub (struct (field (mut i32)))))
13471347

13481348
(rec
1349-
;; CHECK: (type $1 (func (param (ref null $A))))
1350-
13511349
;; CHECK: (rec
13521350
;; CHECK-NEXT: (type $B1 (sub $A (struct (field (mut i32)))))
13531351
(type $B1 (sub $A (struct (field (mut i32)))))
@@ -1359,11 +1357,13 @@
13591357
(type $C1 (sub $B1 (struct (field (mut i32)))))
13601358
)
13611359

1360+
;; CHECK: (type $4 (func (param (ref null $A))))
1361+
13621362
;; CHECK: (type $5 (func (param anyref)))
13631363

13641364
;; CHECK: (export "caller" (func $caller))
13651365

1366-
;; CHECK: (func $called (type $1) (param $x (ref null $A))
1366+
;; CHECK: (func $called (type $4) (param $x (ref null $A))
13671367
;; CHECK-NEXT: (drop
13681368
;; CHECK-NEXT: (ref.cast (ref $B1)
13691369
;; CHECK-NEXT: (local.get $x)

test/lit/passes/gufa-vs-cfp.wast

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,11 +1145,11 @@
11451145
;; different values in the sub-most type. Create the top and bottom types, but
11461146
;; not the middle one.
11471147
(module
1148-
;; CHECK: (type $0 (func))
1149-
11501148
;; CHECK: (type $struct1 (sub (struct (field i32) (field i32))))
11511149
(type $struct1 (sub (struct i32 i32)))
11521150

1151+
;; CHECK: (type $1 (func))
1152+
11531153
;; CHECK: (type $struct2 (sub $struct1 (struct (field i32) (field i32) (field f64) (field f64))))
11541154
(type $struct2 (sub $struct1 (struct i32 i32 f64 f64)))
11551155

@@ -1200,7 +1200,7 @@
12001200
)
12011201
)
12021202

1203-
;; CHECK: (func $get-1 (type $0)
1203+
;; CHECK: (func $get-1 (type $1)
12041204
;; CHECK-NEXT: (drop
12051205
;; CHECK-NEXT: (block (result i32)
12061206
;; CHECK-NEXT: (drop
@@ -1235,7 +1235,7 @@
12351235
)
12361236
)
12371237

1238-
;; CHECK: (func $get-2 (type $0)
1238+
;; CHECK: (func $get-2 (type $1)
12391239
;; CHECK-NEXT: (drop
12401240
;; CHECK-NEXT: (block (result i32)
12411241
;; CHECK-NEXT: (drop
@@ -1295,7 +1295,7 @@
12951295
)
12961296
)
12971297

1298-
;; CHECK: (func $get-3 (type $0)
1298+
;; CHECK: (func $get-3 (type $1)
12991299
;; CHECK-NEXT: (drop
13001300
;; CHECK-NEXT: (block (result i32)
13011301
;; CHECK-NEXT: (drop

0 commit comments

Comments
 (0)