[0/4,MSP430] Tweaks to default configuration to reduce code size

Message ID 20191107213110.0ceea63a@jozef-kubuntu
Headers show
Series
  • Tweaks to default configuration to reduce code size
Related show

Message

Jozef Lawrynowicz Nov. 7, 2019, 9:31 p.m.
When building small programs for MSP430, the impact of the unused
functions pulled in from the CRT libraries is quite noticeable. Most of these
relates to feature that will never be used for MSP430 (Transactional memory,
supporting shared objects and dynamic linking), or rarely used (exception
handling).

The following patches change the default configuration for msp430-elf with the
aim of reducing code size by removing these unsupported features.

Related generic changes to GCC have been submitted here:
https://gcc.gnu.org/ml/gcc-patches/2019-11/msg00415.html
(But note that the first patch to disable eh frame registry has been retracted
as it's no longer necessary).

I picked random C and C++ programs from the testsuite to give an
indication of the size reduction:

$ msp430-elf-gcc testsuite/gcc.dg/20000108-1.c -Os -msim -ffunction-sections \
    -fdata-sections -Wl,-gc-sections
Before:
   text    data     bss     dec     hex filename
    708     242      28     978     3d2 20000108-1.exe
After:
   text    data     bss     dec     hex filename
    444     234       2     680     2a8 20000108-1.exe

$ msp430-elf-g++ -msim -Os testsuite/g++.dg/abi/covariant5.C \
    -ffunction-sections -fdata-sections -Wl,-gc-sections
Before:
   text    data     bss     dec     hex filename
   4090     396      18    4504    1198 covariant5.exe
Before (-fno-exceptions):
   text    data     bss     dec     hex filename
   3912     396      18    4326    10e6 a.out
After:
   text    data     bss     dec     hex filename
   3396     122       2    3520     dc0 covariant5.exe

The writeup for the -minrt patch has some more code size comparisons related to
that option.

Successfully regtested for msp430-elf.

Ok to apply?

Jozef Lawrynowicz (4):
  MSP430: Disable TM clone registry by default
  MSP430: Disable exception handling by default for C++
  MSP430: Disable __cxa_atexit
  MSP430: Remove -minrt option

 config-ml.in                                  | 13 +++++++++
 gcc/config.gcc                                |  7 +++++
 gcc/config/msp430/msp430.c                    |  9 +++++++
 gcc/config/msp430/msp430.h                    | 20 +++++++++++---
 gcc/config/msp430/msp430.opt                  |  4 +--
 gcc/config/msp430/t-msp430                    |  9 ++++---
 gcc/doc/install.texi                          |  3 +++
 gcc/doc/invoke.texi                           | 15 ++++-------
 gcc/testsuite/g++.dg/cpp1y/sized-dealloc2.C   |  2 +-
 gcc/testsuite/g++.dg/cpp2a/explicit1.C        |  2 +-
 gcc/testsuite/g++.dg/cpp2a/explicit2.C        |  2 +-
 gcc/testsuite/g++.dg/cpp2a/explicit5.C        |  2 +-
 gcc/testsuite/g++.dg/dg.exp                   |  9 ++++++-
 gcc/testsuite/g++.dg/eh/array1.C              |  2 +-
 gcc/testsuite/g++.dg/eh/spec11.C              |  2 +-
 gcc/testsuite/g++.dg/eh/spec6.C               |  2 +-
 gcc/testsuite/g++.dg/ext/vla4.C               |  2 +-
 gcc/testsuite/g++.dg/init/dso_handle1.C       |  1 +
 gcc/testsuite/g++.dg/init/dso_handle2.C       |  1 +
 gcc/testsuite/g++.dg/ipa/pr64612.C            |  2 +-
 gcc/testsuite/g++.dg/other/cxa-atexit1.C      |  1 +
 gcc/testsuite/g++.dg/other/error32.C          |  2 +-
 gcc/testsuite/g++.dg/torture/dg-torture.exp   |  9 ++++++-
 gcc/testsuite/g++.dg/torture/pr34850.C        |  2 +-
 gcc/testsuite/g++.dg/tree-ssa/ivopts-3.C      |  2 +-
 gcc/testsuite/g++.dg/tree-ssa/pr33615.C       |  2 +-
 gcc/testsuite/g++.dg/warn/Wcatch-value-1.C    |  2 +-
 gcc/testsuite/g++.dg/warn/Wcatch-value-2.C    |  2 +-
 gcc/testsuite/g++.dg/warn/Wcatch-value-3.C    |  2 +-
 .../g++.dg/warn/Wstringop-truncation-2.C      |  2 +-
 gcc/testsuite/g++.dg/warn/Wterminate1.C       |  2 +-
 gcc/testsuite/g++.dg/warn/pr83054.C           |  2 +-
 gcc/testsuite/g++.old-deja/g++.other/cond5.C  |  2 +-
 gcc/testsuite/g++.old-deja/old-deja.exp       |  9 ++++++-
 gcc/testsuite/lib/gcc-dg.exp                  | 10 +++++++
 gcc/testsuite/lib/target-supports.exp         | 27 ++++++++++++++++---
 libgcc/config.host                            |  3 ++-
 libgcc/config/msp430/t-msp430                 |  6 +++++
 libgcc/configure                              |  9 +++++++
 libgcc/configure.ac                           |  8 ++++++
 40 files changed, 166 insertions(+), 47 deletions(-)

-- 
2.17.1

Comments

Oleg Endo Nov. 8, 2019, 12:14 p.m. | #1
On Thu, 2019-11-07 at 21:31 +0000, Jozef Lawrynowicz wrote:
> When building small programs for MSP430, the impact of the unused

> functions pulled in from the CRT libraries is quite noticeable. Most of these

> relates to feature that will never be used for MSP430 (Transactional memory,

> supporting shared objects and dynamic linking), or rarely used (exception

> handling).


There's a magic switch, which does the business, at least for me, most
of the time:

       -flto

If you're trying to bring down the executable size as much as possible,
but don't use -flto, I think something is wrong.

Cheers,
Oleg
Jozef Lawrynowicz Nov. 8, 2019, 1:27 p.m. | #2
On Fri, 08 Nov 2019 21:14:19 +0900
Oleg Endo <oleg.endo@t-online.de> wrote:

> On Thu, 2019-11-07 at 21:31 +0000, Jozef Lawrynowicz wrote:

> > When building small programs for MSP430, the impact of the unused

> > functions pulled in from the CRT libraries is quite noticeable. Most of these

> > relates to feature that will never be used for MSP430 (Transactional memory,

> > supporting shared objects and dynamic linking), or rarely used (exception

> > handling).  

> 

> There's a magic switch, which does the business, at least for me, most

> of the time:

> 

>        -flto

> 

> If you're trying to bring down the executable size as much as possible,

> but don't use -flto, I think something is wrong.

> 

> Cheers,

> Oleg

> 


Yes, I should have used -flto in my examples. But it doesn't help remove these
CRT library functions which are normally either directly added to the
list of functions to run before main (via .init, .ctors or .init_array) or used
in functions which are themselves added to this list.

The unnecessary functions we want to remove are:
  deregister_tm_clones
  register_tm_clones
  __do_global_dtors_aux
  frame_dummy
LTO can't remove any of them.

Thanks,
Jozef
Oleg Endo Nov. 8, 2019, 1:59 p.m. | #3
On Fri, 2019-11-08 at 13:27 +0000, Jozef Lawrynowicz wrote:
> 

> Yes, I should have used -flto in my examples. But it doesn't help remove these

> CRT library functions which are normally either directly added to the

> list of functions to run before main (via .init, .ctors or .init_array) or used

> in functions which are themselves added to this list.

> 

> The unnecessary functions we want to remove are:

>   deregister_tm_clones

>   register_tm_clones

>   __do_global_dtors_aux

>   frame_dummy

> LTO can't remove any of them.

> 


Ah, right, good point.  That's not MSP430 specific actually.  For those
things I usually have custom init code, which also does other things
occasionally.  Stripping off global dtors is then an option in the
build system which takes care of it (in my case, I do it by modifying
the generated linker script).

But again, as with the exceptions, it might be better to implement
these kind of things outside of the compiler, e.g. by building the app
with -nostartfiles -nodefaultlibs and providing your own substitutes.

Another option is to patch those things in using the OS part of the
target triplet.

Cheers,
Oleg
Jozef Lawrynowicz Nov. 8, 2019, 2:32 p.m. | #4
On Fri, 08 Nov 2019 22:59:18 +0900
Oleg Endo <oleg.endo@t-online.de> wrote:

> On Fri, 2019-11-08 at 13:27 +0000, Jozef Lawrynowicz wrote:

> > 

> > Yes, I should have used -flto in my examples. But it doesn't help remove these

> > CRT library functions which are normally either directly added to the

> > list of functions to run before main (via .init, .ctors or .init_array) or used

> > in functions which are themselves added to this list.

> > 

> > The unnecessary functions we want to remove are:

> >   deregister_tm_clones

> >   register_tm_clones

> >   __do_global_dtors_aux

> >   frame_dummy

> > LTO can't remove any of them.

> >   

> 

> Ah, right, good point.  That's not MSP430 specific actually.  For those

> things I usually have custom init code, which also does other things

> occasionally.  Stripping off global dtors is then an option in the

> build system which takes care of it (in my case, I do it by modifying

> the generated linker script).

> 

> But again, as with the exceptions, it might be better to implement

> these kind of things outside of the compiler, e.g. by building the app

> with -nostartfiles -nodefaultlibs and providing your own substitutes.


I just don't think we need to be putting up this high barrier to entry for users
who want reduced code size but are building GCC from source.

With these changes users are getting a highly size-optimized runtime library
(14 bytes for a program that gets you to main() is always nice to see) out of
the box, by simply removing features that do not make sense on the target, and
they don't have to faff with any extra options.

The size of the CRT code has been a long standing complaint and is some part of
the reason a large chunk of the MSP430 user base still uses "mspgcc" which is
the old downstream GCC port of the target, which hasn't has any development
since 2012.

> 

> Another option is to patch those things in using the OS part of the

> target triplet.


Interesting idea. Something like msp430-unknown-min(imum)? The thing is even
with these changes the target is still ELF compliant.

Although I guess supplying a configuration which disables exceptions is not
very ELF-y.

Thanks,
Jozef
> 

> Cheers,

> Oleg

>