Project Stage 1: Create a GCC pass
🎬SPO600 2025 Winter Project - Project Stage 1: Create a Basic GCC Pass
SUMMARY:
- Iterates through all of the compiled code's functions.
- Outputs the name of the function under analysis.
- Determines how many fundamental blocks there are in the function.
- Determines how many GIMPLE statements are there in each function.
- Lab 04 ✅
- GCC source and build directories were correctly set up✅
- Command to verify : ls -l ~✅
- I navigated to the Gcc directory using this command: "cd ~/gcc/gcc"
- Creation of the pass file. "nano tree_my_pass.cc"
- It defines my_pass, a new GCC pass.
- Every function in the compiled code is iterated through.
- It counts logical code divisions, or basic blocks.
- GIMPLE statements (low-level IR representation) are counted.
- This data is printed to the console.
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "backend.h"
#include "tree-pass.h"
#include "pass_manager.h"
#include "context.h"
#include "diagnostic-core.h"
#include "tree.h"
#include "tree-core.h"
#include "basic-block.h"
#include "gimple.h"
#include "gimple-iterator.h"
namespace {
const pass_data my_pass_data = {
GIMPLE_PASS,
"tree-my-pass",
OPTGROUP_NONE,
TV_TREE_OPS,
0, 0, 0, 0
};
class my_pass : public gimple_opt_pass {
public:
my_pass(gcc::context *ctxt) : gimple_opt_pass(my_pass_data, ctxt) {}
bool gate(function *fun) override {
return true;
}
unsigned int execute(function *fun) override {
fprintf(stderr, "Processing function: %s\n", function_name(fun));
int basic_block_count = 0;
basic_block bb;
FOR_EACH_BB_FN(bb, fun) {
basic_block_count++;
}
fprintf(stderr, "Number of basic blocks: %d\n", basic_block_count);
int gimple_stmt_count = 0;
FOR_EACH_BB_FN(bb, fun) {
for (gimple_stmt_iterator gsi = gsi_start_bb(bb);
!gsi_end_p(gsi);
gsi_next(&gsi)) {
gimple_stmt_count++;
}
}
fprintf(stderr, "Number of GIMPLE statements: %d\n", gimple_stmt_count);
return 0;
}
};
gimple_opt_pass *make_tree_my_pass(gcc::context *ctxt) {
return new my_pass(ctxt);
}
} // namespace
- Open the header file tree-pass.h using "nano ~/gcc/gcc/tree-pass.h"
- Add this statement to the end of the file "extern gimple_opt_pass *make_tree_my_pass(gcc::context *ctxt);"
- Pass will be connected with the GCC
- Open the passes.def file: "nano ~/gcc/gcc/passes.def"
- I edited passes.def and inserted NEXT_PASS(make_tree_my_pass); to include my custom pass in the compilation pipeline after making sure the function was declared in the header. My pass will execute throughout the compilation process thanks to this step.
- Opening Makefile.in using this command: "nano ~/gcc/gcc/Makefile.in"
- Adding my file to the object (.o) files using Ctrl + W and typing "OBJS = " as a shortcut.
- This will ensure that my pass is included in the final build of the gcc.
- After modifying the file, i will delete the old makefile.in and recreate a new one.
- cd ~/gcc-build-001
- rm Makefile
- ../git/gcc/configure --prefix=$HOME/gcc-install --enable-languages=c,c++ --disable-multilib
- cd ~/gcc-build-001
- time make -j$(nproc) |& tee rebuild.log
- To confrm that my pass is incuded: grep "tree_my_pass.o" rebuild.log
- It is failed
Comments
Post a Comment