Skip to content
Open
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 @@ -137,6 +137,7 @@ set(passes_SOURCES
StackCheck.cpp
StripEH.cpp
SSAify.cpp
TailCall.cpp
TupleOptimization.cpp
TranslateEH.cpp
TypeFinalizing.cpp
Expand Down
202 changes: 202 additions & 0 deletions src/passes/TailCall.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
/*
* Copyright 2026 WebAssembly Community Group participants
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <functional>
#include <memory>
#include <unordered_set>
#include <vector>

#include "analysis/cfg.h"
#include "pass.h"
#include "wasm.h"

namespace wasm {

namespace {

using ReturnPoint = Expression*;

void addReturnPoints(Expression* expr, std::vector<ReturnPoint>& returnPoints) {
if (!expr) {
return;
}

if (auto* iff = expr->dynCast<If>()) {
addReturnPoints(iff->ifTrue, returnPoints);
addReturnPoints(iff->ifFalse, returnPoints);
return;
}

if (auto* block = expr->dynCast<Block>()) {
if (!block->list.empty()) {
addReturnPoints(block->list.back(), returnPoints);
}
return;
}

returnPoints.push_back(expr);
}

std::vector<ReturnPoint> findReturnPoints(Module* module, Function* func) {
auto cfg = analysis::CFG::fromFunction(func, module);
const analysis::BasicBlock* exit = nullptr;
for (const auto& block : cfg) {
if (block.isExit()) {
exit = &block;
break;
}
}

std::vector<ReturnPoint> returnPoints;
if (!exit) {
return returnPoints;
}

std::unordered_set<const analysis::BasicBlock*> visited;
std::function<void(const analysis::BasicBlock*)> find =
[&](const analysis::BasicBlock* block) {
if (!block || !visited.insert(block).second) {
return;
}

Expression* last = nullptr;
Expression* secondLast = nullptr;
for (auto it = block->rbegin(); it != block->rend(); ++it) {
auto* expr = *it;
if (expr->is<Block>() || expr->is<Loop>()) {
continue;
}
if (!last) {
last = expr;
} else {
secondLast = expr;
break;
}
}

auto visitPredecessors = [&]() {
for (auto* pred : block->preds()) {
find(pred);
}
};

if (!last || last->is<If>()) {
visitPredecessors();
} else if (auto* ret = last->dynCast<Return>()) {
if (ret->value) {
addReturnPoints(ret->value, returnPoints);
} else if (secondLast && secondLast->type != Type::unreachable) {
addReturnPoints(secondLast, returnPoints);
} else {
visitPredecessors();
}
} else if (auto* br = last->dynCast<Break>()) {
if (br->value) {
Comment on lines +106 to +107

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this account for conditional branches?

addReturnPoints(br->value, returnPoints);
} else if (secondLast && secondLast->type != Type::unreachable) {
addReturnPoints(secondLast, returnPoints);
} else {
visitPredecessors();
}
} else if (auto* sw = last->dynCast<Switch>()) {
if (sw->value) {
addReturnPoints(sw->value, returnPoints);
} else if (secondLast && secondLast->type != Type::unreachable) {
addReturnPoints(secondLast, returnPoints);
} else {
visitPredecessors();
}
} else if (last->type != Type::unreachable) {
addReturnPoints(last, returnPoints);
}
};

find(exit);
return returnPoints;
}

bool convertCall(Expression* expr, Module* module, Function* func) {
if (auto* call = expr->dynCast<Call>()) {
if (call->isReturn) {
return false;
}
auto* target = module->getFunctionOrNull(call->target);
if (!target || target->getResults() != func->getResults()) {
return false;
}
call->isReturn = true;
call->finalize();
return true;
}

if (auto* call = expr->dynCast<CallIndirect>()) {
if (call->isReturn ||
call->heapType.getSignature().results != func->getResults()) {
return false;
}
call->isReturn = true;
call->finalize();
return true;
}

if (auto* call = expr->dynCast<CallRef>()) {
if (call->isReturn || !call->target->type.isRef() ||
!call->target->type.getHeapType().isSignature() ||
call->target->type.getHeapType().getSignature().results !=
func->getResults()) {
return false;
}
call->isReturn = true;
call->finalize();
return true;
}

return false;
}

struct TailCall : public Pass {
bool isFunctionParallel() override { return true; }

std::unique_ptr<Pass> create() override {
return std::make_unique<TailCall>();
}

void runOnFunction(Module* module, Function* func) override {
if (!module->features.hasTailCall() ||
module->features.hasExceptionHandling() || func->imported() ||
!func->body) {
Comment on lines +179 to +180

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checking both func->imported() and !func->body is redundant.

return;
}

bool converted = false;
for (auto* point : findReturnPoints(module, func)) {
converted |= convertCall(point, module, func);
}

if (converted) {
PassRunner runner(module);
runner.setIsNested(true);
runner.add("dce");
runner.runOnFunction(func);
}
}
};

} // anonymous namespace

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

} // namespace wasm

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a newline at the end of the file.

5 changes: 4 additions & 1 deletion src/passes/pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,9 @@ void PassRegistry::registerPasses() {
registerPass("stack-check",
"enforce limits on llvm's __stack_pointer global",
createStackCheckPass);
registerPass("tail-call",
"convert calls in tail position to return calls",
createTailCallPass);
registerPass("strip-debug",
"strip debug info (including the names section)",
createStripDebugPass);
Expand Down Expand Up @@ -733,7 +736,7 @@ void PassRunner::addDefaultFunctionOptimizationPasses() {
addIfNoDWARFIssues(
"remove-unused-brs"); // coalesce-locals opens opportunities
addIfNoDWARFIssues(
"remove-unused-names"); // remove-unused-brs opens opportunities
"remove-unused-names"); // remove-unused-brs opens opportunities
if (options.optimizeLevel >= 3 || options.shrinkLevel >= 1) {
addIfNoDWARFIssues("constraint-analysis");
}
Expand Down
1 change: 1 addition & 0 deletions src/passes/passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ Pass* createStubUnsupportedJSOpsPass();
Pass* createSSAifyPass();
Pass* createSSAifyNoMergePass();
Pass* createTable64LoweringPass();
Pass* createTailCallPass();
Pass* createTranslateToExnrefPass();
Pass* createTupleOptimizationPass();
Pass* createTypeGeneralizingPass();
Expand Down
23 changes: 23 additions & 0 deletions test/lit/passes/tail-call-eh.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
;; Tail-call conversion currently leaves exception-handling modules unchanged.

;; RUN: wasm-opt %s --tail-call --enable-tail-call --enable-exception-handling --enable-reference-types -S -o - | filecheck %s

(module
(tag $exception)

(func $callee
(throw $exception)
)

;; CHECK-LABEL: (func $caller
;; CHECK-NOT: (return_call
;; CHECK: (call $callee)
;; CHECK-NEXT: )
(func $caller
(block $catch
(try_table (catch_all $catch)
(call $callee)
)
)
)
)
111 changes: 111 additions & 0 deletions test/lit/passes/tail-call.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
;; Test converting calls in tail position to return calls.

;; RUN: wasm-opt %s --tail-call --enable-tail-call --enable-gc --enable-reference-types -S -o - | filecheck %s

(module
(type $none-to-none (func))
(type $none-to-i32 (func (result i32)))
(table $table 1 funcref)

(func $void-callee)
(func $value-callee (result i32)
(i32.const 1)
)
(func $ref-callee (result i32)
(i32.const 2)
)
(elem (i32.const 0) $value-callee)

;; CHECK-LABEL: (func $direct-void
;; CHECK-NEXT: (return_call $void-callee)
;; CHECK-NEXT: )
(func $direct-void
(call $void-callee)
)

;; CHECK-LABEL: (func $direct-value
;; CHECK-NEXT: (return_call $value-callee)
;; CHECK-NEXT: )
(func $direct-value (result i32)
(call $value-callee)
(return)
)

;; CHECK-LABEL: (func $conditional
;; CHECK: (if
;; CHECK-NEXT: (local.get $condition)
;; CHECK-NEXT: (then
;; CHECK-NEXT: (return_call $value-callee)
;; CHECK-NEXT: )
;; CHECK-NEXT: (else
;; CHECK-NEXT: (return_call $ref-callee)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $conditional (param $condition i32) (result i32)
(if (result i32)
(local.get $condition)
(then (call $value-callee))
(else (call $ref-callee))
)
)

;; CHECK-LABEL: (func $indirect
;; CHECK-NEXT: (return_call_indirect $table (type $none-to-i32)
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $indirect (result i32)
(i32.const 0)
(call_indirect $table (type $none-to-i32))
)

;; CHECK-LABEL: (func $ref
;; CHECK-NEXT: (return_call_ref $none-to-i32
;; CHECK-NEXT: (ref.func $value-callee)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $ref (result i32)
(call_ref $none-to-i32
(ref.func $value-callee)
)
)

;; CHECK-LABEL: (func $break
;; CHECK-NEXT: (block $out
;; CHECK-NEXT: (if
;; CHECK-NEXT: (local.get $condition)
;; CHECK-NEXT: (then
;; CHECK-NEXT: (return_call $value-callee)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (return_call $ref-callee)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $break (param $condition i32) (result i32)
(block $out (result i32)
(if (local.get $condition)
(then (br $out (call $value-callee)))
)
(call $ref-callee)
)
)

;; CHECK-LABEL: (func $not-tail
;; CHECK-NEXT: (call $void-callee)
;; CHECK-NEXT: (nop)
;; CHECK-NEXT: )
(func $not-tail
(call $void-callee)
(nop)
)

;; CHECK-LABEL: (func $mismatched-result
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (call $value-callee)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $mismatched-result
(drop (call $value-callee))
)
)