-
Notifications
You must be signed in to change notification settings - Fork 745
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a pass to remove exports from the WASM bundle
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
Showing
5 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
) |