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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions src/passes/Heap2Local.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -890,17 +890,24 @@ struct Struct2Local : PostWalker<Struct2Local> {
}
} else {
assert(allocIsCastRef);
// The cast succeeds iff the optimized allocation's descriptor is the
// same as the given descriptor and traps otherwise.
auto type = allocation->desc->type;
replaceCurrent(builder.blockify(
builder.makeDrop(curr->ref),
builder.makeIf(
builder.makeRefEq(
curr->desc,
builder.makeLocalGet(localIndexes[fields.size()], type)),
builder.makeRefNull(allocation->type.getHeapType()),
builder.makeUnreachable())));
if (!Type::isSubType(allocation->type, curr->type)) {
// The cast fails, so it must trap.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe extend the comment with something like this:

"We mark such failing casts as fully consuming their inputs, so we cannot just emit the explicit descriptor equality check below because it would appear to be able to propagate the optimized allocation on to the parent."

replaceCurrent(builder.blockify(builder.makeDrop(curr->ref),
builder.makeDrop(curr->desc),
builder.makeUnreachable()));
} else {
// The cast succeeds iff the optimized allocation's descriptor is the
// same as the given descriptor and traps otherwise.
auto type = allocation->desc->type;
replaceCurrent(builder.blockify(
builder.makeDrop(curr->ref),
builder.makeIf(
builder.makeRefEq(
curr->desc,
builder.makeLocalGet(localIndexes[fields.size()], type)),
builder.makeRefNull(allocation->type.getHeapType()),
builder.makeUnreachable())));
}
}
} else {
// We know this RefCast receives our allocation, so we can see whether it
Expand Down
48 changes: 48 additions & 0 deletions test/lit/passes/heap2local-desc.wast
Original file line number Diff line number Diff line change
Expand Up @@ -898,3 +898,51 @@
)
)

;; A definitely-failing descriptor cast. We have two pairs of descriptor/
;; describee, and create an $A2 that we try to cast to the unrelated $A.
(module
(rec
;; CHECK: (rec
;; CHECK-NEXT: (type $A (descriptor $B (struct)))
(type $A (descriptor $B (struct)))
;; CHECK: (type $B (describes $A (struct)))
(type $B (describes $A (struct)))

;; CHECK: (type $A2 (descriptor $B2 (struct)))
(type $A2 (descriptor $B2 (struct)))
;; CHECK: (type $B2 (describes $A2 (struct)))
(type $B2 (describes $A2 (struct)))
)

;; CHECK: (type $4 (func (result (ref $A))))

;; CHECK: (func $A (type $4) (result (ref $A))
;; CHECK-NEXT: (local $0 (ref (exact $B2)))
;; CHECK-NEXT: (local $1 (ref (exact $B2)))
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (block (result nullref)
;; CHECK-NEXT: (local.set $1
;; CHECK-NEXT: (struct.new_default $B2)
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.set $0
;; CHECK-NEXT: (local.get $1)
;; CHECK-NEXT: )
;; CHECK-NEXT: (ref.null none)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (block (result nullref)
;; CHECK-NEXT: (ref.null none)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (unreachable)
;; CHECK-NEXT: )
(func $A (result (ref $A))
(ref.cast_desc (ref $A)
(struct.new_default $A2
(struct.new_default $B2)
)
(struct.new_default $B)
)
)
)
Loading