Skip to content

Add typing details for cmpxchg and other rmw operations - #92

Merged
tlively merged 2 commits into
mainfrom
cmpxchg-typing
Feb 12, 2025
Merged

tlively merged 2 commits into
mainfrom
cmpxchg-typing

Conversation

@tlively

@tlively tlively commented Jan 31, 2025

Copy link
Copy Markdown
Member

In particular, clarify that the type of the expected value operand to
cmpxchg can be a supertype of the field type. This gives optimizers like
Binaryen more leeway to refine field types because they are constrained
by only the type of the replacement operand and not by the type of the
expected value operand.

In particular, clarify that the type of the expected value operand to
cmpxchg can be a supertype of the field type. This gives optimizers like
Binaryen more leeway to refine field types because they are constrained
by only the type of the replacement operand and not by the type of the
expected value operand.

C |- struct.atomic.rmw.add x y : [(ref null x) t] -> [t]
- C.types[x] = struct fields
- fields[i] = mut t

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

where is i coming from here?


C |- struct.atomic.rmw.cmpxchg x y : [(ref null x) t1 t2] -> [t2]
- C.types[x] = struct fields
- fields[i] = mut t2

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

same here

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Oops, should be y. Will fix.

@conrad-watt

Copy link
Copy Markdown
Collaborator

In particular, clarify that the type of the expected value operand to
cmpxchg can be a supertype of the field type. This gives optimizers like
Binaryen more leeway to refine field types because they are constrained
by only the type of the replacement operand and not by the type of the
expected value operand.

Just to double check, this comes for free from subsumption and isn't a semantic change, right?

@tlively

tlively commented Jan 31, 2025

Copy link
Copy Markdown
Member Author

Just to double check, this comes for free from subsumption and isn't a semantic change, right?

This comes for free from subsumption given the described typing rule, but not from an alternative simpler rule where the "expected" operand also has to be a subtype of the field type, just like the "replacement" operand. With that rule, an optimizer could only refine the field type to be the LUB of the expected and replacement operand types.

@rossberg rossberg left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM modulo Conrad's comments.

@tlively
tlively enabled auto-merge (squash) February 1, 2025 00:31
tlively added a commit to WebAssembly/binaryen that referenced this pull request Feb 1, 2025
Both passes use StructUtils::StructScanner to analyze struct operations.
Add support for RMW operations to this utility and update its users to
provide the new `noteRMW` hook. Test that GTO and TypeRefining
optimizations work as expected in the presence of RMW operations, but
leave a proper implementation in ConstantFieldPropagation to a later PR.

To allow TypeRefining to refine field types based only on the
"replacement" operand and not the "expected" operand of cmpxchg
operations, update validation to allow the "expected" field to be a
supertype of the accessed field type as long as it is still equality
comparable.
WebAssembly/shared-everything-threads#92
clarifies this intended typing in the upstream proposal.
@tlively
tlively requested a review from conrad-watt February 3, 2025 18:48
tlively added a commit to WebAssembly/binaryen that referenced this pull request Feb 4, 2025
Both passes use StructUtils::StructScanner to analyze struct operations.
Add support for RMW operations to this utility and update its users to
provide the new `noteRMW` hook. Test that GTO and TypeRefining
optimizations work as expected in the presence of RMW operations, but
leave a proper implementation in ConstantFieldPropagation to a later PR.

To allow TypeRefining to refine field types based only on the
"replacement" operand and not the "expected" operand of cmpxchg
operations, update validation to allow the "expected" field to be a
supertype of the accessed field type as long as it is still equality
comparable.
WebAssembly/shared-everything-threads#92
clarifies this intended typing in the upstream proposal.
@tlively

tlively commented Feb 10, 2025

Copy link
Copy Markdown
Member Author

ping @conrad-watt

@tlively
tlively disabled auto-merge February 12, 2025 07:13
@tlively
tlively merged commit 6dd2c0c into main Feb 12, 2025
@tlively
tlively deleted the cmpxchg-typing branch February 12, 2025 07:13
stevenfontanella added a commit to WebAssembly/binaryen that referenced this pull request Sep 23, 2026
For `ref` fields, the `expected` ref that's passed in does not need to
be a subtype of the struct's field's type. See [the typing
rules](https://github.com/WebAssembly/shared-everything-threads/blob/main/proposals/shared-everything-threads/Overview.md#managed-waiter-queues)
from the proposal, and
WebAssembly/shared-everything-threads#92 which
explains why. From the spec:

```
wait_expected(i32) = i32
wait_expected(i64) = i64
wait_expected(t) = (ref null (shared eq)) -- if t <: (ref null (shared eq))

C |- struct.wait x y : [(ref null x) (ref null (shared waitqueue)) t i64] -> [i32]
 -- C.types[x] = shared? struct ft*
 -- ft*[y] = mut? t'
 -- t = wait_expected(t')
```

In this case `t <: (ref null (shared eq))` and it's not required that `t
<: t'`.

---

Since the expected value isn't required to be a subtype of the field
type, we no longer need the fixing code in TypeRefining.cpp. This
matches the behavior for the expected field of atomic cmpxchg, which
doesn't touch `curr->expected`. The `type-refining-gufa` test was a
mis-optimization before, because it caused the struct.wait to block (by
changing the expected value to `ref.null`) when it did not block in the
original program.
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.

3 participants