[LLVMdev] Broke my tail (call)

Duncan Sands baldrick at free.fr
Mon Feb 23 00:17:07 PST 2009


Hi Jon,

> >From what I have understood of the LLVM docs about when tail calls get 
> eliminated on x86 and x64 it should be a tail call, yes.
> 
>   http://llvm.org/docs/CodeGenerator.html#tailcallopt
> 
> . Caller and callee have the calling convention fastcc.
> . The call is a tail call - in tail position (ret immediately follows call and 
> ret uses value of call or is void).
> . Option -tailcallopt is enabled.
> . No variable argument lists are used.
> . On x86-64 when generating GOT/PIC code only module-local calls (visibility = 
> hidden or protected) are supported.
> 
> Those are all satisfied.

this list is for the code generator, and it seems obviously incomplete: it makes
no mention of local variables (alloca).  Probably it is implicitly assuming that
the call was marked "tail call" by the LLVM optimizers.  So you also need to check
under what conditions the LLVM optimizers do that.

> > I didn't look closely but at a glance it seems to be passing a local stack
> > variable as a call parameter. 
> 
> In this case, the arguments are a { { i8*, i8* }*, i8* } and a i32.

Maybe, but I'm pretty sure at least one of these values was calculated by
mucking around with allocas.  Did you add the "tail call" mark yourself?
If not, try removing it, and see if the LLVM optimizers add it back.

> I just tested my theory that returning a first-class struct from a function 
> inhibits tail call elimination and it seems that I was correct: altering this 
> function to pass its return struct by pointer in the first argument fixes the 
> stack overflow.
> 
> Is this a bug in LLVM?

Could be, but first I'd like to be sure that you are not misusing tail calls.

Ciao,

Duncan.



More information about the llvm-dev mailing list