Skip to content

Commit fc1a391

Browse files
authored
[Custom Descriptors] Fix TypeMerging cast check (#7873)
Due to an accidental use of the wrong variable, we were not properly checking for casts to descriptor types that should have blocked merging.
1 parent 600ccd0 commit fc1a391

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed

src/passes/TypeMerging.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ bool TypeMerging::merge(MergeKind kind) {
418418
auto chain = type.getDescriptorChain();
419419
bool hasCast =
420420
std::any_of(chain.begin(), chain.end(), [&](HeapType t) -> bool {
421-
return castTypes.count(type);
421+
return castTypes.count(t);
422422
});
423423
if (hasCast || !privateTypes.count(type)) {
424424
ensurePartition(type);

test/lit/passes/type-merging-desc.wast

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,49 @@
243243
(global $B.desc (ref null $B.desc) (ref.null none))
244244
)
245245

246+
(module
247+
;; These chains could be merged except for the cast to the descriptor in the
248+
;; subtype chain.
249+
(rec
250+
;; CHECK: (rec
251+
;; CHECK-NEXT: (type $A (sub (descriptor $A.desc (struct))))
252+
(type $A (sub (descriptor $A.desc (struct))))
253+
;; CHECK: (type $A.desc (sub (describes $A (struct))))
254+
(type $A.desc (sub (describes $A (struct))))
255+
;; CHECK: (type $B (sub $A (descriptor $B.desc (struct))))
256+
(type $B (sub $A (descriptor $B.desc (struct))))
257+
;; CHECK: (type $B.desc (sub $A.desc (describes $B (struct))))
258+
(type $B.desc (sub $A.desc (describes $B (struct))))
259+
)
260+
261+
;; CHECK: (type $4 (func))
262+
263+
;; CHECK: (global $A (ref null $A) (ref.null none))
264+
(global $A (ref null $A) (ref.null none))
265+
;; CHECK: (global $A.desc (ref null $A.desc) (ref.null none))
266+
(global $A.desc (ref null $A.desc) (ref.null none))
267+
;; CHECK: (global $B (ref null $B) (ref.null none))
268+
(global $B (ref null $B) (ref.null none))
269+
;; CHECK: (global $B.desc (ref null $B.desc) (ref.null none))
270+
(global $B.desc (ref null $B.desc) (ref.null none))
271+
272+
;; CHECK: (func $cast-desc (type $4)
273+
;; CHECK-NEXT: (drop
274+
;; CHECK-NEXT: (ref.test (ref (exact $B.desc))
275+
;; CHECK-NEXT: (struct.new_default $B.desc)
276+
;; CHECK-NEXT: )
277+
;; CHECK-NEXT: )
278+
;; CHECK-NEXT: )
279+
(func $cast-desc
280+
(drop
281+
(ref.test (ref $B.desc)
282+
(struct.new $B.desc)
283+
)
284+
)
285+
)
286+
)
287+
288+
246289
(module
247290
;; These chains could be merged except for the exact cast to the descriptor in
248291
;; the supertype chain.
@@ -285,6 +328,46 @@
285328
)
286329
)
287330

331+
(module
332+
;; Like above, but now the cast to the supertype descriptor is not exact, so
333+
;; we can still merge.
334+
(rec
335+
;; CHECK: (rec
336+
;; CHECK-NEXT: (type $A (sub (descriptor $A.desc (struct))))
337+
(type $A (sub (descriptor $A.desc (struct))))
338+
;; CHECK: (type $A.desc (sub (describes $A (struct))))
339+
(type $A.desc (sub (describes $A (struct))))
340+
(type $B (sub $A (descriptor $B.desc (struct))))
341+
(type $B.desc (sub $A.desc (describes $B (struct))))
342+
)
343+
344+
;; CHECK: (type $2 (func (param anyref)))
345+
346+
;; CHECK: (global $A (ref null $A) (ref.null none))
347+
(global $A (ref null $A) (ref.null none))
348+
;; CHECK: (global $A.desc (ref null $A.desc) (ref.null none))
349+
(global $A.desc (ref null $A.desc) (ref.null none))
350+
;; CHECK: (global $B (ref null $A) (ref.null none))
351+
(global $B (ref null $B) (ref.null none))
352+
;; CHECK: (global $B.desc (ref null $A.desc) (ref.null none))
353+
(global $B.desc (ref null $B.desc) (ref.null none))
354+
355+
;; CHECK: (func $cast-desc (type $2) (param $0 anyref)
356+
;; CHECK-NEXT: (drop
357+
;; CHECK-NEXT: (ref.test (ref $A.desc)
358+
;; CHECK-NEXT: (local.get $0)
359+
;; CHECK-NEXT: )
360+
;; CHECK-NEXT: )
361+
;; CHECK-NEXT: )
362+
(func $cast-desc (param anyref)
363+
(drop
364+
(ref.test (ref $A.desc)
365+
(local.get 0)
366+
)
367+
)
368+
)
369+
)
370+
288371
(module
289372
;; $A chain and $B differ only in the in index they reference in the $X chain.
290373
;; They should not be merged.

0 commit comments

Comments
 (0)