Skip to content

Commit

Permalink
[NFC] Make MemoryOrder parameters non-optional (#7171)
Browse files Browse the repository at this point in the history
Update Builder and IRBuilder makeStructGet and makeStructSet functions
to require the memory order to be explicitly supplied. This is slightly
more verbose, but will reduce the chances that we forget to properly
consider synchronization when implementing new features in the future.
  • Loading branch information
tlively authored Dec 21, 2024
1 parent 4d8a933 commit 6ddacde
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 31 deletions.
6 changes: 4 additions & 2 deletions src/binaryen-c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1752,15 +1752,17 @@ BinaryenExpressionRef BinaryenStructGet(BinaryenModuleRef module,
bool signed_) {
return static_cast<Expression*>(
Builder(*(Module*)module)
.makeStructGet(index, (Expression*)ref, Type(type), signed_));
.makeStructGet(
index, (Expression*)ref, MemoryOrder::Unordered, Type(type), signed_));
}
BinaryenExpressionRef BinaryenStructSet(BinaryenModuleRef module,
BinaryenIndex index,
BinaryenExpressionRef ref,
BinaryenExpressionRef value) {
return static_cast<Expression*>(
Builder(*(Module*)module)
.makeStructSet(index, (Expression*)ref, (Expression*)value));
.makeStructSet(
index, (Expression*)ref, (Expression*)value, MemoryOrder::Unordered));
}
BinaryenExpressionRef BinaryenArrayNew(BinaryenModuleRef module,
BinaryenHeapType type,
Expand Down
9 changes: 3 additions & 6 deletions src/parser/contexts.h
Original file line number Diff line number Diff line change
Expand Up @@ -740,15 +740,12 @@ struct NullInstrParserCtx {
HeapTypeT,
FieldIdxT,
bool,
MemoryOrder = MemoryOrder::Unordered) {
MemoryOrder) {
return Ok{};
}
template<typename HeapTypeT>
Result<> makeStructSet(Index,
const std::vector<Annotation>&,
HeapTypeT,
FieldIdxT,
MemoryOrder = MemoryOrder::Unordered) {
Result<> makeStructSet(
Index, const std::vector<Annotation>&, HeapTypeT, FieldIdxT, MemoryOrder) {
return Ok{};
}
template<typename HeapTypeT>
Expand Down
6 changes: 4 additions & 2 deletions src/parser/parsers.h
Original file line number Diff line number Diff line change
Expand Up @@ -2240,7 +2240,8 @@ Result<> makeStructGet(Ctx& ctx,
CHECK_ERR(type);
auto field = fieldidx(ctx, *type);
CHECK_ERR(field);
return ctx.makeStructGet(pos, annotations, *type, *field, signed_);
return ctx.makeStructGet(
pos, annotations, *type, *field, signed_, MemoryOrder::Unordered);
}

template<typename Ctx>
Expand All @@ -2264,7 +2265,8 @@ makeStructSet(Ctx& ctx, Index pos, const std::vector<Annotation>& annotations) {
CHECK_ERR(type);
auto field = fieldidx(ctx, *type);
CHECK_ERR(field);
return ctx.makeStructSet(pos, annotations, *type, *field);
return ctx.makeStructSet(
pos, annotations, *type, *field, MemoryOrder::Unordered);
}

template<typename Ctx>
Expand Down
9 changes: 6 additions & 3 deletions src/passes/Heap2Local.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1066,7 +1066,9 @@ struct Array2Struct : PostWalker<Array2Struct> {
}

// Convert the ArraySet into a StructSet.
replaceCurrent(builder.makeStructSet(index, curr->ref, curr->value));
// TODO: Handle atomic array accesses.
replaceCurrent(builder.makeStructSet(
index, curr->ref, curr->value, MemoryOrder::Unordered));
}

void visitArrayGet(ArrayGet* curr) {
Expand All @@ -1085,8 +1087,9 @@ struct Array2Struct : PostWalker<Array2Struct> {
}

// Convert the ArrayGet into a StructGet.
replaceCurrent(
builder.makeStructGet(index, curr->ref, curr->type, curr->signed_));
// TODO: Handle atomic array accesses.
replaceCurrent(builder.makeStructGet(
index, curr->ref, MemoryOrder::Unordered, curr->type, curr->signed_));
}

// Some additional operations need special handling
Expand Down
3 changes: 2 additions & 1 deletion src/passes/J2CLItableMerging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ struct J2CLItableMerging : public Pass {
replaceCurrent(builder.makeStructGet(
0,
curr->ref,
MemoryOrder::Unordered,
parent.structInfoByITableType[curr->type.getHeapType()]
->javaClass.getStruct()
.fields[0]
Expand Down Expand Up @@ -341,4 +342,4 @@ struct J2CLItableMerging : public Pass {
} // anonymous namespace

Pass* createJ2CLItableMergingPass() { return new J2CLItableMerging(); }
} // namespace wasm
} // namespace wasm
5 changes: 3 additions & 2 deletions src/tools/fuzzing/fuzzing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4493,7 +4493,8 @@ Expression* TranslateToFuzzReader::makeStructGet(Type type) {
auto [structType, fieldIndex] = pick(structFields);
auto* ref = makeTrappingRefUse(structType);
auto signed_ = maybeSignedGet(structType.getStruct().fields[fieldIndex]);
return builder.makeStructGet(fieldIndex, ref, type, signed_);
return builder.makeStructGet(
fieldIndex, ref, MemoryOrder::Unordered, type, signed_);
}

Expression* TranslateToFuzzReader::makeStructSet(Type type) {
Expand All @@ -4505,7 +4506,7 @@ Expression* TranslateToFuzzReader::makeStructSet(Type type) {
auto fieldType = structType.getStruct().fields[fieldIndex].type;
auto* ref = makeTrappingRefUse(structType);
auto* value = make(fieldType);
return builder.makeStructSet(fieldIndex, ref, value);
return builder.makeStructSet(fieldIndex, ref, value, MemoryOrder::Unordered);
}

// Make a bounds check for an array operation, given a ref + index. An optional
Expand Down
3 changes: 2 additions & 1 deletion src/tools/wasm-ctor-eval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -957,7 +957,8 @@ struct CtorEvalExternalInterface : EvallingModuleRunner::ExternalInterface {

Expression* set;
if (global.type.isStruct()) {
set = builder.makeStructSet(index, getGlobal, value);
set =
builder.makeStructSet(index, getGlobal, value, MemoryOrder::Unordered);
} else {
set = builder.makeArraySet(
getGlobal, builder.makeConst(int32_t(index)), value);
Expand Down
6 changes: 3 additions & 3 deletions src/wasm-builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -938,9 +938,9 @@ class Builder {
}
StructGet* makeStructGet(Index index,
Expression* ref,
MemoryOrder order,
Type type,
bool signed_ = false,
MemoryOrder order = MemoryOrder::Unordered) {
bool signed_ = false) {
auto* ret = wasm.allocator.alloc<StructGet>();
ret->index = index;
ret->ref = ref;
Expand All @@ -953,7 +953,7 @@ class Builder {
StructSet* makeStructSet(Index index,
Expression* ref,
Expression* value,
MemoryOrder order = MemoryOrder::Unordered) {
MemoryOrder order) {
auto* ret = wasm.allocator.alloc<StructSet>();
ret->index = index;
ret->ref = ref;
Expand Down
10 changes: 3 additions & 7 deletions src/wasm-ir-builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,9 @@ class IRBuilder : public UnifiedExpressionVisitor<IRBuilder, Result<>> {
makeBrOn(Index label, BrOnOp op, Type in = Type::none, Type out = Type::none);
Result<> makeStructNew(HeapType type);
Result<> makeStructNewDefault(HeapType type);
Result<> makeStructGet(HeapType type,
Index field,
bool signed_,
MemoryOrder order = MemoryOrder::Unordered);
Result<> makeStructSet(HeapType type,
Index field,
MemoryOrder order = MemoryOrder::Unordered);
Result<>
makeStructGet(HeapType type, Index field, bool signed_, MemoryOrder order);
Result<> makeStructSet(HeapType type, Index field, MemoryOrder order);
Result<> makeArrayNew(HeapType type);
Result<> makeArrayNewDefault(HeapType type);
Result<> makeArrayNewData(HeapType type, Name data);
Expand Down
8 changes: 5 additions & 3 deletions src/wasm/wasm-binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4172,13 +4172,15 @@ Result<> WasmBinaryReader::readInst() {
case BinaryConsts::StructGetU: {
auto type = getIndexedHeapType();
auto field = getU32LEB();
return builder.makeStructGet(
type, field, op == BinaryConsts::StructGetS);
return builder.makeStructGet(type,
field,
op == BinaryConsts::StructGetS,
MemoryOrder::Unordered);
}
case BinaryConsts::StructSet: {
auto type = getIndexedHeapType();
auto field = getU32LEB();
return builder.makeStructSet(type, field);
return builder.makeStructSet(type, field, MemoryOrder::Unordered);
}
case BinaryConsts::ArrayNew:
return builder.makeArrayNew(getIndexedHeapType());
Expand Down
2 changes: 1 addition & 1 deletion src/wasm/wasm-ir-builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1801,7 +1801,7 @@ Result<> IRBuilder::makeStructGet(HeapType type,
CHECK_ERR(ChildPopper{*this}.visitStructGet(&curr, type));
CHECK_ERR(validateTypeAnnotation(type, curr.ref));
push(
builder.makeStructGet(field, curr.ref, fields[field].type, signed_, order));
builder.makeStructGet(field, curr.ref, order, fields[field].type, signed_));
return Ok{};
}

Expand Down

0 comments on commit 6ddacde

Please sign in to comment.