public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Segher Boessenkool <segher@kernel.crashing.org>
To: Marc Feeley <feeley@iro.umontreal.ca>
Cc: Florian Weimer <fweimer@redhat.com>,
	Jeff Law <jeffreyalaw@gmail.com>,
	gcc-help@gcc.gnu.org, "Lucier, Bradley J" <lucier@purdue.edu>,
	Bradley Lucier <lucier@math.purdue.edu>
Subject: Re: CALL_EXPR_MUST_TAIL_CALL and LLVM's musttail
Date: Sat, 11 Dec 2021 17:02:31 -0600	[thread overview]
Message-ID: <20211211230231.GC614@gate.crashing.org> (raw)
In-Reply-To: <AD141EC8-4377-49A2-B0C0-428D864563D2@iro.umontreal.ca>

On Thu, Dec 09, 2021 at 08:06:51PM -0500, Marc Feeley wrote:
> > On Dec 9, 2021, at 6:36 PM, Segher Boessenkool <segher@kernel.crashing.org> wrote:
> >> 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.
> > 
> > On most architectures you can simply check if there are any "return"
> > instructions generated, no?  Or do you generate any actual calls (to
> > user code) as well?
> 
> You mean a “return” at the assembly code level?  Seems very brittle… on x86 a “pop %eax; jmp *%eax” will do the same as a “ret” and gcc might generate that sequence (from my point of view).  In any case, as I said previously, I don’t want to check if gcc did not optimize the tail calls… it is too late to do anything other than essentially tell the programmer “sorry I couldn’t compile that program correctly”.

I know of no single compiler that does not have very predictable insns
as return sequence.  There could be more than one sequence you need to
check for, sure, but in reality compilers generate only one or a few
code sequences to do returns: by its very nature a return can not be
combined with some other operation and optimised that way.

 >> 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.
> > 
> > Then you need to write code that works as you want on all configurations
> > you support, state what those configs are, and test that it compiles on
> > all such configs, before every release.
> 
> This does not solve my problem either. No testing will cover all possible situations because for a particular architecture the tail call optimization may work on all the tests I can come up with, but some programmer using Gambit may write a program for which the tail calls in the C code generated by the Gambit compiler are not optimized.

You need to know what a particular compiler (for a particular target, in
a particular configuration, invoked with particular flags) will do with
epilogues, yes.  This is *unavoidable* if the compiler will not tell you
itself if every tail position call is optimised to an actual tail call,
and no existing GCC version will tell you.

> I’m not looking for a guarantee that tail calls are supported transparently on all architectures.  Currently Gambit uses a trampoline to implement Scheme tail calls, and this can be used as a fallback for architectures where C TCO is not guaranteed.
> 
> So what I need is some way to test at compile time if for the target architecture and compile options there is a guarantee that tail calls will be optimized (if the C code obeys the usual set of constraints).  Something like
> 
>   #if __has_attribute(tail_call_supported_on_this_architecture)
>   /* use tail calls for jumping around... */
>   #else
>   /* use trampoline fallback... */
>   #endif

This is impossible to do.  Tail calls are supported on *all* GCC
architectures, but not every (tail position) function call will be
optimised to a tail call, and not every such function call *can* (except
on architectures with a separate return stack of course, but
unfortunately there are very few of those).


Segher

  parent reply	other threads:[~2021-12-11 23:03 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-07 22:38 Bradley Lucier
2021-12-08 23:17 ` Jeff Law
2021-12-09  0:07   ` Segher Boessenkool
2021-12-09  3:31     ` Bradley Lucier
2021-12-09 10:56       ` Florian Weimer
2021-12-09 23:15       ` Segher Boessenkool
2021-12-09 10:57     ` Florian Weimer
2021-12-09 13:30       ` Marc Feeley
2021-12-09 14:04         ` Florian Weimer
2021-12-09 14:17           ` Marc Feeley
2021-12-10  4:31             ` Florian Weimer
2021-12-11  2:44               ` Segher Boessenkool
2021-12-09 23:36         ` Segher Boessenkool
2021-12-10  1:06           ` Marc Feeley
2021-12-10 22:40             ` Jeff Law
2021-12-12 17:32               ` Segher Boessenkool
2021-12-12 18:08                 ` Jonathan Wakely
2021-12-12 21:41                   ` Segher Boessenkool
2021-12-11 23:02             ` Segher Boessenkool [this message]
2021-12-13 17:48             ` Avi Kivity
2021-12-15 18:29               ` Bradley Lucier
2021-12-09 22:00       ` Segher Boessenkool

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211211230231.GC614@gate.crashing.org \
    --to=segher@kernel.crashing.org \
    --cc=feeley@iro.umontreal.ca \
    --cc=fweimer@redhat.com \
    --cc=gcc-help@gcc.gnu.org \
    --cc=jeffreyalaw@gmail.com \
    --cc=lucier@math.purdue.edu \
    --cc=lucier@purdue.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).