All compilers come with standard optimization flags: -O2 or -O3 to generate fast code and -Os or -Oz to generate small code. However, these are general flags that provide standard combinations of all the possible optimizations offered by the compiler — over 200 in the case of GCC — which have generally proved to be good across a wide range of programs and architectures.

For any particular program and architecture these flags will not generate the very best code and up until now the only solution has been to exhaustively try setting the various individual flags. Such iterative compilation takes a huge amount of time and is impractical for all but the most valuable code.

The solution is to “learn” which compiler flags best suit each type of source code and store this information in a database. The compiler can then simply look up the characteristics of each program in order to determine the best optimization flags to use. Embecosm’s MAGEEC is the first commercially robust implementation of such a machine learning system and is available for both GCC and LLVM compilers. Furthermore, it doesn’t just select the best optimization flags for each source file — it selects the best flags for each function.

  • Automatically select the best optimization flags for your code
  • Separate flags selected for each function
  • Optimize for code speed, size or energy-efficiency
  • Available today for LLVM and GCC, and extensible to other compilers

Technical Details

All modern compilers have hundreds of optimization passes, and most of those passes have tuning parameters that can adjust their precise behavior. The general purpose optimization levels -01, -O2 and -Os etc. are collections of those optimizations that have proved generally effective across a wide range of programs.

But these are general purpose flags. For any individual program, the use of these flags will not give the best possible optimization.

More details