Skip to content

Commit

Permalink
Add feature options to emscripten metadata (#1982)
Browse files Browse the repository at this point in the history
Emscripten runs wasm-emscripten-finalize before running wasm-opt, so the target features section is stripped out before optimizations are run. One option would have been to add another wasm-opt invocation at the very end to strip the target features section, but dumping the features as metadata avoids the extra tool invocation. In the long run, it would be nice to have only as single binaryen invocation to do all the work that needs doing.
  • Loading branch information
tlively authored Apr 5, 2019
1 parent 2129cef commit fc13d0f
Show file tree
Hide file tree
Showing 17 changed files with 170 additions and 25 deletions.
29 changes: 11 additions & 18 deletions src/tools/tool-options.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,12 @@ struct ToolOptions : public Options {
disabledFeatures.makeMVP();
});
(*this)
.addFeature("sign-ext", "sign extension operations",
FeatureSet::SignExt)
.addFeature("threads", "atomic operations",
FeatureSet::Atomics)
.addFeature("mutable-globals", "mutable globals",
FeatureSet::MutableGlobals)
.addFeature("nontrapping-float-to-int",
"nontrapping float-to-int operations",
FeatureSet::TruncSat)
.addFeature("simd", "SIMD operations and types",
FeatureSet::SIMD)
.addFeature("bulk-memory", "bulk memory operations",
FeatureSet::BulkMemory)
.addFeature(FeatureSet::SignExt, "sign extension operations")
.addFeature(FeatureSet::Atomics, "atomic operations")
.addFeature(FeatureSet::MutableGlobals, "mutable globals")
.addFeature(FeatureSet::TruncSat, "nontrapping float-to-int operations")
.addFeature(FeatureSet::SIMD, "SIMD operations and types")
.addFeature(FeatureSet::BulkMemory, "bulk memory operations")
.add("--no-validation", "-n",
"Disables validation, assumes inputs are correct",
Options::Arguments::Zero,
Expand All @@ -81,11 +74,11 @@ struct ToolOptions : public Options {
});
}

