Skip to content

[Heap2Local] Fix cmpxchg with same ref and expected - #9333

Merged
tlively merged 1 commit into
mainfrom
heap2local-cmpxchg-ref-and-expected
Nov 2, 2026
Merged

tlively merged 1 commit into
mainfrom
heap2local-cmpxchg-ref-and-expected

Conversation

@tlively

@tlively tlively commented Oct 31, 2026

Copy link
Copy Markdown
Member

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.

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`.
@tlively
tlively requested a review from a team as a code owner October 31, 2026 07:10
@tlively
tlively requested review from aheejin and removed request for a team October 31, 2026 07:10

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

@tlively
tlively merged commit 4950cf0 into main Nov 2, 2026
17 checks passed
@tlively
tlively deleted the heap2local-cmpxchg-ref-and-expected branch November 2, 2026 06:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants