This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH v4][C][ADA] use function descriptors instead of trampolines in C


Am Sonntag, den 16.12.2018, 09:13 -0700 schrieb Jeff Law:
> On 12/16/18 6:45 AM, Uecker, Martin wrote:
> > Am Freitag, den 14.12.2018, 18:20 -0700 schrieb Martin Sebor:
> > > On 12/14/18 4:36 PM, Jeff Law wrote:
> > > > On 12/14/18 3:05 AM, Uecker, Martin wrote:
> > > > > 
> > > > > Am Donnerstag, den 13.12.2018, 16:35 -0700 schrieb Jeff Law:
> > > > > > On 12/12/18 11:12 AM, Uecker, Martin wrote:
> > > > > 
> > > > > ...
> > > > > > > > > diff --git a/gcc/c/c-objc-common.h b/gcc/c/c-objc-common.h
> > > > > > > > > index 78e768c2366..ef039560eb9 100644
> > > > > > > > > --- a/gcc/c/c-objc-common.h
> > > > > > > > > +++ b/gcc/c/c-objc-common.h
> > > > > > > > > @@ -110,4 +110,7 @@ along with GCC; see the file COPYING3.  If
> > > > > > > > > not see
> > > > > > > > >   
> > > > > > > > >   #undef LANG_HOOKS_TREE_INLINING_VAR_MOD_TYPE_P
> > > > > > > > >   #define LANG_HOOKS_TREE_INLINING_VAR_MOD_TYPE_P c_vla_unspec_p
> > > > > > > > > +
> > > > > > > > > +#undef LANG_HOOKS_CUSTOM_FUNCTION_DESCRIPTORS
> > > > > > > > > +#define LANG_HOOKS_CUSTOM_FUNCTION_DESCRIPTORS true
> > > > > > > > >   #endif /* GCC_C_OBJC_COMMON */
> > > > > > > > 
> > > > > > > > I wonder if we even need the lang hook anymore.  ISTM that a
> > > > > > > > front-end
> > > > > > > > that wants to use the function descriptors can just set
> > > > > > > > FUNC_ADDR_BY_DESCRIPTOR and we'd use the function descriptor,
> > > > > > > > else we'll
> > > > > > > > use the trampoline.  Thoughts?
> > > > > > > 
> > > > > > > The lang hook also affects the minimum alignment for function
> > > > > > > pointers via the FUNCTION_ALIGNMENT macro (gcc/default.h). This
> > > > > > > does
> > > > > > > not appear to change the default alignment on any architecture, but
> > > > > > > it causes a failure in i386/gcc.target/i386/attr-aligned.c when
> > > > > > > requesting a smaller alignment which is then silently ignored.
> > > > > > 
> > > > > > Ugh.  I didn't see that.
> > > > > 
> > > > > The test is new (2019-11-29 Martin Sebor), but one could
> > > > > argue that we could simply remove this specific test as 'aligned'
> > > > > is only required to increase alignment. Martin?
> > > > 
> > > > The test is meant to test that we do the right thing consistently.  If
> > > > we're failing with your patch, then that needs to be addressed.
> > > 
> > > I haven't been paying attention here and so I don't know how the test
> > > fails after the change.  It's meant to verify that attribute aligned
> > > successfully reduces the alignment of functions that have not been
> > > previously declared with one all the way down to the supported minimum
> > > (which is 1 on i386).  I agree with Jeff that removing the test would
> > > not be right unless the failure is due to some bad assumptions on my
> > > part.  If it's the built-in that fails that could be due to a bug in
> > > it (it's very new).
> > 
> > There is a choice to be made: 
> > 
> > Either we activate the lang hook for C, then the minimum alignment for
> > functions on is not 1 anymore, because we need one bit to identify the 
> > descriptors.  From a correcntess point of view, this is OK as 'alignas'
> > is only required to increase alignment. It is also not really regression
> > in my opinion, as it is nowhere documented that one can reduce alignment
> > to '1'. The test also has just been added a couple of days ago (if I am
> > not mistaken). For these reasons, I think it would be OK to remove the test.
> > 
> > The other option is to decide that having an alignment of only '1'
> > is a valuable feature and should be preserved on those platforms which
> > support it. I have no idea what this could be good for, but maybe
> > there are use cases.  In this case, it makes of course sense to keep
> > the test.  We should then remove the lang hook (as it could never be
> > activated for most languages) and instead live with the fact that
> > '-fno-trampoline' and using alignof(1) on functions are simply
> > incompatible. A warning could be added if the compiler sees
> > alignof(1) when -fno-trampoline is active.
> 
> There's certainly targets where 1 byte function alignment is important
> from a code space perspective -- it's likely that function descriptors
> will never be supported on those targets.

But most architectures require a higher alignment anyway.
Here is a list of all targets where function alignment
is 1 byte:

gcc/config/avr/avr.h:#define FUNCTION_BOUNDARY 8
gcc/config/i386/i386.h:#define FUNCTION_BOUNDARY 8
gcc/config/m32c/m32c.h:#define FUNCTION_BOUNDARY 8
gcc/config/mn10300/mn10300.h:#define FUNCTION_BOUNDARY 8
gcc/config/pa/pa.h:#define FUNCTION_BOUNDARY BITS_PER_WORD
gcc/config/rl78/rl78.h:#define FUNCTION_BOUNDARY 		8
gcc/config/rx/rx.h:#define FUNCTION_BOUNDARY 		((rx_cpu_type == RX100 || rx_cpu_type == RX200) ? 4 : 8)


From this list only i386 currently adds the target hook and
would be affected by this patch. But arm is also affected:
 
> It's also important to remember that not every target which uses
> function descriptors uses the LSB.  On some targets the LSB may switch
> between modes (arm vs thumb for example).  So on those targets the use
> of descriptors may imply an even larger minimum alignment.

Yes, arm reserves the lowest 2 bits so since we require an additional
bit and this increases the minimum alignment from 4 to 8 bytes on 
aarch64.

Here are the architectures which currently support function
descriptors and the bit used:

$ grep TARGET_CUSTOM gcc/config/*/*.c | grep define
gcc/config/aarch64/aarch64.c:#define TARGET_CUSTOM_FUNCTION_DESCRIPTORS 4
gcc/config/arm/arm.c:#define TARGET_CUSTOM_FUNCTION_DESCRIPTORS 2
gcc/config/csky/csky.c:#define TARGET_CUSTOM_FUNCTION_DESCRIPTORS 1
gcc/config/i386/i386.c:#define TARGET_CUSTOM_FUNCTION_DESCRIPTORS 1
gcc/config/ia64/ia64.c:#define TARGET_CUSTOM_FUNCTION_DESCRIPTORS 0
gcc/config/mips/mips.c:#define TARGET_CUSTOM_FUNCTION_DESCRIPTORS 2
gcc/config/or1k/or1k.c:#define TARGET_CUSTOM_FUNCTION_DESCRIPTORS 1
gcc/config/powerpcspe/powerpcspe.c:#define TARGET_CUSTOM_FUNCTION_DESCRIPTORS 1
gcc/config/riscv/riscv.c:#define TARGET_CUSTOM_FUNCTION_DESCRIPTORS 1
gcc/config/rs6000/rs6000.c:#define TARGET_CUSTOM_FUNCTION_DESCRIPTORS 1
gcc/config/s390/s390.c:#define TARGET_CUSTOM_FUNCTION_DESCRIPTORS 1
gcc/config/sparc/sparc.c:#define TARGET_CUSTOM_FUNCTION_DESCRIPTORS 1

For mips minimum alignment is 4 bytes, so here alignment would
not change.

> Ultimately using function descriptors is an ABI breaking choice and we
> might declare that function descriptors imply higher function
> alignments.  

Increasing the alignment is not an ABI breaking change.

But the alignment increase itself on 'i386' and 'aarch64'
might be unacceptable. In this case, the only safe change
is to make the higher alignment also depend on
"-fno-trampolines".  Would this be acceptable?

 
(Code compiled with "-fno-trampolines" is of course
ABI-incompatible to existing code with respect to exported
descriptors and imported function pointers which happen
to be misaligned. Generally increasing alignment would
be nice as it would reduce this incompatibility in one
direction.)

> The ABI implications also likely mean that function
> descriptors aren't likely going to achieve widespread use.
> Sigh.

There might be other options for architectures
which are problematic: We could use unused higher
order bits or one could read the first bytes of the
function/descriptor and use a special tag for
descriptors which cannot occur in machine code.

Also on the language level, one could mark pointers which
point to designators and keep them separate. This is
what Apple's "blocks" language extension does.


Best,
Martin







Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]