Skip to content

[Shared-Everything] ArrayRMW and ArrayCmpxchg #7632

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 35 commits into from
Jun 2, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
yolo
  • Loading branch information
kripken committed May 29, 2025
commit 778aa2e2c0e04596fab2a69d0fb368e980b1654f
7 changes: 7 additions & 0 deletions scripts/gen-s-parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,13 @@
("array.fill", "makeArrayFill()"),
("array.init_data", "makeArrayInitData()"),
("array.init_elem", "makeArrayInitElem()"),
("array.atomic.rmw.add", "makeArrayRMW(RMWAdd)"),
("array.atomic.rmw.sub", "makemakeArrayRMW(RMWSub)"),
("array.atomic.rmw.and", "makemakeArrayRMW(RMWAnd)"),
("array.atomic.rmw.or", "makemakeArrayRMW(RMWOr)"),
("array.atomic.rmw.xor", "makemakeArrayRMW(RMWXor)"),
("array.atomic.rmw.xchg", "makemakeArrayRMW(RMWXchg)"),
("array.atomic.rmw.cmpxchg", "makemakeArrayCmpxchg()"),
("ref.as_non_null", "makeRefAs(RefAsNonNull)"),
("extern.internalize", "makeRefAs(AnyConvertExtern)"), # Deprecated
("extern.externalize", "makeRefAs(ExternConvertAny)"), # Deprecated
Expand Down
27 changes: 27 additions & 0 deletions src/ir/child-typer.h
Original file line number Diff line number Diff line change
Expand Up @@ -1126,6 +1126,33 @@ template<typename Subtype> struct ChildTyper : OverriddenVisitor<Subtype> {
note(&curr->size, Type::i32);
}

void visitArrayRMW(ArrayRMW* curr,
std::optional<HeapType> ht = std::nullopt) {
if (!ht) {
if (self().skipUnreachable() && !curr->ref->type.isRef()) {
return;
}
ht = curr->ref->type.getHeapType();
}
auto type = ht->getArray().element.type;
note(&curr->ref, Type(*ht, Nullable));
note(&curr->value, type.type);
}

void visitArrayCmpxchg(ArrayCmpxchg* curr,
std::optional<HeapType> ht = std::nullopt) {
if (!ht) {
if (self().skipUnreachable() && !curr->ref->type.isRef()) {
return;
}
ht = curr->ref->type.getHeapType();
}
auto type = ht->getArray().element.type;
note(&curr->ref, Type(*ht, Nullable));
note(&curr->expected, type.type);
note(&curr->replacement, type.type);
}

