GCC and GIMPLE
One of the
very first thing GCC asks the GSoC applicants to do, even before writing the application, is to try various different debugging techniques using GCC. I was personally familiar with the basic, compile-with-g-flag-and-use-gdb method. Turns out, there's more: GIMPLE.
A Simple but Non-trivial Program
Problem Description
The instruction asks to compile a simple but non-trivial program with some flags that generates debugging information:
-O3 -S -fdump-tree-all -fdump-ipa-all -fdump-rtl-all
. Because I was keep reading on ways to debug GCC just prior to the statement, I immediately thought "GCC" and tried
make -j8 CXXFLAGS="-O3 -S -fdump-tree-all -fdump-ipa-all -fdump-rtl-all"
. This was a mistake: turns out, GCC can't be compiled with those flags. Thankfully, GCC developers have a very active IRC channel for me to signal SOS.
Resolution
jakub
and
segher
were quick to respond to my call for help.
jakub: it isn't meant that you should build gcc with those flags, you should pick some source and compile that with the newly built gcc with those flags
jakub: and look at the files it produces
jakub: the dumps show you what gcc has been doing in each pass, so e.g. when one is looking at a wrong-code bug, one can look at which dump shows the bad code first and debug the corresponding pass
jakub: another common method (at least recently) is, if looking e.g. at a wrong-code regression where some older gcc version worked fine and current doesn't
jakub: to bisect which gcc commit changed the behavior and from diffing the dumps with gcc immediately before that change and after find out what changed and try to understand why
segher:where "recently" is ten or so years :-)
segher:(but diffing dump files isn't great still)
So, the above flags provide even more depth of understanding on what is happenning from the compiler's perspective. Digging around in
GCC Developer Options documentation and the gcc
man
output, I found what some of the flags were for:
-S
: Stop after the stage of compilation proper; do not assemble
-fdump-tree-all
: Control the dumping at various stages of processing the intermediate language tree to a file. In this case, all stages.
-fdump-ipa-all
: Control the dumping at various stages of inter-procedural analysis language tree to a file. In this case, all inter-procedural analysis dump.
-fdump-rtl-all
: Says to make debugging dumps during compilation, too big a list to repeat it here
This adds a whole new depth of information I hadn't imagined before. I dusted out my old assignments from OpenMP class and decided to give it a spin.
Dusting out my old assignments
The assignment was a simple affair, comparing efficiencies of various different features of OpenMP addressing one particular problem: reduction. I decided to take a look particularly at worksharing example: it had some single-thread operations as well as several different OpenMP operations, and I hoped that would give me a glimps at various different formation of output. Since my ultimate goal would be to work on OMPD, brushing up on OpenMP context seemed logical. My source code was all in the solitary
w4.work-sharing.cpp
file, so I issued the compilation command:
g++ -std=c++17 -fopenmp -O3 -S -fdump-tree-all -fdump-ipa-all -fdump-rtl-all w4.work-sharing.cpp
. No errors. Instead, I ended up with 225 dump files, written in an intermediate code language called GIMPLE.
GIMPLE
GIMPLE, as defined in
GIMPLE documentation, is "a three-address representation derived from GENERIC by breaking down GENERIC expressions into tuples of no more than 3 operands (with some exceptions like function calls)." My shallow understanding and assumption is that GIMPLE is language and architecture independent, which sounds similar to Java bytecode idea, although the latter is probably very dependent to JVM as the target architech. It is also here where many of the optimization takes place. Since GIMPLE is intermediary code to all languages supported by GCC, and to all architecture, this means same optimization done on GIMPLE would affect all languages and architectures.
Files of interests
I could not dare read all 255 files. Perhaps some day, but it would have overwhelmed me. Besides, it seems like each file is evolution from another, making them look very similar to each other with specific tweaks applied on each step. That said, I was immediately attracted to
.gimple
, which seems to be the point where the code was translated to GIMPLE, as well as
.omplower
that has lower level GIMPLE methods specific to OpenMP, and
.ompexp
and
.ompexpssa2
, each with different optimization. More studying to do.
Going forward
I am going to learn more about GIMPLE, and try to understand the OpenMP portion of it more in-depth. I should also start reading up on OMPD documentation to find some correlation to link the two projects together. This is exciting for me, and I can't wait to take the next step.
Amazing!
ReplyDelete