Xinlong | 实验室 - RISCV
type: channel
link: https://wulongxin.com/index.php/tag/RISCV/
items: Compile a C file with Zce ext in LLVM

Compile a C file with Zce ext in LLVM
type: item
date: 2021-09-06T21:27:00+08:00
description: update detailsupdate branch of LLVMchange -march from rv64imaczca0p50 into rv64imaczce0p50How to compile with Zce compile switchs.Specify the arch/abi as rv64imac/lp64 when build gnu toolchain. because default is rv64imafdc/lp64dmotify c filesCompile a C file with Zce ext in LLVMIn 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 MaterialsBuild GNU tool chainLooking riscv-gnu-toolchain repo for more informationgit 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 # NewlibExport the Env path for GNU tool chainexport PATH=$HOME/opt/riscv/bin:$PATHBuild 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 15gbimport the PATHexport PATH=$HOME/llvm-project/build/bin:$PATHCompile C programWrite C fileWrite code into hello.cint 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; }CompileThan compile source code by following commandclang --target=riscv64 -march=rv64imaczce0p50 -menable-experimental-extensions --gcc-toolchain=$HOME/opt/riscv -o hello.o hello.c # or clang --target=riscv64 -march=rv64imaczce0p50 …