ToolOptions& addFeature(const std::string& name,
const std::string& description,
FeatureSet::Feature feature) {
ToolOptions& addFeature(FeatureSet::Feature feature,
const std::string& description) {

(*this)
.add(std::string("--enable-") + name, "",
.add(std::string("--enable-") + FeatureSet::toString(feature), "",
std::string("Enable ") + description, Arguments::Zero,
[=](Options*, const std::string&) {
hasFeatureOptions = true;
Expand All @@ -94,7 +87,7 @@ struct ToolOptions : public Options {
disabledFeatures.set(feature, false);
})

.add(std::string("--disable-") + name, "",
.add(std::string("--disable-") + FeatureSet::toString(feature), "",
std::string("Disable ") + description, Arguments::Zero,
[=](Options*, const std::string&) {
hasFeatureOptions = true;
Expand Down
18 changes: 13 additions & 5 deletions src/tools/wasm-emscripten-finalize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@

#include "ir/trapping.h"
#include "support/colors.h"
#include "support/command-line.h"
#include "support/file.h"
#include "tool-options.h"
#include "wasm-binary.h"
#include "wasm-emscripten.h"
#include "wasm-io.h"
Expand All @@ -50,8 +50,8 @@ int main(int argc, const char *argv[]) {
bool legalizeJavaScriptFFI = true;
uint64_t globalBase = INVALID_BASE;
uint64_t initialStackPointer = INVALID_BASE;
Options options("wasm-emscripten-finalize",
"Performs Emscripten-specific transforms on .wasm files");
ToolOptions options("wasm-emscripten-finalize",
"Performs Emscripten-specific transforms on .wasm files");
options
.add("--output", "-o", "Output file",
Options::Arguments::One,
Expand Down Expand Up @@ -130,6 +130,8 @@ int main(int argc, const char *argv[]) {
Fatal() << "error in parsing wasm source map";
}

options.calculateFeatures(wasm);

if (options.debug) {
std::cerr << "Module before:\n";
WasmPrinter::printModule(&wasm, std::cerr);
Expand Down Expand Up @@ -204,12 +206,11 @@ int main(int argc, const char *argv[]) {
legalizeJavaScriptFFI ? ABI::LegalizationLevel::Full
: ABI::LegalizationLevel::Minimal
));
passRunner.add("strip-target-features");
passRunner.run();
}

// Substantial changes to the wasm are done, enough to create the metadata.
std::string metadata = generator.generateEmscriptenMetadata(dataSize, initializerFunctions);
std::string metadata = generator.generateEmscriptenMetadata(dataSize, initializerFunctions, options.passOptions.features);

// Finally, separate out data segments if relevant (they may have been needed
// for metadata).
Expand All @@ -226,6 +227,13 @@ int main(int argc, const char *argv[]) {
WasmPrinter::printModule(&wasm, std::cerr);
}

// Strip target features section (its information is in the metadata)
{
PassRunner passRunner(&wasm);
passRunner.add("strip-target-features");
passRunner.run();
}

auto outputBinaryFlag = emitBinary ? Flags::Binary : Flags::Text;
Output output(outfile, outputBinaryFlag, Flags::Release);
ModuleWriter writer;
Expand Down
3 changes: 2 additions & 1 deletion src/wasm-emscripten.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ class EmscriptenGlueGenerator {
void replaceStackPointerGlobal();

std::string generateEmscriptenMetadata(
Address staticBump, std::vector<Name> const& initializerFunctions);
Address staticBump, std::vector<Name> const& initializerFunctions,
FeatureSet features);

void fixInvokeFunctionNames();

Expand Down
25 changes: 25 additions & 0 deletions src/wasm-features.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
#define wasm_features_h

#include <stdint.h>
#include <string>

#include "compiler-support.h"

struct FeatureSet {
enum Feature : uint32_t {
Expand All @@ -31,6 +34,18 @@ struct FeatureSet {
All = Atomics | MutableGlobals | TruncSat | SIMD | BulkMemory | SignExt
};

static std::string toString(Feature f) {
switch (f) {
case Atomics: return "threads";
case MutableGlobals: return "mutable-globals";
case TruncSat: return "nontrapping-float-to-int";
case SIMD: return "simd";
case BulkMemory: return "bulk-memory";
case SignExt: return "sign-ext";
default: WASM_UNREACHABLE();
}
}

FeatureSet() : features(MVP) {}
FeatureSet(uint32_t features) : features(features) {}

Expand Down Expand Up @@ -59,6 +74,16 @@ struct FeatureSet {
features = features & ~other.features & All;
}

template<typename F>
void iterFeatures(F f) {
if (hasAtomics()) f(Atomics);
if (hasMutableGlobals()) f(MutableGlobals);
if (hasTruncSat()) f(TruncSat);
if (hasSIMD()) f(SIMD);
if (hasBulkMemory()) f(BulkMemory);
if (hasSignExt()) f(SignExt);
}

bool operator<=(const FeatureSet& other) {
return !(features & ~other.features);
}
Expand Down
12 changes: 11 additions & 1 deletion src/wasm/wasm-emscripten.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,8 @@ void printSet(std::ostream& o, C& c) {
}

std::string EmscriptenGlueGenerator::generateEmscriptenMetadata(
Address staticBump, std::vector<Name> const& initializerFunctions) {
Address staticBump, std::vector<Name> const& initializerFunctions,
FeatureSet features) {
bool commaFirst;
auto nextElement = [&commaFirst]() {
if (commaFirst) {
Expand Down Expand Up @@ -926,6 +927,15 @@ std::string EmscriptenGlueGenerator::generateEmscriptenMetadata(
}
});
meta << "\n ]\n";

meta << " \"features\": [";
commaFirst = true;
meta << nextElement() << "\"--mvp-features\"";
features.iterFeatures([&](FeatureSet::Feature f) {
meta << nextElement() << "\"--enable-" << FeatureSet::toString(f) << '"';
});
meta << "\n ]\n";

meta << "}\n";

return meta.str();
Expand Down
9 changes: 9 additions & 0 deletions test/lld/duplicate_imports.wast.out
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,15 @@
"invokeFuncs": [
"invoke_ffd"
]
"features": [
"--mvp-features",
"--enable-threads",
"--enable-mutable-globals",
"--enable-nontrapping-float-to-int",
"--enable-simd",
"--enable-bulk-memory",
"--enable-sign-ext"
]
}
-- END METADATA --
;)
9 changes: 9 additions & 0 deletions test/lld/em_asm.wast.mem.out
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,15 @@
},
"invokeFuncs": [
]
"features": [
"--mvp-features",
"--enable-threads",
"--enable-mutable-globals",
"--enable-nontrapping-float-to-int",
"--enable-simd",
"--enable-bulk-memory",
"--enable-sign-ext"
]
}
-- END METADATA --
;)
9 changes: 9 additions & 0 deletions test/lld/em_asm.wast.out
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,15 @@
},
"invokeFuncs": [
]
"features": [
"--mvp-features",
"--enable-threads",
"--enable-mutable-globals",
"--enable-nontrapping-float-to-int",
"--enable-simd",
"--enable-bulk-memory",
"--enable-sign-ext"
]
}
-- END METADATA --
;)
9 changes: 9 additions & 0 deletions test/lld/em_asm_O0.wast.out
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,15 @@
},
"invokeFuncs": [
]
"features": [
"--mvp-features",
"--enable-threads",
"--enable-mutable-globals",
"--enable-nontrapping-float-to-int",
"--enable-simd",
"--enable-bulk-memory",
"--enable-sign-ext"
]
}
-- END METADATA --
;)
9 changes: 9 additions & 0 deletions test/lld/em_asm_table.wast.out
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@
},
"invokeFuncs": [
]
"features": [
"--mvp-features",
"--enable-threads",
"--enable-mutable-globals",
"--enable-nontrapping-float-to-int",
"--enable-simd",
"--enable-bulk-memory",
"--enable-sign-ext"
]
}
-- END METADATA --
;)
9 changes: 9 additions & 0 deletions test/lld/em_js_O0.wast.out
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@
},
"invokeFuncs": [
]
"features": [
"--mvp-features",
"--enable-threads",
"--enable-mutable-globals",
"--enable-nontrapping-float-to-int",
"--enable-simd",
"--enable-bulk-memory",
"--enable-sign-ext"
]
}
-- END METADATA --
;)
9 changes: 9 additions & 0 deletions test/lld/hello_world.wast.mem.out
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@
},
"invokeFuncs": [
]
"features": [
"--mvp-features",
"--enable-threads",
"--enable-mutable-globals",
"--enable-nontrapping-float-to-int",
"--enable-simd",
"--enable-bulk-memory",
"--enable-sign-ext"
]
}
-- END METADATA --
;)
9 changes: 9 additions & 0 deletions test/lld/hello_world.wast.out
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@
},
"invokeFuncs": [
]
"features": [
"--mvp-features",
"--enable-threads",
"--enable-mutable-globals",
"--enable-nontrapping-float-to-int",
"--enable-simd",
"--enable-bulk-memory",
"--enable-sign-ext"
]
}
-- END METADATA --
;)
9 changes: 9 additions & 0 deletions test/lld/init.wast.out
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@
},
"invokeFuncs": [
]
"features": [
"--mvp-features",
"--enable-threads",
"--enable-mutable-globals",
"--enable-nontrapping-float-to-int",
"--enable-simd",
"--enable-bulk-memory",
"--enable-sign-ext"
]
}
-- END METADATA --
;)
9 changes: 9 additions & 0 deletions test/lld/recursive.wast.out
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,15 @@
},
"invokeFuncs": [
]
"features": [
"--mvp-features",
"--enable-threads",
"--enable-mutable-globals",
"--enable-nontrapping-float-to-int",
"--enable-simd",
"--enable-bulk-memory",
"--enable-sign-ext"
]
}
-- END METADATA --
;)
9 changes: 9 additions & 0 deletions test/lld/reserved_func_ptr.wast.out
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,15 @@
},
"invokeFuncs": [
]
"features": [
"--mvp-features",
"--enable-threads",
"--enable-mutable-globals",
"--enable-nontrapping-float-to-int",
"--enable-simd",
"--enable-bulk-memory",
"--enable-sign-ext"
]
}
-- END METADATA --
;)
9 changes: 9 additions & 0 deletions test/lld/shared.wast.out
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@
},
"invokeFuncs": [
]
"features": [
"--mvp-features",
"--enable-threads",
"--enable-mutable-globals",
"--enable-nontrapping-float-to-int",
"--enable-simd",
"--enable-bulk-memory",
"--enable-sign-ext"
]
}
-- END METADATA --
;)

0 comments on commit fc13d0f

Please sign in to comment.