Skip to content

Fix scratch local when optimizing cmpxchg in Heap2Local#8496

Open
tlively wants to merge 4 commits intomainfrom
heap2local-nullable-expected-scratch
Open

Fix scratch local when optimizing cmpxchg in Heap2Local#8496
tlively wants to merge 4 commits intomainfrom
heap2local-nullable-expected-scratch

Conversation

@tlively
Copy link
Member

@tlively tlively commented Mar 19, 2026

When optimizing the ref operand to a StructCmpxchg in Heap2Local, we previously used a scratch local with the type of the accessed struct field to hold expected. But expected is allowed to have any subtype of eqref (or shared eqref). Fix the type of the scratch local to avoid validation failures.

tlively added 4 commits March 18, 2026 19:03
We previously assumed that if we were optimizing a StructCmpxchg in Heap2Local, then the flow must be from the `ref` operand. This was based on an assumption that allocations are processed in order of appearance, and because the `ref` operand appears before the `expected operand`, it must be the one getting optimized. But this neglected the fact the array allocations are processed before struct allocations, so if `expected` is an array, it could be optimized first. This faulty assumption led to assertion failures and invalid code.

Fix the problem by handling flows from `expected` explicitly. When a non-escaping allocation flows into `expected`, we know it cannot possibly match the value already in the accessed struct, so we can optimize the `cmpxchg` to an atomic `struct.get`.

WIP because this is not quite correct due to stale LocalGraph information. When `ref` is subsequently optimized, the LocalGraph does not know about the scratch local it is written to, so the `struct.atomic.get` it now flows into is not properly optimized out.
When optimizing the `ref` operand to a StructCmpxchg in Heap2Local, we previously used a scratch local with the type of the accessed struct field to hold `expected`. But `expected` is allowed to have any subtype of `eqref` (or shared `eqref`). Fix the type of the scratch local to avoid validation failures.
@tlively tlively requested a review from kripken March 19, 2026 07:06
Base automatically changed from heap2local to main March 20, 2026 01:30
@tlively tlively requested a review from a team as a code owner March 20, 2026 01:30
@tlively tlively requested review from stevenfontanella and removed request for a team March 20, 2026 01:30
@stevenfontanella stevenfontanella removed their request for review March 20, 2026 01:43
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