Skip to content
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

[threads] Add a "shared-everything" feature #6658

Merged
merged 4 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/tools/tool-options.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ struct ToolOptions : public Options {
.addFeature(FeatureSet::Strings, "strings")
.addFeature(FeatureSet::MultiMemory, "multimemory")
.addFeature(FeatureSet::TypedContinuations, "typed continuations")
.addFeature(FeatureSet::SharedEverything, "shared-everything threads")
.add("--enable-typed-function-references",
"",
"Deprecated compatibility flag",
Expand Down
13 changes: 12 additions & 1 deletion src/wasm-features.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,12 @@ struct FeatureSet {
Strings = 1 << 14,
MultiMemory = 1 << 15,
TypedContinuations = 1 << 16,
SharedEverything = 1 << 17,
MVP = None,
// Keep in sync with llvm default features:
// https://github.com/llvm/llvm-project/blob/c7576cb89d6c95f03968076e902d3adfd1996577/clang/lib/Basic/Targets/WebAssembly.cpp#L150-L153
Default = SignExt | MutableGlobals,
All = (1 << 17) - 1,
All = (1 << 18) - 1,
};

static std::string toString(Feature f) {
Expand Down Expand Up @@ -88,6 +89,8 @@ struct FeatureSet {
return "multimemory";
case TypedContinuations:
return "typed-continuations";
case SharedEverything:
return "shared-everything";
default:
WASM_UNREACHABLE("unexpected feature");
}
Expand Down Expand Up @@ -135,6 +138,9 @@ struct FeatureSet {
bool hasTypedContinuations() const {
return (features & TypedContinuations) != 0;
}
bool hasSharedEverything() const {
return (features & SharedEverything) != 0;
}
bool hasAll() const { return (features & All) != 0; }

void set(FeatureSet f, bool v = true) {
Expand All @@ -157,6 +163,7 @@ struct FeatureSet {
void setStrings(bool v = true) { set(Strings, v); }
void setMultiMemory(bool v = true) { set(MultiMemory, v); }
void setTypedContinuations(bool v = true) { set(TypedContinuations, v); }
void setSharedEverything(bool v = true) { set(SharedEverything, v); }
void setMVP() { features = MVP; }
void setAll() { features = All; }

Expand Down Expand Up @@ -186,6 +193,10 @@ struct FeatureSet {
return *this;
}

FeatureSet operator-(FeatureSet& other) const {
return features & ~other.features;
}

uint32_t features;
};

Expand Down
13 changes: 10 additions & 3 deletions src/wasm/wasm-type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -932,10 +932,17 @@ FeatureSet Type::getFeatures() const {
}
}

if (heapType->isStruct() || heapType->isArray() ||
heapType->getRecGroup().size() > 1 ||
if (heapType->getRecGroup().size() > 1 ||
heapType->getDeclaredSuperType() || heapType->isOpen()) {
feats |= FeatureSet::ReferenceTypes | FeatureSet::GC;
}

if (heapType->isShared()) {
feats |= FeatureSet::SharedEverything;
}

if (heapType->isStruct() || heapType->isArray()) {
feats |= FeatureSet::ReferenceTypes | FeatureSet::GC;
} else if (heapType->isSignature()) {
// This is a function reference, which requires reference types and
// possibly also multivalue (if it has multiple returns). Note that
Expand Down Expand Up @@ -969,7 +976,7 @@ FeatureSet Type::getFeatures() const {
collector.noteChild(&heapType);
return collector.feats;
}
TODO_SINGLE_COMPOUND(t);

switch (t.getBasic()) {
case Type::v128:
return FeatureSet::SIMD;
Expand Down
19 changes: 19 additions & 0 deletions src/wasm/wasm-validator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3701,6 +3701,25 @@ static void validateGlobals(Module& module, ValidationInfo& info) {
seen.insert(curr);
}
});

// Check that globals have allowed types.
for (auto& g : module.globals) {
auto globalFeats = g->type.getFeatures();
if (!info.shouldBeTrue(globalFeats <= module.features, g->name, "")) {
auto& stream = info.getStream(nullptr);
stream << "global type requires additional features [";
bool first = true;
(globalFeats - module.features).iterFeatures([&](FeatureSet feat) {
if (first) {
first = false;
} else {
stream << " ";
}
stream << "--enable-" << feat.toString();
});
stream << "]\n";
}
}
}

static void validateMemories(Module& module, ValidationInfo& info) {
Expand Down
2 changes: 1 addition & 1 deletion test/binaryen.js/kitchen-sink.js.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Features.RelaxedSIMD: 4096
Features.ExtendedConst: 8192
Features.Strings: 16384
Features.MultiMemory: 32768
Features.All: 131071
Features.All: 262143
InvalidId: 0
BlockId: 1
IfId: 2
Expand Down
2 changes: 1 addition & 1 deletion test/example/c-api-kitchen-sink.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ BinaryenFeatureMemory64: 2048
BinaryenFeatureRelaxedSIMD: 4096
BinaryenFeatureExtendedConst: 8192
BinaryenFeatureStrings: 16384
BinaryenFeatureAll: 131071
BinaryenFeatureAll: 262143
(f32.neg
(f32.const -33.61199951171875)
)
Expand Down
4 changes: 4 additions & 0 deletions test/lit/help/wasm-as.test
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@
;; CHECK-NEXT:
;; CHECK-NEXT: --disable-typed-continuations Disable typed continuations
;; CHECK-NEXT:
;; CHECK-NEXT: --enable-shared-everything Enable shared-everything threads
;; CHECK-NEXT:
;; CHECK-NEXT: --disable-shared-everything Disable shared-everything threads
;; CHECK-NEXT:
;; CHECK-NEXT: --enable-typed-function-references Deprecated compatibility flag
;; CHECK-NEXT:
;; CHECK-NEXT: --disable-typed-function-references Deprecated compatibility flag
Expand Down
4 changes: 4 additions & 0 deletions test/lit/help/wasm-ctor-eval.test
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@
;; CHECK-NEXT:
;; CHECK-NEXT: --disable-typed-continuations Disable typed continuations
;; CHECK-NEXT:
;; CHECK-NEXT: --enable-shared-everything Enable shared-everything threads
;; CHECK-NEXT:
;; CHECK-NEXT: --disable-shared-everything Disable shared-everything threads
;; CHECK-NEXT:
;; CHECK-NEXT: --enable-typed-function-references Deprecated compatibility flag
;; CHECK-NEXT:
;; CHECK-NEXT: --disable-typed-function-references Deprecated compatibility flag
Expand Down
4 changes: 4 additions & 0 deletions test/lit/help/wasm-dis.test
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@
;; CHECK-NEXT:
;; CHECK-NEXT: --disable-typed-continuations Disable typed continuations
;; CHECK-NEXT:
;; CHECK-NEXT: --enable-shared-everything Enable shared-everything threads
;; CHECK-NEXT:
;; CHECK-NEXT: --disable-shared-everything Disable shared-everything threads
;; CHECK-NEXT:
;; CHECK-NEXT: --enable-typed-function-references Deprecated compatibility flag
;; CHECK-NEXT:
;; CHECK-NEXT: --disable-typed-function-references Deprecated compatibility flag
Expand Down
4 changes: 4 additions & 0 deletions test/lit/help/wasm-emscripten-finalize.test
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@
;; CHECK-NEXT:
;; CHECK-NEXT: --disable-typed-continuations Disable typed continuations
;; CHECK-NEXT:
;; CHECK-NEXT: --enable-shared-everything Enable shared-everything threads
;; CHECK-NEXT:
;; CHECK-NEXT: --disable-shared-everything Disable shared-everything threads
;; CHECK-NEXT:
;; CHECK-NEXT: --enable-typed-function-references Deprecated compatibility flag
;; CHECK-NEXT:
;; CHECK-NEXT: --disable-typed-function-references Deprecated compatibility flag
Expand Down
4 changes: 4 additions & 0 deletions test/lit/help/wasm-merge.test
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@
;; CHECK-NEXT:
;; CHECK-NEXT: --disable-typed-continuations Disable typed continuations
;; CHECK-NEXT:
;; CHECK-NEXT: --enable-shared-everything Enable shared-everything threads
;; CHECK-NEXT:
;; CHECK-NEXT: --disable-shared-everything Disable shared-everything threads
;; CHECK-NEXT:
;; CHECK-NEXT: --enable-typed-function-references Deprecated compatibility flag
;; CHECK-NEXT:
;; CHECK-NEXT: --disable-typed-function-references Deprecated compatibility flag
Expand Down
4 changes: 4 additions & 0 deletions test/lit/help/wasm-metadce.test
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@
;; CHECK-NEXT:
;; CHECK-NEXT: --disable-typed-continuations Disable typed continuations
;; CHECK-NEXT:
;; CHECK-NEXT: --enable-shared-everything Enable shared-everything threads
;; CHECK-NEXT:
;; CHECK-NEXT: --disable-shared-everything Disable shared-everything threads
;; CHECK-NEXT:
;; CHECK-NEXT: --enable-typed-function-references Deprecated compatibility flag
;; CHECK-NEXT:
;; CHECK-NEXT: --disable-typed-function-references Deprecated compatibility flag
Expand Down
5 changes: 5 additions & 0 deletions test/lit/help/wasm-opt.test
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,11 @@
;; CHECK-NEXT:
;; CHECK-NEXT: --disable-typed-continuations Disable typed continuations
;; CHECK-NEXT:
;; CHECK-NEXT: --enable-shared-everything Enable shared-everything threads
;; CHECK-NEXT:
;; CHECK-NEXT: --disable-shared-everything Disable shared-everything
;; CHECK-NEXT: threads
;; CHECK-NEXT:
;; CHECK-NEXT: --enable-typed-function-references Deprecated compatibility flag
;; CHECK-NEXT:
;; CHECK-NEXT: --disable-typed-function-references Deprecated compatibility flag
Expand Down
4 changes: 4 additions & 0 deletions test/lit/help/wasm-reduce.test
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@
;; CHECK-NEXT:
;; CHECK-NEXT: --disable-typed-continuations Disable typed continuations
;; CHECK-NEXT:
;; CHECK-NEXT: --enable-shared-everything Enable shared-everything threads
;; CHECK-NEXT:
;; CHECK-NEXT: --disable-shared-everything Disable shared-everything threads
;; CHECK-NEXT:
;; CHECK-NEXT: --enable-typed-function-references Deprecated compatibility flag
;; CHECK-NEXT:
;; CHECK-NEXT: --disable-typed-function-references Deprecated compatibility flag
Expand Down
4 changes: 4 additions & 0 deletions test/lit/help/wasm-split.test
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@
;; CHECK-NEXT:
;; CHECK-NEXT: --disable-typed-continuations Disable typed continuations
;; CHECK-NEXT:
;; CHECK-NEXT: --enable-shared-everything Enable shared-everything threads
;; CHECK-NEXT:
;; CHECK-NEXT: --disable-shared-everything Disable shared-everything threads
;; CHECK-NEXT:
;; CHECK-NEXT: --enable-typed-function-references Deprecated compatibility flag
;; CHECK-NEXT:
;; CHECK-NEXT: --disable-typed-function-references Deprecated compatibility flag
Expand Down
5 changes: 5 additions & 0 deletions test/lit/help/wasm2js.test
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,11 @@
;; CHECK-NEXT:
;; CHECK-NEXT: --disable-typed-continuations Disable typed continuations
;; CHECK-NEXT:
;; CHECK-NEXT: --enable-shared-everything Enable shared-everything threads
;; CHECK-NEXT:
;; CHECK-NEXT: --disable-shared-everything Disable shared-everything
;; CHECK-NEXT: threads
;; CHECK-NEXT:
;; CHECK-NEXT: --enable-typed-function-references Deprecated compatibility flag
;; CHECK-NEXT:
;; CHECK-NEXT: --disable-typed-function-references Deprecated compatibility flag
Expand Down
12 changes: 12 additions & 0 deletions test/lit/validation/shared-struct.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
;; Test that shared structs require shared-everything threads

;; RUN: not wasm-opt %s 2>&1 | filecheck %s --check-prefix NO-SHARED
;; RUN: wasm-opt %s --enable-reference-types --enable-gc --enable-shared-everything -o - -S | filecheck %s --check-prefix SHARED

;; NO-SHARED: global type requires additional features [--enable-reference-types --enable-gc --enable-shared-everything]
;; SHARED: (type $t (shared (struct )))

(module
(type $t (shared (struct)))
(global (import "" "") (ref null $t))
)
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
--enable-strings
--enable-multimemory
--enable-typed-continuations
--enable-shared-everything
(module
(type $0 (func (result v128 externref)))
(func $foo (type $0) (result v128 externref)
Expand Down
Loading