diff options
author | Alon Zakai <azakai@google.com> | 2022-08-29 13:41:32 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-29 20:41:32 +0000 |
commit | 272330a0d20a4adbe5012dee16b7ba972ebb397a (patch) | |
tree | 42193b1ebbba1a7fc75b4fce429ff14a0ca2d566 /src | |
parent | 0dba0860e1b285292acdb3e9db6bcd9c102750a6 (diff) | |
download | binaryen-272330a0d20a4adbe5012dee16b7ba972ebb397a.tar.gz binaryen-272330a0d20a4adbe5012dee16b7ba972ebb397a.tar.bz2 binaryen-272330a0d20a4adbe5012dee16b7ba972ebb397a.zip |
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.
Diffstat (limited to 'src')
-rw-r--r-- | src/ir/module-utils.h | 55 |
1 files changed, 29 insertions, 26 deletions
diff --git a/src/ir/module-utils.h b/src/ir/module-utils.h index 81f832b40..319f1a7a4 100644 --- a/src/ir/module-utils.h +++ b/src/ir/module-utils.h @@ -176,40 +176,43 @@ inline void clearModule(Module& wasm) { // Rename functions along with all their uses. // Note that for this to work the functions themselves don't necessarily need // to exist. For example, it is possible to remove a given function and then -// call this redirect all of its uses. +// call this to redirect all of its uses. template<typename T> inline void renameFunctions(Module& wasm, T& map) { // Update the function itself. for (auto& [oldName, newName] : map) { - if (Function* F = wasm.getFunctionOrNull(oldName)) { - assert(!wasm.getFunctionOrNull(newName) || F->name == newName); - F->name = newName; + if (Function* func = wasm.getFunctionOrNull(oldName)) { + assert(!wasm.getFunctionOrNull(newName) || func->name == newName); + func->name = newName; } } wasm.updateMaps(); - // Update other global things. - auto maybeUpdate = [&](Name& name) { - auto iter = map.find(name); - if (iter != map.end()) { - name = iter->second; - } - }; - maybeUpdate(wasm.start); - ElementUtils::iterAllElementFunctionNames(&wasm, maybeUpdate); - for (auto& exp : wasm.exports) { - if (exp->kind == ExternalKind::Function) { - maybeUpdate(exp->value); - } - } - // Update call instructions. - for (auto& func : wasm.functions) { - // TODO: parallelize - if (!func->imported()) { - FindAll<Call> calls(func->body); - for (auto* call : calls.list) { - maybeUpdate(call->target); + + // Update all references to it. + struct Updater : public WalkerPass<PostWalker<Updater>> { + bool isFunctionParallel() override { return true; } + + T& map; + + void maybeUpdate(Name& name) { + if (auto iter = map.find(name); iter != map.end()) { + name = iter->second; } } - } + + Updater(T& map) : map(map) {} + + Updater* create() override { return new Updater(map); } + + void visitCall(Call* curr) { maybeUpdate(curr->target); } + + void visitRefFunc(RefFunc* curr) { maybeUpdate(curr->func); } + }; + + Updater updater(map); + updater.maybeUpdate(wasm.start); + PassRunner runner(&wasm); + updater.run(&runner, &wasm); + updater.runOnModuleCode(&runner, &wasm); } inline void renameFunction(Module& wasm, Name oldName, Name newName) { |