TVM uses LLVM to generate LLVM IR (model.ll
) and then compile it to object file (model.o
).
After that the object file should be âlinkedâ to a shared library.
TVM can run g++
or clang++
command for you to link the object file (model.o
) to a shared library (model.so
) - it is implemented inside lib.export_library
method.
Because the process of generating model.so
has two stages - LLVM stage and linking stage (g++/clang++), we need to make sure that both stages generate output for the same mcpu/march/mabi
.
E.g.
# LLVM compilation for cpu sifive-u54 (llvm knows its arch rv64gc) and use lp64d ABI
target="llvm -mtriple=riscv64-unknown-linux-gnu -mcpu=sifive-u54 -mabi=lp64d -device=arm_cpu"
# Linking for arch rv64gc and use lp64d ABI.
lib.export_library("model.so", cc="riscv64-unknown-linux-gnu-g++", options=["-march=rv64gc", "-mabi=lp64d", "-mcpu=sifive-u54"])
--enable-multilib !!!
The linking process links not just your model.o
but other system libraries which model.o
needs (C Lib and C++ STL lib).
So, your cross-compilation toolchain should have pre-compiled system libraries for mcpu/march/mabi
which you are using in TVM. By default riscv-gnu-toolchain
only has lib64 rv64gc / lp64d
system libraries.
Check what ABIs you have
ls <riscv-gnu-toolchain_install_dir>/riscv64-unknown-linux-gnu/lib64
lp64 lp64d
ls <riscv-gnu-toolchain_install_dir>/riscv64-unknown-linux-gnu/lib32
ilp32 ilp32d
To get pre-compiled system libraries for all ABIs use --enable-multilib
flag when building riscv-gnu-toolchain
./configure --prefix=/opt/riscv --enable-multilib
--print-multi-lib
You can also use g++ --print-multi-lib
flag to list pre-compiled system libraries which your toolchain has. ( march/mabi
combinations)
riscv64-unknown-linux-gnu-g++ --print-multi-lib
.;
lib32/ilp32;@march=rv32imac@mabi=ilp32
lib32/ilp32d;@march=rv32imafdc@mabi=ilp32d
lib64/lp64;@march=rv64imac@mabi=lp64
-static-libstdc++
-static-libstdc++
is a flag for the linker (g++/clang++) to include C++ STL functions into you shared library (model.so
). In that case the model (model.so
) will not need libstdc++.so
on the device.
If your device has C++ STL library libstdc++.so
then you do not need to use -static-libstdc++
flag.
To check model.so
dependencies:
readelf -d model.so | grep NEEDED