llvm-llc生成riscv64文件并测试的流程
环境:
- Ubuntu 18.04、Qemu 5.4、LLVM 11.0、GNU交叉编译工具链
- Benchmark:Mibench:http://vhosts.eecs.umich.edu/mibench/
- 配置过程见https://blog.csdn.net/qq_40037872/article/details/114667480
在上述环境下,修改LLVM中Codegen面相RISC-V的源码,并测试的流程如下:
-
在Clion下修改LLVM源码并build
-
找到测试文件——这里用Mibbench中的basicmath作为例子
-
cd ~/llvmwd/Mibench/automotive/basicmath
-
-
使用clang生成LLVM IR(.ll文件)
-
clang -O3 -emit-llvm --target=riscv64-unknown-linux-gnu --gcc-toolchain=/opt/riscv64 --sysroot=/opt/riscv64/sysroot/ -S *.c
-
-
使用llc命令生成汇编文件
-
llc -O3 --march=riscv64 --enable-machine-outliner=always basicmath_small.ll llc -O3 --march=riscv64 --enable-machine-outliner=always basicmath_large.ll llc -O3 --march=riscv64 --enable-machine-outliner=always cubic.ll llc -O3 --march=riscv64 --enable-machine-outliner=always rad2deg.ll llc -O3 --march=riscv64 --enable-machine-outliner=always isqrt.ll
-
-
使用clang命令进行汇编和链接
-
clang -O3 --target=riscv64-unknown-linux-gnu --gcc-toolchain=/opt/riscv64 --sysroot=/opt/riscv64/sysroot/ --static basicmath_large.s cubic.s isqrt.s rad2deg.s -lm
-
-
打开risc64-linux文件夹并挂载文件系统到rootfs
-
sudo mount -o loop rootfs.img ~/riscv64-linux/rootfs
-
-
将生成的可执行文件复制到文件系统中
-
sudo cp ~/llvmwd/Mibench/automotive/basicmath/basicmath rootfs/root/
-
-
取消挂载
-
sudo umount rootfs
-
-
启动qemu-riscv64并执行
-
qemu-system-riscv64 -M virt -m 256M -nographic -kernel linux/arch/riscv/boot/Image -drive file=rootfs.img,format=raw,id=hd0 -device v irtio-blk-device,drive=hd0 -append "root=/dev/vda rw console=ttyS0" ./root/basicmath
-
关闭qemu
-
poweroff