Skip to content

Commit 2f0fc13

Browse files
Add feature flag for relaxed atomics
1 parent 7c6eb7c commit 2f0fc13

27 files changed

+113
-6
lines changed

src/binaryen-c.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,9 @@ BinaryenFeatures BinaryenFeatureBulkMemoryOpt(void) {
487487
BinaryenFeatures BinaryenFeatureCallIndirectOverlong(void) {
488488
return static_cast<BinaryenFeatures>(FeatureSet::CallIndirectOverlong);
489489
}
490+
BinaryenFeatures BinaryenFeatureRelaxedAtomics(void) {
491+
return static_cast<BinaryenFeatures>(FeatureSet::RelaxedAtomics);
492+
}
490493
BinaryenFeatures BinaryenFeatureAll(void) {
491494
return static_cast<BinaryenFeatures>(FeatureSet::All);
492495
}

src/binaryen-c.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ BINARYEN_API BinaryenFeatures BinaryenFeatureSharedEverything(void);
230230
BINARYEN_API BinaryenFeatures BinaryenFeatureFP16(void);
231231
BINARYEN_API BinaryenFeatures BinaryenFeatureBulkMemoryOpt(void);
232232
BINARYEN_API BinaryenFeatures BinaryenFeatureCallIndirectOverlong(void);
233+
BINARYEN_API BinaryenFeatures BinaryenFeatureRelaxedAtomics(void);
233234
BINARYEN_API BinaryenFeatures BinaryenFeatureAll(void);
234235

235236
// Modules

src/tools/tool-options.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ struct ToolOptions : public Options {
108108
.addFeature(FeatureSet::FP16, "float 16 operations")
109109
.addFeature(FeatureSet::CustomDescriptors,
110110
"custom descriptors (RTTs) and exact references")
111+
.addFeature(FeatureSet::RelaxedAtomics,
112+
"acquire/release atomic memory operations")
111113
.add("--enable-typed-function-references",
112114
"",
113115
"Deprecated compatibility flag",

src/wasm-binary.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,7 @@ extern const char* FP16Feature;
460460
extern const char* BulkMemoryOptFeature;
461461
extern const char* CallIndirectOverlongFeature;
462462
extern const char* CustomDescriptorsFeature;
463+
extern const char* RelaxedAtomicsFeature;
463464

464465
enum Subsection {
465466
NameModule = 0,

src/wasm-features.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,12 @@ struct FeatureSet {
5555
// it does nothing. Binaryen always accepts LEB call-indirect encodings.
5656
CallIndirectOverlong = 1 << 20,
5757
CustomDescriptors = 1 << 21,
58+
RelaxedAtomics = 1 << 22,
5859
MVP = None,
5960
// Keep in sync with llvm default features:
6061
// https://github.com/llvm/llvm-project/blob/c7576cb89d6c95f03968076e902d3adfd1996577/clang/lib/Basic/Targets/WebAssembly.cpp#L150-L153
6162
Default = SignExt | MutableGlobals,
62-
All = (1 << 22) - 1,
63+
All = (1 << 23) - 1,
6364
};
6465

6566
static std::string toString(Feature f) {
@@ -108,6 +109,8 @@ struct FeatureSet {
108109
return "call-indirect-overlong";
109110
case CustomDescriptors:
110111
return "custom-descriptors";
112+
case RelaxedAtomics:
113+
return "relaxed-atomics";
111114
case MVP:
112115
case Default:
113116
case All:
@@ -168,6 +171,7 @@ struct FeatureSet {
168171
bool hasCustomDescriptors() const {
169172
return (features & CustomDescriptors) != 0;
170173
}
174+
bool hasRelaxedAtomics() const { return (features & RelaxedAtomics) != 0; }
171175
bool hasAll() const { return (features & All) != 0; }
172176

173177
void set(FeatureSet f, bool v = true) {
@@ -194,6 +198,7 @@ struct FeatureSet {
194198
void setFP16(bool v = true) { set(FP16, v); }
195199
void setBulkMemoryOpt(bool v = true) { set(BulkMemoryOpt, v); }
196200
void setCustomDescriptors(bool v = true) { set(CustomDescriptors, v); }
201+
void setRelaxedAtomics(bool v = true) { set(RelaxedAtomics, v); }
197202
void setMVP() { features = MVP; }
198203
void setAll() { features = All; }
199204

src/wasm/wasm-binary.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,6 +1441,8 @@ void WasmBinaryWriter::writeFeaturesSection() {
14411441
return BinaryConsts::CustomSections::CallIndirectOverlongFeature;
14421442
case FeatureSet::CustomDescriptors:
14431443
return BinaryConsts::CustomSections::CustomDescriptorsFeature;
1444+
case FeatureSet::RelaxedAtomics:
1445+
return BinaryConsts::CustomSections::RelaxedAtomicsFeature;
14441446
case FeatureSet::None:
14451447
case FeatureSet::Default:
14461448
case FeatureSet::All:
@@ -5296,6 +5298,8 @@ void WasmBinaryReader::readFeatures(size_t sectionPos, size_t payloadLen) {
52965298
feature = FeatureSet::FP16;
52975299
} else if (name == BinaryConsts::CustomSections::CustomDescriptorsFeature) {
52985300
feature = FeatureSet::CustomDescriptors;
5301+
} else if (name == BinaryConsts::CustomSections::RelaxedAtomicsFeature) {
5302+
feature = FeatureSet::RelaxedAtomics;
52995303
} else {
53005304
// Silently ignore unknown features (this may be and old binaryen running
53015305
// on a new wasm).

src/wasm/wasm-validator.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,6 +1108,12 @@ void FunctionValidator::visitLoad(Load* curr) {
11081108
curr,
11091109
"Atomic load should be i32 or i64");
11101110
}
1111+
if (curr->order == MemoryOrder::AcqRel) {
1112+
shouldBeTrue(getModule()->features.hasRelaxedAtomics(),
1113+
curr,
1114+
"Acquire/release operations require relaxed atomics "
1115+
"[--enable-relaxed-atomics]");
1116+
}
11111117
if (curr->type == Type::v128) {
11121118
shouldBeTrue(getModule()->features.hasSIMD(),
11131119
curr,

src/wasm/wasm.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ const char* FP16Feature = "fp16";
6161
const char* BulkMemoryOptFeature = "bulk-memory-opt";
6262
const char* CallIndirectOverlongFeature = "call-indirect-overlong";
6363
const char* CustomDescriptorsFeature = "custom-descriptors";
64+
const char* RelaxedAtomicsFeature = "relaxed-atomics";
6465

6566
} // namespace BinaryConsts::CustomSections
6667

test/binaryen.js/kitchen-sink.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ function test_features() {
100100
console.log("Features.ExtendedConst: " + binaryen.Features.ExtendedConst);
101101
console.log("Features.Strings: " + binaryen.Features.Strings);
102102
console.log("Features.MultiMemory: " + binaryen.Features.MultiMemory);
103+
console.log("Features.RelaxedAtomics: " + binaryen.Features.RelaxedAtomics);
103104
console.log("Features.All: " + binaryen.Features.All);
104105
}
105106

test/binaryen.js/kitchen-sink.js.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ Features.RelaxedSIMD: 4096
3333
Features.ExtendedConst: 8192
3434
Features.Strings: 16384
3535
Features.MultiMemory: 32768
36-
Features.All: 4194303
36+
Features.RelaxedAtomics: 4194304
37+
Features.All: 8388607
3738
InvalidId: 0
3839
BlockId: 1
3940
IfId: 2

0 commit comments

Comments
 (0)