update details
- update branch of LLVM
- change
-march
fromrv64imaczca0p50
intorv64imaczce0p50
- How to compile with Zce compile switchs.
- Specify the arch/abi as
rv64imac
/lp64
when build gnu toolchain. because default isrv64imafdc
/lp64d
- 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 areDebug
,Release
,RelWithDebInfo
, andMinSizeRel
. Default isDebug
.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.
update details
1. Specify the arch/abi as 'rv64imac'/'lp64' when build gnu toolchain. because default is `rv64imafdc`/'lp64d'
2. motify c files
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
Exactly, Thanks
./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 .....
oh, that's my fault. Fix it.
Hi, 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,
ok