diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/binaryen-c.cpp | 3 | ||||
-rw-r--r-- | src/binaryen-c.h | 1 | ||||
-rw-r--r-- | src/ir/global-utils.h | 10 | ||||
-rw-r--r-- | src/ir/properties.h | 20 | ||||
-rw-r--r-- | src/js/binaryen.js-post.js | 1 | ||||
-rw-r--r-- | src/tools/tool-options.h | 1 | ||||
-rw-r--r-- | src/wasm-binary.h | 1 | ||||
-rw-r--r-- | src/wasm-features.h | 7 | ||||
-rw-r--r-- | src/wasm/wasm-binary.cpp | 4 | ||||
-rw-r--r-- | src/wasm/wasm-validator.cpp | 29 | ||||
-rw-r--r-- | src/wasm/wasm.cpp | 1 |
11 files changed, 62 insertions, 16 deletions
diff --git a/src/binaryen-c.cpp b/src/binaryen-c.cpp index df868dc62..d884626b2 100644 --- a/src/binaryen-c.cpp +++ b/src/binaryen-c.cpp @@ -252,6 +252,9 @@ BinaryenFeatures BinaryenFeatureTypedFunctionReferences(void) { BinaryenFeatures BinaryenFeatureRelaxedSIMD(void) { return static_cast<BinaryenFeatures>(FeatureSet::RelaxedSIMD); } +BinaryenFeatures BinaryenFeatureExtendedConst(void) { + return static_cast<BinaryenFeatures>(FeatureSet::ExtendedConst); +} BinaryenFeatures BinaryenFeatureAll(void) { return static_cast<BinaryenFeatures>(FeatureSet::All); } diff --git a/src/binaryen-c.h b/src/binaryen-c.h index 7511a65a4..e5a02d556 100644 --- a/src/binaryen-c.h +++ b/src/binaryen-c.h @@ -162,6 +162,7 @@ BINARYEN_API BinaryenFeatures BinaryenFeatureGC(void); BINARYEN_API BinaryenFeatures BinaryenFeatureMemory64(void); BINARYEN_API BinaryenFeatures BinaryenFeatureTypedFunctionReferences(void); BINARYEN_API BinaryenFeatures BinaryenFeatureRelaxedSIMD(void); +BINARYEN_API BinaryenFeatures BinaryenFeatureExtendedConst(void); BINARYEN_API BinaryenFeatures BinaryenFeatureAll(void); // Modules diff --git a/src/ir/global-utils.h b/src/ir/global-utils.h index 26aec7790..ca00047a0 100644 --- a/src/ir/global-utils.h +++ b/src/ir/global-utils.h @@ -53,20 +53,18 @@ getGlobalInitializedToImport(Module& wasm, Name module, Name base) { return ret; } -inline bool canInitializeGlobal(Expression* curr) { +inline bool canInitializeGlobal(Expression* curr, FeatureSet features) { if (auto* tuple = curr->dynCast<TupleMake>()) { for (auto* op : tuple->operands) { - if (!canInitializeGlobal(op)) { + if (!canInitializeGlobal(op, features)) { return false; } } return true; } - if (Properties::isSingleConstantExpression(curr) || curr->is<GlobalGet>() || - curr->is<RttCanon>() || curr->is<RttSub>() || curr->is<StructNew>() || - curr->is<ArrayNew>() || curr->is<ArrayInit>() || curr->is<I31New>()) { + if (Properties::isValidInConstantExpression(curr, features)) { for (auto* child : ChildIterator(curr)) { - if (!canInitializeGlobal(child)) { + if (!canInitializeGlobal(child, features)) { return false; } } diff --git a/src/ir/properties.h b/src/ir/properties.h index d2c5affd2..07898169f 100644 --- a/src/ir/properties.h +++ b/src/ir/properties.h @@ -414,6 +414,26 @@ inline bool canEmitSelectWithArms(Expression* ifTrue, Expression* ifFalse) { // bool isGenerative(Expression* curr, FeatureSet features); +inline bool isValidInConstantExpression(Expression* expr, FeatureSet features) { + if (isSingleConstantExpression(expr) || expr->is<GlobalGet>() || + expr->is<RttCanon>() || expr->is<RttSub>() || expr->is<StructNew>() || + expr->is<ArrayNew>() || expr->is<ArrayInit>() || expr->is<I31New>()) { + return true; + } + + if (features.hasExtendedConst()) { + if (expr->is<Binary>()) { + auto bin = static_cast<Binary*>(expr); + if (bin->op == AddInt64 || bin->op == SubInt64 || bin->op == MulInt64 || + bin->op == AddInt32 || bin->op == SubInt32 || bin->op == MulInt32) { + return true; + } + } + } + + return false; +} + } // namespace wasm::Properties #endif // wasm_ir_properties_h diff --git a/src/js/binaryen.js-post.js b/src/js/binaryen.js-post.js index ee90aa1fc..7dfeee26c 100644 --- a/src/js/binaryen.js-post.js +++ b/src/js/binaryen.js-post.js @@ -149,6 +149,7 @@ function initializeConstants() { 'Memory64', 'TypedFunctionReferences', 'RelaxedSIMD', + 'ExtendedConst', 'All' ].forEach(name => { Module['Features'][name] = Module['_BinaryenFeature' + name](); diff --git a/src/tools/tool-options.h b/src/tools/tool-options.h index f8278e950..bc948fc97 100644 --- a/src/tools/tool-options.h +++ b/src/tools/tool-options.h @@ -93,6 +93,7 @@ struct ToolOptions : public Options { "typed function references") .addFeature(FeatureSet::GCNNLocals, "GC non-null locals") .addFeature(FeatureSet::RelaxedSIMD, "relaxed SIMD") + .addFeature(FeatureSet::ExtendedConst, "extended const expressions") .add("--no-validation", "-n", "Disables validation, assumes inputs are correct", diff --git a/src/wasm-binary.h b/src/wasm-binary.h index b31447ed9..051f77f58 100644 --- a/src/wasm-binary.h +++ b/src/wasm-binary.h @@ -425,6 +425,7 @@ extern const char* GCFeature; extern const char* Memory64Feature; extern const char* TypedFunctionReferencesFeature; extern const char* RelaxedSIMDFeature; +extern const char* ExtendedConstFeature; enum Subsection { NameModule = 0, diff --git a/src/wasm-features.h b/src/wasm-features.h index 02e1660bb..df4397e78 100644 --- a/src/wasm-features.h +++ b/src/wasm-features.h @@ -42,7 +42,8 @@ struct FeatureSet { // TODO: Remove this feature when the wasm spec stabilizes. GCNNLocals = 1 << 13, RelaxedSIMD = 1 << 14, - All = (1 << 15) - 1 + ExtendedConst = 1 << 15, + All = (1 << 16) - 1 }; static std::string toString(Feature f) { @@ -77,6 +78,8 @@ struct FeatureSet { return "gc-nn-locals"; case RelaxedSIMD: return "relaxed-simd"; + case ExtendedConst: + return "extended-const"; default: WASM_UNREACHABLE("unexpected feature"); } @@ -122,6 +125,7 @@ struct FeatureSet { } bool hasGCNNLocals() const { return (features & GCNNLocals) != 0; } bool hasRelaxedSIMD() const { return (features & RelaxedSIMD) != 0; } + bool hasExtendedConst() const { return (features & ExtendedConst) != 0; } bool hasAll() const { return (features & All) != 0; } void set(FeatureSet f, bool v = true) { @@ -144,6 +148,7 @@ struct FeatureSet { } void setGCNNLocals(bool v = true) { set(GCNNLocals, v); } void setRelaxedSIMD(bool v = true) { set(RelaxedSIMD, v); } + void setExtendedConst(bool v = true) { set(ExtendedConst, v); } void setMVP() { features = MVP; } void setAll() { // Do not set GCNNLocals, which forces the user to opt in to that feature diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp index 1ddb009cf..f43346fdb 100644 --- a/src/wasm/wasm-binary.cpp +++ b/src/wasm/wasm-binary.cpp @@ -1084,6 +1084,8 @@ void WasmBinaryWriter::writeFeaturesSection() { return BinaryConsts::UserSections::TypedFunctionReferencesFeature; case FeatureSet::RelaxedSIMD: return BinaryConsts::UserSections::RelaxedSIMDFeature; + case FeatureSet::ExtendedConst: + return BinaryConsts::UserSections::ExtendedConstFeature; default: WASM_UNREACHABLE("unexpected feature flag"); } @@ -3350,6 +3352,8 @@ void WasmBinaryBuilder::readFeatures(size_t payloadLen) { feature = FeatureSet::TypedFunctionReferences; } else if (name == BinaryConsts::UserSections::RelaxedSIMDFeature) { feature = FeatureSet::RelaxedSIMD; + } else if (name == BinaryConsts::UserSections::ExtendedConstFeature) { + feature = FeatureSet::ExtendedConst; } else { // Silently ignore unknown features (this may be and old binaryen running // on a new wasm). diff --git a/src/wasm/wasm-validator.cpp b/src/wasm/wasm-validator.cpp index 65fcc7a9d..86fc4811b 100644 --- a/src/wasm/wasm-validator.cpp +++ b/src/wasm/wasm-validator.cpp @@ -2767,13 +2767,21 @@ void FunctionValidator::visitFunction(Function* curr) { } } -static bool checkSegmentOffset(Expression* curr, Address add, Address max) { - if (curr->is<GlobalGet>()) { - return true; +static bool checkSegmentOffset(Expression* curr, + Address add, + Address max, + FeatureSet features) { + if (!Properties::isValidInConstantExpression(curr, features)) { + return false; } auto* c = curr->dynCast<Const>(); if (!c) { - return false; + // Unless the instruction is actually a const instruction, we don't + // currently try to evaluate it. + // TODO: Attempt to evaluate other expressions that might also be const + // such as `global.get` or more complex instruction sequences involving + // add/sub/mul/etc. + return true; } uint64_t raw = c->value.getInteger(); if (raw > std::numeric_limits<Address::address32_t>::max()) { @@ -2999,9 +3007,10 @@ static void validateGlobals(Module& module, ValidationInfo& info) { info.shouldBeTrue( curr->init != nullptr, curr->name, "global init must be non-null"); assert(curr->init); - info.shouldBeTrue(GlobalUtils::canInitializeGlobal(curr->init), - curr->name, - "global init must be valid"); + info.shouldBeTrue( + GlobalUtils::canInitializeGlobal(curr->init, module.features), + curr->name, + "global init must be valid"); if (!info.shouldBeSubType(curr->init->type, curr->type, @@ -3066,7 +3075,8 @@ static void validateMemory(Module& module, ValidationInfo& info) { } info.shouldBeTrue(checkSegmentOffset(segment.offset, segment.data.size(), - curr.initial * Memory::kPageSize), + curr.initial * Memory::kPageSize, + module.features), segment.offset, "memory segment offset should be reasonable"); if (segment.offset->is<Const>()) { @@ -3171,7 +3181,8 @@ static void validateTables(Module& module, ValidationInfo& info) { "element segment offset should be i32"); info.shouldBeTrue(checkSegmentOffset(segment->offset, segment->data.size(), - table->initial * Table::kPageSize), + table->initial * Table::kPageSize, + module.features), segment->offset, "table segment offset should be reasonable"); if (module.features.hasTypedFunctionReferences()) { diff --git a/src/wasm/wasm.cpp b/src/wasm/wasm.cpp index aac8b44fe..ca9691346 100644 --- a/src/wasm/wasm.cpp +++ b/src/wasm/wasm.cpp @@ -49,6 +49,7 @@ const char* GCFeature = "gc"; const char* Memory64Feature = "memory64"; const char* TypedFunctionReferencesFeature = "typed-function-references"; const char* RelaxedSIMDFeature = "relaxed-simd"; +const char* ExtendedConstFeature = "extended-const"; } // namespace UserSections } // namespace BinaryConsts |