Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix
  • Loading branch information
kripken committed Jan 24, 2024
commit 0c8a66d88b7da66342b19dce43155fa1205beb05
22 changes: 14 additions & 8 deletions src/passes/RemoveUnusedModuleElements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -635,16 +635,24 @@ struct RemoveUnusedModuleElements : public Pass {
// Active segments that write to imported tables and memories are roots
// because those writes are externally observable even if the module does
// not otherwise use the tables or memories.
//
// Likewise, if traps are possible during startup then just trapping is an
// effect (which can happen if the offset is out of bounds).
// TODO: We could infer a trap cannot happen if we see the offset is in
// bounds.
auto trapsMatter = !getPassOptions().trapsNeverHappen;
ModuleUtils::iterActiveDataSegments(*module, [&](DataSegment* segment) {
if (module->getMemory(segment->memory)->imported() &&
!segment->data.empty()) {
auto writesToVisible = module->getMemory(segment->memory)->imported() &&
!segment->data.empty();
if (writesToVisible || trapsMatter) {
roots.emplace_back(ModuleElementKind::DataSegment, segment->name);
}
});
ModuleUtils::iterActiveElementSegments(
*module, [&](ElementSegment* segment) {
if (module->getTable(segment->table)->imported() &&
!segment->data.empty()) {
auto writesToVisible = module->getTable(segment->table)->imported() &&
!segment->data.empty();
if (writesToVisible || trapsMatter) {
roots.emplace_back(ModuleElementKind::ElementSegment, segment->name);
}
});
Expand Down Expand Up @@ -700,13 +708,11 @@ struct RemoveUnusedModuleElements : public Pass {
module->removeTables([&](Table* curr) {
return !needed(ModuleElement(ModuleElementKind::Table, curr->name));
});
// Segments that are not used may still have a noticeable effect, if they
// cause a trap during startup.
module->removeDataSegments([&](DataSegment* curr) {
return !needed(ModuleElement(ModuleElementKind::DataSegment, curr->name)) && (!curr->offset || options.trapsNeverHappen);
return !needed(ModuleElement(ModuleElementKind::DataSegment, curr->name));
});
module->removeElementSegments([&](ElementSegment* curr) {
return !needed({ModuleElementKind::ElementSegment, curr->name}) && (!curr->offset || options.trapsNeverHappen);
return !needed({ModuleElementKind::ElementSegment, curr->name});
});
// TODO: After removing elements, we may be able to remove more things, and
// should continue to work. (For example, after removing a reference
Expand Down
2 changes: 2 additions & 0 deletions test/lit/passes/remove-unused-module-elements_tnh.wast
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
;; T_N_H: (import "fuzzing-support" "log-i32" (func $fimport$0 (type $0) (param i32)))
(import "fuzzing-support" "log-i32" (func $fimport$0 (param i32)))

;; CHECK: (memory $0 16 17 shared)
(memory $0 16 17 shared)
;; CHECK: (data $1 (i32.const -1) "")
(data $1 (i32.const -1) "")

;; CHECK: (export "func_13_invoker" (func $0))
Expand Down