How to shrink your C++ code to fit on an Epiphany core

A forum for documenting the Parallella and Epiphany . (Not Q&A!)

How to shrink your C++ code to fit on an Epiphany core

Postby Gravis » Tue Apr 21, 2015 12:50 pm

While C++ is fully supported on the Epiphany, there are few things that will cause G++ to generate code too large to fit on an Epiphany core.

What to exclude from your C++ code:
  • exceptions - any use of try, throw and catch will inflate your code.
  • virtual functions - code generated to manage vtables is very large so any use of virtual inheritance is a bad idea.
  • Run-Time Type Information - it's only useful when used with virtual functions, so dont use it.
  • anything from the STL - the STL is great but it relies on the things mentioned above
G++ flags you should use:
Code: Select all
-Os -std=c++1y -fno-exceptions -fno-rtti -fno-threadsafe-statics -freg-struct-return -fno-default-inline

Explanation of flags:
  • -Os - causes the compiler to optimize for size
  • -std=c++1y - lets you use all the latest C++ features
  • -fno-exceptions - excludes additional code needed for exception handling
  • -fno-rtti - excludes Run-Time Type Information
  • -fno-threadsafe-statics - excludes safety mechanism for static functions intended for multithreaded code
  • -freg-struct-return - if you return a struct from a function it will try to fit it into a register
  • -fno-default-inline - code in your class definition won't automatically be inlined (it does by default)
If you are serious about C++, this is worth reading: Using C++ Efficiently In Embedded Applications and Practical Guide to Bare Metal C++
Last edited by Gravis on Wed Apr 22, 2015 7:57 pm, edited 2 times in total.
User avatar
Gravis
 
Posts: 445
Joined: Mon Dec 17, 2012 3:27 am
Location: East coast USA.

Re: How to shrink your C++ code to fit on an Epiphany core

Postby piotr5 » Wed Apr 22, 2015 9:43 am

thanks for your hints and suggestions. there's one point I'm unsure of though: -fno-default-inline
as far as I have seen most people put only wrappers in the class-definition.
for example when porting a c-lib to c++ class structure. it makes sense to inline those!
everything else is then put elsewhere in the header or an external header file to be included there.
but then, maybe gcc will automatically optimize out wrapper functions anyway?
or will the wrapper function then linger around in the executable, never to be used?

also I should mention, using c++1y might not be a good idea. maybe better compromise for c++14?

as for not being allowed to use c++stdlib, anybody knows of a smaller lib?
any replacement for utf8 string-manipulation?
piotr5
 
Posts: 230
Joined: Sun Dec 23, 2012 2:48 pm

Re: How to shrink your C++ code to fit on an Epiphany core

Postby Gravis » Wed Apr 22, 2015 7:30 pm

piotr5 wrote:there's one point I'm unsure of though: -fno-default-inline
as far as I have seen most people put only wrappers in the class-definition.
for example when porting a c-lib to c++ class structure. it makes sense to inline those!

the functions that will end up being smaller by inlining will be inlined. the just prevents the compiler from always inlining.

piotr5 wrote:also I should mention, using c++1y might not be a good idea. maybe better compromise for c++14?

c++1y is currently the closest implementation to c++14 for gcc. your other option is c++11 which leaves out some nice fixes made in c++14. find the "-std=" options in the man page for e-gcc to see all the options.

piotr5 wrote:as for not being allowed to use c++stdlib, anybody knows of a smaller lib?

you are probably best off just taking bits from another library and/or making your own code which is specific to the task.
here are some small libs:

piotr5 wrote:any replacement for utf8 string-manipulation?

utf8 is backwards compatible with ascii, so they are binary compatible. however, the semantics to have full feature parity across all of UTF is far too large to fit. so long as you don't need to know details about individual characters, basic ascii manipulation will work just fine for utf8. however, i think the real question is why someone would be doing string manipulation on the epiphany.
User avatar
Gravis
 
Posts: 445
Joined: Mon Dec 17, 2012 3:27 am
Location: East coast USA.

Re: How to shrink your C++ code to fit on an Epiphany core

Postby piotr5 » Wed Apr 22, 2015 10:08 pm

you're right, thanks for the helpful hints.
piotr5
 
Posts: 230
Joined: Sun Dec 23, 2012 2:48 pm

Re: How to shrink your C++ code to fit on an Epiphany core

Postby dobkeratops » Wed Jul 08, 2015 11:01 am

virtual functions - code generated to manage vtables is very large so any use of virtual inheritance is a bad idea.


virtual functions were hugely problematic on the sony PS3 because they weren't even in the same address space.

Does the epiphany C++ compiler make any attempt to support them (perhaps generating some sort of stubs that dynamically read the required code in) or - like back on the PS3 - you're just much better off structuring code differently to avoid them altogether - sorting by types up-front, using a component architecture instead of OO.
dobkeratops
 
Posts: 189
Joined: Fri Jun 05, 2015 6:42 pm
Location: uk

Re: How to shrink your C++ code to fit on an Epiphany core

Postby piotr5 » Thu Jul 16, 2015 6:28 am

imho any hybrid-architecture like coprocessors and such is not fit for call-backs and the related stuff. maybe compilers should store a 2d-list of function-addresses, one entry per core? suppose you had an epiphany with 1024 cores, should every callback come with a list of 1025 function-variants and 1025 copies of that single function? I don't think so, 2 copies are enough, epiphany can execute code stored on other cores. if you're using callbacks you likely don't need high speed. however, c++ has an interesting feature, it can treat a fixed function as if it were a callback, thereby those functions are stored on the individual cores which can read the object. i.e. at compile-time the actual function being used is determined by template-parameters. with such a feature, what do you need vtables for? well, of course if datatype isn't known at compile-time...
piotr5
 
Posts: 230
Joined: Sun Dec 23, 2012 2:48 pm

Re: How to shrink your C++ code to fit on an Epiphany core

Postby paran » Thu Jul 16, 2015 12:06 pm

Thanks for hints. They are very helpful.
paran
 
Posts: 53
Joined: Sat Jun 28, 2014 5:22 pm
Location: Georgetown, Ontario, Canada


Return to Quick Start Guides , Documentation, Examples (Start Here!)

Who is online

Users browsing this forum: No registered users and 0 guests

cron