LLVM is a widely used open-source compiler infrastructure that provides a set of modular and reusable compiler and toolchain technologies. One of the key strengths of LLVM is its extensibility, allowing developers to add support for new architectures by building backend targets. In this blog, we will walk you through the process of building the Cpu0 backend in LLVM, enabling you to generate code for the Cpu0 architecture. Let’s get started!

The Cpu0 Backend

Cpu0 is a 32-bit architecture. This is the reference of the Cpu0 backend in LLVM Tutorial: Creating an LLVM Backend for the Cpu0 Architecture

WARNING

This tutorial was based on LLVM version 3.1. It does not have support for the recent versions of LLVM.

For this reason, I decided to use the LLVM version 8.0 after making a few changes in the codebase. This repository has the code which supports the LLVM version 8.0

Building the backend
  1. Build the LLVM version 8.0

  2. Clone this repository.

  3. Run the following commands:

    cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Debug ../llvm
    ninja
    

    or just to build the Cpu0 target, run

    cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Debug -DLLVM_TARGETS_TO_BUILD=Cpu0 ../llvm
    ninja
    
  4. Then run
    cmake -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_C_COMPILER=clang -DCMAKE_BUILD_TYPE=Debug -G "Ninja" ../llvm
    ninja
    
  5. Followed by
    build/bin/llc --version
    
    TIP

    Remember to invoke the llc that we just built, not the system llc. You can check which llc is being used by running which llc. Source the built compiler path

  6. Run these commands
      export PATH=<path>:$PATH 
      export LIBRARY_PATH=<path>:$LIBRARY_PATH
      export LD_LIBRARY_PATH=<path>:$LD_LIBRARY_PATH
    
  7. Try running some test cases/examples
    build/bin/clang -target x86_64-unknown-linux-gnu -c example.c -S -emit-llvm -o example.ll
    build/bin/llc -march=cpu0 -mcpu=cpu032I -relocation-model=pic -filetype=asm example.ll -o -
    
My Week 2 progress

During the second week while building the Cpu0 backend in LLVM, I encountered some challenges related to the compatibility of LLVM versions. Here’s a breakdown of my progress and the solutions I found:

  • Attempt: LLVM Version 12 In the beginning, I attempted to build the Cpu0 backend using LLVM version 12. However, I discovered that the Cpu0 backend was not supported in this version. Despite my efforts to modify the LLVM source code, it became clear that the necessary components for the Cpu0 backend were not present.

    Solution 1: Realizing that version 12 did not support the Cpu0 backend, I decided to explore earlier versions of LLVM to find the desired compatibility.

  • Attempt 2: LLVM Version 10 Next, I tried building the Cpu0 backend using LLVM version 10. Unfortunately, I encountered the same issue as with version 12.

    Solution 2: Undeterred, I turned to the open-source community to find a solution. After some research, I came across a repository that contained an unofficial version of the Cpu0 backend for LLVM 8.

  • Attempt 3: LLVM Version 8 Using the GitHub repository as a reference, I started building the Cpu0 backend in LLVM version 8. This involved downloading the LLVM 8 source code and incorporating the necessary files from the repository.

    Solution 3: By following the instructions provided in the GitHub repository, I successfully built the Cpu0 backend in LLVM version 8.