Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I compile with LLVM/Clang to RISC-V target?

I want to compile a simple program "int main(){return 0;}" to RISC-V processor. LLVM/Clang version is 9.0 and I want to run the compiled program with a RISC-V simulator like this https://github.com/riscv/riscv-tools

My problem is that I can't list the clang supported targets only the LLC-s whith these commands:

llc --version
llc -march=xxARCHTYPExx -mattr=help

And there is no any type of riscv processor listed.

So I tried to look the triple file: llvm-project\llvm\include\llvm\ADT\Triple.h

and try a command like: clang hello.c -target riscv32 -march=rv32imafd

But I get the following error:

error: unable to create target: 'No available targets are compatible with triple "riscv32"'

Can somebody help me to how get a valid RISC-V target? I just simple can't compile the program but I know LLVM has a RISC-V support.

like image 735
Zoltán Avatar asked Feb 13 '19 13:02

Zoltán


1 Answers

LLVM 9 release notes explicitly state that RISC-V support was promoted from experimental to official.

And indeed, on my Fedora 31 machine, the LLVM 9 Fedora package does support RISC-V:

$ llvm-objdump --version | grep riscv
    riscv32    - 32-bit RISC-V
    riscv64    - 64-bit RISC-V

Also, I can create RISC-V binary code with the LLVM toolchain:

$ clang --target=riscv64 -march=rv64gc rotate.s -c -o rotate.o
$ file rotate.o
rotate.o: ELF 64-bit LSB relocatable, UCB RISC-V, version 1 (SYSV), not stripped

Although it doesn't include a libc for RISC-V targets:

$ clang --target=riscv64 -march=rv64gc hello-world.c -o hello-world
hello-world.c:1:10: fatal error: 'stdio.h' file not found
#include <stdio.h>
         ^~~~~~~~~
1 error generated.

However, you don't really need one - you could directly call syscalls for your hello world, e.g.:

$ clang  --target=riscv64 -march=rv64gc  hello.s -c -o hello.o
$ ld.lld hello.o -o hello
$ spike --isa=RV64gc ~/local/riscv/pk/riscv64-unknown-elf/bin/pk ./hello
bbl loader
Hello World

I don't use clang for linking because it seems that I can't convince clang with -fuse-ld to use another linker besides /usr/bin/ld.

like image 57
maxschlepzig Avatar answered Oct 03 '22 11:10

maxschlepzig