If code compiled with -g0 is faster than -g3? - performance

I'm only using my program for myself.
Should I just always compile it with -g3 (Debug max) because sometimes I need to debug it?
If with -g0 (Debug none) my program executes faster comparing to -g3 (Debug max)?

About the -gLEVEL (from gcc manual):
Request debugging information and also use level to specify how much information. The default level is 2.
Level 0 produces no debug information at all. Thus, -g0 negates -g.
Level 1 produces minimal information, enough for making backtraces in parts of the program that you don't plan to debug. This includes descriptions of functions and external variables, and line number tables, but no information about local variables.
Level 3 includes extra information, such as all the macro definitions present in the program. Some debuggers support macro expansion when you use -g3.
So the difference between -g0 and -g3 is that with level 0 you don't get debugging symbols, with level 3 you get a lot of symbols.
Anyway the debug symbols are located in totally different sections from the code/data sections. You can check with objdump (and you can read How do debug symbols affect performance of a Linux executable compiled by GCC?).
Strictly speaking with -g0 you shouldn't get a faster program BUT with many debug symbols program load time can be longer.
It's interesting to note that for GCC the presence of debug symbols (-g) and the optimization level (e.g. -O2) are orthogonal, you can use -g -O2 without losing compiler optimization (you just get less useful debug info because optimized code less closely resembles the original source code).
I'd also consider the -Og optimization level (introduced with GCC 4.8):
A new general optimization level, -Og, has been introduced. It
addresses the need for fast compilation and a superior debugging
experience while providing a reasonable level of runtime performance.
Overall experience for development should be better than the default
optimization level -O0.

Related

Compile with different optimzation levels for different parts of a code

I understand that compiling a code with different optimization levels, e.g. -O2 and -O3, can generate different assembly code and consequently cause different performance. For a large project, we often have several third-party libraries, which are often well tested. We normally choose the Release type to build the dependencies, i.e. with -O3 flag. For our newly developed parts, we could use other optimization levels instead of -O3 for debugging and testing reasons. I am wondering if there are other implications for using different optimzation levels on different parts of a code besides the downgrade of performance.
In terms of correctness, there shouldn't be. It would be a compiler bug if an optimization level changed the ABI.
It's 100% normal to make test/debug builds at -O0 or -Og and link against optimized libraries (including but not limited to system libraries like libc).
You only need -O3 -flto -march=native etc. etc. when testing / optimizing performance.

Performance overhead with "-g" (debug) flag of GCC? [duplicate]

I'm compiling a program with -O3 for performance and -g for debug symbols (in case of crash I can use the core dump). One thing bothers me a lot, does the -g option results in a performance penalty? When I look on the output of the compilation with and without -g, I see that the output without -g is 80% smaller than the output of the compilation with -g. If the extra space goes for the debug symbols, I don't care about it (I guess) since this part is not used during runtime. But if for each instruction in the compilation output without -g I need to do 4 more instructions in the compilation output with -g than I certainly prefer to stop using -g option even at the cost of not being able to process core dumps.
How to know the size of the debug symbols section inside the program and in general does compilation with -g creates a program which runs slower than the same code compiled without -g?
Citing from the gcc documentation
GCC allows you to use -g with -O. The shortcuts taken by optimized
code may occasionally produce surprising results: some variables you
declared may not exist at all; flow of control may briefly move where
you did not expect it; some statements may not be executed because
they compute constant results or their values are already at hand;
some statements may execute in different places because they have been
moved out of loops.
that means:
I will insert debugging symbols for you but I won't try to retain them if an optimization pass screws them out, you'll have to deal with that
Debugging symbols aren't written into the code but into another section called "debug section" which isn't even loaded at runtime (only by a debugger). That means: no code changes. You shouldn't notice any performance difference in code execution speed but you might experience some slowness if the loader needs to deal with the larger binary or if it takes into account the increased binary size somehow. You will probably have to benchmark the app yourself to be 100% sure in your specific case.
Notice that there's also another option from gcc 4.8:
-Og
Optimize debugging experience. -Og enables optimizations that do not interfere with debugging. It should be the optimization level of choice for the standard edit-compile-debug cycle, offering a reasonable level of optimization while maintaining fast compilation and a good debugging experience.
This flag will impact performance because it will disable any optimization pass that would interfere with debugging infos.
Finally, it might even happen that some optimizations are better suited to a specific architecture rather than another one and unless instructed to do so for your specific processor (see march/mtune options for your architecture), in O3 gcc will do its best for a generic architecture. That means you might even experience O3 being slower than O2 in some contrived scenarios. "Best-effort" doesn't always mean "the best available".

