Understanding Compiler Passes

Contributing to the GCC compiler requires that I understand the compilation process and the many source code passes it makes in order to produce the final executable.

I only have a basic idea about the compilation process, so it was quite tricky for me to understand how compilation passes work. Our professor directed us to try out the -fdump-tree-all and -fdump-rtl-all compiler options, so I did just that.

From what I understand so far (and with the help of ChatGPT) the source code goes through a lot of compilation passes before it turns into the fully compiled executable.

We can use the -fdump-tree-all option to see the intermediate representation (IR) of our code during each compilation pass. We can also use -fdump-tree-<pass> to get just the singular dump of a pass

let's take this simple "Hello World" program in C for example.

#include <stdio.h>

int main() {
    printf("Hello World!\n");
}

and compile it with gcc using the -fdump-tree-all option

$ gcc -fdump-tree-all -o hello hello.c

we get all these files in the format of <filename>.<pass num>t.<pass>. Each file shows the IR of our source code during each compilation pass; mainly in the GIMPLE format. These passes occur in ascending order of t.

$ ls -la
total 140
drwxr-xr-x. 2 jmendoza23 jmendoza23   4096 Nov  3 07:34 .
drwxr-xr-x. 7 jmendoza23 jmendoza23   4096 Nov  3 07:21 ..
-rwxr-xr-x. 1 jmendoza23 jmendoza23 202376 Nov  3 07:34 hello
-rw-r--r--. 1 jmendoza23 jmendoza23     69 Nov  3 07:17 hello.c
-rw-r--r--. 1 jmendoza23 jmendoza23    124 Nov  3 07:34 hello.c.005t.original
-rw-r--r--. 1 jmendoza23 jmendoza23    103 Nov  3 07:34 hello.c.006t.gimple
-rw-r--r--. 1 jmendoza23 jmendoza23    188 Nov  3 07:34 hello.c.009t.omplower
-rw-r--r--. 1 jmendoza23 jmendoza23    217 Nov  3 07:34 hello.c.010t.lower
-rw-r--r--. 1 jmendoza23 jmendoza23    217 Nov  3 07:34 hello.c.013t.eh
-rw-r--r--. 1 jmendoza23 jmendoza23    345 Nov  3 07:34 hello.c.015t.cfg
-rw-r--r--. 1 jmendoza23 jmendoza23    218 Nov  3 07:34 hello.c.017t.ompexp
-rw-r--r--. 1 jmendoza23 jmendoza23    218 Nov  3 07:34 hello.c.021t.fixup_cfg1
-rw-r--r--. 1 jmendoza23 jmendoza23    220 Nov  3 07:34 hello.c.022t.ssa
-rw-r--r--. 1 jmendoza23 jmendoza23    220 Nov  3 07:34 hello.c.023t.walloca1
-rw-r--r--. 1 jmendoza23 jmendoza23    436 Nov  3 07:34 hello.c.026t.waccess1
-rw-r--r--. 1 jmendoza23 jmendoza23    220 Nov  3 07:34 hello.c.030t.fixup_cfg2
-rw-r--r--. 1 jmendoza23 jmendoza23    624 Nov  3 07:34 hello.c.031t.local-fnsummary1
-rw-r--r--. 1 jmendoza23 jmendoza23    220 Nov  3 07:34 hello.c.032t.einline
-rw-r--r--. 1 jmendoza23 jmendoza23      0 Nov  3 07:34 hello.c.052t.profile_estimate
-rw-r--r--. 1 jmendoza23 jmendoza23    261 Nov  3 07:34 hello.c.056t.release_ssa
-rw-r--r--. 1 jmendoza23 jmendoza23    624 Nov  3 07:34 hello.c.057t.local-fnsummary2
-rw-r--r--. 1 jmendoza23 jmendoza23    220 Nov  3 07:34 hello.c.098t.fixup_cfg3
-rw-r--r--. 1 jmendoza23 jmendoza23    220 Nov  3 07:34 hello.c.105t.adjust_alignment
-rw-r--r--. 1 jmendoza23 jmendoza23    220 Nov  3 07:34 hello.c.251t.veclower
-rw-r--r--. 1 jmendoza23 jmendoza23    220 Nov  3 07:34 hello.c.252t.cplxlower0
-rw-r--r--. 1 jmendoza23 jmendoza23    220 Nov  3 07:34 hello.c.253t.bitintlower0
-rw-r--r--. 1 jmendoza23 jmendoza23    220 Nov  3 07:34 hello.c.255t.switchlower_O0
-rw-r--r--. 1 jmendoza23 jmendoza23    220 Nov  3 07:34 hello.c.263t.isel
-rw-r--r--. 1 jmendoza23 jmendoza23    436 Nov  3 07:34 hello.c.266t.waccess3
-rw-r--r--. 1 jmendoza23 jmendoza23    220 Nov  3 07:34 hello.c.267t.optimized
-rw-r--r--. 1 jmendoza23 jmendoza23      0 Nov  3 07:34 hello.c.364t.statistics
-rw-r--r--. 1 jmendoza23 jmendoza23      0 Nov  3 07:34 hello.c.365t.earlydebug
-rw-r--r--. 1 jmendoza23 jmendoza23      0 Nov  3 07:34 hello.c.366t.debug
-rw-r--r--. 1 jmendoza23 jmendoza23    485 Nov  3 07:17 Makefile

