Subject: Re: bug#63365: 30.0.50; GCC 13.1 breaks building Emacs with native-compilation

From: Andrea Corallo <[email-masked]>
Date: Fri, 16 Jun 2023 10:52:26 -0400

Andrea Corallo <[email-masked]> writes:

> Eli Zaretskii <[email-masked]> writes:
>
>>> From: Cyril Arnould <[email-masked]>
>>> CC: "[email-masked]" <[email-masked]>, "[email-masked]"
>>> 	<[email-masked]>, "  [email-masked]" <[email-masked]>,
>>> 	András Svraka <[email-masked]>
>>> Date: Fri, 16 Jun 2023 09:04:16 +0000
>>> 
>>> I've been playing around with compiler options. From my findings, the
>>> 
>>> -foptimize-sibling-calls flag breaks the build:
>>> 
>>>  
>>> 
>>> ./autogen.sh
>>> 
>>> CFLAGS='-g3 -O1 -gdwarf-2 -foptimize-sibling-calls' \
>>> 
>>> ./configure --with-native-compilation
>>> 
>>> make
>>> 
>>>  
>>> 
>>> OTOH, using -O1 with every flag listed under -O2 *but*
>>> 
>>> -foptimize-sibling-calls results in the build succeeding. The following
>>> 
>>> works as well for me:
>>> 
>>>  
>>> 
>>> ./autogen.sh
>>> 
>>> CFLAGS='-g3 -O2 -gdwarf-2 -fno-optimize-sibling-calls' \
>>> 
>>> ./configure --with-native-compilation
>>> 
>>> make
>>
>> Andrea, any reason that this GCC switch should break
>> native-compilation?
>
> At this point I start to suspect that this is not strictly related to
> native compilation.
>
> The configure flags are used only for our regular C code and not for
> Elisp native compilation.  So it might be that some kind of
> misscompilaiton of our C code is happening only with
> --with-native-compilation, but looks to me it's not our compiler or
> libgccjit the issue here.
>
>> Does the same problem happen on GNU/Linux?
>
> I tried now on GCC 13.1.1
>
> git clean -xfd && ./autogen.sh
> CFLAGS='-g3 -O1 -gdwarf-2 -foptimize-sibling-calls'
> ./configure --with-native-compilation && make -j16
>
> and have complete bootstrap succesfully on emacs-29 (65f355ea0a3), my
> dev machine is still x86_64-pc-linux-gnu.
>
> One idea might be asking GCC to log what optimize-sibling-calls is doing
> and see if any of our C function supporting native compilation is
> touched by this pass (I believe is called in GCC tree-tailcall).
>
> CFLAGS='-g3 -O1 -gdwarf-2 -foptimize-sibling-calls -fdump-tree-tailr' && make bootstrap
>
> should let GCC dump what this pass is for each C compilation unit, I'd
> start looking at the output for comp.c (I've comp.c.045t.tailr1 and comp.c.125t.tailr2).  
>
> Hope it helps!
>
>   Andrea

Just to add...

Looking at GCC source code, one should be able to grep within those
files for "Found tail call " or "Eliminated tail recursion in bb" as a
marker of some optimization happening there :)

Best Regards

  Andrea