[Marc Feeley here, the author of the Gambit Scheme compiler that generates the code which would benefit from tail-calls] What is needed (for Gambit) is a way for the programmer to express in the source code that the tail-call is known to be optimizable, i.e. it does not violate any of the tail-calling constraints, such as all local variables are dead (including those whose address was taken with “&var”) at the moment of the call. The code generated by the Gambit compiler is sufficiently complex that it would be impossible (in the halting problem sense) to check that the tail-calling constraints are not violated. However, I know the constraints are not violated because the code was designed that way. And if I’m mistaken in this belief, then I am the one that will bare the blame of the ensuing problems. So having a way to check during or after the compilation that tail-calls were optimized is not sufficient. A Scheme compiler that sometimes fails to compile is not very useful. What is needed is a guarantee that specific C calls are optimized. FYI the code generated by Gambit is put in “host” functions that look like this: typedef struct ___processor_state_struct { … }; typedef struct ___processor_state_struct *___processor_state; typedef void (*___host)(___processor_state ___ps); static void host1(___processor_state ___ps) { … ___host h = …; h(___ps); } So the last statement in the host function is a call to the next host function. The parameter is the same type, and indeed the same value (a pointer to a structure containing the state of the virtual machine such as registers), and there is no result. I would think this is the easiest situation to handle for a tail-call optimization. Marc > On Dec 9, 2021, at 5:57 AM, Florian Weimer <fweimer@xxxxxxxxxx> wrote: > > * Segher Boessenkool: > >> On Wed, Dec 08, 2021 at 04:17:33PM -0700, Jeff Law via Gcc-help wrote: >>> On 12/7/2021 3:38 PM, Bradley Lucier via Gcc-help wrote: >>>> So I've been investigating whether gcc's -foptimize-sibling-calls >>>> option (for which I've found very little documentation) might produce >>>> similar results. >>> The option has been around for probably 20+ years at this point, you >>> just might not have been aware of it. They try, but do not guarantee >>> tail call optimization. They have all kinds of checks that might cause >>> any particular call site to be rejected for tail call elimination. It's >>> enabled by default at O2 or higher. >> >> (Including -Os.) >> >> GCC will never not do a tail call if it knows any way to do it. > > GCC will never do a tail call if the target function is known not to > return normally, to preserve the stack backtrace. > > Thanks, > Florian