Skip to content

Commit

Permalink
[EH] Add --experimental-new-eh option to wasm-opt (WebAssembly#6270)
Browse files Browse the repository at this point in the history
This adds `--experimental-new-eh` option to `wasm-opt`. The difference
between this and `--translate-to-new-eh` is, `--translate-to-new-eh`
just runs `TranslateToNewEH` pass, while `--experimental-new-eh`
attaches `TranslateToNewEH` pass at the end of the whole optimization
pipeline. So if no other passes or optimization options (`-On`) are
specified, it is equivalent to `--translate-to-new-eh`. If other
optimization passes are specified, it runs them and at the end run the
translator to ensure the new EH instructions are emitted. The reason we
are doing this this way is that the optimization pipeline as a whole
does not support the new EH instruction yet, but we would like to
provide an option to emit a reasonably OK code with the new EH
instructions.

This also means when the optimization level > 3, it will also run
the StackIR + local2stack optimization after the translation.

Not sure how to test the output of this option, given that there is not
much point in testing the default optimization passes, and it is also
not clear how to print the stack IR if the stack ir generation and
optimization runs as a part of the pipeline and not the explicit command
line options.

This is created in favor of WebAssembly#6267, which added the option to
`optimization-options.h`. It had a problem of running the translator
multiple times when `-On` was given multiple times in the command line,
which I learned was rather a common usage. This adds the option directly
to `wasm-opt.cpp`, which avoids the problem. With this, it is still
possible to create and optimize Stack IR unnecessarily, but that feels a
better alternative.
  • Loading branch information
aheejin authored Feb 6, 2024
1 parent d490318 commit 3db60df
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 2 deletions.
39 changes: 37 additions & 2 deletions src/tools/wasm-opt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ int main(int argc, const char* argv[]) {
std::string inputSourceMapFilename;
std::string outputSourceMapFilename;
std::string outputSourceMapUrl;
bool experimentalNewEH = false;

const std::string WasmOptOption = "wasm-opt options";

Expand Down Expand Up @@ -240,7 +241,18 @@ int main(int argc, const char* argv[]) {
Options::Arguments::One,
[](Options* o, const std::string& argument) {
o->extra["infile"] = argument;
});
})
.add("--experimental-new-eh",
"",
"After running all requested transformations / optimizations, "
"translate the instruction to use the new EH instructions at the end. "
"Depending on the optimization level specified, this may do some more "
"post-translation optimizations.",
WasmOptOption,
Options::Arguments::Zero,
[&experimentalNewEH](Options*, const std::string&) {
experimentalNewEH = true;
});
options.parse(argc, argv);

Module wasm;
Expand Down Expand Up @@ -360,8 +372,11 @@ int main(int argc, const char* argv[]) {
std::cout << "[extra-fuzz-command first output:]\n" << firstOutput << '\n';
}

bool translateToNewEH =
wasm.features.hasExceptionHandling() && experimentalNewEH;

if (!options.runningPasses()) {
if (!options.quiet) {
if (!options.quiet && !translateToNewEH) {
std::cerr << "warning: no passes specified, not doing any work\n";
}
} else {
Expand Down Expand Up @@ -398,6 +413,26 @@ int main(int argc, const char* argv[]) {
}
}

if (translateToNewEH) {
BYN_TRACE("translating to new EH instructions...\n");
PassRunner runner(&wasm, options.passOptions);
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);
if (!valid) {
exitOnInvalidWasm("error after opts");
}
}
}

if (fuzzExecAfter) {
results.check(wasm);
}
Expand Down
9 changes: 9 additions & 0 deletions test/lit/help/wasm-opt.test
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@
;; CHECK-NEXT: --new-wat-parser Use the experimental new WAT
;; CHECK-NEXT: parser
;; CHECK-NEXT:
;; CHECK-NEXT: --experimental-new-eh After running all requested
;; CHECK-NEXT: transformations / optimizations,
;; CHECK-NEXT: translate the instruction to use
;; CHECK-NEXT: the new EH instructions at the
;; CHECK-NEXT: end. Depending on the
;; CHECK-NEXT: optimization level specified,
;; CHECK-NEXT: this may do some more
;; CHECK-NEXT: post-translation optimizations.
;; CHECK-NEXT:
;; CHECK-NEXT:
;; CHECK-NEXT: Optimization passes:
;; CHECK-NEXT: --------------------
Expand Down
28 changes: 28 additions & 0 deletions test/lit/passes/experimental-new_eh.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
;; When given alone, --experimental-new-eh just runs --translate-to-new-eh
;; RUN: wasm-opt %s -all --translate-to-new-eh -S -o %a.wasm
;; RUN: wasm-opt %s -all --experimental-new-eh -S -o %b.wasm
;; RUN: diff %a.wasm %b.wasm

;; When given with other flags, --experimental-new-eh runs the translator after
;; running other passes. If --optimize-level >=3, --experimenal-new-eh also runs
;; StackIR (+ local2stack) optimization. So running '-O --experimental-new-eh'
;; should be the same as running all these passes separately.
;; RUN: wasm-opt %s -all -O --translate-to-new-eh --optimize-level=3 --generate-stack-ir --optimize-stack-ir -o %a.wasm
;; RUN: wasm-opt %s -all -O --experimental-new-eh -o %b.wasm
;; RUN: diff %a.wasm %b.wasm

(module
(import "env" "foo" (func $foo))
(start $test)
(func $test
(try $l
(do
(call $foo)
)
(catch_all
(call $foo)
(rethrow $l)
)
)
)
)

0 comments on commit 3db60df

Please sign in to comment.