Skip to main content

Debugging with GCC: GIMPLE

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.

Comments

Post a Comment

Popular posts from this blog

Creating a patch for GNU GCC using Git

Overview and Target Audience The GNU GCC project followed a blend of a traditional method with contemporary git tools when it comes to contributing code, making the experience unique from some other, git-based projects. This blog post will explore different aspects of the process, helpful commands, and various scripts that would make the experience more pleasent for new contributors. While this blog aims to help new contributors get acustomed to the GNU GCC code culture and make contributing easier, it must be stressed that this is not in any way in-depth exploration of the process. This should help you put your first foot forward; the community will help you take the rest of the steps from that point on. This post also assumes the user is in a POSIX environment (e.g. Linux, FreeBSD). Git and GNU As stated in this phoronix post , GNU GCC made a full transition to git early 2020. As of this writing, the community seems to be adjusting to the new tools. GNU GCC hosts its own git serve

Setting up IRC

About IRC Internet Relay Chat was once the most popular real-time communication method that uses computer for a large spectrum of community groups. While it has largely been supplanted by more contemporarly methods for most people, IRC is still a prefered means of communication among older software projects like GCC. The Client As an old technology that is also very favored among programmers, IRC has many clients with different flavors, from GUI to CLI to headless. As a linux user with strong attraction to tmux, I chose weechat. Depending on your distro or OS, install your client first. Configuring Weechat I will be using GCC channel in OFTC server as the example. How are we configuring this? While Weechat has a configuration file, Weechat officially advises against using the file to configure the program behavior. Instead, it promotes `/set` dialog within the client to set the proper configuration. Connect to server Let's first connect to server. The `#gcc` channel is host