Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[EH] Add --experimental-new-eh option to wasm-opt #6270

Merged
merged 7 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
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
37 changes: 37 additions & 0 deletions test/lit/binary/experimental-new-eh_O.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
;; RUN: wasm-opt %s.old.wasm -all --experimental-new-eh -O -o %result.wasm
;; RUN: diff %result.wasm %s.new.wasm

;; experimental-new-eh_O.test.old.wasm was generated from:
;; (module
;; (import "env" "foo" (func $fimport$0))
;; (start $0)
;; (func $0
;; (try $l
;; (do
;; (call $fimport$0)
;; )
;; (catch_all
;; (rethrow $l)
;; )
;; )
;; )
;; )

;; experimental-new-eh_O.test.new.wasm should be in the form of:
;; (module
;; (import "env" "foo" (func $fimport$0))
;; (start $0)
;; (func $0
;; (local $0 exnref)
;; (block $label$1
;; (throw_ref
;; (block $label$2 (result exnref)
;; (try_table (catch_all_ref $label$2)
;; (call $fimport$0)
;; )
;; (br $label$1)
;; )
;; )
;; )
;; )
;; )
Binary file added test/lit/binary/experimental-new-eh_O.test.new.wasm
Binary file not shown.
Binary file added test/lit/binary/experimental-new-eh_O.test.old.wasm
Binary file not shown.
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
Loading