summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHeejin Ahn <aheejin@gmail.com>2023-06-15 17:20:28 -0700
committerGitHub <noreply@github.com>2023-06-15 17:20:28 -0700
commita317958d3026632dcdeda5cc9850fd79dda59e9d (patch)
treec29462ee3b12e4c9deeab7ab65fe096f15da2572
parent599a9627ce88b66eeb4e2be24163fa62f0533743 (diff)
downloadbinaryen-a317958d3026632dcdeda5cc9850fd79dda59e9d.tar.gz
binaryen-a317958d3026632dcdeda5cc9850fd79dda59e9d.tar.bz2
binaryen-a317958d3026632dcdeda5cc9850fd79dda59e9d.zip
[EH] Add pass to remove EH instructions (#5770)
This pass strips all EH stuff, including EH instructions and tags, from the input module and disables the EH feature from the features section. 1. This removes `catch` and `catch_all` blocks from the code. So ```wast (try (do (some code) ) (catch ... ) ) ``` becomes just `(some code)`. Note that all `rethrow`s will be removed with `catch`es. Note that all `rethrow`s will be removed with `catch`es. 2. This converts 'throw (...)` into `unreachable`. Note that `rethrows 3. This removes all tags from the module, which are unused anyway after 1 and 2. 4. This removes exception handling feature from the features section. You can use the pass with ```console $ wasm-opt --enable-exception-handling --strip-eh INPUT -o OUTPUT ``` This is not an optimization pass, so it is not run unless you specify the pass explicitly. This is in effect similar to Clang's `-fignore-exceptions`, in which you can throw but it will result in a crash and we compile away all landing pads. This can be used for people who don't (or can't) use `-fignore-exceptions` in their build settings or who want to compile away `catch` blocks later. Closes emscripten-core/emscripten#19585.
-rw-r--r--src/passes/CMakeLists.txt1
-rw-r--r--src/passes/StripEH.cpp78
-rw-r--r--src/passes/pass.cpp1
-rw-r--r--src/passes/passes.h1
-rw-r--r--test/lit/help/wasm-opt.test2
-rw-r--r--test/lit/help/wasm2js.test2
-rw-r--r--test/lit/passes/strip-eh.wast117
7 files changed, 202 insertions, 0 deletions
diff --git a/src/passes/CMakeLists.txt b/src/passes/CMakeLists.txt
index 9d16f9782..4cf28fa65 100644
--- a/src/passes/CMakeLists.txt
+++ b/src/passes/CMakeLists.txt
@@ -110,6 +110,7 @@ set(passes_SOURCES
Souperify.cpp
SpillPointers.cpp
StackCheck.cpp
+ StripEH.cpp
SSAify.cpp
Untee.cpp
Vacuum.cpp
diff --git a/src/passes/StripEH.cpp b/src/passes/StripEH.cpp
new file mode 100644
index 000000000..d2a68d630
--- /dev/null
+++ b/src/passes/StripEH.cpp
@@ -0,0 +1,78 @@
+/*
+ * Copyright 2023 WebAssembly Community Group participants
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+//
+// Removes all EH instructions and tags. Removes catch blocks and converts
+// 'throw's into 'unreachable's. Any exception thrown will crash the program as
+// they are now traps.
+//
+
+#include <ir/drop.h>
+#include <ir/utils.h>
+
+namespace wasm {
+
+namespace {
+
+struct StripEHImpl : public WalkerPass<PostWalker<StripEHImpl>> {
+ bool isFunctionParallel() override { return true; }
+
+ bool refinalize = false;
+
+ std::unique_ptr<Pass> create() override {
+ return std::make_unique<StripEHImpl>();
+ }
+
+ void visitThrow(Throw* curr) {
+ auto& wasm = *getModule();
+ Builder builder(wasm);
+ replaceCurrent(getDroppedChildrenAndAppend(curr,
+ wasm,
+ getPassOptions(),
+ builder.makeUnreachable(),
+ DropMode::IgnoreParentEffects));
+ }
+
+ void visitTry(Try* curr) {
+ replaceCurrent(curr->body);
+ refinalize = true;
+ }
+
+ void visitFunction(Function* curr) {
+ if (refinalize) {
+ ReFinalize().walkFunctionInModule(curr, getModule());
+ }
+ }
+};
+
+struct StripEH : public Pass {
+ void run(Module* wasm) override {
+ PassRunner runner(wasm);
+ // We run this as an inner pass to make it parallel. This StripEH pass
+ // itself cannot be parallel because we need to disable the EH feature.
+ runner.add(std::make_unique<StripEHImpl>());
+ runner.setIsNested(true);
+ runner.run();
+ wasm->removeTags([](Tag*) { return true; });
+ wasm->features.disable(FeatureSet::ExceptionHandling);
+ }
+};
+
+} // anonymous namespace
+
+Pass* createStripEHPass() { return new StripEH(); }
+
+} // namespace wasm
diff --git a/src/passes/pass.cpp b/src/passes/pass.cpp
index ff2c7d45f..0411dcc75 100644
--- a/src/passes/pass.cpp
+++ b/src/passes/pass.cpp
@@ -464,6 +464,7 @@ void PassRegistry::registerPasses() {
registerPass("strip-producers",
"strip the wasm producers section",
createStripProducersPass);
+ registerPass("strip-eh", "strip EH instructions", createStripEHPass);
registerPass("strip-target-features",
"strip the wasm target features section",
createStripTargetFeaturesPass);
diff --git a/src/passes/passes.h b/src/passes/passes.h
index 346741b1a..d8855d4a8 100644
--- a/src/passes/passes.h
+++ b/src/passes/passes.h
@@ -145,6 +145,7 @@ Pass* createStripTargetFeaturesPass();
Pass* createSouperifyPass();
Pass* createSouperifySingleUsePass();
Pass* createSpillPointersPass();
+Pass* createStripEHPass();
Pass* createStubUnsupportedJSOpsPass();
Pass* createSSAifyPass();
Pass* createSSAifyNoMergePass();
diff --git a/test/lit/help/wasm-opt.test b/test/lit/help/wasm-opt.test
index 2e10ed073..30369e62f 100644
--- a/test/lit/help/wasm-opt.test
+++ b/test/lit/help/wasm-opt.test
@@ -452,6 +452,8 @@
;; CHECK-NEXT:
;; CHECK-NEXT: --strip-dwarf strip dwarf debug info
;; CHECK-NEXT:
+;; CHECK-NEXT: --strip-eh strip EH instructions
+;; CHECK-NEXT:
;; CHECK-NEXT: --strip-producers strip the wasm producers section
;; CHECK-NEXT:
;; CHECK-NEXT: --strip-target-features strip the wasm target features
diff --git a/test/lit/help/wasm2js.test b/test/lit/help/wasm2js.test
index 1766d771d..916dbbb4d 100644
--- a/test/lit/help/wasm2js.test
+++ b/test/lit/help/wasm2js.test
@@ -411,6 +411,8 @@
;; CHECK-NEXT:
;; CHECK-NEXT: --strip-dwarf strip dwarf debug info
;; CHECK-NEXT:
+;; CHECK-NEXT: --strip-eh strip EH instructions
+;; CHECK-NEXT:
;; CHECK-NEXT: --strip-producers strip the wasm producers section
;; CHECK-NEXT:
;; CHECK-NEXT: --strip-target-features strip the wasm target features
diff --git a/test/lit/passes/strip-eh.wast b/test/lit/passes/strip-eh.wast
new file mode 100644
index 000000000..dc394aebf
--- /dev/null
+++ b/test/lit/passes/strip-eh.wast
@@ -0,0 +1,117 @@
+;; NOTE: Assertions have been generated by update_lit_checks.py and should not be edited.
+;; RUN: wasm-opt %s -all --strip-eh -S -o - | filecheck %s
+
+;; Remove all EH instructions and tags. Converts 'throw's into 'unreachable's.
+
+(module
+ (tag $e-i32 (param i32))
+ (tag $e-f32 (param f32))
+
+ ;; CHECK: (func $throw-i32 (type $none_=>_none)
+ ;; CHECK-NEXT: (unreachable)
+ ;; CHECK-NEXT: )
+ (func $throw-i32
+ (throw $e-i32 (i32.const 0))
+ )
+ ;; CHECK: (func $throw-f32 (type $none_=>_none)
+ ;; CHECK-NEXT: (unreachable)
+ ;; CHECK-NEXT: )
+ (func $throw-f32
+ (throw $e-f32 (f32.const 0.0))
+ )
+
+ ;; CHECK: (func $try-catch (type $none_=>_none)
+ ;; CHECK-NEXT: (call $throw-i32)
+ ;; CHECK-NEXT: )
+ (func $try-catch
+ (try
+ (do
+ (call $throw-i32)
+ )
+ (catch $e-i32
+ (drop (pop i32))
+ (call $throw-f32)
+ )
+ )
+ )
+
+ ;; CHECK: (func $try-catch2 (type $none_=>_none)
+ ;; CHECK-NEXT: (call $throw-i32)
+ ;; CHECK-NEXT: (call $throw-f32)
+ ;; CHECK-NEXT: )
+ (func $try-catch2
+ (try
+ (do
+ (call $throw-i32)
+ (call $throw-f32)
+ )
+ (catch $e-i32
+ (drop (pop i32))
+ )
+ )
+ )
+
+ ;; CHECK: (func $try-catch-all (type $none_=>_none)
+ ;; CHECK-NEXT: (unreachable)
+ ;; CHECK-NEXT: )
+ (func $try-catch-all
+ (try
+ (do
+ (throw $e-i32 (i32.const 0))
+ )
+ (catch_all
+ (call $throw-f32)
+ )
+ )
+ )
+
+ ;; CHECK: (func $try-catch-nested (type $none_=>_none)
+ ;; CHECK-NEXT: (call $throw-i32)
+ ;; CHECK-NEXT: )
+ (func $try-catch-nested
+ (try
+ (do
+ (try
+ (do
+ (call $throw-i32)
+ )
+ (catch $e-i32
+ (drop (pop i32))
+ )
+ )
+ )
+ (catch_all
+ (try
+ (do
+ (call $throw-f32)
+ )
+ (catch_all
+ (call $throw-f32)
+ )
+ )
+ )
+ )
+ )
+
+ ;; CHECK: (func $try-unreachable-body (type $none_=>_i32) (result i32)
+ ;; CHECK-NEXT: (i32.add
+ ;; CHECK-NEXT: (unreachable)
+ ;; CHECK-NEXT: (i32.const 0)
+ ;; CHECK-NEXT: )
+ ;; CHECK-NEXT: )
+ (func $try-unreachable-body (result i32)
+ (i32.add
+ ;; This becomes unreachable while the parent expects i32, so this requires
+ ;; refinalization.
+ (try (result i32)
+ (do
+ (throw $e-i32 (i32.const 0))
+ )
+ (catch $e-i32
+ (pop i32)
+ )
+ )
+ (i32.const 0)
+ )
+ )
+)