Update(s):
- Jan 16th 2022 adding section on Objdump
RISC-V Vector Extension (RVV) has recently been ratified in its version 1.0 (announcement and specification pdf). The 1.0 milestone is key, it means RVV maturity has reached a stable state: numerous commercial and free implementation of the standard are appearing and software developers can now dedicate significant effort to port and develop library on top of RVV without fear of seeing the specification rug being pulled under their feet. In this article we will review how to build a version of the clang compiler compatible with RVV (v0.10) and to develop, build and execute our first RVV program.
Building the compiler
# update to the your intended install directory export RISCV=~/RISCV-TOOLS/ # downloading basic riscv gnu toolchain, providing: # - runtime environement for riscv64-unknown-elf (libc, ...) # - spike simulator git clone https://github.com/riscv-collab/riscv-gnu-toolchain ./configure --prefix=$RISCV make -j3
# downloading llvm-project source from github git clone https://github.com/llvm/llvm-project.git cd llvm-project # configuring build to build llvm and clang in Release mode # using ninja # and to use gold as the linker (less RAM required) # limiting targets to RISCV, and using riscv-gnu-toolchian # as basis for sysroot cmake -G Ninja -DLLVM_ENABLE_PROJECTS="clang;lld;" \ -DCMAKE_BUILD_TYPE=Release \ -DDEFAULT_SYSROOT="$RISCV/riscv64-unknown-elf/" \ -DGCC_INSTALL_PREFIX="$RISCV" \ -S llvm -B build-riscv/ -DLLVM_TARGETS_TO_BUILD="RISCV" # building clang/llvm using 4 jobs (can be tuned to your machine) cmake --build build/ -j4
This process will generate clang binary in llvm-project/build/bin/clang .
More information on how to download and build clang/llvm can be found on the project github page.
Development.
As a first exercise, we will use the SAXPY example from rvv-intrinsic-doc rvv_saxpy.c.
Building the simulator
# downloading and install proxy-kernel for riscv64-unknown-elf git clone https://github.com/riscv-software-src/riscv-pk.git cd riscv-v mkdir build && cd build make -j4 && make install ../configure --prefix=$RISCV --host=riscv64-unknown-elf
Let's now build the simulator (directly from the top of the master branch, why not !).
git clone https://github.com/riscv-software-src/riscv-isa-sim cd riscv-isa-sim mkdir build && cd build ../configure --prefix=$RISCV make -j4 && make install
Building the program
RVV is supported as part of the experimental extensions of clang. Thus it must be enabled explicitly when executing clang, and it must be associated with a version number, the current master of clang only support v0.10 of the RVV specification.clang -L $RISCV/riscv64-unknown-elf/lib/ --gcc-toolchain=$RISCV/ \
rvv_saxpy.c -menable-experimental-extensions -march=rv64gcv0p10 \
-target riscv64 -O3 -mllvm --riscv-v-vector-bits-min=256 \
-o test-riscv-clang
Executing
To execute the program we are going to use the spike simulator and the riscv-pk proxy kernel.
Spike is part of the riscv-gnu-toolchain available at https://github.com/riscv-collab/riscv-gnu-toolchain , riscvv-pk is also available on github. https://github.com/riscv-software-src/riscv-pk
the binary image of pk must be the first unnamed argument to spike before the main elf.
$RISCV/bin/spike --isa rv64gcv $RISCV/riscv64-unknown-elf/bin/pk \
test-riscv-clang
NOTES: I tried to use riscv-tools (https://github.com/riscv-software-src/riscv-tools) does not seem actively maintain and several issue poped up when I tried building it.
Objdump
Not all objdump support RISC-V vector extension. If you have built llvm has indicated above, you should be able to use the llvm-objdump program built within to disassemble a program with vector instructions.
llvm-objdump -d --mattr=+experimental-v <binary_file>
References
- IREE (MLIR dialect) page of riscv-v cross compilation https://google.github.io/iree/building-from-source/riscv/
- Official llvm github project https://github.com/llvm/llvm-project
- RVV intrinsic documentation on github https://github.com/riscv-non-isa/rvv-intrinsic-doc
- https://groups.google.com/a/groups.riscv.org/g/sw-dev/c/GiTkPw-9r8A?pli=1
- Properly configuring clang/llvm build https://stackoverflow.com/questions/68580399/using-clang-to-compile-for-risc-v