ravi icon indicating copy to clipboard operation
ravi copied to clipboard

OP_TAILCALL optimization for tail recursion

Open dibyendumajumdar opened this issue 9 years ago • 5 comments

dibyendumajumdar avatar Mar 21 '15 23:03 dibyendumajumdar

Can't you make LLVM generate a tailcall function (incompatible with C calling standards) and a wrapper C function (incompatible with tailcall), and pick one of them depending on OP_TAILCALL vs OP_CALL?

SoniEx2 avatar Jul 02 '15 17:07 SoniEx2

http://lua-users.org/lists/lua-l/2015-03/msg00282.html

dibyendumajumdar avatar Jul 02 '15 19:07 dibyendumajumdar

I also have many other things to finish before I can spare time to look at this issue as I don't have use cases where I need deep recursion.

dibyendumajumdar avatar Jul 02 '15 19:07 dibyendumajumdar

  1. I don't see the issue.
  2. Can't LLVM optimize a tail call (I mean LLVM's tail calls have a different calling convention which breaks the ABI - but listen to this thought) IF you have both a tail-call-version of the function AND a normal-call-version of the function? The normal-call-version would call the tail-call-version from its return, which would workaround the ABI break. On OP_TAILCALL, you would use the tail-call-version, and on OP_CALL, the normal-call wrapper.

(Then again I don't know much about LLVM, so this is mostly a guess)

SoniEx2 avatar Jul 02 '15 19:07 SoniEx2

Thanks for the ideas - I haven't really explored this deeply. My initial thoughts were to just support the recursive case which can be implemented by doing what Lua does - adjust the stack and then jump to the beginning of the current function.

dibyendumajumdar avatar Jul 02 '15 20:07 dibyendumajumdar