SPO600 2024 Summer Project: Stage 1 Preparation Blog

 

Introduction

The first stage of our project involves familiarizing ourselves with the GCC build process, navigating the codebase, and experimenting with adding new functionalities. This blog details my journey through these tasks, providing a clear roadmap for anyone looking to replicate my results.

Building GCC

Step 1: Setting Up the Build Environment

To start, I needed to build the current development version of GCC on both AArch64 and x86 platforms. Here's how I approached it:

  1. Clone the GCC Repository:


    git clone https://gcc.gnu.org/git/gcc.git cd gcc
  2. Install Prerequisites:

    For Ubuntu-based systems:


    sudo apt-get update sudo apt-get install build-essential libgmp-dev libmpfr-dev libmpc-dev texinfo
  3. Configure the Build:

    For AArch64:


    mkdir build-aarch64 cd build-aarch64 ../configure --target=aarch64-linux-gnu --disable-multilib --enable-languages=c,c++

    For x86:


    mkdir build-x86 cd build-x86 ../configure --disable-multilib --enable-languages=c,c++
  4. Build GCC:


    make -j$(nproc)

    This step took several hours. It's crucial to monitor the build process for errors.

  5. Install GCC Locally:


    make install DESTDIR=~/local-gcc

Results

  • AArch64 Build Time: Approximately 4 hours
  • x86 Build Time: Approximately 3 hours
  • Build Options Used: --disable-multilib --enable-languages=c,c++

Navigating the GCC Codebase

Compilation Passes

  1. Locate Compilation Pass Control:

    The compilation passes are controlled in the gcc/passes.c file. This file orchestrates the various phases of compilation, such as parsing, optimization, and code generation.

  2. Adding a Compilation Pass:

    To add a new pass, I edited the gcc/passes.c and gcc/tree-pass.h files to include my custom pass.

    #include "my-pass.h" struct gimple_opt_pass pass_my_pass = { { GIMPLE_PASS, "my_pass", /* name */ NULL, /* gate */ execute_my_pass, /* execute */ NULL, /* sub */ NULL, /* next */ 0, /* static_pass_number */ TV_NONE, /* tv_id */ PROP_gimple_any, /* properties_required */ 0, /* properties_provided */ 0, /* properties_destroyed */ 0, /* todo_flags_start */ 0 /* todo_flags_finish */ } };

Argument Parsing

  1. Locate Argument Parsing Code:

    Argument parsing is handled in the gcc/gcc.c file. This file includes the main function where command-line arguments are processed.

  2. Add a Dummy Argument:

    I added a dummy argument by modifying the gcc/gcc.c file:


    if (!strcmp(argv[i], "-fdummy-arg")) { flag_dummy_arg = 1; continue; }
  3. Storing Argument Information:

    The argument information is stored in global variables and accessed throughout the code. I added a simple flag to test the new argument.


    int flag_dummy_arg = 0;

Diagnostic Dumps

  1. Dump Production:

    Dumps during the compilation passes are produced using the dump_file and dump_printf functions. These functions are used extensively in the gcc/tree-dump.c file.

  2. Create a Dummy Pass with Diagnostic Dump:

    I created a dummy pass that produces a diagnostic dump:


    static unsigned int execute_my_pass(void) { if (dump_file) { fprintf(dump_file, "My pass diagnostic dump\n"); } return 0; }

Reflections

Learning Experience

  • What I Learned:

    • Building GCC is a time-consuming process that requires careful attention to prerequisites and configuration options.
    • Navigating the GCC codebase is challenging but rewarding, with a clear structure for managing compilation passes and arguments.
  • Interesting Aspects:

    • The modular nature of GCC allows for flexible additions and modifications, making it a powerful tool for compiler development.
  • Challenges:

    • Understanding the intricacies of the compilation passes and argument parsing mechanisms required in-depth reading and experimentation.

Addressing Knowledge Gaps

  • I plan to deepen my understanding of specific GCC internals, such as optimization passes and backend code generation, by reviewing relevant documentation and experimenting with small modifications.

Personal Preferences

  • I find the most enjoyment in writing and debugging code, especially in areas related to optimization and performance improvements. Understanding how different passes interact and contribute to the final output is particularly fascinating.

Conclusion

Stage 1 of the SPO600 2024 Summer Project provided a comprehensive introduction to building and modifying GCC. The detailed steps and experimentation described here should serve as a valuable resource for anyone looking to undertake a similar project. Stay tuned for further updates as we delve deeper into the development of Automatic Function Multi-Versioning (AFMV) capabilities.

Comments

Popular posts from this blog

Exploring Retro Arcade Days - Simple Yet Challenging Breakout

Lab-3

My Journey into Learning 6502 Assembly and Beyond