Automatic Function Multi-Versioning for GCC
Introduction:
For this project, I will be working with the GCC compiler and learning how it works by building it from scratch on an AArch64 platform (which is 64-Bit). Setting up GCC like this lets me dig into how it compiles code and optimizes functions for different CPUs, especially as the goal is to experiment with Automatic Function Multi-Versioning (AFMV).
Guide to Building GCC for AArch64
Step 1: Cloning the Source Code
To get started, I needed the GCC source code. Cloning from the official GCC repository keeps things organized and up to date.
Here’s the command to clone it:
This creates a directory called ~/gcc
, where all the source files are stored. The cloning process might take a few minutes, depending on your internet speed.
Step 2: Setting Up a Build Directory
It’s recommended to create a separate directory for building GCC instead of doing it directly in the source directory. This keeps things organized.
Commands:
Now I’m in the gcc_build
directory, ready to start configuring the build.
Step 3: Configuring the GCC Build
Next, I set up the configuration for GCC with a custom install path. This way, I can install GCC without overwriting the system’s default version.
Command:
--target=aarch64-linux-gnu
sets GCC up to build specifically for AArch64.--prefix=$HOME/gcc-test-1
tells it to install in ~/gcc-test-1 , keeping it separate.--enable-languages=c,c++
limits it to C and C++ (just to make the build faster).
Step 4: Starting the Build Process
Building GCC is requires good amount of processing speeds, so I ran the make
command with -j
to set up parallel jobs (one job per CPU core, plus one).
Command:
Explanation:
time
tracks how long the build takes.-j 17
runs 13 jobs at once (I’m using a 12-core server).|& tee build.log
saves the output and errors to build.log, which helps if there are any issues.
- CPU: Intel Core i7-10750H
- OS: Windows 11
- Build Time: ~1.5 Hours
Step 5: Installing GCC Locally
Once the build completed, I installed GCC in the custom directory specified during configuration. Since this is a local install, there’s no need to use sudo.
Command:
This installs GCC into ~/gcc-test-1
, so I can use it without affecting any system files.
Step 6: Setting Up the Custom GCC Build
To make my terminal recognize the new GCC build as the default, I added its bin directory to the PATH.
Command:
To confirm that my system recognizes gcc, I used this command:
Reflection:
The thought of building GCC from scratch felt a bit overwhelming. I knew I’d be dealing with complex processes and that there would probably be a few challenges along the way, especially with my limited experience in setting up compilers. But once I got started, it wasn’t as daunting as it seemed, and working through each step helped me gain a much clearer understanding of how compilers actually work.
One of the most valuable parts was exploring the various dump options. These dumps offered a peek into each stage of compilation and showed me exactly how GCC optimizes and processes code.
I ran into some issues with configuring the build environment and making sure my custom GCC version was recognized by the system, but once I got it working, I could see the steps more clearly.
Comments
Post a Comment