MENU

Compile a C file with Zce ext in LLVM

September 6, 2021 • 实习

update details

  1. update branch of LLVM
  2. change -march from rv64imaczca0p50 into rv64imaczce0p50
  3. How to compile with Zce compile switchs.
  4. Specify the arch/abi as rv64imac/lp64 when build gnu toolchain. because default is rv64imafdc/lp64d
  5. motify c files

Compile a C file with Zce ext in LLVM

In this article, we try to compile a C program in LLVM using RISCV architecture and Zce Extension.

The files mentioned in the article are in follow links Materials

Build GNU tool chain

Looking riscv-gnu-toolchain repo for more information

git clone https://github.com/riscv/riscv-gnu-toolchain
cd riscv-gnu-toolchain
./configure --prefix=$HOME/opt/riscv --enable-multilib --with-arch=rv64imac --with-abi=lp64
make # Newlib

Export the Env path for GNU tool chain

export PATH=$HOME/opt/riscv/bin:$PATH

Build LLVM

# clone the source code of LLVM
git clone -b riscv-zce-llvm14 https://github.com/plctlab/llvm-project.git
cd llvm-project/
mkdir build
cd build
cmake -DLLVM_TARGETS_TO_BUILD="RISCV" -DLLVM_ENABLE_PROJECTS="clang;lld;libcxx;libcxxabi" -G "Unix Makefiles"  ../llvm
make -j
  • -DCMAKE_BUILD_TYPE=type — Valid options for type are Debug, Release, RelWithDebInfo, and MinSizeRel. Default is Debug.

    Note: release builds are 2.2gb while debug builds are 15gb

import the PATH

export PATH=$HOME/llvm-project/build/bin:$PATH

Compile C program

Write C file

Write code into hello.c

int test(){
    int c = 63;
    int a = 6;
    int b = a * c * 30;
    return b;
}

#include<stdio.h>
const int GLOAL_ZCE = 1;
int fbm(int a, int b, int count){
    int res = a * b;
    printf("a --> %d, b --> %d\n", a, b);
    printf("result is %d\n", res);
    if(count>1)
        return fbm(b,res,count-1);
    else
        return res;
}

int main(int argc, char const *argv[])
{
    char x=-1;
    printf("%d\n",  x);
    int a = 1;
    int b = 2;
    int result = fbm(a,b,7) + test();
    return 0;
}

Compile

Than compile source code by following command

clang --target=riscv64 -march=rv64imaczce0p50 -menable-experimental-extensions --gcc-toolchain=$HOME/opt/riscv -o hello.o hello.c
# or
clang --target=riscv64 -march=rv64imaczce0p50 -menable-experimental-extensions --gcc-toolchain=$HOME/opt/riscv -fuse-ld=lld -mno-relax -o hello.o hello.c

the second command is use lld as linker

some instructions of Zce require M ext

We use gnu tool chain here. The parameter --gcc-toolchain should be the path of GNU tool chain you have just build.

Dump the Obj file

llvm-objdump --mattr=+experimental-zce,+m,+c -d -r hello.o

For param --mattr, we'd better keep it same as which do we used in compiling.

We can find the c.mul has been been used.

Compile with switchs

we also implement the compile switchs so that you can use only specific instructions of Zce Ext to compile or evaluate.

e.g. compile with only MULI, use following command

clang --target=riscv64 -march=rv64imac -mzce-muli -gcc-toolchain=$HOME/opt/riscv -o hello.o hello.c
# or
clang --target=riscv64 -march=rv64imac -mzce-muli --gcc-toolchain=$HOME/opt/riscv -fuse-ld=lld -mno-relax -o hello.o hello.c

Then Dump Obj file again, we find the muli while c.mul disappears.

Run on Qemu

Build Qemu

git clone -b plct-zce-dev https://github.com/plctlab/plct-qemu.git
cd plct-qemu
./configure --target-list=riscv64-linux-user
make

import the PATH

export PATH=$HOME/report/plct-qemu/build:$PATH

Run on qemu

qemu-riscv64 -cpu rv64,x-zce=true hello.o
You can comment below or email me if you have any issues.
Last Modified: December 31, 2021
Leave a Comment

7 Comments
  1. XinlongWu XinlongWu

    update details

    1. Specify the arch/abi as 'rv64imac'/'lp64' when build gnu toolchain. because default is `rv64imafdc`/'lp64d'

    2. motify c files

  2. ibrahim ibrahim

    Hi,

    It might be worth it to mention -DCMAKE_BUILD_TYPE=Release as release builds are 2.2gb while debug builds are 15gb !

    Kind regards,
    Ibrahim

    1. @ibrahimExactly, Thanks

    2. ibrahim ibrahim

      @Vincent./configure --prefix=/home/wuxinlong/opt/riscv --enable-multilib --with-arch=rv64imac --with-abi=lp64

      I think you should have the --prefix as $HOME/opt/riscv in this doc so its not specific to your machine .....

    3. @ibrahimoh, that's my fault. Fix it.

    4. ibrahim ibrahim

      @VincentHi, could you please change rv64imaczcea0p50 to rv64imaczce0p50 as that will enable all sub extensions, and we want people trying to compiler to see them all,

    5. @ibrahimok