gcc -Og flag is optimizing out variables set by inline calls [duplicate]

When I compile my C++ program with g++ using the -Og option I see variables that are <optimized out>, and also the current line sometimes skips around. Is this behaviour expected at this optimization level, or do I have some problem? The man page of gcc says:
-Og Optimize debugging experience. -Og enables optimizations that do not interfere with debugging. It should be the optimization level of choice for the standard edit-compile-debug cycle, offering a reasonable level of optimization while maintaining fast compilation and a good debugging experience.
hence I did not expect this behaviour. On my system I have g++ version 4.9.2 and gdb version 7.7.1.
This is normal behaviour when compiling with the -Og option. At this optimisation level the compiler is allowed to optimize as long as it adheres to the as-if rule. This could include the removal of variables (or conversion to constants), as well as the dropping of unused functions.
The recommendation is either to get used to the skipping or to compile with the -O0option.

GCC optimization levels. Which is better?

I am focusing on the CPU/memory consumption of compiled programs by GCC.
Executing code compiled with O3 is it always so greedy in term of resources ?
Is there any scientific reference or specification that shows the difference of Mem/cpu consumption of different levels?
People working on this problem often focus on the impact of these optimizations on the execution time, compiled code size, energy. However, I can't find too much work talking about resource consumption (by enabling optimizations).
Thanks in advance.
No, there is no absolute way, because optimization in compilers is an art (and is even not well defined, and might be undecidable or intractable).
But some guidelines first:
be sure that your program is correct and has no bugs before optimizing anything, so do debug and test your program
have well designed test cases and representative benchmarks (see this).
be sure that your program has no undefined behavior (and this is tricky, see this), since GCC will optimize strangely (but very often correctly, according to C99 or C11 standards) if you have UB in your code; use the -fsanitize=style options (and gdb and valgrind ....) during debugging phase.
profile your code (on various benchmarks), in particular to find out what parts are worth optimization efforts; often (but not always) most of the CPU time happens in a small fraction of the code (rule of thumb: 80% of time spent in 20% of code; on some applications like the gcc compiler this is not true, check with gcc -ftime-report to ask gcc to show time spent in various compiler modules).... Most of the time "premature optimization is the root of all evil" (but there are exceptions to this aphorism).
improve your source code (e.g. use carefully and correctly restrict and const, add some pragmas or function or variable attributes, perhaps use wisely some GCC builtins __builtin_expect, __builtin_prefetch -see this-, __builtin_unreachable...)
use a recent compiler. Current version (october 2015) of GCC is 5.2 (and GCC 8 in june 2018) and continuous progress on optimization is made ; you might consider compiling GCC from its source code to have a recent version.
enable all warnings (gcc -Wall -Wextra) in the compiler, and try hard to avoid all of them; some warnings may appear only when you ask for optimization (e.g. with -O2)
Usually, compile with -O2 -march=native (or perhaps -mtune=native, I assume that you are not cross-compiling, if you do add the good -march option ...) and benchmark your program with that
Consider link-time optimization by compiling and linking with -flto and the same optimization flags. E.g., put CC= gcc -flto -O2 -march=native in your Makefile (then remove -O2 -mtune=native from your CFLAGS there)...
Try also -O3 -march=native, usually (but not always, you might sometimes has slightly faster code with -O2 than with -O3 but this is uncommon) you might get a tiny improvement over -O2
If you want to optimize the generated program size, use -Os instead of -O2 or -O3; more generally, don't forget to read the section Options That Control Optimization of the documentation. I guess that both -O2 and -Os would optimize the stack usage (which is very related to memory consumption). And some GCC optimizations are able to avoid malloc (which is related to heap memory consumption).
you might consider profile-guided optimizations, -fprofile-generate, -fprofile-use, -fauto-profile options
dive into the documentation of GCC, it has numerous optimization & code generation arguments (e.g. -ffast-math, -Ofast ...) and parameters and you could spend months trying some more of them; beware that some of them are not strictly C standard conforming!
recent GCC and Clang can emit DWARF debug information (somehow "approximate" if strong optimizations have been applied) even when optimizing, so passing both -O2 and -g could be worthwhile (you still would be able, with some pain, to use the gdb debugger on optimized executable)
if you have a lot of time to spend (weeks or months), you might customize GCC using MELT (or some other plugin) to add your own new (application-specific) optimization passes; but this is difficult (you'll need to understand GCC internal representations and organization) and probably rarely worthwhile, except in very specific cases (those when you can justify spending months of your time for improving optimization)
you might want to understand the stack usage of your program, so use -fstack-usage
you might want to understand the emitted assembler code, use -S -fverbose-asm in addition of optimization flags (and look into the produced .s assembler file)
you might want to understand the internal working of GCC, use various -fdump-* flags (you'll get hundred of dump files!).
Of course the above todo list should be used in an iterative and agile fashion.
For memory leaks bugs, consider valgrind and several -fsanitize= debugging options. Read also about garbage collection (and the GC handbook), notably Boehm's conservative garbage collector, and about compile-time garbage collection techniques.
Read about the MILEPOST project in GCC.
Consider also OpenMP, OpenCL, MPI, multi-threading, etc... Notice that parallelization is a difficult art.
Notice that even GCC developers are often unable to predict the effect (on CPU time of the produced binary) of such and such optimization. Somehow optimization is a black art.
Perhaps gcc-help#gcc.gnu.org might be a good place to ask more specific & precise and focused questions about optimizations in GCC
You could also contact me on basileatstarynkevitchdotnet with a more focused question... (and mention the URL of your original question)
For scientific papers on optimizations, you'll find lots of them. Start with ACM TOPLAS, ACM TACO etc... Search for iterative compiler optimization etc.... And define better what resources you want to optimize for (memory consumption means next to nothing....).

What flags do you set for your GFORTRAN debugger/compiler to catch faulty code?

I think I won't find that in any textbook, because answering this takes experience.
I am currently in the stage of testing/validating my code / hunting bugs to get it into production state and any errors would lead to many people suffering e.g. the dark side.
What kind of flags do you set when you compile your program for Fortran for debugging purposes?
What kind of flags do you set for the production system?
What do you do before you deploy?
The production version uses ifort as a compiler, yet I do my testing with gfortran. Am I doing it wrong?
Bare minimum
-Og/-O0
-O0 basically tells the compiler to make no optimisations. Optimiser can remove some local variables, merge some code blocks, etc. and as an outcome it can make debugging unpredictable. The price for -O0 option is very slow code execution, but starting from version 4.8 GCC compilers (including the Fortran one) accept a newly introduced optimisation level -Og:
-Og
Optimize debugging experience. -Og enables optimizations that do not interfere with debugging. It should be the optimization level of choice for the standard edit-compile-debug cycle, offering a reasonable level of optimization while maintaining fast compilation and a good debugging experience.
So, if possible use -Og, otherwise use -O0.
-g
This option actually makes debugging possible by requesting the compiler to produce debugging information intended to be used by interactive debugger (GDB).
Addititonal
There are a plenty of them. The most useful in my opinion are:
-Wall to "enable all the warnings about constructions that some users consider questionable, and that are easy to avoid (or modify to prevent the warning), even in conjunction with macros."
-Wextra to "enable some extra warning flags that are not enabled by -Wall."
-pedantic to generate warnings about language features that are supported by gfortran but are not part of the official Fortran 95 standard. It possible to be even more "pedantic" and use -std=f95 flag for warnings to become errors.
-fimplicit-none to "specify that no implicit typing is allowed, unless overridden by explicit IMPLICIT statements. This is the equivalent of adding implicit none to the start of every procedure."
-fcheck=all to "enable run-time tests", such as, for instance, array bounds checks.
-fbacktrace to "specify that, when a runtime error is encountered or a deadly signal is emitted (segmentation fault, illegal instruction, bus error or floating-point exception), the Fortran runtime library should output a backtrace of the error."
For debugging I use: -O2 -fimplicit-none -Wall -Wline-truncation -Wcharacter-truncation -Wsurprising -Waliasing -Wimplicit-interface -Wunused-parameter -fwhole-file -fcheck=all -std=f2008 -pedantic -fbacktrace. For ones not already explained, check the gfortran manual. -fcheck=all includes -fcheck=bounds.
For production I use: -O3 -march=native -fimplicit-none -Wall -Wline-truncation -fwhole-file -std=f2008. Runtime checks such as bounds checking increase the execution time of the resulting executable. I've found the cost to frequently be surprisingly low, but it can be high. Hence no runtime checks on compiles for production.

Resources