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

Add a pass to remove exports from the WASM bundle #6844

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
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
2 changes: 2 additions & 0 deletions src/passes/pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,8 @@ void PassRegistry::registerPasses() {
registerPass("remove-imports",
"removes imports and replaces them with nops",
createRemoveImportsPass);
registerPass(
"remove-exports", "remove function 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
2 changes: 2 additions & 0 deletions test/lit/help/wasm-metadce.test
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,8 @@
;; CHECK-NEXT: --propagate-globals-globally propagate global values to other
;; CHECK-NEXT: globals (useful for tests)
;; CHECK-NEXT:
;; CHECK-NEXT: --remove-exports remove function exports
;; CHECK-NEXT:
;; CHECK-NEXT: --remove-imports removes imports and replaces
;; CHECK-NEXT: them with nops
;; CHECK-NEXT:
Expand Down
2 changes: 2 additions & 0 deletions test/lit/help/wasm-opt.test
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,8 @@
;; CHECK-NEXT: --propagate-globals-globally propagate global values to other
;; CHECK-NEXT: globals (useful for tests)
;; CHECK-NEXT:
;; CHECK-NEXT: --remove-exports remove function exports
;; CHECK-NEXT:
;; CHECK-NEXT: --remove-imports removes imports and replaces
;; CHECK-NEXT: them with nops
;; CHECK-NEXT:
Expand Down
2 changes: 2 additions & 0 deletions test/lit/help/wasm2js.test
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,8 @@
;; CHECK-NEXT: --propagate-globals-globally propagate global values to other
;; CHECK-NEXT: globals (useful for tests)
;; CHECK-NEXT:
;; CHECK-NEXT: --remove-exports remove function exports
;; CHECK-NEXT:
;; CHECK-NEXT: --remove-imports removes imports and replaces
;; CHECK-NEXT: them with nops
;; CHECK-NEXT:
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))
)
Loading