From 1891d2dc0d836bc84fa29b9b0138d8c4ba966969 Mon Sep 17 00:00:00 2001 From: stevenfontanella Date: Tue, 21 Apr 2026 21:25:54 +0000 Subject: [PATCH] Add feature flag for wide arithmetic --- src/binaryen-c.cpp | 3 +++ src/binaryen-c.h | 1 + src/js/binaryen.js-post.js | 1 + src/tools/tool-options.h | 1 + src/wasm-binary.h | 1 + src/wasm-features.h | 7 ++++++- src/wasm/wasm-binary.cpp | 4 ++++ src/wasm/wasm.cpp | 1 + test/binaryen.js/kitchen-sink.js | 1 + test/binaryen.js/kitchen-sink.js.txt | 3 ++- test/example/c-api-kitchen-sink.c | 2 ++ test/example/c-api-kitchen-sink.txt | 3 ++- test/lit/help/wasm-as.test | 4 ++++ test/lit/help/wasm-ctor-eval.test | 4 ++++ test/lit/help/wasm-dis.test | 4 ++++ test/lit/help/wasm-emscripten-finalize.test | 4 ++++ test/lit/help/wasm-merge.test | 4 ++++ test/lit/help/wasm-metadce.test | 4 ++++ test/lit/help/wasm-opt.test | 4 ++++ test/lit/help/wasm-reduce.test | 4 ++++ test/lit/help/wasm-split.test | 4 ++++ test/lit/help/wasm2js.test | 4 ++++ ...rget-features_roundtrip_print-features_all-features.txt | 1 + test/unit/test_features.py | 1 + 24 files changed, 67 insertions(+), 3 deletions(-) diff --git a/src/binaryen-c.cpp b/src/binaryen-c.cpp index 133d17e92b4..53926af7680 100644 --- a/src/binaryen-c.cpp +++ b/src/binaryen-c.cpp @@ -508,6 +508,9 @@ BinaryenFeatures BinaryenFeatureMultibyte(void) { BinaryenFeatures BinaryenFeatureCustomPageSizes(void) { return static_cast(FeatureSet::CustomPageSizes); } +BinaryenFeatures BinaryenFeatureWideArithmetic(void) { + return static_cast(FeatureSet::WideArithmetic); +} BinaryenFeatures BinaryenFeatureAll(void) { return static_cast(FeatureSet::All); } diff --git a/src/binaryen-c.h b/src/binaryen-c.h index 24e0fe3071a..10e01fef3aa 100644 --- a/src/binaryen-c.h +++ b/src/binaryen-c.h @@ -246,6 +246,7 @@ BINARYEN_API BinaryenFeatures BinaryenFeatureCallIndirectOverlong(void); BINARYEN_API BinaryenFeatures BinaryenFeatureRelaxedAtomics(void); BINARYEN_API BinaryenFeatures BinaryenFeatureMultibyte(void); BINARYEN_API BinaryenFeatures BinaryenFeatureCustomPageSizes(void); +BINARYEN_API BinaryenFeatures BinaryenFeatureWideArithmetic(void); BINARYEN_API BinaryenFeatures BinaryenFeatureAll(void); // Modules diff --git a/src/js/binaryen.js-post.js b/src/js/binaryen.js-post.js index 0044b5e7c36..8c965d70ee0 100644 --- a/src/js/binaryen.js-post.js +++ b/src/js/binaryen.js-post.js @@ -194,6 +194,7 @@ function initializeConstants() { 'CallIndirectOverlong', 'RelaxedAtomics', 'CustomPageSizes', + 'WideArithmetic', 'All' ].forEach(name => { Module['Features'][name] = Module['_BinaryenFeature' + name](); diff --git a/src/tools/tool-options.h b/src/tools/tool-options.h index cc39daad4fc..87362d455d2 100644 --- a/src/tools/tool-options.h +++ b/src/tools/tool-options.h @@ -112,6 +112,7 @@ struct ToolOptions : public Options { .addFeature(FeatureSet::RelaxedAtomics, "acquire/release atomic memory operations") .addFeature(FeatureSet::CustomPageSizes, "custom page sizes") + .addFeature(FeatureSet::WideArithmetic, "wide arithmetic") .add("--enable-typed-function-references", "", "Deprecated compatibility flag", diff --git a/src/wasm-binary.h b/src/wasm-binary.h index 386f495a905..abacbda0d13 100644 --- a/src/wasm-binary.h +++ b/src/wasm-binary.h @@ -474,6 +474,7 @@ extern const char* CustomDescriptorsFeature; extern const char* RelaxedAtomicsFeature; extern const char* MultibyteFeature; extern const char* CustomPageSizesFeature; +extern const char* WideArithmeticFeature; enum Subsection { NameModule = 0, diff --git a/src/wasm-features.h b/src/wasm-features.h index 056e6a4ab55..833281c0c11 100644 --- a/src/wasm-features.h +++ b/src/wasm-features.h @@ -58,11 +58,12 @@ struct FeatureSet { RelaxedAtomics = 1 << 22, CustomPageSizes = 1 << 23, Multibyte = 1 << 24, + WideArithmetic = 1 << 25, 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 << 25) - 1, + All = (1 << 26) - 1, }; static std::string toString(Feature f) { @@ -117,6 +118,8 @@ struct FeatureSet { return "custom-page-sizes"; case Multibyte: return "multibyte"; + case WideArithmetic: + return "wide-arithmetic"; case MVP: case Default: case All: @@ -180,6 +183,7 @@ struct FeatureSet { bool hasRelaxedAtomics() const { return (features & RelaxedAtomics) != 0; } bool hasCustomPageSizes() const { return (features & CustomPageSizes) != 0; } bool hasMultibyte() const { return (features & Multibyte) != 0; } + bool hasWideArithmetic() const { return (features & WideArithmetic) != 0; } bool hasAll() const { return (features & All) != 0; } void set(FeatureSet f, bool v = true) { @@ -208,6 +212,7 @@ struct FeatureSet { void setCustomDescriptors(bool v = true) { set(CustomDescriptors, v); } void setRelaxedAtomics(bool v = true) { set(RelaxedAtomics, v); } void setMultibyte(bool v = true) { set(Multibyte, v); } + void setWideArithmetic(bool v = true) { set(WideArithmetic, v); } void setMVP() { features = MVP; } void setAll() { features = All; } diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp index da49533f55d..50dbc333bf2 100644 --- a/src/wasm/wasm-binary.cpp +++ b/src/wasm/wasm-binary.cpp @@ -1486,6 +1486,8 @@ void WasmBinaryWriter::writeFeaturesSection() { return BinaryConsts::CustomSections::RelaxedAtomicsFeature; case FeatureSet::CustomPageSizes: return BinaryConsts::CustomSections::CustomPageSizesFeature; + case FeatureSet::WideArithmetic: + return BinaryConsts::CustomSections::WideArithmeticFeature; case FeatureSet::None: case FeatureSet::Default: case FeatureSet::All: @@ -5446,6 +5448,8 @@ void WasmBinaryReader::readFeatures(size_t sectionPos, size_t payloadLen) { feature = FeatureSet::RelaxedAtomics; } else if (name == BinaryConsts::CustomSections::CustomPageSizesFeature) { feature = FeatureSet::CustomPageSizes; + } else if (name == BinaryConsts::CustomSections::WideArithmeticFeature) { + feature = FeatureSet::WideArithmetic; } else { // Silently ignore unknown features (this may be and old binaryen running // on a new wasm). diff --git a/src/wasm/wasm.cpp b/src/wasm/wasm.cpp index 31de2de0362..589f9b33afc 100644 --- a/src/wasm/wasm.cpp +++ b/src/wasm/wasm.cpp @@ -79,6 +79,7 @@ const char* CustomDescriptorsFeature = "custom-descriptors"; const char* RelaxedAtomicsFeature = "relaxed-atomics"; const char* MultibyteFeature = "multibyte"; const char* CustomPageSizesFeature = "custom-page-sizes"; +const char* WideArithmeticFeature = "wide-arithmetic"; } // namespace BinaryConsts::CustomSections diff --git a/test/binaryen.js/kitchen-sink.js b/test/binaryen.js/kitchen-sink.js index 89753393591..354668d0c12 100644 --- a/test/binaryen.js/kitchen-sink.js +++ b/test/binaryen.js/kitchen-sink.js @@ -102,6 +102,7 @@ function test_features() { console.log("Features.MultiMemory: " + binaryen.Features.MultiMemory); console.log("Features.RelaxedAtomics: " + binaryen.Features.RelaxedAtomics); console.log("Features.CustomPageSizes: " + binaryen.Features.CustomPageSizes); + console.log("Features.WideArithmetic: " + binaryen.Features.WideArithmetic); console.log("Features.All: " + binaryen.Features.All); } diff --git a/test/binaryen.js/kitchen-sink.js.txt b/test/binaryen.js/kitchen-sink.js.txt index 9ef0ade0bcc..b6f57fa8236 100644 --- a/test/binaryen.js/kitchen-sink.js.txt +++ b/test/binaryen.js/kitchen-sink.js.txt @@ -35,7 +35,8 @@ Features.Strings: 16384 Features.MultiMemory: 32768 Features.RelaxedAtomics: 4194304 Features.CustomPageSizes: 8388608 -Features.All: 33554431 +Features.WideArithmetic: 33554432 +Features.All: 67108863 InvalidId: 0 BlockId: 1 IfId: 2 diff --git a/test/example/c-api-kitchen-sink.c b/test/example/c-api-kitchen-sink.c index 225e5feef58..e046b942303 100644 --- a/test/example/c-api-kitchen-sink.c +++ b/test/example/c-api-kitchen-sink.c @@ -379,6 +379,8 @@ void test_features() { printf("BinaryenFeatureCustomPageSizes: %d\n", BinaryenFeatureCustomPageSizes()); printf("BinaryenFeatureMultibyte: %d\n", BinaryenFeatureMultibyte()); + printf("BinaryenFeatureWideArithmetic: %d\n", + BinaryenFeatureWideArithmetic()); printf("BinaryenFeatureAll: %d\n", BinaryenFeatureAll()); } diff --git a/test/example/c-api-kitchen-sink.txt b/test/example/c-api-kitchen-sink.txt index 5ab868a05f9..cf66a4cba9b 100644 --- a/test/example/c-api-kitchen-sink.txt +++ b/test/example/c-api-kitchen-sink.txt @@ -50,7 +50,8 @@ BinaryenFeatureStrings: 16384 BinaryenFeatureRelaxedAtomics: 4194304 BinaryenFeatureCustomPageSizes: 8388608 BinaryenFeatureMultibyte: 16777216 -BinaryenFeatureAll: 33554431 +BinaryenFeatureWideArithmetic: 33554432 +BinaryenFeatureAll: 67108863 (f32.neg (f32.const -33.61199951171875) ) diff --git a/test/lit/help/wasm-as.test b/test/lit/help/wasm-as.test index 463a8c4270c..35ea0de7868 100644 --- a/test/lit/help/wasm-as.test +++ b/test/lit/help/wasm-as.test @@ -148,6 +148,10 @@ ;; CHECK-NEXT: ;; CHECK-NEXT: --disable-custom-page-sizes Disable custom page sizes ;; CHECK-NEXT: +;; CHECK-NEXT: --enable-wide-arithmetic Enable wide arithmetic +;; CHECK-NEXT: +;; CHECK-NEXT: --disable-wide-arithmetic Disable wide arithmetic +;; CHECK-NEXT: ;; CHECK-NEXT: --enable-typed-function-references Deprecated compatibility flag ;; CHECK-NEXT: ;; CHECK-NEXT: --disable-typed-function-references Deprecated compatibility flag diff --git a/test/lit/help/wasm-ctor-eval.test b/test/lit/help/wasm-ctor-eval.test index 5f02cb6d8e9..e56f2b215bc 100644 --- a/test/lit/help/wasm-ctor-eval.test +++ b/test/lit/help/wasm-ctor-eval.test @@ -155,6 +155,10 @@ ;; CHECK-NEXT: ;; CHECK-NEXT: --disable-custom-page-sizes Disable custom page sizes ;; CHECK-NEXT: +;; CHECK-NEXT: --enable-wide-arithmetic Enable wide arithmetic +;; CHECK-NEXT: +;; CHECK-NEXT: --disable-wide-arithmetic Disable wide arithmetic +;; CHECK-NEXT: ;; CHECK-NEXT: --enable-typed-function-references Deprecated compatibility flag ;; CHECK-NEXT: ;; CHECK-NEXT: --disable-typed-function-references Deprecated compatibility flag diff --git a/test/lit/help/wasm-dis.test b/test/lit/help/wasm-dis.test index 73a8cd2fe47..3c0fca5a0f4 100644 --- a/test/lit/help/wasm-dis.test +++ b/test/lit/help/wasm-dis.test @@ -141,6 +141,10 @@ ;; CHECK-NEXT: ;; CHECK-NEXT: --disable-custom-page-sizes Disable custom page sizes ;; CHECK-NEXT: +;; CHECK-NEXT: --enable-wide-arithmetic Enable wide arithmetic +;; CHECK-NEXT: +;; CHECK-NEXT: --disable-wide-arithmetic Disable wide arithmetic +;; CHECK-NEXT: ;; CHECK-NEXT: --enable-typed-function-references Deprecated compatibility flag ;; CHECK-NEXT: ;; CHECK-NEXT: --disable-typed-function-references Deprecated compatibility flag diff --git a/test/lit/help/wasm-emscripten-finalize.test b/test/lit/help/wasm-emscripten-finalize.test index 21d4c5ef261..6ed8c98771f 100644 --- a/test/lit/help/wasm-emscripten-finalize.test +++ b/test/lit/help/wasm-emscripten-finalize.test @@ -183,6 +183,10 @@ ;; CHECK-NEXT: ;; CHECK-NEXT: --disable-custom-page-sizes Disable custom page sizes ;; CHECK-NEXT: +;; CHECK-NEXT: --enable-wide-arithmetic Enable wide arithmetic +;; CHECK-NEXT: +;; CHECK-NEXT: --disable-wide-arithmetic Disable wide arithmetic +;; CHECK-NEXT: ;; CHECK-NEXT: --enable-typed-function-references Deprecated compatibility flag ;; CHECK-NEXT: ;; CHECK-NEXT: --disable-typed-function-references Deprecated compatibility flag diff --git a/test/lit/help/wasm-merge.test b/test/lit/help/wasm-merge.test index 8c94b13745c..f1ff0631278 100644 --- a/test/lit/help/wasm-merge.test +++ b/test/lit/help/wasm-merge.test @@ -178,6 +178,10 @@ ;; CHECK-NEXT: ;; CHECK-NEXT: --disable-custom-page-sizes Disable custom page sizes ;; CHECK-NEXT: +;; CHECK-NEXT: --enable-wide-arithmetic Enable wide arithmetic +;; CHECK-NEXT: +;; CHECK-NEXT: --disable-wide-arithmetic Disable wide arithmetic +;; CHECK-NEXT: ;; CHECK-NEXT: --enable-typed-function-references Deprecated compatibility flag ;; CHECK-NEXT: ;; CHECK-NEXT: --disable-typed-function-references Deprecated compatibility flag diff --git a/test/lit/help/wasm-metadce.test b/test/lit/help/wasm-metadce.test index ea5806e0199..1b0cc7d4569 100644 --- a/test/lit/help/wasm-metadce.test +++ b/test/lit/help/wasm-metadce.test @@ -814,6 +814,10 @@ ;; CHECK-NEXT: ;; CHECK-NEXT: --disable-custom-page-sizes Disable custom page sizes ;; CHECK-NEXT: +;; CHECK-NEXT: --enable-wide-arithmetic Enable wide arithmetic +;; CHECK-NEXT: +;; CHECK-NEXT: --disable-wide-arithmetic Disable wide arithmetic +;; CHECK-NEXT: ;; CHECK-NEXT: --enable-typed-function-references Deprecated compatibility flag ;; CHECK-NEXT: ;; CHECK-NEXT: --disable-typed-function-references Deprecated compatibility flag diff --git a/test/lit/help/wasm-opt.test b/test/lit/help/wasm-opt.test index 1c0a53801e4..d616e1cf085 100644 --- a/test/lit/help/wasm-opt.test +++ b/test/lit/help/wasm-opt.test @@ -846,6 +846,10 @@ ;; CHECK-NEXT: ;; CHECK-NEXT: --disable-custom-page-sizes Disable custom page sizes ;; CHECK-NEXT: +;; CHECK-NEXT: --enable-wide-arithmetic Enable wide arithmetic +;; CHECK-NEXT: +;; CHECK-NEXT: --disable-wide-arithmetic Disable wide arithmetic +;; CHECK-NEXT: ;; CHECK-NEXT: --enable-typed-function-references Deprecated compatibility flag ;; CHECK-NEXT: ;; CHECK-NEXT: --disable-typed-function-references Deprecated compatibility flag diff --git a/test/lit/help/wasm-reduce.test b/test/lit/help/wasm-reduce.test index f403191fed6..48da8b968f3 100644 --- a/test/lit/help/wasm-reduce.test +++ b/test/lit/help/wasm-reduce.test @@ -231,6 +231,10 @@ ;; CHECK-NEXT: ;; CHECK-NEXT: --disable-custom-page-sizes Disable custom page sizes ;; CHECK-NEXT: +;; CHECK-NEXT: --enable-wide-arithmetic Enable wide arithmetic +;; CHECK-NEXT: +;; CHECK-NEXT: --disable-wide-arithmetic Disable wide arithmetic +;; CHECK-NEXT: ;; CHECK-NEXT: --enable-typed-function-references Deprecated compatibility flag ;; CHECK-NEXT: ;; CHECK-NEXT: --disable-typed-function-references Deprecated compatibility flag diff --git a/test/lit/help/wasm-split.test b/test/lit/help/wasm-split.test index 39740806aad..950d3e5cea9 100644 --- a/test/lit/help/wasm-split.test +++ b/test/lit/help/wasm-split.test @@ -287,6 +287,10 @@ ;; CHECK-NEXT: ;; CHECK-NEXT: --disable-custom-page-sizes Disable custom page sizes ;; CHECK-NEXT: +;; CHECK-NEXT: --enable-wide-arithmetic Enable wide arithmetic +;; CHECK-NEXT: +;; CHECK-NEXT: --disable-wide-arithmetic Disable wide arithmetic +;; CHECK-NEXT: ;; CHECK-NEXT: --enable-typed-function-references Deprecated compatibility flag ;; CHECK-NEXT: ;; CHECK-NEXT: --disable-typed-function-references Deprecated compatibility flag diff --git a/test/lit/help/wasm2js.test b/test/lit/help/wasm2js.test index 39bc23554a9..a91d5b5c050 100644 --- a/test/lit/help/wasm2js.test +++ b/test/lit/help/wasm2js.test @@ -778,6 +778,10 @@ ;; CHECK-NEXT: ;; CHECK-NEXT: --disable-custom-page-sizes Disable custom page sizes ;; CHECK-NEXT: +;; CHECK-NEXT: --enable-wide-arithmetic Enable wide arithmetic +;; CHECK-NEXT: +;; CHECK-NEXT: --disable-wide-arithmetic Disable wide arithmetic +;; CHECK-NEXT: ;; CHECK-NEXT: --enable-typed-function-references Deprecated compatibility flag ;; CHECK-NEXT: ;; CHECK-NEXT: --disable-typed-function-references Deprecated compatibility flag diff --git a/test/passes/strip-target-features_roundtrip_print-features_all-features.txt b/test/passes/strip-target-features_roundtrip_print-features_all-features.txt index 8172cc77f9c..2272fb36ff1 100644 --- a/test/passes/strip-target-features_roundtrip_print-features_all-features.txt +++ b/test/passes/strip-target-features_roundtrip_print-features_all-features.txt @@ -23,6 +23,7 @@ --enable-relaxed-atomics --enable-custom-page-sizes --enable-multibyte +--enable-wide-arithmetic (module (type $0 (func (result v128 externref))) (func $foo (type $0) (result v128 externref) diff --git a/test/unit/test_features.py b/test/unit/test_features.py index 2bd3e74f6fc..2d693ded087 100644 --- a/test/unit/test_features.py +++ b/test/unit/test_features.py @@ -458,4 +458,5 @@ def test_emit_all_features(self): '--enable-custom-descriptors', '--enable-relaxed-atomics', '--enable-custom-page-sizes', + '--enable-wide-arithmetic', ], p2.stdout.splitlines())