Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 12 additions & 0 deletions src/passes/GlobalTypeOptimization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,18 @@ struct GlobalTypeOptimization : public Pass {
curr->index = newIndex;
}

void visitRefCast(RefCast* curr) {
// Unreachable ref.cast_desc instructions would not have been counted as
// reading the descriptor field, so their descriptor operands may no
// longer be descriptors. This is invalid, so replace such casts
// entirely.
if (curr->type == Type::unreachable && curr->desc &&
curr->desc->type != Type::unreachable) {
assert(curr->ref->type == Type::unreachable);
replaceCurrent(curr->ref);
Copy link
Member

Choose a reason for hiding this comment

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

Don't we need to drop the desc for any effects it might have?

Copy link
Member Author

Choose a reason for hiding this comment

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

No, because the preceding ref operand is unreachable. The desc operand will never be executed.

Copy link
Member

Choose a reason for hiding this comment

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

Oh right, thanks.

}
}

void visitFunction(Function* curr) {
if (needEHFixups) {
EHUtils::handleBlockNestedPops(curr, *getModule());
Expand Down
31 changes: 31 additions & 0 deletions test/lit/passes/gto-desc.wast
Original file line number Diff line number Diff line change
Expand Up @@ -1396,3 +1396,34 @@
)
)

;; We can optimize away the descriptor since its only use is unreachable, but
;; we must update that use to remain valid despite being unreachable.
(module
(rec
;; CHECK: (rec
;; CHECK-NEXT: (type $struct (sub (struct)))
(type $struct (sub (descriptor $desc (struct))))
;; CHECK: (type $desc (sub (struct)))
(type $desc (sub (describes $struct (struct))))
)
;; CHECK: (type $2 (func))

;; CHECK: (func $test (type $2)
;; CHECK-NEXT: (local $struct (ref $struct))
;; CHECK-NEXT: (local $desc (ref $desc))
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (unreachable)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $test
(local $struct (ref $struct))
(local $desc (ref $desc))
(drop
(ref.cast_desc (ref $struct)
(unreachable)
(struct.new $desc)
)
)
)
)

Loading