From 0fa99fb09b881985cf94e74b8c0b339bdef61be6 Mon Sep 17 00:00:00 2001 From: Thomas Lively Date: Fri, 14 Jun 2024 12:07:44 -0700 Subject: [threads] Add a "shared-everything" feature (#6658) 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. --- src/tools/tool-options.h | 1 + src/wasm-binary.h | 1 + src/wasm-features.h | 13 ++++++++++++- src/wasm/wasm-binary.cpp | 11 +++++++++-- src/wasm/wasm-type.cpp | 13 ++++++++++--- src/wasm/wasm-validator.cpp | 19 +++++++++++++++++++ src/wasm/wasm.cpp | 1 + 7 files changed, 53 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/tools/tool-options.h b/src/tools/tool-options.h index 599b3b22c..10a03acc7 100644 --- a/src/tools/tool-options.h +++ b/src/tools/tool-options.h @@ -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", diff --git a/src/wasm-binary.h b/src/wasm-binary.h index 71de2f915..ebfc27b1f 100644 --- a/src/wasm-binary.h +++ b/src/wasm-binary.h @@ -428,6 +428,7 @@ extern const char* ExtendedConstFeature; extern const char* StringsFeature; extern const char* MultiMemoryFeature; extern const char* TypedContinuationsFeature; +extern const char* SharedEverythingFeature; enum Subsection { NameModule = 0, diff --git a/src/wasm-features.h b/src/wasm-features.h index 2ace67c27..cda3ce447 100644 --- a/src/wasm-features.h +++ b/src/wasm-features.h @@ -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) { @@ -88,6 +89,8 @@ struct FeatureSet { return "multimemory"; case TypedContinuations: return "typed-continuations"; + case SharedEverything: + return "shared-everything"; default: WASM_UNREACHABLE("unexpected feature"); } @@ -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) { @@ -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; } @@ -186,6 +193,10 @@ struct FeatureSet { return *this; } + FeatureSet operator-(FeatureSet& other) const { + return features & ~other.features; + } + uint32_t features; }; diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp index 32dcc883a..7b88bdd76 100644 --- a/src/wasm/wasm-binary.cpp +++ b/src/wasm/wasm-binary.cpp @@ -1319,9 +1319,14 @@ void WasmBinaryWriter::writeFeaturesSection() { return BinaryConsts::CustomSections::MultiMemoryFeature; case FeatureSet::TypedContinuations: return BinaryConsts::CustomSections::TypedContinuationsFeature; - default: - WASM_UNREACHABLE("unexpected feature flag"); + case FeatureSet::SharedEverything: + return BinaryConsts::CustomSections::SharedEverythingFeature; + case FeatureSet::None: + case FeatureSet::Default: + case FeatureSet::All: + break; } + WASM_UNREACHABLE("unexpected feature flag"); }; std::vector features; @@ -3825,6 +3830,8 @@ void WasmBinaryReader::readFeatures(size_t payloadLen) { } else if (name == BinaryConsts::CustomSections::TypedContinuationsFeature) { feature = FeatureSet::TypedContinuations; + } else if (name == BinaryConsts::CustomSections::SharedEverythingFeature) { + feature = FeatureSet::SharedEverything; } else { // Silently ignore unknown features (this may be and old binaryen running // on a new wasm). diff --git a/src/wasm/wasm-type.cpp b/src/wasm/wasm-type.cpp index 83bf47107..532ebb913 100644 --- a/src/wasm/wasm-type.cpp +++ b/src/wasm/wasm-type.cpp @@ -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 @@ -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; diff --git a/src/wasm/wasm-validator.cpp b/src/wasm/wasm-validator.cpp index 689872a19..f8bd08e1d 100644 --- a/src/wasm/wasm-validator.cpp +++ b/src/wasm/wasm-validator.cpp @@ -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) { diff --git a/src/wasm/wasm.cpp b/src/wasm/wasm.cpp index cae413397..ed05e9b47 100644 --- a/src/wasm/wasm.cpp +++ b/src/wasm/wasm.cpp @@ -54,6 +54,7 @@ const char* ExtendedConstFeature = "extended-const"; const char* StringsFeature = "strings"; const char* MultiMemoryFeature = "multimemory"; const char* TypedContinuationsFeature = "typed-continuations"; +const char* SharedEverythingFeature = "shared-everything"; } // namespace CustomSections } // namespace BinaryConsts -- cgit v1.2.3