Alwaysproblem / Toy-llvm-backend

an Toy backend RiscV example for LLVM

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Toy LLVM Backend Example For RISC-V

Yet another RISC-V LLVM Backend. This is a project for learning how LLVM backend works and what RISC-V architecture is by reimplementing the official LLVM's RISC-V backend.

This example is inspired by:

Getting started

See through code within llvm directory

  • create soft link in llvm/.vscode

    cd llvm-project/llvm/.vscode
    ln -s ../../../scripts_in_llvm/run_with_llc.sh run_with_llc.sh

Clone this repository:

git clone --recursive https://github.com/Alwaysproblem/Toy-llvm-backend

Build riscv64-unknown-linux-gnu-gcc

git clone https://github.com/riscv-collab/riscv-gnu-toolchain
cd /path/to/riscv-gnu-toolchain
configure --prefix=/opt/gcc-riscv --with-arch=rv64gc --with-abi=lp64d --enable-multilib
make linux

Build Clang

cd /path/to/Toy-llvm-backend
bash tools/build_clang.sh

Chapter 1 Registering the LLVM backend

more details

  • build and run

    bash tools/build_llc.sh -p Ch1
    ./llvm-project/llvm/build/bin/llc --version
    # LLVM (http://llvm.org/):
    #   LLVM version x.x.x
    #   DEBUG build with assertions.
    #   Default target: 
    #   Host CPU: alderlake
    
    #   Registered Targets:
    #     toy - TOY Backend for riscv (32-bit) [experimental]
  • build and run

    bash tools/clean_llvm_branch.sh # Optional
    bash tools/build_llc.sh -p Ch2
    ./llvm-project/build_clang/bin/clang --target=riscv64-unknown-gnu -march=rv32g examples/arith.c -c -emit-llvm -O0 -o arith.bc
    ./llvm-project/llvm/build/bin/llc -debug -march=toy -filetype=asm arith.bc -o arith.S
    # Args: ./build/bin/llc -debug -march=toy -filetype=asm arith.bc -o arith.S 
    # llc: /root/Desktop/dockerVolumn/Toy-llvm-backend/llvm-project/llvm/lib/CodeGen/LLVMTargetMachine.cpp:42: void llvm::LLVMTargetMachine::initAsmInfo(): Assertion 'MRI && "Unable to create reg info"' failed.
    # ...

you can apply the Ch3 patch to the llvm-project folder and see through the changes.

bash tools/apply_patch.sh -p Ch3

Instruction Set

About

an Toy backend RiscV example for LLVM

License:MIT License


Languages

Language:Shell 100.0%