CppSharp icon indicating copy to clipboard operation
CppSharp copied to clipboard

Simplify and optimize the generated C++ by using -femit-all-decls

Open ddobrev opened this issue 4 years ago • 4 comments

Clang has a flag named -femit-all-decls which generates symbols for any inlined and any used implicit functions. Combined with -O, it removes private ones. This allows for much simpler generated C++ which simply allocates classes and gets most needed symbols. There are some complications, however, to think about:

  1. -O excludes protected functions too which means they either need special code or -O must be dropped which will bloat the binary with private functions;
  2. Free functions must be checked for symbols as well;
  3. This would limit the symbols to compilation with Clang only.

This Godbolt workspace demonstrates the approach as well as its problems.

ddobrev avatar Nov 14 '20 00:11 ddobrev

What we seem to be doing right now works regardless of the compiler because the C++ standard requires it (as far as I know) and we are only exporting the symbols we actually need.

Usually compilers implement different versions with different names for flags like this and gathering the commands for all the compilers and expecting them to have the same behavior and work exactly as we want seems very hard and pointless when we already have a way to do it. Some compilers may export more symbols than we need, and some will do the opposite. I also don't see how this would give us faster performance, if that's what you actually meant when you said optimize.

josetr avatar Nov 15 '20 12:11 josetr

I agree with @josetr on this one.

tritao avatar Nov 15 '20 12:11 tritao

We might have an issue in our current code where constructors of abstract specializations of templates don't get symbols. In case the problem's real, the approach above can solve it as seen as seen here. There are a few conditions to meet for actual results, however:

  1. Clang must use optimizations no more than -O;
  2. GCC must use no optimizations or get -fkeep-inline-functions too;
  3. MSVC seems to generate all symbols with any optimization but some research to make sure is required.

ddobrev avatar Nov 15 '20 21:11 ddobrev

In other words, point 3 from the description is not obligatory under the conditions above.

ddobrev avatar Nov 15 '20 21:11 ddobrev