Skip to content

Commit 272330a

Browse files
authored
Fix ModuleUtils::renameFunctions on RefFunc (#4978)
It had hardcoded handling of the table, but was missing RefFunc (and maybe more?) Also some cleanups around that code.
1 parent 0dba086 commit 272330a

File tree

1 file changed

+29
-26
lines changed

1 file changed

+29
-26
lines changed

src/ir/module-utils.h

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -176,40 +176,43 @@ inline void clearModule(Module& wasm) {
176176
// Rename functions along with all their uses.
177177
// Note that for this to work the functions themselves don't necessarily need
178178
// to exist. For example, it is possible to remove a given function and then
179-
// call this redirect all of its uses.
179+
// call this to redirect all of its uses.
180180
template<typename T> inline void renameFunctions(Module& wasm, T& map) {
181181
// Update the function itself.
182182
for (auto& [oldName, newName] : map) {
183-
if (Function* F = wasm.getFunctionOrNull(oldName)) {
184-
assert(!wasm.getFunctionOrNull(newName) || F->name == newName);
185-
F->name = newName;
183+
if (Function* func = wasm.getFunctionOrNull(oldName)) {
184+
assert(!wasm.getFunctionOrNull(newName) || func->name == newName);
185+
func->name = newName;
186186
}
187187
}
188188
wasm.updateMaps();
189-
// Update other global things.
190-
auto maybeUpdate = [&](Name& name) {
191-
auto iter = map.find(name);
192-
if (iter != map.end()) {
193-
name = iter->second;
194-
}
195-
};
196-
maybeUpdate(wasm.start);
197-
ElementUtils::iterAllElementFunctionNames(&wasm, maybeUpdate);
198-
for (auto& exp : wasm.exports) {
199-
if (exp->kind == ExternalKind::Function) {
200-
maybeUpdate(exp->value);
201-
}
202-
}
203-
// Update call instructions.
204-
for (auto& func : wasm.functions) {
205-
// TODO: parallelize
206-
if (!func->imported()) {
207-
FindAll<Call> calls(func->body);
208-
for (auto* call : calls.list) {
209-
maybeUpdate(call->target);
189+
190+
// Update all references to it.
191+
struct Updater : public WalkerPass<PostWalker<Updater>> {
192+
bool isFunctionParallel() override { return true; }
193+
194+
T& map;
195+
196+
void maybeUpdate(Name& name) {
197+
if (auto iter = map.find(name); iter != map.end()) {
198+
name = iter->second;
210199
}
211200
}
212-
}
201+
202+
Updater(T& map) : map(map) {}
203+
204+
Updater* create() override { return new Updater(map); }
205+
206+
void visitCall(Call* curr) { maybeUpdate(curr->target); }
207+
208+
void visitRefFunc(RefFunc* curr) { maybeUpdate(curr->func); }
209+
};
210+
211+
Updater updater(map);
212+
updater.maybeUpdate(wasm.start);
213+
PassRunner runner(&wasm);
214+
updater.run(&runner, &wasm);
215+
updater.runOnModuleCode(&runner, &wasm);
213216
}
214217

215218
inline void renameFunction(Module& wasm, Name oldName, Name newName) {

0 commit comments

Comments
 (0)