On Fri, 2021-04-23 at 16:07 -0400, David Malcolm wrote:
> On Fri, 2021-04-23 at 12:44 -0700, Josh Haberman via Gcc wrote:
> > Would it be feasible to implement a "musttail" statement attribute
> > in
> > GCC to get a guarantee that tail call optimization will be
> > performed?
> > 
> > Such an attribute has just landed in the trunk of Clang
> > (https://reviews.llvm.org/D99517). It makes it possible to write
> > algorithms that use arbitrarily long chains of tail calls without
> > risk
> > of blowing the stack. I would love to see something like this land
> > in
> > GCC also (ultimately I'd love to see it standardized).
> 
> 
> FWIW I implemented something like this in GCC's middle-end here:
>  
> https://gcc.gnu.org/git/?p=gcc.git;a=commitdiff;h=9a385c2d3d74ffed78f2ed9ad47b844d2f294105
> exposing it in API form for libgccjit:
>  
> https://gcc.gnu.org/git/?p=gcc.git;a=commitdiff;h=15c671a79ca66df5b1de70dd1a0b78414fe003ef
>  
> https://gcc.gnu.org/onlinedocs/jit/topics/expressions.html#c.gcc_jit_rvalue_set_bool_require_tail_call
> but IIRC it's not yet exposed to the regular GCC frontends.

I hacked on the plugin you created to test CALL_EXPR_MUST_TAIL_CALL to
add the musttail attribute for functions and pushed it here:

https://github.com/pietro/gcc-musttail-plugin

This patch adds it to the plugin testsuite and successfully runs the
same tests as the original plugin:

https://gist.github.com/pietro/9a582b054b008e9a4ad3e1afe3a4cc22

It seems that in order to be able to add attributes to the return
statement changes are needed to the frontend parser. If this attribute
is something people think is usefull I'll try to implement it as a
proper patch to GCC.

Pietro


Reply via email to