Before we start...
If you have just stumbled upon my SPO600 series of blog posts, it has been created to document and share my learnings as I progress through my Software Portability and Optimization college course.
As I continue studying the ins and outs of the GCC compiler, I want to share a bit about the debug dumps generated during its compilation passes.
- See how I build GCC from source: Building GCC from Source on AArch64
- Read about
-fdump
options in the docs: 3.18 GCC Developer Options
Debug dumps in GCC
Dumps are the diagnostic output files generated at various stages of the compilation process, showing different intermediate code representations. They provide insights into how the compiler transforms and optimizes the source code, and are useful for debugging the compilation passes.
Intermediate representations serve as intermediaries between source code and machine code. The main intermediate representations in GCC are GIMPLE and RTL (Register Transfer Language).
Producing & reviewing the debug dumps
Diagnostic dumps can be generated using the -fdump
feature flags. To test them out I used a simple hello.c
file:
#include <stdio.h>
int main() {
printf("Hello World!\n");
}
Tree dumps
These represent GIMPLE (which takes the form of the tree) at various compilation levels. GIMPLE is used for target and language independent optimizations, such as inlining and redundancy eliminations.
-fdump-tree-all
gcc -fdump-tree-all hello.c -o hello
Using this flag during compilation will produce all available tree dumps for each compilation pass.
-fdump-tree-pass
This flag generates a tree dump for a specific compilation pass. For instance, if we run the command below, we will only get the diagnostic dump of the
original
pass.gcc -fdump-tree-original hello.c -o hello
We can open the debug dump in a readable form by running:
less hello.c.005t.original
In the dump's filename:
005
is the number of the pass,t
indicates that it is a transform pass, andoriginal
is the pass name.We can see that the source code hasn't changed much, as
original
acts as a baseline before any optimizations are applied.
RTL dumps
These are representations of Register Transfer Language (RTL), they help analyze how the compiler manages the registers and enables the final code generation by translating high-level code into machine-level instructions.
-fdump-rtl-all
gcc -fdump-rtl-all hello.c -o hello
Using this flag during compilation will produce diagnostic output files of all compilation passes that generate RTL representations.
-fdump-rtl-pass
This flag generates an RTL dump for a specific compilation pass. The command below will produce a diagnostic dump for the
reginfo
pass.gcc -fdump-rtl-reginfo hello.c -o hello
Looking at the dump, we can see that the
reginfo
pass results in low-level code that provides detailed information about register allocation and usage.
Afterthoughts
Producing and reviewing diagnostic dumps has enhanced my understanding of intermediate representations and their role in the compilation process + Getting to know more about how the compiler functions has been very interesting. However, I would still say that my understanding is mostly high-level. To learn more about what is happening under the hood, I plan to look into how some of these passes are implemented.