Skip to content

[Heap2Local2] Support ref.cast and ref.cast_desc_eq - #9410

Merged
tlively merged 3 commits into
mainfrom
heap2local2-casts
Nov 21, 2026
Merged

tlively merged 3 commits into
mainfrom
heap2local2-casts

Conversation

@tlively

@tlively tlively commented Nov 20, 2026

Copy link
Copy Markdown
Member

Track ref.cast and ref.cast_desc_eq instructions as CastSites during origin propagation so casts on candidate allocations propagate exact origins (or Bottom when statically trapping) without escaping their operands, and lower casts on non-escaping allocations in Optimizer::visitRefCast while preserving operand evaluation order and null/descriptor trap semantics.

Track ref.cast and ref.cast_desc_eq instructions as CastSites during origin propagation so casts on candidate allocations propagate exact origins (or Bottom when statically trapping) without escaping their operands, and lower casts on non-escaping allocations in Optimizer::visitRefCast while preserving operand evaluation order and null/descriptor trap semantics.
@tlively
tlively requested a review from kripken November 20, 2026 01:50
Comment thread src/passes/Heap2Local2.cpp Outdated
// Casts do not escape their inputs, but depending on the input origin,
// descriptor reachability, and target subtype check they either propagate an
// allocation origin, trap (Bot), or produce Top, so Phase 2 must re-evaluate
// them as input origins are discovered rather than using plain identity edges.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't quite see why we can't treat them as identity edges. What specific thing would not get optimized in that case?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We wouldn't be able to recognize and optimize failing casts, which means we would only be able to safely optimize flows through casts at all if we assume traps never happen. That would also block us from optimizing branching casts on the future.

@kripken kripken Nov 20, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Would failing casts not be optimized by other passes? IIUC we need the cast to definitely fail so that we can infer here, but such a cast should be removed anyhow. Or is this more subtle?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I guess GUFA probably does at least as much data flow analysis as this pass, so it could probably optimize all the same casts. But then you'd have to come back and do another Heap2Local{2} afterward to fully optimize. And GUFA is pretty heavyweight, anyway. I think it would be best to optimize here because we can.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I apologize if it seems like I'm pushing back here, but my gut feeling is that this kind of cast analysis is separate from this pass. Here is a concrete experiment, a LocalGUFA pass:

https://github.com/WebAssembly/binaryen/pull/9413/changes#diff-e263ea25b39bfabfbd16cb785c023b7aff6969b1e120358489fe0ad0c493abb5R672

That links to the tests that show it can optimize casts using data flow. I haven't investigated this AI code in detail yet, but this might be a viable path to get better cast opts at the function level - it is pretty simple (given GUFA exists) and should be fast enough to run in the main pipeline. If we landed that pass, I think we wouldn't need cast opts here (or elsewhere), what do you think?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It seems plausible, but my top priority here is to meet and exceed parity with Heap2Local so we can confidently remove the old implementation.

Also, this cast analysis is necessary to optimize descriptor casts that may or may not succeed by lowering them to ref.eq on the descriptor value. No other pass would be able to do that because it is inherently an SROA optimization.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ok, if old Heap2Local did this kind of thing I guess I won't object.

Comment thread src/passes/Heap2Local2.cpp Outdated
// Skip sink nodes that have no outgoing identity edges to propagate into.
// For each node, the index of the cast it flows into (either as ref or desc),
// if such a cast exists.
std::vector<std::optional<Index>> nodeCasts;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is starting to represent the graph using multiple arrays. I wonder if a single unified graph would be simpler? That is, rather than have outOffsets and nodeCasts (as managed by e.g. hasOutgoing below) there could be a single outOffsets but it would have additional information on the edge, if it is a cast (or if we have the Expression* there, maybe we don't even need that, and can just check if we are reaching a RefCast).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, good idea. This enabled a lot of simplification.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes, this looks much nicer now!

for (const auto& [u, v] : state.edges) {
if (state.nodeExprs[v]->is<RefCast>()) {
continue;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm not following this, how can we tell this is the descriptor operand and not the ref operand?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We can't tell, but it doesn't matter. Only the ref value flows through RefCast, so it is never a source of mixing. I've expanded the comment.

@tlively
tlively enabled auto-merge (squash) November 21, 2026 00:44
@tlively
tlively merged commit 4408fed into main Nov 21, 2026
18 checks passed
@tlively
tlively deleted the heap2local2-casts branch November 21, 2026 00:50
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