diff options
author | Alon Zakai <azakai@google.com> | 2024-05-09 15:00:13 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-09 15:00:13 -0700 |
commit | 7b2e0190213487b5d2505fe86aa9bbbd30e80fcc (patch) | |
tree | 2ae614b27102d83452b0f075612c7558c4493aa6 /src/tools | |
parent | 006181bb98118c70d36e84e6f1f72b5d60264817 (diff) | |
download | binaryen-7b2e0190213487b5d2505fe86aa9bbbd30e80fcc.tar.gz binaryen-7b2e0190213487b5d2505fe86aa9bbbd30e80fcc.tar.bz2 binaryen-7b2e0190213487b5d2505fe86aa9bbbd30e80fcc.zip |
[StackIR] Run StackIR during binary writing and not as a pass (#6568)
Previously we had passes --generate-stack-ir, --optimize-stack-ir, --print-stack-ir
that could be run like any other passes. After generating StackIR it was stashed on
the function and invalidated if we modified BinaryenIR. If it wasn't invalidated then
it was used during binary writing. This PR switches things so that we optionally
generate, optimize, and print StackIR only during binary writing. It also removes
all traces of StackIR from wasm.h - after this, StackIR is a feature of binary writing
(and printing) logic only.
This is almost NFC, but there are some minor noticeable differences:
1. We no longer print has StackIR in the text format when we see it is there. It
will not be there during normal printing, as it is only present during binary writing.
(but --print-stack-ir still works as before; as mentioned above it runs during writing).
2. --generate/optimize/print-stack-ir change from being passes to being flags that
control that behavior instead. As passes, their order on the commandline mattered,
while now it does not, and they only "globally" affect things during writing.
3. The C API changes slightly, as there is no need to pass it an option "optimize" to
the StackIR APIs. Whether we optimize is handled by --optimize-stack-ir which is
set like other optimization flags on the PassOptions object, so we don't need the
old option to those C APIs.
The main benefit here is simplifying the code, so we don't need to think about
StackIR in more places than just binary writing. That may also allow future
improvements to our usage of StackIR.
Diffstat (limited to 'src/tools')
-rw-r--r-- | src/tools/optimization-options.h | 11 | ||||
-rw-r--r-- | src/tools/tool-options.h | 30 | ||||
-rw-r--r-- | src/tools/wasm-as.cpp | 2 | ||||
-rw-r--r-- | src/tools/wasm-ctor-eval.cpp | 2 | ||||
-rw-r--r-- | src/tools/wasm-emscripten-finalize.cpp | 2 | ||||
-rw-r--r-- | src/tools/wasm-merge.cpp | 2 | ||||
-rw-r--r-- | src/tools/wasm-metadce.cpp | 2 | ||||
-rw-r--r-- | src/tools/wasm-opt.cpp | 16 | ||||
-rw-r--r-- | src/tools/wasm-reduce.cpp | 4 | ||||
-rw-r--r-- | src/tools/wasm-split/wasm-split.cpp | 2 |
10 files changed, 56 insertions, 17 deletions
diff --git a/src/tools/optimization-options.h b/src/tools/optimization-options.h index 0a47d9f70..8772edd29 100644 --- a/src/tools/optimization-options.h +++ b/src/tools/optimization-options.h @@ -26,6 +26,17 @@ namespace wasm { struct OptimizationOptions : public ToolOptions { + void parse(int argc, const char* argv[]) { + ToolOptions::parse(argc, argv); + + // After parsing the arguments, update defaults based on the optimize/shrink + // levels. + if (passOptions.optimizeLevel >= 2 || passOptions.shrinkLevel >= 1) { + passOptions.generateStackIR = true; + passOptions.optimizeStackIR = true; + } + } + static constexpr const char* DEFAULT_OPT_PASSES = "O"; static constexpr const int OS_OPTIMIZE_LEVEL = 2; static constexpr const int OS_SHRINK_LEVEL = 1; diff --git a/src/tools/tool-options.h b/src/tools/tool-options.h index 6d68ff3c1..599b3b22c 100644 --- a/src/tools/tool-options.h +++ b/src/tools/tool-options.h @@ -151,7 +151,35 @@ struct ToolOptions : public Options { Options::Arguments::Zero, [this](Options*, const std::string&) { passOptions.closedWorld = true; - }); + }) + .add("--generate-stack-ir", + "", + "generate StackIR during writing", + ToolOptionsCategory, + Options::Arguments::Zero, + [&](Options* o, const std::string& arguments) { + passOptions.generateStackIR = true; + }) + .add("--optimize-stack-ir", + "", + "optimize StackIR during writing", + ToolOptionsCategory, + Options::Arguments::Zero, + [&](Options* o, const std::string& arguments) { + // Also generate StackIR, to have something to optimize. + passOptions.generateStackIR = true; + passOptions.optimizeStackIR = true; + }) + .add("--print-stack-ir", + "", + "print StackIR during writing", + ToolOptionsCategory, + Options::Arguments::Zero, + [&](Options* o, const std::string& arguments) { + // Also generate StackIR, to have something to print. + passOptions.generateStackIR = true; + passOptions.printStackIR = &std::cout; + }); } ToolOptions& addFeature(FeatureSet::Feature feature, diff --git a/src/tools/wasm-as.cpp b/src/tools/wasm-as.cpp index 311605326..a767e6908 100644 --- a/src/tools/wasm-as.cpp +++ b/src/tools/wasm-as.cpp @@ -129,7 +129,7 @@ int main(int argc, const char* argv[]) { if (options.debug) { std::cerr << "writing..." << std::endl; } - ModuleWriter writer; + ModuleWriter writer(options.passOptions); writer.setBinary(true); writer.setDebugInfo(debugInfo); if (sourceMapFilename.size()) { diff --git a/src/tools/wasm-ctor-eval.cpp b/src/tools/wasm-ctor-eval.cpp index 4018be0e7..d3f798084 100644 --- a/src/tools/wasm-ctor-eval.cpp +++ b/src/tools/wasm-ctor-eval.cpp @@ -1491,7 +1491,7 @@ int main(int argc, const char* argv[]) { if (options.debug) { std::cout << "writing..." << std::endl; } - ModuleWriter writer; + ModuleWriter writer(options.passOptions); writer.setBinary(emitBinary); writer.setDebugInfo(debugInfo); writer.write(wasm, options.extra["output"]); diff --git a/src/tools/wasm-emscripten-finalize.cpp b/src/tools/wasm-emscripten-finalize.cpp index a19d27328..6b4e994ac 100644 --- a/src/tools/wasm-emscripten-finalize.cpp +++ b/src/tools/wasm-emscripten-finalize.cpp @@ -279,7 +279,7 @@ int main(int argc, const char* argv[]) { if (writeOutput) { Output output(outfile, emitBinary ? Flags::Binary : Flags::Text); - ModuleWriter writer; + ModuleWriter writer(options.passOptions); writer.setDebugInfo(debugInfo); // writer.setSymbolMap(symbolMap); writer.setBinary(emitBinary); diff --git a/src/tools/wasm-merge.cpp b/src/tools/wasm-merge.cpp index 8e8e2d80d..449f0cfdb 100644 --- a/src/tools/wasm-merge.cpp +++ b/src/tools/wasm-merge.cpp @@ -752,7 +752,7 @@ Input source maps can be specified by adding an -ism option right after the modu // Output. if (options.extra.count("output") > 0) { - ModuleWriter writer; + ModuleWriter writer(options.passOptions); writer.setBinary(emitBinary); writer.setDebugInfo(debugInfo); if (outputSourceMapFilename.size()) { diff --git a/src/tools/wasm-metadce.cpp b/src/tools/wasm-metadce.cpp index 1b429a723..301470206 100644 --- a/src/tools/wasm-metadce.cpp +++ b/src/tools/wasm-metadce.cpp @@ -600,7 +600,7 @@ int main(int argc, const char* argv[]) { graph.apply(); if (options.extra.count("output") > 0) { - ModuleWriter writer; + ModuleWriter writer(options.passOptions); writer.setBinary(emitBinary); writer.setDebugInfo(debugInfo); if (outputSourceMapFilename.size()) { diff --git a/src/tools/wasm-opt.cpp b/src/tools/wasm-opt.cpp index 32f9f1aad..cb6d0bcec 100644 --- a/src/tools/wasm-opt.cpp +++ b/src/tools/wasm-opt.cpp @@ -35,6 +35,7 @@ #include "wasm-interpreter.h" #include "wasm-io.h" #include "wasm-s-parser.h" +#include "wasm-stack.h" #include "wasm-validator.h" #include "wasm2c-wrapper.h" @@ -347,7 +348,7 @@ int main(int argc, const char* argv[]) { if (extraFuzzCommand.size() > 0 && options.extra.count("output") > 0) { BYN_TRACE("writing binary before opts, for extra fuzz command...\n"); - ModuleWriter writer; + ModuleWriter writer(options.passOptions); writer.setBinary(emitBinary); writer.setDebugInfo(options.passOptions.debugInfo); writer.write(wasm, options.extra["output"]); @@ -379,7 +380,7 @@ int main(int argc, const char* argv[]) { // size no longer decreasing. auto getSize = [&]() { BufferWithRandomAccess buffer; - WasmBinaryWriter writer(&wasm, buffer); + WasmBinaryWriter writer(&wasm, buffer, options.passOptions); writer.write(); return buffer.size(); }; @@ -402,11 +403,6 @@ int main(int argc, const char* argv[]) { runner.add("translate-to-new-eh"); // Perform Stack IR optimizations here, at the very end of the // optimization pipeline. - if (options.passOptions.optimizeLevel >= 2 || - options.passOptions.shrinkLevel >= 1) { - runner.addIfNoDWARFIssues("generate-stack-ir"); - runner.addIfNoDWARFIssues("optimize-stack-ir"); - } runner.run(); if (options.passOptions.validate) { bool valid = WasmValidator().validate(wasm, options.passOptions); @@ -420,6 +416,10 @@ int main(int argc, const char* argv[]) { results.check(wasm); } + if (options.passOptions.printStackIR) { + printStackIR(std::cout, &wasm, options.passOptions); + } + if (options.extra.count("output") == 0) { if (!options.quiet) { std::cerr << "warning: no output file specified, not emitting output\n"; @@ -428,7 +428,7 @@ int main(int argc, const char* argv[]) { } BYN_TRACE("writing...\n"); - ModuleWriter writer; + ModuleWriter writer(options.passOptions); writer.setBinary(emitBinary); writer.setDebugInfo(options.passOptions.debugInfo); if (outputSourceMapFilename.size()) { diff --git a/src/tools/wasm-reduce.cpp b/src/tools/wasm-reduce.cpp index 094338bea..ac5b7722b 100644 --- a/src/tools/wasm-reduce.cpp +++ b/src/tools/wasm-reduce.cpp @@ -401,7 +401,7 @@ struct Reducer bool writeAndTestReduction(ProgramResult& out) { // write the module out - ModuleWriter writer; + ModuleWriter writer(toolOptions.passOptions); writer.setBinary(binary); writer.setDebugInfo(debugInfo); writer.write(*getModule(), test); @@ -1368,7 +1368,7 @@ int main(int argc, const char* argv[]) { if (resultOnInvalid == expected) { // Try it on a valid input. Module emptyModule; - ModuleWriter writer; + ModuleWriter writer(options.passOptions); writer.setBinary(true); writer.write(emptyModule, test); ProgramResult resultOnValid(command); diff --git a/src/tools/wasm-split/wasm-split.cpp b/src/tools/wasm-split/wasm-split.cpp index bea3ddce7..d7dc19d67 100644 --- a/src/tools/wasm-split/wasm-split.cpp +++ b/src/tools/wasm-split/wasm-split.cpp @@ -91,7 +91,7 @@ void adjustTableSize(Module& wasm, int initialSize) { void writeModule(Module& wasm, std::string filename, const WasmSplitOptions& options) { - ModuleWriter writer; + ModuleWriter writer(options.passOptions); writer.setBinary(options.emitBinary); writer.setDebugInfo(options.passOptions.debugInfo); if (options.emitModuleNames) { |