Tails calls are not being emitted as such

e3ecba5
Opened by Jeff Muizelaar at 2023-04-05 17:36:51

The following rust code should get tail calls:

pub fn moo() -> String {
    goo()
}

#[inline(never)]
pub fn goo() -> String {
    "https://".to_string()
}

Instead we get:

example::moo:
        push    rbp
        mov     rbp, rsp
        push    rbx
        push    rax
        mov     rbx, rdi
        call    example::goo@PLT
        mov     rax, rbx
        add     rsp, 8
        pop     rbx
        pop     rbp
        ret

example::goo:
        push    rbp
        mov     rbp, rsp
        push    rbx
        push    rax
        mov     rbx, rdi
        lea     rsi, [rip + str.0]
        mov     edx, 8
        call    <alloc::string::String as core::convert::From<&'a str>>::from@PLT
        mov     rax, rbx
        add     rsp, 8
        pop     rbx
        pop     rbp
        ret

str.0:
        .ascii  "https://"
  1. @sunfishcode thoughts?

    Jeff Muizelaar at 2017-10-24 17:19:21

  2. The X86 backend currently refuses to tail-call any call with sret: https://github.com/llvm-mirror/llvm/commit/a375d471378b1674a9d77d180a0b05ea8c90cb4b. I don't know why that check is needed, and unfortunately there are no comments or testcases explaining it.

    One option would be to dig into that code and figure out what the problems with struct returns are, if there are any, and then remove or refine that check.

    Another might be to change Rust's lowering for things like String return types to use first-class aggregate return types rather than sret. This would have a much broader impact, and could potentially have other benefits, though it would have risks too.

    Dan Gohman at 2017-10-24 18:16:33

  3. I've filed https://bugs.llvm.org/show_bug.cgi?id=35065

    Jeff Muizelaar at 2017-10-24 19:55:03