Skip to content

Commit

Permalink
Add a pass to remove exports from the WASM bundle
Browse files Browse the repository at this point in the history
This is helpful when e.g. there's a WASM shared library with all possible
functions exported but the application only needs a subset of those exports.
Removing exports in that case will enable further optimizers to remove more
code from the file.
  • Loading branch information
loganek committed Aug 16, 2024
1 parent 7209629 commit 4190e50
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/passes/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ set(passes_SOURCES
StripTargetFeatures.cpp
TraceCalls.cpp
RedundantSetElimination.cpp
RemoveExports.cpp
RemoveImports.cpp
RemoveMemory.cpp
RemoveNonJSOps.cpp
Expand Down
55 changes: 55 additions & 0 deletions src/passes/RemoveExports.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2024 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 function exports. This is particularly helpful for shared
// libraries where not all the exported functions are being used for
// a specific use case.
//

#include <unordered_map>

#include "pass.h"
#include "support/string.h"

namespace wasm {

struct RemoveExports : public Pass {
void run(Module* module) override {
auto arg =
getArgument("remove-exports",
"RemoveExports usage: wasm-opt "
"--remove-exports=EXPORT_NAME_1[,EXPORT_NAME_2[,...]]");

auto argsItems = String::Split(arg, ",");
auto exportsToRemove =
std::unordered_set<std::string>(argsItems.begin(), argsItems.end());

auto it = module->exports.begin();
while (it != module->exports.end()) {
if ((*it)->kind == ExternalKind::Function &&
exportsToRemove.count((*it)->name.toString())) {
it = module->exports.erase(it);
} else {
++it;
}
}
}
};

Pass* createRemoveExportsPass() { return new RemoveExports(); }

} // namespace wasm
1 change: 1 addition & 0 deletions src/passes/pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ void PassRegistry::registerPasses() {
registerPass("remove-imports",
"removes imports and replaces them with nops",
createRemoveImportsPass);
registerPass("remove-exports", "removes exports", createRemoveExportsPass);
registerPass(
"remove-memory", "removes memory segments", createRemoveMemoryPass);
registerPass("remove-unused-brs",
Expand Down
1 change: 1 addition & 0 deletions src/passes/passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ Pass* createPrintFeaturesPass();
Pass* createPrintFunctionMapPass();
Pass* createPropagateGlobalsGloballyPass();
Pass* createRemoveNonJSOpsPass();
Pass* createRemoveExportsPass();
Pass* createRemoveImportsPass();
Pass* createRemoveMemoryPass();
Pass* createRemoveUnusedBrsPass();
Expand Down
33 changes: 33 additions & 0 deletions test/lit/passes/remove-exports.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
;; NOTE: Assertions have been generated by update_lit_checks.py --all-items and should not be edited.

;; RUN: wasm-opt --remove-exports=foo,bar %s -S -o - | filecheck %s

(module

;; CHECK: (type $0 (func))

;; CHECK: (export "baz" (func $baz))

;; CHECK: (func $foo
;; CHECK-NEXT: (unreachable)
;; CHECK-NEXT: )
(func $foo
(unreachable)
)
;; CHECK: (func $bar
;; CHECK-NEXT: (unreachable)
;; CHECK-NEXT: )
(func $bar
(unreachable)
)
;; CHECK: (func $baz
;; CHECK-NEXT: (unreachable)
;; CHECK-NEXT: )
(func $baz
(unreachable)
)

(export "foo" (func $foo))
(export "bar" (func $bar))
(export "baz" (func $baz))
)

0 comments on commit 4190e50

Please sign in to comment.