Skip to content

Commit ec2aa7e

Browse files
authored
[Wasm Exceptions] Scan catch events in RemoveUnusedModuleElements (#3582)
Also refactor away some annoying repeated code in that pass. visitTry is the only actual change.
1 parent 7089ada commit ec2aa7e

File tree

2 files changed

+18
-24
lines changed

2 files changed

+18
-24
lines changed

src/passes/RemoveUnusedModuleElements.cpp

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -85,32 +85,26 @@ struct ReachabilityAnalyzer : public PostWalker<ReachabilityAnalyzer> {
8585
}
8686
}
8787

88-
void visitCall(Call* curr) {
89-
if (reachable.count(
90-
ModuleElement(ModuleElementKind::Function, curr->target)) == 0) {
91-
queue.emplace_back(ModuleElementKind::Function, curr->target);
88+
void maybeAdd(ModuleElement element) {
89+
if (reachable.count(element) == 0) {
90+
queue.emplace_back(element);
9291
}
9392
}
93+
94+
void visitCall(Call* curr) {
95+
maybeAdd(ModuleElement(ModuleElementKind::Function, curr->target));
96+
}
9497
void visitCallIndirect(CallIndirect* curr) {
9598
assert(!module->tables.empty() && "call-indirect to undefined table.");
9699

97-
if (reachable.count(ModuleElement(ModuleElementKind::Table, curr->table)) ==
98-
0) {
99-
queue.emplace_back(ModuleElementKind::Table, curr->table);
100-
}
100+
maybeAdd(ModuleElement(ModuleElementKind::Table, curr->table));
101101
}
102102

103103
void visitGlobalGet(GlobalGet* curr) {
104-
if (reachable.count(ModuleElement(ModuleElementKind::Global, curr->name)) ==
105-
0) {
106-
queue.emplace_back(ModuleElementKind::Global, curr->name);
107-
}
104+
maybeAdd(ModuleElement(ModuleElementKind::Global, curr->name));
108105
}
109106
void visitGlobalSet(GlobalSet* curr) {
110-
if (reachable.count(ModuleElement(ModuleElementKind::Global, curr->name)) ==
111-
0) {
112-
queue.emplace_back(ModuleElementKind::Global, curr->name);
113-
}
107+
maybeAdd(ModuleElement(ModuleElementKind::Global, curr->name));
114108
}
115109

116110
void visitLoad(Load* curr) { usesMemory = true; }
@@ -127,15 +121,14 @@ struct ReachabilityAnalyzer : public PostWalker<ReachabilityAnalyzer> {
127121
void visitMemorySize(MemorySize* curr) { usesMemory = true; }
128122
void visitMemoryGrow(MemoryGrow* curr) { usesMemory = true; }
129123
void visitRefFunc(RefFunc* curr) {
130-
if (reachable.count(
131-
ModuleElement(ModuleElementKind::Function, curr->func)) == 0) {
132-
queue.emplace_back(ModuleElementKind::Function, curr->func);
133-
}
124+
maybeAdd(ModuleElement(ModuleElementKind::Function, curr->func));
134125
}
135126
void visitThrow(Throw* curr) {
136-
if (reachable.count(ModuleElement(ModuleElementKind::Event, curr->event)) ==
137-
0) {
138-
queue.emplace_back(ModuleElementKind::Event, curr->event);
127+
maybeAdd(ModuleElement(ModuleElementKind::Event, curr->event));
128+
}
129+
void visitTry(Try* curr) {
130+
for (auto event : curr->catchEvents) {
131+
maybeAdd(ModuleElement(ModuleElementKind::Event, event));
139132
}
140133
}
141134
};

test/passes/remove-unused-module-elements_all-features.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,11 +291,12 @@
291291
)
292292
)
293293
(module
294-
(type $none_=>_none (func))
295294
(type $i32_=>_none (func (param i32)))
295+
(type $none_=>_none (func))
296296
(type $i64_=>_none (func (param i64)))
297297
(event $e-export (attr 0) (param i64))
298298
(event $e-throw (attr 0) (param i32))
299+
(event $e-catch (attr 0) (param i32))
299300
(export "e-export" (event $e-export))
300301
(start $start)
301302
(func $start

0 commit comments

Comments
 (0)