Skip to content
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
29 changes: 29 additions & 0 deletions src/passes/RemoveUnusedModuleElements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
// reference forces us to keep something in the IR to be referred to, but only
// a use actually makes us keep its contents as well.
//
// The remove-unused-module-elements-consider-empty-exports-unused pass
// argument treats exports of empty functions as unused. This is useful for
// runtimes where such exports are optional.
//

#include <memory>
#include <vector>
Expand Down Expand Up @@ -887,6 +891,31 @@ struct RemoveUnusedModuleElements : public Pass {
void run(Module* module) override {
prepare(module);

// Some runtimes treat missing exports as optional. For those runtimes,
// allow exported functions that do nothing to be handled like other
// unused module elements. Remove the exports before finding roots so that
// an otherwise-unreferenced function can be removed as well.
if (!rootAllFunctions &&
hasArgument(
"remove-unused-module-elements-consider-empty-exports-unused")) {
module->removeExports([&](Export* curr) {
if (curr->kind != ExternalKind::Function) {
return false;
}
auto* func = module->getFunction(*curr->getInternalName());
if (func->imported()) {
return false;
}
if (func->body->is<Nop>()) {
return true;
}
if (auto* block = func->body->dynCast<Block>()) {
return block->list.empty();
}
return false;
});
}

std::vector<ModuleElement> roots;
// Module start is a root.
if (module->start.is()) {
Expand Down
36 changes: 36 additions & 0 deletions test/lit/passes/remove-unused-module-elements-empty-exports.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
;; RUN: wasm-opt %s --remove-unused-module-elements -S -o - | filecheck %s --check-prefix=DEFAULT
;; RUN: wasm-opt %s --remove-unused-module-elements --pass-arg=remove-unused-module-elements-consider-empty-exports-unused -S -o - | filecheck %s --check-prefix=OPTION

(module
(import "env" "noop" (func $imported))

(func $empty)
(func $empty-used)
(func $call-empty
(call $empty-used)
)
(func $nonempty
(drop
(i32.const 0)
)
)

(export "empty" (func $empty))
(export "empty-used" (func $empty-used))
(export "call-empty" (func $call-empty))
(export "nonempty" (func $nonempty))
(export "imported" (func $imported))
)

;; DEFAULT: (export "empty" (func $empty))
;; DEFAULT-NEXT: (export "empty-used" (func $empty-used))
;; DEFAULT: (func $empty
;; DEFAULT: (func $empty-used

;; OPTION-NOT: (export "empty"
;; OPTION-NOT: (export "empty-used"
;; OPTION: (export "call-empty" (func $call-empty))
;; OPTION-NEXT: (export "nonempty" (func $nonempty))
;; OPTION-NEXT: (export "imported" (func $imported))
;; OPTION-NOT: (func $empty{{[[:space:]]*$}}
;; OPTION: (func $empty-used