[PATCH] D99517: Implemented [[clang::musttail]] attribute for guaranteed tail calls.

David Chisnall via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 7 03:52:39 PDT 2021


theraven added a comment.

Here's a minimal test:

  void tail(int, float);
  
  __attribute__((always_inline))
  void caller(float x)
  {
    [[clang::musttail]]
    return tail(42, x);
  }
  
  void outer(int x, float y)
  {
          return caller(y);
  }

This raises this error:

  tail.cc:7:3: error: cannot perform a tail call to function 'tail' because its signature is incompatible with the calling function
    return tail(42, x);
    ^
  tail.cc:1:1: note: target function has different number of parameters (expected 1 but has 2)
  void tail(int, float);
  ^
  tail.cc:6:5: note: tail call required by 'musttail' attribute here
    [[clang::musttail]]
      ^

There's also an interesting counterexample:

  void tail(int, float);
  
  __attribute__((always_inline))
  void caller(int a, float x)
  {
    [[clang::musttail]]
    return tail(a, x);
  }
  
  void outer(float y)
  {
          return caller(42, y);
  }

This *is* accepted by clang, but then generates this IR at -O0:

  define dso_local void @_Z5outerf(float %0) #2 {
    %2 = alloca i32, align 4
    %3 = alloca float, align 4
    %4 = alloca float, align 4
    store float %0, float* %4, align 4
    %5 = load float, float* %4, align 4
    store i32 42, i32* %2, align 4
    store float %5, float* %3, align 4
    %6 = load i32, i32* %2, align 4
    %7 = load float, float* %3, align 4
    call void @_Z4tailif(i32 %6, float %7)
    ret void
  }

And this IR at -O1:

  ; Function Attrs: uwtable mustprogress
  define dso_local void @_Z5outerf(float %0) local_unnamed_addr #2 {
    call void @_Z4tailif(i32 42, float %0)
    ret void
  }

Note that in both cases, the alway-inline attribute is respected (even at -O0, the always-inline inliner runs) but the musttail annotation is lost.  The inlining has inserted the call into a function with a different set of parameters and so it cannot have a `musttail` IR annotation.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D99517/new/

https://reviews.llvm.org/D99517



More information about the cfe-commits mailing list