If we want to see the passes that affect the Register Transfer Language (RTL) representation of our code, we can pass the option -fdump-rtl-all or -fdump-rtl-<pass> to dump just a specific pass.

This gives us an output similar to -fdump-tree-all but for the RTL passes of our code. Passes also occur in ascending order of r.

$ gcc -fdump-rtl-all -o hello hello.c
$ ls -la
total 184
drwxr-xr-x. 2 jmendoza23 jmendoza23   4096 Nov  3 08:38 .
drwxr-xr-x. 7 jmendoza23 jmendoza23   4096 Nov  3 07:21 ..
-rwxr-xr-x. 1 jmendoza23 jmendoza23 202376 Nov  3 08:38 hello
-rw-r--r--. 1 jmendoza23 jmendoza23     69 Nov  3 07:21 hello.c
-rw-r--r--. 1 jmendoza23 jmendoza23   2078 Nov  3 08:38 hello.c.268r.expand
-rw-r--r--. 1 jmendoza23 jmendoza23   1705 Nov  3 08:38 hello.c.269r.vregs
-rw-r--r--. 1 jmendoza23 jmendoza23   1706 Nov  3 08:38 hello.c.270r.into_cfglayout
-rw-r--r--. 1 jmendoza23 jmendoza23   3325 Nov  3 08:38 hello.c.271r.jump
-rw-r--r--. 1 jmendoza23 jmendoza23   1674 Nov  3 08:38 hello.c.283r.reginfo
-rw-r--r--. 1 jmendoza23 jmendoza23   1733 Nov  3 08:38 hello.c.306r.outof_cfglayout
-rw-r--r--. 1 jmendoza23 jmendoza23   1705 Nov  3 08:38 hello.c.307r.split1
-rw-r--r--. 1 jmendoza23 jmendoza23   3591 Nov  3 08:38 hello.c.309r.dfinit
-rw-r--r--. 1 jmendoza23 jmendoza23   3627 Nov  3 08:38 hello.c.310r.mode_sw
-rw-r--r--. 1 jmendoza23 jmendoza23   3545 Nov  3 08:38 hello.c.311r.asmcons
-rw-r--r--. 1 jmendoza23 jmendoza23   5769 Nov  3 08:38 hello.c.318r.ira
-rw-r--r--. 1 jmendoza23 jmendoza23   8449 Nov  3 08:38 hello.c.319r.reload
-rw-r--r--. 1 jmendoza23 jmendoza23   5411 Nov  3 08:38 hello.c.326r.pro_and_epilogue
-rw-r--r--. 1 jmendoza23 jmendoza23   4954 Nov  3 08:38 hello.c.329r.jump2
-rw-r--r--. 1 jmendoza23 jmendoza23   4922 Nov  3 08:38 hello.c.349r.zero_call_used_regs
-rw-r--r--. 1 jmendoza23 jmendoza23   4922 Nov  3 08:38 hello.c.350r.alignments
-rw-r--r--. 1 jmendoza23 jmendoza23   4892 Nov  3 08:38 hello.c.354r.barriers
-rw-r--r--. 1 jmendoza23 jmendoza23   4892 Nov  3 08:38 hello.c.356r.split5
-rw-r--r--. 1 jmendoza23 jmendoza23   4981 Nov  3 08:38 hello.c.359r.shorten
-rw-r--r--. 1 jmendoza23 jmendoza23   4981 Nov  3 08:38 hello.c.360r.nothrow
-rw-r--r--. 1 jmendoza23 jmendoza23   5661 Nov  3 08:38 hello.c.361r.dwarf2
-rw-r--r--. 1 jmendoza23 jmendoza23   4981 Nov  3 08:38 hello.c.362r.final
-rw-r--r--. 1 jmendoza23 jmendoza23   3269 Nov  3 08:38 hello.c.363r.dfinish
-rw-r--r--. 1 jmendoza23 jmendoza23    510 Nov  3 08:12 Makefile

These options will help us understand how passes work and how we can develop custom passes in the compiler which will be crucial in our development of the new Automatic Function Multi-Versioning (AFMV) feature of GCC.

Until then, thanks for reading!

Comments

Popular posts from this blog

Lab 03 - Breakout Game

Building GCC

Lab 04 - Diving into 64-bit Assembly