void visitRefAs(RefAs* curr) {
switch (curr->op) {
case RefAsNonNull:
Expand Down
9 changes: 9 additions & 0 deletions src/ir/cost.h
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,15 @@ struct CostAnalyzer : public OverriddenVisitor<CostAnalyzer, CostType> {
return 6 + visit(curr->ref) + visit(curr->index) + visit(curr->offset) +
visit(curr->size);
}
CostType visitArrayRMW(ArrayRMW* curr) {
return AtomicCost + nullCheckCost(curr->ref) + visit(curr->ref) +
visit(curr->index) + visit(curr->value);
}
CostType visitArrayCmpxchg(ArrayCmpxchg* curr) {
return AtomicCost + nullCheckCost(curr->ref) + visit(curr->ref) +
visit(curr->index) + visit(curr->expected) +
visit(curr->replacement);
}
CostType visitRefAs(RefAs* curr) { return 1 + visit(curr->value); }
CostType visitStringNew(StringNew* curr) {
return 8 + visit(curr->ref) + maybeVisit(curr->start) +
Expand Down
26 changes: 26 additions & 0 deletions src/ir/effects.h
Original file line number Diff line number Diff line change
Expand Up @@ -1000,6 +1000,32 @@ class EffectAnalyzer {
}
void visitArrayInitData(ArrayInitData* curr) { visitArrayInit(curr); }
void visitArrayInitElem(ArrayInitElem* curr) { visitArrayInit(curr); }
void visitArrayRMW(ArrayRMW* curr) {
if (curr->ref->type.isNull()) {
parent.trap = true;
return;
}
parent.readsMutableArray = true;
parent.writesArray = true;
if (curr->ref->type.isNullable()) {
parent.implicitTrap = true;
}
assert(curr->order != MemoryOrder::Unordered);
parent.isAtomic = true;
}
void visitArrayCmpxchg(ArrayCmpxchg* curr) {
if (curr->ref->type.isNull()) {
parent.trap = true;
return;
}
parent.readsMutableArray = true;
parent.writesArray = true;
if (curr->ref->type.isNullable()) {
parent.implicitTrap = true;
}
assert(curr->order != MemoryOrder::Unordered);
parent.isAtomic = true;
}
void visitRefAs(RefAs* curr) {
if (curr->op == AnyConvertExtern || curr->op == ExternConvertAny) {
// These conversions are infallible.
Expand Down
15 changes: 15 additions & 0 deletions src/ir/subtype-exprs.h
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,21 @@ struct SubtypingDiscoverer : public OverriddenVisitor<SubType> {
auto* seg = self()->getModule()->getElementSegment(curr->segment);
self()->noteSubtype(seg->type, array.element.type);
}
void visitArrayRMW(ArrayRMW* curr) {
if (!curr->ref->type.isArray()) {
return;
}
auto array = curr->ref->type.getHeapType().getArray();
self()->noteSubtype(curr->value, array.element.type);
}
void visitArrayCmpxchg(ArrayCmpxchg* curr) {
if (!curr->ref->type.isArray()) {
return;
}
auto array = curr->ref->type.getHeapType().getArray();
self()->noteSubtype(curr->expected, array.element.type);
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't seem right. The expected value should be allowed to be a supertype of the element type, so it should only be required to be a subtype of eq. I see this is wrong for StructCmpxchg as well.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed both. Though it seems the spec could be more strict here..?

Copy link
Member

Choose a reason for hiding this comment

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

This way is actually better for us. See the discussion at WebAssembly/shared-everything-threads#92.

self()->noteSubtype(curr->replacement, array.element.type);
}
void visitRefAs(RefAs* curr) {
if (curr->op == RefAsNonNull) {
self()->noteCast(curr->value, curr);
Expand Down
16 changes: 16 additions & 0 deletions src/wasm-delegations-fields.def
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,22 @@ DELEGATE_FIELD_CHILD(ArrayInitElem, index)
DELEGATE_FIELD_IMMEDIATE_TYPED_CHILD(ArrayInitElem, ref)
DELEGATE_FIELD_CASE_END(ArrayInitElem)

DELEGATE_FIELD_CASE_START(ArrayRMW)
DELEGATE_FIELD_INT(ArrayRMW, op)
DELEGATE_FIELD_CHILD(ArrayRMW, value)
DELEGATE_FIELD_CHILD(ArrayRMW, index)
DELEGATE_FIELD_IMMEDIATE_TYPED_CHILD(ArrayRMW, ref)
DELEGATE_FIELD_INT(ArrayRMW, order)
DELEGATE_FIELD_CASE_END(ArrayRMW)

DELEGATE_FIELD_CASE_START(ArrayCmpxchg)
DELEGATE_FIELD_CHILD(ArrayCmpxchg, replacement)
DELEGATE_FIELD_CHILD(ArrayCmpxchg, expected)
DELEGATE_FIELD_CHILD(ArrayCmpxchg, index)
DELEGATE_FIELD_IMMEDIATE_TYPED_CHILD(ArrayCmpxchg, ref)
DELEGATE_FIELD_INT(ArrayCmpxchg, order)
DELEGATE_FIELD_CASE_END(ArrayCmpxchg)

DELEGATE_FIELD_CASE_START(RefAs)
DELEGATE_FIELD_INT(RefAs, op)
DELEGATE_FIELD_CHILD(RefAs, value)
Expand Down
2 changes: 2 additions & 0 deletions src/wasm-delegations.def
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ DELEGATE(ArrayCopy);
DELEGATE(ArrayFill);
DELEGATE(ArrayInitData);
DELEGATE(ArrayInitElem);
DELEGATE(ArrayRMW);
DELEGATE(ArrayCmpxchg);
DELEGATE(RefAs);
DELEGATE(StringNew);
DELEGATE(StringConst);
Expand Down
30 changes: 30 additions & 0 deletions src/wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,8 @@ class Expression {
ArrayFillId,
ArrayInitDataId,
ArrayInitElemId,
ArrayRMWId,
ArrayCmpxchgId,
RefAsId,
StringNewId,
StringConstId,
Expand Down Expand Up @@ -1871,6 +1873,34 @@ class ArrayInitElem : public SpecificExpression<Expression::ArrayInitElemId> {
void finalize();
};

class ArrayRMW : public SpecificExpression<Expression::ArrayRMWId> {
public:
ArrayRMW() = default;
ArrayRMW(MixedArena& allocator) {}

AtomicRMWOp op;
Expression* ref;
Expression* index;
Expression* value;
MemoryOrder order;

void finalize();
};

class ArrayCmpxchg : public SpecificExpression<Expression::ArrayCmpxchgId> {
public:
ArrayCmpxchg() = default;
ArrayCmpxchg(MixedArena& allocator) {}

Expression* ref;
Expression* index;
Expression* expected;
Expression* replacement;
MemoryOrder order;

void finalize();
};

class RefAs : public SpecificExpression<Expression::RefAsId> {
public:
RefAs() = default;
Expand Down