Repository navigation
[Heap2Local2] Support ref.cast and ref.cast_desc_eq - #9410
Conversation
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.
| // 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. |
There was a problem hiding this comment.
I don't quite see why we can't treat them as identity edges. What specific thing would not get optimized in that case?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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:
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Ok, if old Heap2Local did this kind of thing I guess I won't object.
| // 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; |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
Yes, good idea. This enabled a lot of simplification.
There was a problem hiding this comment.
Yes, this looks much nicer now!
| for (const auto& [u, v] : state.edges) { | ||
| if (state.nodeExprs[v]->is<RefCast>()) { | ||
| continue; | ||
| } |
There was a problem hiding this comment.
I'm not following this, how can we tell this is the descriptor operand and not the ref operand?
There was a problem hiding this comment.
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.
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.