Project Stage 1: Create a GCC pass

 

🎬SPO600 2025 Winter Project - Project Stage 1: Create a Basic GCC Pass


SUMMARY:




A GCC pass is a step in the compilation process that processes intermediate representations (IR) of the source code, performing various optimizations and analyses before generating the final machine code. The objective of this project is to extend the GCC compiler by adding a custom pass that can analyze the functions being compiled.
一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一
💣How Is This Custom Pass Going to Work?
  1.  Iterates through all of the compiled code's functions.
  2. Outputs the name of the function under analysis.
  3. Determines how many fundamental blocks there are in the function.
  4. Determines how many GIMPLE statements are there in each function.
一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一
⭐STEP 1 =================>  VEFIRFYING DETAILS💬
  1. Lab 04 ✅
  2. GCC source and build directories were correctly set up✅
  3. Command to verify : ls  -l ~✅
一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一
⭐STEP 2 ==================> CREATING A NEW GCC PASS

"Creating the Pass File"

  1. I navigated to the Gcc directory using this command: "cd ~/gcc/gcc"
  2. Creation of the pass file. "nano tree_my_pass.cc"
⭐PURPOSE OF THIS CODE💥
    1. It defines my_pass, a new GCC pass.
    2.  Every function in the compiled code is iterated through.
    3. It counts logical code divisions, or basic blocks.
    4. GIMPLE statements (low-level IR representation) are counted.
    5. 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


    一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一
    ⭐STEP 3 ==================> REGISTER THE PASS

    "Modifying tree-pass.h"
    1. Open the header file tree-pass.h using "nano ~/gcc/gcc/tree-pass.h"
    2. Add this statement to the end of the file "extern gimple_opt_pass *make_tree_my_pass(gcc::context *ctxt);"
    3. Pass will be connected with the GCC



    "Modifying passes.def"

    1. Open the passes.def file: "nano ~/gcc/gcc/passes.def"
    2. 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.

    一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一
    ⭐STEP 4 =================> Adding object file to GCC

    1. Opening Makefile.in using this command: "nano ~/gcc/gcc/Makefile.in"
    2. Adding my file to the object (.o) files using Ctrl + W and typing "OBJS = " as a shortcut.
    3. This will ensure that my pass is included in the final build of the gcc.

    一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一
    ⭐STEP 5 =================>  NEW MAKEFILE.IN


    1. After modifying the file, i will delete the old makefile.in and recreate a new one.
    2. cd ~/gcc-build-001
    3. rm Makefile
    4. ../git/gcc/configure --prefix=$HOME/gcc-install --enable-languages=c,c++ --disable-multilib
    一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一
    ⭐STEP 6 =================> Rebuilding GCC

    1. cd ~/gcc-build-001
    2. time make -j$(nproc) |& tee rebuild.log
    3. To confrm that my pass is incuded: grep "tree_my_pass.o" rebuild.log
    4. It is failed
    一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一
    THE END👿💭







    Comments

    Popular posts from this blog

    LAB 03

    Project Stage 2: Part 2( The End)