Skip to content
Merged
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 @@ -105,6 +105,7 @@ set(passes_SOURCES
TraceCalls.cpp
RandomizeBranchHints.cpp
RedundantSetElimination.cpp
RemoveExports.cpp
RemoveImports.cpp
RemoveMemoryInit.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 2026 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.
*/

//
// Remove exports using a wildcard. For example:
//
// --remove-exports=__*
//
// That will remove all exports with names like "__foo" and "__bar".
//

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

namespace wasm {

namespace {

struct RemoveExports : public Pass {
void run(Module* module) override {
std::string pattern =
getArgument(name, "Usage usage: wasm-opt --" + name + "=WILDCARD");

std::vector<Name> toRemove;
for (auto& exp : module->exports) {
if (String::wildcardMatch(pattern, exp->name.toString())) {
toRemove.push_back(exp->name);
}
}

for (auto& name : toRemove) {
module->removeExport(name);
}
}
};

} // anonymous namespace

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

} // namespace wasm
3 changes: 3 additions & 0 deletions src/passes/pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,9 @@ void PassRegistry::registerPasses() {
registerPass("remove-relaxed-simd",
"replaces relaxed SIMD instructions with unreachable",
createRemoveRelaxedSIMDPass);
registerPass("remove-exports",
"removes exports using a wildcard",
createRemoveExportsPass);
registerPass("remove-imports",
"removes imports and replaces them with nops",
createRemoveImportsPass);
Expand Down
1 change: 1 addition & 0 deletions src/passes/passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ Pass* createPropagateGlobalsGloballyPass();
Pass* createRandomizeBranchHintsPass();
Pass* createRemoveNonJSOpsPass();
Pass* createRemoveRelaxedSIMDPass();
Pass* createRemoveExportsPass();
Pass* createRemoveImportsPass();
Pass* createRemoveMemoryInitPass();
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 @@ -383,6 +383,8 @@
;; CHECK-NEXT: --propagate-globals-globally propagate global values to other
;; CHECK-NEXT: globals (useful for tests)
;; CHECK-NEXT:
;; CHECK-NEXT: --remove-exports removes exports using a wildcard
;; 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 @@ -415,6 +415,8 @@
;; CHECK-NEXT: --propagate-globals-globally propagate global values to other
;; CHECK-NEXT: globals (useful for tests)
;; CHECK-NEXT:
;; CHECK-NEXT: --remove-exports removes exports using a wildcard
;; 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 @@ -347,6 +347,8 @@
;; CHECK-NEXT: --propagate-globals-globally propagate global values to other
;; CHECK-NEXT: globals (useful for tests)
;; CHECK-NEXT:
;; CHECK-NEXT: --remove-exports removes exports using a wildcard
;; CHECK-NEXT:
;; CHECK-NEXT: --remove-imports removes imports and replaces
;; CHECK-NEXT: them with nops
;; CHECK-NEXT:
Expand Down
77 changes: 77 additions & 0 deletions test/lit/passes/remove-exports.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
;; NOTE: Assertions have been generated by update_lit_checks.py --all-items and should not be edited.
;; NOTE: This test was ported using port_passes_tests_to_lit.py and could be cleaned up.

;; RUN: foreach %s %t wasm-opt "--remove-exports=__*" -all -S -o - | filecheck %s

;; foo and bar will be kept as exports, but __foo, __bar, and __ will not.
(module
;; CHECK: (type $0 (func))

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

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

;; CHECK: (func $foo (type $0)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i32.const 1)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $foo (export "foo")
(drop (i32.const 1))
)

;; CHECK: (func $__foo (type $0)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i32.const 2)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $__foo (export "__foo")
(drop (i32.const 2))
)

;; CHECK: (func $bar (type $0)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i32.const 3)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $bar (export "bar")
(drop (i32.const 3))
)

;; CHECK: (func $__bar (type $0)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i32.const 4)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $__bar (export "__bar")
(drop (i32.const 4))
)

;; CHECK: (func $__ (type $0)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i32.const 4)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $__ (export "__")
(drop (i32.const 4))
)
)

;; Test non-function exports. The prefixed __mem and __table exports vanish.
(module
;; CHECK: (memory $memory 10 20)
(memory $memory 10 20)

;; CHECK: (table $table 10 20 funcref)
(table $table 10 20 funcref)

;; CHECK: (export "mem" (memory $memory))
(export "mem" (memory $memory))

(export "__mem" (memory $memory))

;; CHECK: (export "tab" (table $table))
(export "tab" (table $table))

(export "__tab" (table $table))
)
Loading