Repository navigation
Add typing details for cmpxchg and other rmw operations - #92
Conversation
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Oops, should be y. Will fix.
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
left a comment
There was a problem hiding this comment.
LGTM modulo Conrad's comments.
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.
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.
|
ping @conrad-watt |
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.
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.