Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions src/binaryen-c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,9 @@ BinaryenFeatures BinaryenFeatureCustomPageSizes(void) {
BinaryenFeatures BinaryenFeatureWideArithmetic(void) {
return static_cast<BinaryenFeatures>(FeatureSet::WideArithmetic);
}
BinaryenFeatures BinaryenFeatureCompactImports(void) {
return static_cast<BinaryenFeatures>(FeatureSet::CompactImports);
}
BinaryenFeatures BinaryenFeatureAll(void) {
return static_cast<BinaryenFeatures>(FeatureSet::All);
}
Expand Down
1 change: 1 addition & 0 deletions src/binaryen-c.h
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ BINARYEN_API BinaryenFeatures BinaryenFeatureRelaxedAtomics(void);
BINARYEN_API BinaryenFeatures BinaryenFeatureMultibyte(void);
BINARYEN_API BinaryenFeatures BinaryenFeatureCustomPageSizes(void);
BINARYEN_API BinaryenFeatures BinaryenFeatureWideArithmetic(void);
BINARYEN_API BinaryenFeatures BinaryenFeatureCompactImports(void);
BINARYEN_API BinaryenFeatures BinaryenFeatureAll(void);

// Modules
Expand Down
1 change: 1 addition & 0 deletions src/js/binaryen.js-post.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ function initializeConstants() {
'RelaxedAtomics',
'CustomPageSizes',
'WideArithmetic',
'CompactImports',
'All'
].forEach(name => {
Module['Features'][name] = Module['_BinaryenFeature' + name]();
Expand Down
1 change: 1 addition & 0 deletions src/tools/tool-options.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ struct ToolOptions : public Options {
"acquire/release atomic memory operations")
.addFeature(FeatureSet::CustomPageSizes, "custom page sizes")
.addFeature(FeatureSet::WideArithmetic, "wide arithmetic")
.addFeature(FeatureSet::CompactImports, "compact import section")
.add("--enable-typed-function-references",
"",
"Deprecated compatibility flag",
Expand Down
8 changes: 8 additions & 0 deletions src/wasm-binary.h
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,10 @@ constexpr uint32_t HasMemoryIndexMask = 1 << 6;
constexpr uint8_t HasTableInitializer = 0x40;
constexpr uint8_t TableReservedByte = 0x00;

// TODO(sbc): Use upstream names for these schemes if/when they are decided.
constexpr uint8_t CompactImportsSharedModule = 0x7f;
constexpr uint8_t CompactImportsSharedAll = 0x7e;

enum EncodedType {
// value types
i32 = -0x1, // 0x7f
Expand Down Expand Up @@ -475,6 +479,7 @@ extern const char* RelaxedAtomicsFeature;
extern const char* MultibyteFeature;
extern const char* CustomPageSizesFeature;
extern const char* WideArithmeticFeature;
extern const char* CompactImportsFeature;

enum Subsection {
NameModule = 0,
Expand Down Expand Up @@ -1693,6 +1698,9 @@ class WasmBinaryReader {
std::unique_ptr<Global> readGlobalImport(Name module, Name base);
std::unique_ptr<Tag> readTagImport(Name module, Name base);

template<typename T, typename ReadFunc>
void readCompactImportsShared(Name module, ReadFunc readFunc);

// The signatures of each function, including imported functions, given in the
// import and function sections. Store HeapTypes instead of Signatures because
// reconstructing the HeapTypes from the Signatures is expensive.
Expand Down
7 changes: 6 additions & 1 deletion src/wasm-features.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,12 @@ struct FeatureSet {
CustomPageSizes = 1 << 23,
Multibyte = 1 << 24,
WideArithmetic = 1 << 25,
CompactImports = 1 << 26,
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 << 26) - 1,
All = (1 << 27) - 1,
};

static std::string toString(Feature f) {
Expand Down Expand Up @@ -120,6 +121,8 @@ struct FeatureSet {
return "multibyte";
case WideArithmetic:
return "wide-arithmetic";
case CompactImports:
return "compact-imports";
case MVP:
case Default:
case All:
Expand Down Expand Up @@ -184,6 +187,7 @@ struct FeatureSet {
bool hasCustomPageSizes() const { return (features & CustomPageSizes) != 0; }
bool hasMultibyte() const { return (features & Multibyte) != 0; }
bool hasWideArithmetic() const { return (features & WideArithmetic) != 0; }
bool hasCompactImports() const { return (features & CompactImports) != 0; }
bool hasAll() const { return (features & All) != 0; }

void set(FeatureSet f, bool v = true) {
Expand Down Expand Up @@ -213,6 +217,7 @@ struct FeatureSet {
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 setCompactImports(bool v = true) { set(CompactImports, v); }
void setMVP() { features = MVP; }
void setAll() { features = All; }

Expand Down
64 changes: 62 additions & 2 deletions src/wasm/wasm-binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1489,6 +1489,8 @@ void WasmBinaryWriter::writeFeaturesSection() {
return BinaryConsts::CustomSections::CustomPageSizesFeature;
case FeatureSet::WideArithmetic:
return BinaryConsts::CustomSections::WideArithmeticFeature;
case FeatureSet::CompactImports:
return BinaryConsts::CustomSections::CompactImportsFeature;
case FeatureSet::None:
case FeatureSet::Default:
case FeatureSet::All:
Expand Down Expand Up @@ -3156,6 +3158,18 @@ std::unique_ptr<Tag> WasmBinaryReader::readTagImport(Name module, Name base) {
return curr;
}

template<typename T, typename ReadFunc>
void WasmBinaryReader::readCompactImportsShared(Name module,
ReadFunc readBaseDetails) {
std::unique_ptr<T> baseDetails = readBaseDetails(Name());
size_t numCompactImports = getU32LEB();
while (numCompactImports--) {
auto details = std::make_unique<T>(*baseDetails);
details->base = getInlineString();
addImport(std::move(details));
}
}

void WasmBinaryReader::readImport(Name module, Name base, uint32_t kind) {
switch (kind & ~BinaryConsts::ExactImport) {
case ExternalKind::Function:
Expand Down Expand Up @@ -3183,8 +3197,52 @@ void WasmBinaryReader::readImports() {
for (size_t i = 0; i < num; i++) {
auto module = getInlineString();
auto base = getInlineString();
auto kind = getU32LEB();
readImport(module, base, kind);
auto kind = getInt8();
if (base == "" && (kind == BinaryConsts::CompactImportsSharedModule ||
kind == BinaryConsts::CompactImportsSharedAll)) {
if (!wasm.features.hasCompactImports()) {
throwError("compact imports not supported");
}
if (kind == BinaryConsts::CompactImportsSharedModule) {
size_t numCompactImports = getU32LEB();
while (numCompactImports--) {
base = getInlineString();
kind = getInt8();
readImport(module, base, kind);
}
} else {
kind = getInt8();
switch (kind & ~BinaryConsts::ExactImport) {
case ExternalKind::Function:
readCompactImportsShared<Function>(module, [&](Name base) {
return readFunctionImport(module, base, kind);
});
break;
case ExternalKind::Table:
readCompactImportsShared<Table>(
module, [&](Name base) { return readTableImport(module, base); });
break;
case ExternalKind::Memory:
readCompactImportsShared<Memory>(module, [&](Name base) {
return readMemoryImport(module, base);
});
break;
case ExternalKind::Global:
readCompactImportsShared<Global>(module, [&](Name base) {
return readGlobalImport(module, base);
});
break;
case ExternalKind::Tag:
readCompactImportsShared<Tag>(
module, [&](Name base) { return readTagImport(module, base); });
break;
default:
throwError("bad import kind");
}
}
} else {
readImport(module, base, kind);
}
}
numFuncImports = wasm.functions.size();
}
Expand Down Expand Up @@ -5519,6 +5577,8 @@ void WasmBinaryReader::readFeatures(size_t sectionPos, size_t payloadLen) {
feature = FeatureSet::CustomPageSizes;
} else if (name == BinaryConsts::CustomSections::WideArithmeticFeature) {
feature = FeatureSet::WideArithmetic;
} else if (name == BinaryConsts::CustomSections::CompactImportsFeature) {
feature = FeatureSet::CompactImports;
} else {
// Silently ignore unknown features (this may be and old binaryen running
// on a new wasm).
Expand Down
1 change: 1 addition & 0 deletions src/wasm/wasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ const char* RelaxedAtomicsFeature = "relaxed-atomics";
const char* MultibyteFeature = "multibyte";
const char* CustomPageSizesFeature = "custom-page-sizes";
const char* WideArithmeticFeature = "wide-arithmetic";
const char* CompactImportsFeature = "compact-imports";

} // namespace BinaryConsts::CustomSections

Expand Down
1 change: 1 addition & 0 deletions test/binaryen.js/kitchen-sink.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ function test_features() {
console.log("Features.RelaxedAtomics: " + binaryen.Features.RelaxedAtomics);
console.log("Features.CustomPageSizes: " + binaryen.Features.CustomPageSizes);
console.log("Features.WideArithmetic: " + binaryen.Features.WideArithmetic);
console.log("Features.CompactImports: " + binaryen.Features.CompactImports);
console.log("Features.All: " + binaryen.Features.All);
}

Expand Down
3 changes: 2 additions & 1 deletion test/binaryen.js/kitchen-sink.js.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ Features.MultiMemory: 32768
Features.RelaxedAtomics: 4194304
Features.CustomPageSizes: 8388608
Features.WideArithmetic: 33554432
Features.All: 67108863
Features.CompactImports: 67108864
Features.All: 134217727
InvalidId: 0
BlockId: 1
IfId: 2
Expand Down
2 changes: 2 additions & 0 deletions test/example/c-api-kitchen-sink.c
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,8 @@ void test_features() {
printf("BinaryenFeatureMultibyte: %d\n", BinaryenFeatureMultibyte());
printf("BinaryenFeatureWideArithmetic: %d\n",
BinaryenFeatureWideArithmetic());
printf("BinaryenFeatureCompactImports: %d\n",
BinaryenFeatureCompactImports());
printf("BinaryenFeatureAll: %d\n", BinaryenFeatureAll());
}

Expand Down
3 changes: 2 additions & 1 deletion test/example/c-api-kitchen-sink.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ BinaryenFeatureRelaxedAtomics: 4194304
BinaryenFeatureCustomPageSizes: 8388608
BinaryenFeatureMultibyte: 16777216
BinaryenFeatureWideArithmetic: 33554432
BinaryenFeatureAll: 67108863
BinaryenFeatureCompactImports: 67108864
BinaryenFeatureAll: 134217727
(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 @@ -152,6 +152,10 @@
;; CHECK-NEXT:
;; CHECK-NEXT: --disable-wide-arithmetic Disable wide arithmetic
;; CHECK-NEXT:
;; CHECK-NEXT: --enable-compact-imports Enable compact import section
;; CHECK-NEXT:
;; CHECK-NEXT: --disable-compact-imports Disable compact import section
;; 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 @@ -159,6 +159,10 @@
;; CHECK-NEXT:
;; CHECK-NEXT: --disable-wide-arithmetic Disable wide arithmetic
;; CHECK-NEXT:
;; CHECK-NEXT: --enable-compact-imports Enable compact import section
;; CHECK-NEXT:
;; CHECK-NEXT: --disable-compact-imports Disable compact import section
;; 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 @@ -145,6 +145,10 @@
;; CHECK-NEXT:
;; CHECK-NEXT: --disable-wide-arithmetic Disable wide arithmetic
;; CHECK-NEXT:
;; CHECK-NEXT: --enable-compact-imports Enable compact import section
;; CHECK-NEXT:
;; CHECK-NEXT: --disable-compact-imports Disable compact import section
;; 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 @@ -187,6 +187,10 @@
;; CHECK-NEXT:
;; CHECK-NEXT: --disable-wide-arithmetic Disable wide arithmetic
;; CHECK-NEXT:
;; CHECK-NEXT: --enable-compact-imports Enable compact import section
;; CHECK-NEXT:
;; CHECK-NEXT: --disable-compact-imports Disable compact import section
;; 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 @@ -182,6 +182,10 @@
;; CHECK-NEXT:
;; CHECK-NEXT: --disable-wide-arithmetic Disable wide arithmetic
;; CHECK-NEXT:
;; CHECK-NEXT: --enable-compact-imports Enable compact import section
;; CHECK-NEXT:
;; CHECK-NEXT: --disable-compact-imports Disable compact import section
;; 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 @@ -825,6 +825,10 @@
;; CHECK-NEXT:
;; CHECK-NEXT: --disable-wide-arithmetic Disable wide arithmetic
;; CHECK-NEXT:
;; CHECK-NEXT: --enable-compact-imports Enable compact import section
;; CHECK-NEXT:
;; CHECK-NEXT: --disable-compact-imports Disable compact import section
;; 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-opt.test
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,10 @@
;; CHECK-NEXT:
;; CHECK-NEXT: --disable-wide-arithmetic Disable wide arithmetic
;; CHECK-NEXT:
;; CHECK-NEXT: --enable-compact-imports Enable compact import section
;; CHECK-NEXT:
;; CHECK-NEXT: --disable-compact-imports Disable compact import section
;; 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 @@ -235,6 +235,10 @@
;; CHECK-NEXT:
;; CHECK-NEXT: --disable-wide-arithmetic Disable wide arithmetic
;; CHECK-NEXT:
;; CHECK-NEXT: --enable-compact-imports Enable compact import section
;; CHECK-NEXT:
;; CHECK-NEXT: --disable-compact-imports Disable compact import section
;; 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 @@ -296,6 +296,10 @@
;; CHECK-NEXT:
;; CHECK-NEXT: --disable-wide-arithmetic Disable wide arithmetic
;; CHECK-NEXT:
;; CHECK-NEXT: --enable-compact-imports Enable compact import section
;; CHECK-NEXT:
;; CHECK-NEXT: --disable-compact-imports Disable compact import section
;; 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/wasm2js.test
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,10 @@
;; CHECK-NEXT:
;; CHECK-NEXT: --disable-wide-arithmetic Disable wide arithmetic
;; CHECK-NEXT:
;; CHECK-NEXT: --enable-compact-imports Enable compact import section
;; CHECK-NEXT:
;; CHECK-NEXT: --disable-compact-imports Disable compact import section
;; CHECK-NEXT:
;; CHECK-NEXT: --enable-typed-function-references Deprecated compatibility flag
;; CHECK-NEXT:
;; CHECK-NEXT: --disable-typed-function-references Deprecated compatibility flag
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
--enable-custom-page-sizes
--enable-multibyte
--enable-wide-arithmetic
--enable-compact-imports
(module
(type $0 (func (result v128 externref)))
(func $foo (type $0) (result v128 externref)
Expand Down
Loading
Loading