[WasmGC] Heap2Local: Optimize RefEq#6703
Conversation
| if (!analyzer.reached.count(curr)) { | ||
| return; | ||
| } |
There was a problem hiding this comment.
Just to make sure I understand: we've done a data flow analysis and determined that this expression is not dead? Is it possible for reached to be nonzero and still have type Type::unreachable below?
There was a problem hiding this comment.
Ah, yeah, "reach" is used in two ways here. reached means that the allocation, the struct.new / array.new, reaches this location. So this first check is just making sure that we only look in the places we traced the path of the allocation.
There was a problem hiding this comment.
Consider
(ref.eq
(unreachable)
(struct.new ..)
)That allocation reaches the ref.eq, but also the ref.eq is unreachable code, separately.
| int32_t result = analyzer.reached.count(curr->left) > 0 && | ||
| analyzer.reached.count(curr->right) > 0; |
There was a problem hiding this comment.
How is this checking whether the expression is compared to itself?
There was a problem hiding this comment.
reached is the places the allocation reaches. So if the allocation is on both sides, we must be equal. If the allocation reached only one, then since it did not escape to a place we can't analyze, the other arm must be something totally different.
There was a problem hiding this comment.
What if one (or both) of the arms may or may not be the analyzed allocation?
There was a problem hiding this comment.
We are never in an ambiguous situation. If one arm can contain a mixture of the analyzed allocation and something else then we consider that the Mixes situation, which we ignore:
binaryen/src/passes/Heap2Local.cpp
Lines 243 to 248 in 60050be
In theory we could add runtime checks "is this the allocation", but it seems unpromising.
If an allocation does not escape, then we can compute
ref.eqfor it: whencompared to itself the result is 1, and when compared to anything else it
is 0 (since it did not escape, anything else must be different).
This unblocks a few cases on real-world Java code I am looking at.