Repository navigation
[Heap2Local] Fix cmpxchg with same ref and expected - #9333
Merged
Merged
Conversation
When a non-escaping struct allocation flows into both the `ref` and `expected` operands of a `struct.atomic.rmw.cmpxchg`, Struct2Local replaces `expected` with a dummy `ref.null` before visiting the cmpxchg. Previously, `visitStructCmpxchg` checked whether the allocation flowed into `ref` and emitted a `ref.eq` comparison between the field local and `expected`, which ended up comparing the field local against `null` and spuriously writing `replacement` into the field local whenever the field held `null`. Since a non-escaping allocation can never be stored into any struct field, the cmpxchg comparison against `expected` can never succeed; fix this by just dropping the cmpxchg children and reading the field local when the allocation flows into both `ref` and `expected`.
There was a problem hiding this comment.
Code Review
This pull request optimizes struct.atomic.rmw.cmpxchg operations within the Heap2Local pass when the expected operand flows from an unescaped allocation. Because the allocation does not escape, it cannot match the value already in the struct, allowing the cmpxchg to be simplified to a read operation that drops its operands and retrieves the local variable. Corresponding test cases have been updated and added in heap2local-rmw.wast to validate this optimization. There are no review comments, so I have no feedback to provide.
aheejin
approved these changes
Nov 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When a non-escaping struct allocation flows into both the
refandexpectedoperands of astruct.atomic.rmw.cmpxchg, Struct2Local replacesexpectedwith a dummyref.nullbefore visiting the cmpxchg. Previously,visitStructCmpxchgchecked whether the allocation flowed intorefand emitted aref.eqcomparison between the field local andexpected, which ended up comparing the field local againstnulland spuriously writingreplacementinto the field local whenever the field heldnull. Since a non-escaping allocation can never be stored into any struct field, the cmpxchg comparison againstexpectedcan never succeed; fix this by just dropping the cmpxchg children and reading the field local when the allocation flows into bothrefandexpected.