Skip to main content

Command Palette

Search for a command to run...

Project Stage 2

Published
•2 min read•View as Markdown
S

Looping around thinking to write it down...

From 2 000 lines of red to a clean GCC build 🚦

“Nothing teaches you GCC internals faster than fixing your own pass.”


1 Context

  • Goal: create a GIMPLE‑level pass that prints
    NOPRUNE: for the first function with a unique structure and
    PRUNE: for every subsequent clone (same structure).

  • Directory layout

~/git/gcc/            ← GCC source (trunk)
~/gcc-build-001/      ← out‑of‑tree build
~/gcc-test-001/       ← install prefix
└─ gcc/
   ├─ tree-my-pass.cc        ← Stage 1 helper
   └─ tree-cloneprune.cc     ← Stage 2 pass

2 Initial implementation

#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "backend.h"
#undef  optimize                     // avoid options.h macro
#include "tree-pass.h"
#include "context.h"

#include "tree.h"                  // must precede gimple.h
#include "tree-core.h"
#include "function.h"
#include "basic-block.h"
#include "gimple.h"
#include "gimple-iterator.h"
#include "cgraph.h"

#include <string>
#include <unordered_map>
#include <vector>

Generate a signature by concatenating each GIMPLE opcode;


3 Compilation hell & fixes

#ScreenshotGCC errorRoot causeFix
1wall‑of‑redTREE_CODE / NULL_TREE / SSA_NAME_DEF_STMT not declaredtree.h was included after gimple.hmove #include "tree.h" above gimple.h
2optimize‑macroexpected initializer before '.' tokenbackend.h pulls options.h; macro optimize collides with pass_data#undef optimize right after #include "backend.h"
3gsi_ errorsgsi_start_bb / gsi_next not declaredforgot iterator helpersadd #include "gimple-iterator.h"

After patches, tree-cloneprune.o compiled cleanly (only the benign todo_flags_finish warning).


4 Linking the pass

# gcc/Makefile.in
OBJS = … \
        tree-my-pass.o \
        tree-cloneprune.o
// gcc/tree-pass.h
extern gimple_opt_pass *make_tree_my_pass(gcc::context *);
extern gimple_opt_pass *make_pass_cloneprune(gcc::context *);
// gcc/passes.def (late in pipeline)
NEXT_PASS(tree_my_pass)
NEXT_PASS(pass_cloneprune)

Full rebuild:

cd ~/gcc-build-001
make -j$(nproc)
make install

6 Key take‑aways

  1. Header order is sacred in GCC; tree.h must come before gimple.h.

  2. optimize macro from options.h is a silent build killer — always #undef optimize in new passes.

  3. Incremental rebuilds (make file.o) cut turn‑around from minutes to seconds.

https://github.com/samartho4/SPO-600/tree/main/Project%202