Skip to content

[WasmGC] Heap2Local: Optimize RefCast failures #6727

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 28 commits into from
Jul 11, 2024

Conversation

kripken
Copy link
Member

@kripken kripken commented Jul 11, 2024

Previously we just did not optimize cases where our escape analysis showed an
allocation flowed into a cast that failed. However, after inlining there can be
real-world cases where that happens, even in traps-never-happen mode (if the
cast is behind a conditional branch), so it seems worth optimizing.

Note that a field was added to the struct $B in the test, which is needed for some of
the new checks. That extra field causes minor changes to existing tests (the extra
value is dropped).

@kripken kripken requested a review from tlively July 11, 2024 16:42
;; CHECK-NEXT: (local.get $3)
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.set $2
;; CHECK-NEXT: (local.get $4)
Copy link
Member Author

Choose a reason for hiding this comment

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

These changes are what I meant in the second paragraph - $B has an extra field now, which is also handled here (with value 1). This does not affect what the test is testing.

// succeeds or fails.
if (Type::isSubType(allocation->type, curr->type)) {
// The cast succeeds, so it is a no-op, and we can skip it.
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.

Even if the cast is known to succeed, we might still need it to make the static types work out, right? This replacement is only valid if curr->ref->type is a subtype of curr->type, not necessarily if allocation->type is a subtype.

This isn't a new issue, so I must be missing something that makes this safe.

Aha, it's because we're going to remove the allocation entirely. This is worth a comment!

Copy link
Member Author

Choose a reason for hiding this comment

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

Sounds good, I added a comment.

@kripken kripken merged commit 020e6cc into WebAssembly:main Jul 11, 2024
13 checks passed
@kripken kripken deleted the heap2local.castfailz branch July 11, 2024 19:34
kripken added a commit that referenced this pull request Jul 18, 2024
Followup to #6727 which added support for failing casts in Struct2Local, but it
turns out that it required Array2Struct changes as well. Specifically, when we
turn an array into a struct then casts can look like they behave differently
(what used to be an array input, becomes a struct), so like with RefTest that we
already handled, check if the cast succeeds in the original form and handle
that.
@gkdn gkdn mentioned this pull request Aug 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants