iree-org / iree

A retargetable MLIR-based machine learning compiler and runtime toolkit.
http://iree.dev/
Apache License 2.0
2.87k stars 625 forks source link

RISCV cross compile completely infeasible #19057

Open soant opened 3 weeks ago

soant commented 3 weeks ago

What happened?

Step 1:

I compiled the RISCV-GNU-TOOLCHAIN for riscv64-unknown-linux-gnu-gcc:

riscv64-unknown-linux-gnu-gcc -v
Using built-in specs.
COLLECT_GCC=riscv64-unknown-linux-gnu-gcc
COLLECT_LTO_WRAPPER=/opt/riscv/libexec/gcc/riscv64-unknown-linux-gnu/13.2.0/lto-wrapper
Target: riscv64-unknown-linux-gnu
Configured with: /home/runner/work/riscv-gnu-toolchain/riscv-gnu-toolchain/gcc/configure --target=riscv64-unknown-linux-gnu --prefix=/opt/riscv --with-sysroot=/opt/riscv/sysroot --with-pkgversion= --with-system-zlib --enable-shared --enable-tls --enable-languages=c,c++,fortran --disable-libmudflap --disable-libssp --disable-libquadmath --disable-libsanitizer --disable-nls --disable-bootstrap --src=.././gcc --disable-multilib --with-abi=lp64d --with-arch=rv64gc --with-tune=rocket --with-isa-spec=20191213 'CFLAGS_FOR_TARGET=-O2    -mcmodel=medlow' 'CXXFLAGS_FOR_TARGET=-O2    -mcmodel=medlow'
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 13.2.0 () 

Step 2:

Build the LLVM for RISCV target using command:

cmake -G "Unix Makefiles" -DDEFAULT_SYSROOT="/opt/riscv/sysroot" -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra;lld;" -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_ASSERTIONS=On -DLLVM_DEFAULT_TARGET_TRIPLE="riscv64-unknown-linux-gnu" -DLLVM_TARGETS_TO_BUILD="RISCV" -DLLVM_PARALLEL_LINK_JOBS=1 ../llvm

and compiled clang shows:

clang -v
clang version 20.0.0git (https://github.com/iree-org/llvm-project.git 864902e9b4d8bc6d3f0852d5c475e3dc97dd8335)
Target: riscv64-unknown-linux-gnu
Thread model: posix
InstalledDir: /code/iree/third_party/llvm-project/build/bin
Build config: +assertions

test test.c file using above compiled compiler with no problem:

clang test.c -S -O3 -o - --gcc-toolchain=/opt/riscv

Step 3:

Trying to compile the IREE using above compiler for RISCV host tools:

cmake -GNinja -B ../iree-build/ \
-DCMAKE_C_COMPILER=clang \
-DCMAKE_CXX_COMPILER=clang++ \
-DCMAKE_C_FLAGS="--gcc-toolchain=/opt/riscv --sysroot=/opt/riscv/sysroot --target=riscv64 -march=rv64gc" \
-DCMAKE_CXX_FLAGS="--gcc-toolchain=/opt/riscv --sysroot=/opt/riscv/sysroot --target=riscv64 -march=rv64gc" \
-DCMAKE_INSTALL_PREFIX=../iree-build/install \
-DCMAKE_BUILD_TYPE=RelWithDebInfo .

generate error:

  The C compiler

    "/code/iree/third_party/llvm-project/build/bin/clang"

  is not able to compile a simple test program.

  It fails with the following output:

    Change Dir: /code/iree-build/CMakeFiles/CMakeTmp

    Run Build Command(s):/usr/bin/ninja cmTC_3e84d && [1/2] Building C object CMakeFiles/cmTC_3e84d.dir/testCCompiler.c.o
    [2/2] Linking C executable cmTC_3e84d
    FAILED: cmTC_3e84d 
    : && /home/barry/code/iree/third_party/llvm-project/build/bin/clang --gcc-toolchain=/opt/riscv --sysroot=/opt/riscv/sysroot --target=riscv64 -march=rv64gc -lstdc++ -lpthread -lm -ldl CMakeFiles/cmTC_3e84d.dir/testCCompiler.c.o -o cmTC_3e84d   && :
    /opt/riscv/lib/gcc/riscv64-unknown-linux-gnu/13.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: cannot find crt0.o: no that file or directory
    clang: error: ld command failed with exit code 1 (use -v to see invocation)
    ninja: build stopped: subcommand failed.

  CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
  CMakeLists.txt:19 (project)

crt0.o only generated for riscv64-unknown-elf type, I build for riscv64-unknown-linux-gnu, how does this happen?

ScottTodd commented 3 weeks ago

We have some instructions you can follow at https://iree.dev/building-from-source/riscv/ and https://iree.dev/guides/deployment-configurations/bare-metal/.

For your step 3, I see a few areas to debug: