Skip to content

musttail doesn't optimize sibling calls #147813

Closed

Description

@brandtbucher

For example, this code:

int bar(int);

int
foo_tail(int x)
{
    // Correct, compiles to a single jmp:
    return bar(x);
}

int
foo_musttail(int x)
{
    // Buggy, loads and re-spills all parameters:
    __attribute__((musttail)) return bar(x);
}

Compiles to:

_foo_tail:
  jmp _bar

_foo_musttail:
  movl 4(%esp), %eax
  movl %eax, 4(%esp)
  jmp _bar

Compiler Explorer

Adding more parameters to the function (and tail call) increases the number of redundant loads/spills, too.

Activity

  1. llvmbot commented on Jul 10, 2025

    @llvmbot
    Member

    @llvm/issue-subscribers-backend-x86

    Author: Brandt Bucher (brandtbucher)

    Details For example, this code:
    int bar(int);
    
    int
    foo_tail(int x)
    {
        // Correct, compiles to a single jmp:
        return bar(x);
    }
    
    int
    foo_musttail(int x)
    {
        // Buggy, loads and re-spills all parameters:
        __attribute__((musttail)) return bar(x);
    }

    Compiles to:

    _foo_tail:
      jmp _bar
    
    _foo_musttail:
      movl 4(%esp), %eax
      movl %eax, 4(%esp)
      jmp _bar

    Compiler Explorer

    Adding more parameters to the function (and tail call) increases the number of redundant loads/spills, too.

  2. changed the title [-]`musttail` generates redundant moves on 32-bit platforms[/-] [+]`musttail` doesn't optimize sibling calls[/+] on Jul 22, 2025
  3. brandtbucher commented on Jul 22, 2025

    @brandtbucher
    ContributorAuthor

    Actually, this isn't a 32-bit issue, it's just easier to trigger with fewer parameters there. Here's the same thing on 64-bit: https://godbolt.org/z/1cao7Gxhv

    Turns out musttail just doesn't optimize sibling calls. Patch incoming.

  4. Hendiadyoin1 commented on Jul 31, 2025

    @Hendiadyoin1

    I am getting this, or a closely related issue in c++ land and a member-pointer sibling call which does not consistently
    result in a tail-call, without a diagnostic at that
    (LadybirdBrowser/ladybird#5641 or the thread here on the ladybird discord as a reference)
    I was not yet able to make a minimal reproducer,
    but I can confirm that the linked patch does not suffice

  5. brandtbucher commented on Jul 31, 2025

    @brandtbucher
    ContributorAuthor

    Is your issue that tail-calls aren't being emitted at all? That seems like something separate.

    This is just a code quality issue, not a correctness one.

  6. Hendiadyoin1 commented on Jul 31, 2025

    @Hendiadyoin1

    ah, yeah it fully fails to tail-call in some cases, seems like I misread the issue (multiple times)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions