Skip to content

feat: implement tail call optimization #7641

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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 @@ -114,6 +114,7 @@ set(passes_SOURCES
ReorderGlobals.cpp
ReorderLocals.cpp
ReReloop.cpp
TailCall.cpp
TrapMode.cpp
TypeGeneralizing.cpp
TypeRefining.cpp
Expand Down
82 changes: 82 additions & 0 deletions src/passes/TailCall.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@

#include "pass.h"
#include "wasm-traversal.h"
#include "wasm.h"
#include <vector>

namespace wasm {

namespace {

struct Finder : PostWalker<Finder> {
std::vector<Call*> tailCalls;
std::vector<CallIndirect*> tailCallIndirects;
void visitFunction(Function* curr) { checkTailCall(curr->body); }
void visitReturn(Return* curr) { checkTailCall(curr->value); }

private:
void checkTailCall(Expression* expr) {
if (expr == nullptr) {
return;
}
if (auto* call = expr->dynCast<Call>()) {
if (!call->isReturn && call->type == getFunction()->getResults()) {
tailCalls.push_back(call);
}
return;
}
if (auto* call = expr->dynCast<CallIndirect>()) {
if (!call->isReturn && call->type == getFunction()->getResults()) {
tailCallIndirects.push_back(call);
}
return;
}
if (auto* block = expr->dynCast<Block>()) {
return checkTailCall(block->list);
}
if (auto* ifElse = expr->dynCast<If>()) {
checkTailCall(ifElse->ifTrue);
checkTailCall(ifElse->ifFalse);
return;
}
}
void checkTailCall(ExpressionList const& exprs) {
if (exprs.empty()) {
return;
}
checkTailCall(exprs.back());
return;
}
};

} // namespace

struct TailCallOptimizer : public Pass {
bool isFunctionParallel() override { return true; }
std::unique_ptr<Pass> create() override {
return std::make_unique<TailCallOptimizer>();
}
void runOnFunction(Module* module, Function* function) override {
if (!module->features.hasTailCall()) {
return;
}
Finder finder{};
finder.walkFunctionInModule(function, module);
for (Call* call : finder.tailCalls) {
if (!call->isReturn) {
call->isReturn = true;
call->finalize();
}
}
for (CallIndirect* call : finder.tailCallIndirects) {
if (!call->isReturn) {
call->isReturn = true;
call->finalize();
}
}
}
};

Pass* createTailCallPass() { return new TailCallOptimizer(); }

} // namespace wasm
2 changes: 2 additions & 0 deletions src/passes/pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,8 @@ void PassRegistry::registerPasses() {
registerPass("strip-target-features",
"strip the wasm target features section",
createStripTargetFeaturesPass);
registerPass(
"tail-call", "transform call to return call", createTailCallPass);
registerPass("translate-to-new-eh",
"deprecated; same as translate-to-exnref",
createTranslateToExnrefPass);
Expand Down
1 change: 1 addition & 0 deletions src/passes/passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ Pass* createStripEHPass();
Pass* createStubUnsupportedJSOpsPass();
Pass* createSSAifyPass();
Pass* createSSAifyNoMergePass();
Pass* createTailCallPass();
Pass* createTable64LoweringPass();
Pass* createTranslateToExnrefPass();
Pass* createTrapModeClamp();
Expand Down
Loading