Skip to content

Commit

Permalink
[threads] Add a "shared-everything" feature
Browse files Browse the repository at this point in the history
Add the feature and flags to enable and disable it. Require the new feature to
be enabled for shared heap types to validate. To make the test work, update the
validator to actually check features for global types.
  • Loading branch information
tlively committed Jun 13, 2024
1 parent 3468619 commit b388e08
Show file tree
Hide file tree
Showing 15 changed files with 97 additions and 4 deletions.
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
14 changes: 11 additions & 3 deletions src/wasm/wasm-type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -936,7 +936,13 @@ FeatureSet Type::getFeatures() const {
heapType->getRecGroup().size() > 1 ||
heapType->getDeclaredSuperType() || heapType->isOpen()) {
feats |= FeatureSet::ReferenceTypes | FeatureSet::GC;
} else if (heapType->isSignature()) {
}

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

if (heapType->isSignature()) {
// This is a function reference, which requires reference types and
// possibly also multivalue (if it has multiple returns). Note that
// technically typed function references also require GC, however,
Expand All @@ -949,7 +955,9 @@ FeatureSet Type::getFeatures() const {
if (sig.results.isTuple()) {
feats |= FeatureSet::Multivalue;
}
} else if (heapType->isContinuation()) {
}

if (heapType->isContinuation()) {
feats |= FeatureSet::TypedContinuations;
}

Expand All @@ -969,7 +977,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
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))
)

0 comments on commit b388e08

Please sign in to comment.