Skip to content
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

Implement guaranteed tail calls with the become keyword in the LLVM backend #138555

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions compiler/rustc_codegen_llvm/src/builder.rs
Original file line number Diff line number Diff line change
@@ -1419,6 +1419,11 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
let cold_inline = llvm::AttributeKind::Cold.create_attr(self.llcx);
attributes::apply_to_callsite(llret, llvm::AttributePlace::Function, &[cold_inline]);
}

fn set_tail_call(&mut self, call_inst: &'ll Value) {
// LLVMSetTailCall is marked as safe in the FFI definition
llvm::LLVMSetTailCall(call_inst, llvm::True);
}
}

impl<'ll> StaticBuilderMethods for Builder<'_, 'll, '_> {
93 changes: 87 additions & 6 deletions compiler/rustc_codegen_ssa/src/mir/block.rs
Original file line number Diff line number Diff line change
@@ -342,6 +342,90 @@ impl<'a, 'tcx> TerminatorCodegenHelper<'tcx> {

/// Codegen implementations for some terminator variants.
impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
fn codegen_tail_call_terminator(
&mut self,
bx: &mut Bx,
func: &mir::Operand<'tcx>,
args: &[Spanned<mir::Operand<'tcx>>],
fn_span: Span,
) {
// We don't need source_info as we already have fn_span for diagnostics
let func = self.codegen_operand(bx, func);
let fn_ty = func.layout.ty;

// Create the callee. This is a fn ptr or zero-sized and hence a kind of scalar.
let (fn_ptr, fn_abi, instance) = match *fn_ty.kind() {
ty::FnDef(def_id, substs) => {
let instance = ty::Instance::expect_resolve(
bx.tcx(),
bx.typing_env(),
def_id,
substs,
fn_span,
);
let fn_ptr = bx.get_fn_addr(instance);
let fn_abi = bx.fn_abi_of_instance(instance, ty::List::empty());
(fn_ptr, fn_abi, Some(instance))
}
ty::FnPtr(..) => {
let sig = fn_ty.fn_sig(bx.tcx());
let extra_args = bx.tcx().mk_type_list(&[]);
let fn_ptr = func.immediate();
let fn_abi = bx.fn_abi_of_fn_ptr(sig, extra_args);
(fn_ptr, fn_abi, None)
}
_ => bug!("{} is not callable", func.layout.ty),
};

let mut llargs = Vec::with_capacity(args.len());

// Process arguments
for arg in args {
let op = self.codegen_operand(bx, &arg.node);
let arg_idx = llargs.len();

if arg_idx < fn_abi.args.len() {
self.codegen_argument(bx, op, &mut llargs, &fn_abi.args[arg_idx]);
} else {
// This can happen in case of C-variadic functions
let is_immediate = match op.val {
Immediate(_) => true,
_ => false,
};

if is_immediate {
llargs.push(op.immediate());
} else {
let temp = PlaceRef::alloca(bx, op.layout);
op.val.store(bx, temp);
llargs.push(bx.load(
bx.backend_type(op.layout),
temp.val.llval,
temp.val.align,
));
}
}
}

// Call the function
let fn_ty = bx.fn_decl_backend_type(fn_abi);
let fn_attrs = if let Some(instance) = instance
&& bx.tcx().def_kind(instance.def_id()).has_codegen_attrs()
{
Some(bx.tcx().codegen_fn_attrs(instance.def_id()))
} else {
None
};

// Perform the actual function call
let llret = bx.call(fn_ty, fn_attrs, Some(fn_abi), fn_ptr, &llargs, None, instance);

// Mark as tail call - this is the critical part
bx.set_tail_call(llret);

// Return the result
bx.ret(llret);
}
/// Generates code for a `Resume` terminator.
fn codegen_resume_terminator(&mut self, helper: TerminatorCodegenHelper<'tcx>, bx: &mut Bx) {
if let Some(funclet) = helper.funclet(self) {
@@ -1430,12 +1514,9 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
fn_span,
mergeable_succ(),
),
mir::TerminatorKind::TailCall { .. } => {
// FIXME(explicit_tail_calls): implement tail calls in ssa backend
span_bug!(
terminator.source_info.span,
"`TailCall` terminator is not yet supported by `rustc_codegen_ssa`"
)
mir::TerminatorKind::TailCall { ref func, ref args, fn_span } => {
self.codegen_tail_call_terminator(bx, func, args, fn_span);
MergingSucc::False
}
mir::TerminatorKind::CoroutineDrop | mir::TerminatorKind::Yield { .. } => {
bug!("coroutine ops in codegen")
4 changes: 4 additions & 0 deletions compiler/rustc_codegen_ssa/src/traits/builder.rs
Original file line number Diff line number Diff line change
@@ -555,6 +555,10 @@ pub trait BuilderMethods<'a, 'tcx>:
funclet: Option<&Self::Funclet>,
instance: Option<Instance<'tcx>>,
) -> Self::Value;

/// Mark a call instruction as a tail call (guaranteed tail call optimization)
/// Used for implementing the `become` expression
fn set_tail_call(&mut self, call_inst: Self::Value);
fn zext(&mut self, val: Self::Value, dest_ty: Self::Type) -> Self::Value;

fn apply_attrs_to_cleanup_callsite(&mut self, llret: Self::Value);
26 changes: 26 additions & 0 deletions tests/run-make/tail-call-llvm-ir/Makefile
Copy link
Member

Choose a reason for hiding this comment

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

This is not how we do codegen tests.

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

In addition, we have entirely removed support for Makefile-based tests. This test tests nothing, that's why it passes.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# This test verifies that the `become` keyword generates tail call instructions in LLVM IR

# Use the Rust compiler from the build directory
RUSTC ?= ../../../build/aarch64-apple-darwin/stage1/bin/rustc

all: verify-tail-call

verify-tail-call:
# Create test file with debuginfo to ensure function names are preserved
$(RUSTC) tail-call-test.rs --emit=llvm-ir -g -C opt-level=0

# Check that `become` generates 'tail call' instructions for with_tail function
grep -q "tail call.*@_ZN14tail_call_test9with_tail" tail-call-test.ll || (echo "ERROR: 'tail call' instruction not found for with_tail"; exit 1)

# Check that regular recursive calls don't use tail call optimization by default
grep -q "@_ZN14tail_call_test7no_tail" tail-call-test.ll || (echo "ERROR: no_tail function not found"; exit 1)
! grep -q "tail call.*@_ZN14tail_call_test7no_tail" tail-call-test.ll || (echo "ERROR: Regular function call incorrectly marked as tail call"; exit 1)

# Check mutual recursion with 'become'
grep -q "tail call.*@_ZN14tail_call_test14even_with_tail" tail-call-test.ll || (echo "ERROR: 'tail call' instruction not found for even_with_tail"; exit 1)
grep -q "tail call.*@_ZN14tail_call_test13odd_with_tail" tail-call-test.ll || (echo "ERROR: 'tail call' instruction not found for odd_with_tail"; exit 1)

# The test passes only if all checks above confirm that:
# 1. Functions using 'become' generate LLVM tail call instructions
# 2. Regular recursive functions don't generate tail call instructions
@echo "LLVM IR verification successful: 'become' correctly generates 'tail call' instructions"
Loading
Loading