llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
29.12k stars 12.01k forks source link

recipe for target 'CMakeFiles/clang_rt.builtins-riscv64.dir/emutls.c.o' failed, build llvm for riscv failed #59228

Open dshm opened 1 year ago

dshm commented 1 year ago

LLVM code is tag llvmorg-15.0.4, build for RISC-V target.

cmake -G "Unix Makefiles" -DGCC_INSTALL_PREFIX=/data/dushaomin/task20221114/installtest20221128 -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra;lld;" -DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi;libunwind;libc;compiler-rt"  -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_ASSERTIONS=On -DDEFAULT_SYSROOT="/data/dushaomin/task20221109/test2/install/riscv64-unknown-elf" -DGCC_INSTALL_PREFIX="/data/dushaomin/task20221109/test2/install" -DLLVM_TARGETS_TO_BUILD="RISCV" -DLLVM_DEFAULT_TARGET_TRIPLE="riscv64-unknown-elf" ../llvm

/data/dushaomin/task20221109/test2/install/ is where I install riscv-gnu-toolchain

make -j

and I get the following error:

[ 93%] Building C object CMakeFiles/clang_rt.builtins-riscv64.dir/floatunditf.c.o
[ 94%] Building C object CMakeFiles/clang_rt.builtins-riscv64.dir/floatunsitf.c.o
[ 94%] Building C object CMakeFiles/clang_rt.builtins-riscv64.dir/floatuntitf.c.o
[ 95%] Building C object CMakeFiles/clang_rt.builtins-riscv64.dir/multc3.c.o
[ 95%] Building C object CMakeFiles/clang_rt.builtins-riscv64.dir/powitf2.c.o
[ 96%] Building C object CMakeFiles/clang_rt.builtins-riscv64.dir/multf3.c.o
/data/dushaomin/task20221114/llvm-project/compiler-rt/lib/builtins/enable_execute_stack.c:12:10: fatal error: 'sys/mman.h' file not found
#include <sys/mman.h>
         ^~~~~~~~~~~~
[ 97%] Building C object CMakeFiles/clang_rt.builtins-riscv64.dir/subtf3.c.o
[ 98%] Building C object CMakeFiles/clang_rt.builtins-riscv64.dir/trunctfdf2.c.o
1 error generated.
[ 98%] Building C object CMakeFiles/clang_rt.builtins-riscv64.dir/trunctfhf2.c.o
[ 99%] Building C object CMakeFiles/clang_rt.builtins-riscv64.dir/trunctfsf2.c.o
/data/dushaomin/task20221114/llvm-project/compiler-rt/lib/builtins/emutls.c:52:8: error: unknown type name 'pthread_mutex_t'
static pthread_mutex_t emutls_mutex = PTHREAD_MUTEX_INITIALIZER;
       ^
CMakeFiles/clang_rt.builtins-riscv64.dir/build.make:1780: recipe for target 'CMakeFiles/clang_rt.builtins-riscv64.dir/enable_execute_stack.c.o' failed
make[5]: *** [CMakeFiles/clang_rt.builtins-riscv64.dir/enable_execute_stack.c.o] Error 1
make[5]: *** Waiting for unfinished jobs....
/data/dushaomin/task20221114/llvm-project/compiler-rt/lib/builtins/emutls.c:52:39: error: use of undeclared identifier 'PTHREAD_MUTEX_INITIALIZER'
static pthread_mutex_t emutls_mutex = PTHREAD_MUTEX_INITIALIZER;
                                      ^
/data/dushaomin/task20221114/llvm-project/compiler-rt/lib/builtins/emutls.c:53:8: error: unknown type name 'pthread_key_t'
static pthread_key_t emutls_pthread_key;
       ^
/data/dushaomin/task20221114/llvm-project/compiler-rt/lib/builtins/emutls.c:93:3: warning: call to undeclared function 'pthread_setspecific'; ISO C99 and
 later do not support implicit function declarations [-Wimplicit-function-declaration]  pthread_setspecific(emutls_pthread_key, (void *)value);
  ^
/data/dushaomin/task20221114/llvm-project/compiler-rt/lib/builtins/emutls.c:97:34: warning: call to undeclared function 'pthread_getspecific'; ISO C99 an
d later do not support implicit function declarations [-Wimplicit-function-declaration]  return (emutls_address_array *)pthread_getspecific(emutls_pthread_key);
                                 ^
/data/dushaomin/task20221114/llvm-project/compiler-rt/lib/builtins/emutls.c:118:7: warning: call to undeclared function 'pthread_key_create'; ISO C99 and
 later do not support implicit function declarations [-Wimplicit-function-declaration]  if (pthread_key_create(&emutls_pthread_key, emutls_key_destructor) != 0)
      ^
/data/dushaomin/task20221114/llvm-project/compiler-rt/lib/builtins/emutls.c:124:10: error: unknown type name 'pthread_once_t'
  static pthread_once_t once = PTHREAD_ONCE_INIT;
         ^
/data/dushaomin/task20221114/llvm-project/compiler-rt/lib/builtins/emutls.c:124:32: error: use of undeclared identifier 'PTHREAD_ONCE_INIT'
  static pthread_once_t once = PTHREAD_ONCE_INIT;
                               ^
/data/dushaomin/task20221114/llvm-project/compiler-rt/lib/builtins/emutls.c:125:3: warning: call to undeclared function 'pthread_once'; ISO C99 and later
 do not support implicit function declarations [-Wimplicit-function-declaration]  pthread_once(&once, emutls_init);
  ^
/data/dushaomin/task20221114/llvm-project/compiler-rt/lib/builtins/emutls.c:128:42: warning: call to undeclared function 'pthread_mutex_lock'; ISO C99 an
d later do not support implicit function declarations [-Wimplicit-function-declaration]static __inline void emutls_lock(void) { pthread_mutex_lock(&emutls_mutex); }
                                         ^
/data/dushaomin/task20221114/llvm-project/compiler-rt/lib/builtins/emutls.c:130:44: warning: call to undeclared function 'pthread_mutex_unlock'; ISO C99 
and later do not support implicit function declarations [-Wimplicit-function-declaration]static __inline void emutls_unlock(void) { pthread_mutex_unlock(&emutls_mutex); }
                                           ^
6 warnings and 5 errors generated.
CMakeFiles/clang_rt.builtins-riscv64.dir/build.make:1766: recipe for target 'CMakeFiles/clang_rt.builtins-riscv64.dir/emutls.c.o' failed
make[5]: *** [CMakeFiles/clang_rt.builtins-riscv64.dir/emutls.c.o] Error 1
CMakeFiles/Makefile2:303: recipe for target 'CMakeFiles/clang_rt.builtins-riscv64.dir/all' failed
make[4]: *** [CMakeFiles/clang_rt.builtins-riscv64.dir/all] Error 2
Makefile:135: recipe for target 'all' failed
make[3]: *** [all] Error 2
runtimes/CMakeFiles/builtins.dir/build.make:85: recipe for target 'runtimes/builtins-stamps/builtins-build' failed
make[2]: *** [runtimes/builtins-stamps/builtins-build] Error 2
CMakeFiles/Makefile2:100027: recipe for target 'runtimes/CMakeFiles/builtins.dir/all' failed
make[1]: *** [runtimes/CMakeFiles/builtins.dir/all] Error 2
Makefile:155: recipe for target 'all' failed
make: *** [all] Error 2

These days I have been trying to using LLVM as the compiler to generate code that can be run on RISC-V, and the test platform is spike [https://github.com/riscv-software-src/riscv-isa-sim.git]. I have meet some trouble in build LLVM for RISC-V and I also raise issues but it is not solved. In this issue, I will state the error in detail I met and ask some questions, which I Google much time and find many people asked , but there are not a complete answer.

The main question is does LLVM libc, compiler-rt and etc support RISC-V? as seen in issue https://github.com/llvm/llvm-project/issues/59026 , it said "With respect to libc, it does not work on RISC-V so the libc failures are expected. With respect to compiler-rt, I am not sure if we can list it under LLVM_ENABLE_RUNTIMES. Adding @petrhosek who can confirm/help." at the bottom of the webpage. and shown in comipler-rt offical page https://compiler-rt.llvm.org/ platform support section, it said "builtins is known to work on the following platforms: Machine Architectures: i386, X86-64, SPARC64, ARM, PowerPC, PowerPC 64. OS: DragonFlyBSD, FreeBSD, NetBSD, OpenBSD, Linux, Darwin." RISC-V is not in the list. But as the answer in https://github.com/llvm/llvm-project/issues/59026 said by @BertalanD, "I think your issue is that the command you ran does not build the compiler-rt library, just the compiler. Check out the documentation for how to cross-compile the builtins library." it seems that compiler-rt support RISC-V? I tried as he said -DLLVM_BUILTIN_TARGETS=riscv64-unknown-elf -DBUILTINS_riscv64-unknown-elf_CMAKE_SYSROOT=<path to sysroot> but failed, and I also tried write compiler-rt libc and etc in -DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi;libunwind;libc;compiler-rt when CMake, but the build process failed. As seen in officail page https://llvm.org/docs/CMake.html , the target in DLLVM_ENABLE_RUNTIMES is compilerd using just built compiler, thus just built Clang target RISC-V. The answer by different people is contradictory and I am not sure which is right and which wrong.

Could someone please answer me directly? Does LLVM support RISC-V target completely? Does LLVM libc, compiler-rt and etc. supported RISC-V? I know using Clang with --gcc-toolchain and --sysroot params can generate code for RISC-V target, gcc-toolchain and sysroot points to riscv-gnu toolchain install path. But can I use Clang without --gcc-toolchain and --sysroot? Thus using LLVM completely without riscv-gnu toolchain riscv64-unknown-elf-gcc? As error asked in issue [https://github.com/llvm/llvm-project/issues/59026], there are friendly people answer me that it was the problem of compiler-rt, but I can't build compiler-rt for RISC-V successful. Does LLVM libc, compiler-rt and etc. supported RISC-V? I have tried much to build it and Google it but not make sense. If it support RISC-V, Could someone please give a complete proccess how to make it successful, If it not support RISC-V, I may not waste time on it. Wait for someone warm-hearted to answer me and Thank you very much!

llvmbot commented 1 year ago

@llvm/issue-subscribers-backend-risc-v

jrtc27 commented 1 year ago

LLVM libc definitely is not supported. Compiler-RT definitely is supported. Your problem is bare metal builds aren’t auto detected, you need to set COMPILER_RT_BAREMETAL_BUILD in the runtimes build manually, otherwise it tries to build those files which you observe are broken bare metal (on any target, RISC-V or not).

dshm commented 1 year ago

LLVM libc definitely is not supported. Compiler-RT definitely is supported. Your problem is bare metal builds aren’t auto detected, you need to set COMPILER_RT_BAREMETAL_BUILD in the runtimes build manually, otherwise it tries to build those files which you observe are broken bare metal (on any target, RISC-V or not).

Thank you for your reply, I tried as you said, it make successful but meet other problems. As stated below.

the build command I use is:

cmake -G "Unix Makefiles" -DGCC_INSTALL_PREFIX=/data/dushaomin/task20221114/installtest20221128 -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra;lld;" -DLLVM_ENABLE_RUNTIMES="compiler-rt" -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_ASSERTIONS=On -DCOMPILER_RT_BAREMETAL_BUILD=ON -DDEFAULT_SYSROOT="/data/dushaomin/task20221109/test2/install/riscv64-unknown-elf" -DGCC_INSTALL_PREFIX="/data/dushaomin/task20221109/test2/install" -DLLVM_TARGETS_TO_BUILD="RISCV" -DLLVM_DEFAULT_TARGET_TRIPLE="riscv64-unknown-elf"

make -j

it make successful and some log is as below:

[100%] Performing build step for 'runtimes' [ 4%] Copying compiler-rt's orc/c_api.h... [ 9%] Copying compiler-rt's profile/InstrProfData.inc... [ 13%] Copying compiler-rt's sanitizer/hwasan_interface.h... [ 18%] Copying compiler-rt's fuzzer/FuzzedDataProvider.h... [ 22%] Copying compiler-rt's sanitizer/msan_interface.h... [ 27%] Copying compiler-rt's profile/MemProfData.inc... [ 31%] Copying compiler-rt's sanitizer/coverage_interface.h... [ 40%] Copying compiler-rt's sanitizer/dfsan_interface.h... [ 40%] Copying compiler-rt's sanitizer/asan_interface.h... [ 45%] Copying compiler-rt's sanitizer/lsan_interface.h... [ 50%] Copying compiler-rt's sanitizer/linux_syscall_hooks.h... [ 54%] Copying compiler-rt's sanitizer/allocator_interface.h... [ 59%] Copying compiler-rt's sanitizer/memprof_interface.h... [ 63%] Copying compiler-rt's sanitizer/common_interface_defs.h... [ 68%] Copying compiler-rt's sanitizer/netbsd_syscall_hooks.h... [ 72%] Copying compiler-rt's sanitizer/scudo_interface.h... [ 77%] Copying compiler-rt's sanitizer/tsan_interface_atomic.h... [ 81%] Copying compiler-rt's sanitizer/ubsan_interface.h... [ 86%] Copying compiler-rt's sanitizer/tsan_interface.h... [ 90%] Copying compiler-rt's xray/xray_interface.h... [ 95%] Copying compiler-rt's xray/xray_log_interface.h... [100%] Copying compiler-rt's xray/xray_records.h... [100%] Built target compiler-rt-headers [100%] Built target compiler-rt [100%] No install step for 'runtimes' [100%] Completed 'runtimes' [100%] Built target runtimes

I use the built clang to compile code, first is hello.c whose content is shown below:

include

int main() {

printf("hello dsm\n");
return 0;

}

the command I use is clang --target=riscv64-unknown-elf -march=rv64gc ./hello.c -o hello it gives error:

./hello.c:1:9: fatal error: 'stdio.h' file not found

include

    ^~~~~~~~~

1 error generated.

I also tried another code without standard lib addtest.c whose content is shown below:

int main() { float b=7.7; float a=5.6; float c= a+b; return 0; }

the command I use is clang --target=riscv64-unknown-elf -march=rv64gc ./addtest.c -o addtest it gives error:

ld.lld: error: unable to find library -lc ld.lld: error: unable to find library -lm ld.lld: error: unable to find library -lclang_rt.builtins-riscv64 clang-15: error: ld.lld command failed with exit code 1 (use -v to see invocation)

the above two error is solved when passing --gcc-toolchain=/data/dushaomin/task20221109/test2/install param to clang, and the generated runnable file can run successful using spike.

thus I think: 1) Even with compiler-rt built successful,clang needs gcc-toolchain to compile code to runnable on RISCV target, whether the standard library (such as libc) is used in code or not. Is this point right? if it is wrong, could you please tell me how to solve the errors above? 2) -DDEFAULT_SYSROOT and -DGCC_INSTALL_PREFIX is not necessary when cmake? As we need to set --gcc-toolchain param when use clang. Thank you very much and wait for reply. @jrtc27

jrtc27 commented 1 year ago

Clang doesn't need a GCC toolchain. If you want to use parts of the standard library then you need an implementation of it, whether that's newlib, glibc, musl, whatever. In your case your GCC toolchain presumably includes a newlib sysroot with headers, libc and libm, hence why that works, but you perhaps should be using --sysroot instead as you're at risk of accidentally using GNU ld instead of LLD, but the directory hierarchy used by GCC may not be a proper sysroot, it's not something I use.

How you get Clang to see your GCC toolchain / sysroot is up to you and what's best depends on your use case.

dshm commented 1 year ago

Clang doesn't need a GCC toolchain. If you want to use parts of the standard library then you need an implementation of it, whether that's newlib, glibc, musl, whatever. In your case your GCC toolchain presumably includes a newlib sysroot with headers, libc and libm, hence why that works, but you perhaps should be using --sysroot instead as you're at risk of accidentally using GNU ld instead of LLD, but the directory hierarchy used by GCC may not be a proper sysroot, it's not something I use.

How you get Clang to see your GCC toolchain / sysroot is up to you and what's best depends on your use case.

Thanks for your reply. I set --sysroot parameter to clang, but it give errors as below: For hello.c whose content is shown below:

include

int main() {

printf("hello dsm\n"); return 0; }

the command I use is clang --sysroot=/data/dushaomin/task20221109/test2/install/riscv64-unknown-elf --target=riscv64-unknown-elf -march=rv64gc ./hello.c -o hello it still gives error: ./hello.c:1:9: fatal error: 'stdio.h' file not found

include

    ^~~~~~~~~

1 error generated.

for another code without standard lib addtest.c whose content is shown below:

int main() { float b=7.7; float a=5.6; float c= a+b; return 0; }

the command I use is clang --sysroot=/data/dushaomin/task20221109/test2/install/riscv64-unknown-elf --target=riscv64-unknown-elf -march=rv64gc ./addtest.c -o addtest it still gives error: ld.lld: error: unable to find library -lc ld.lld: error: unable to find library -lm ld.lld: error: unable to find library -lclang_rt.builtins-riscv64 clang-15: error: ld.lld command failed with exit code 1 (use -v to see invocation)

while when I use command clang --gcc-toolchain=/data/dushaomin/task20221109/test2/install/ --target=riscv64-unknown-elf -march=rv64gc ./hello.c -o hello and clang --gcc-toolchain=/data/dushaomin/task20221109/test2/install/ --target=riscv64-unknown-elf -march=rv64gc ./addtest.c -o addtest . they compile successful and can run using spike. Why does --sysroot not work?

the contents in /data/dushaomin/task20221109/test2/install/riscv64-unknown-elf is shown below: ls /data/dushaomin/task20221109/test2/install/riscv64-unknown-elf bin include lib

ls /data/dushaomin/task20221109/test2/install/riscv64-unknown-elf/include/ alloca.h c++ elf.h fenv.h ieeefp.h machine newlib-nano regdef.h signal.h stdio.h tar.h unistd.h _ansi.h complex.h envlock.h fnmatch.h inttypes.h malloc.h _newlib_version.h regex.h spawn.h stdlib.h termios.h utime.h argz.h cpio.h envz.h getopt.h langinfo.h math.h paths.h rpc ssp string.h tgmath.h utmp.h ar.h ctype.h errno.h glob.h libgen.h memory.h pthread.h sched.h stdatomic.h strings.h threads.h wchar.h assert.h devctl.h fastmath.h grp.h limits.h ndbm.h pwd.h search.h stdint.h sys time.h wctype.h bits dirent.h fcntl.h iconv.h locale.h newlib.h reent.h setjmp.h stdio_ext.h _syslist.h unctrl.h wordexp.h

ls /data/dushaomin/task20221109/test2/install/riscv64-unknown-elf/lib/ crt0.o libc_nano.a libgloss_nano.a libm_nano.a libsim.a libstdc++.la nano.specs sim.specs ldscripts libg.a libg_nano.a libnosys.a libstdc++.a libsupc++.a nosys.specs libc.a libgloss.a libm.a libsemihost.a libstdc++.a-gdb.py libsupc++.la semihost.specs

while the contents in gcc-toolchain /data/dushaomin/task20221109/test2/install/ is: ls /data/dushaomin/task20221109/test2/install/ bin include lib libexec riscv64-unknown-elf share

ls /data/dushaomin/task20221109/test2/install/bin/ riscv64-unknown-elf-addr2line riscv64-unknown-elf-gcc riscv64-unknown-elf-gdb riscv64-unknown-elf-objdump riscv64-unknown-elf-ar riscv64-unknown-elf-gcc-12.2.0 riscv64-unknown-elf-gdb-add-index riscv64-unknown-elf-ranlib riscv64-unknown-elf-as riscv64-unknown-elf-gcc-ar riscv64-unknown-elf-gprof riscv64-unknown-elf-readelf riscv64-unknown-elf-c++ riscv64-unknown-elf-gcc-nm riscv64-unknown-elf-ld riscv64-unknown-elf-run riscv64-unknown-elf-c++filt riscv64-unknown-elf-gcc-ranlib riscv64-unknown-elf-ld.bfd riscv64-unknown-elf-size riscv64-unknown-elf-cpp riscv64-unknown-elf-gcov riscv64-unknown-elf-lto-dump riscv64-unknown-elf-strings riscv64-unknown-elf-elfedit riscv64-unknown-elf-gcov-dump riscv64-unknown-elf-nm riscv64-unknown-elf-strip riscv64-unknown-elf-g++ riscv64-unknown-elf-gcov-tool riscv64-unknown-elf-objcopy

while the gcc-toolchain's sysroot is also the parameter I pass to clang. ./riscv64-unknown-elf-gcc -print-sysroot /data/dushaomin/task20221109/test2/install/riscv64-unknown-elf

At last, there are something I want to ask:

  1. As you said "the directory hierarchy used by GCC may not be a proper sysroot, it's not something I use". What's the sysroot you use? Or how do you find standard lib when using clang to compile code on RISCV?
  2. As you said in early reply that "LLVM libc definitely is not supported" RISCV. So if I want to use standard libs, One possible way is to compile the source code using just built clang target RISCV. Or other way is to use already exited standard libs, such in gcc-toolchain. Is this point right? Thank you very much and wait for your reply! @jrtc27
hiraditya commented 1 year ago

Is this related to: https://github.com/llvm/llvm-project/issues/59500?

jrtc27 commented 1 year ago

No. Building the runtime requires no compiler support. Though it's true we probably shouldn't bother building it if we can't compile anything to use it, unless GCC supports -femulated-tls for RISC-V and someone wants to use compiler-rt with it, which seems exceedingly niche.

fasiddique commented 1 year ago

Hi, I think adding the c flag to the command will fix your issue. For example you can try the following: clang --target=riscv64 -march=rv64gc -c ./hello.c -o hello

hrtan99 commented 7 months ago

Hi, I think adding the c flag to the command will fix your issue. For example you can try the following: clang --target=riscv64 -march=rv64gc -c ./hello.c -o hello

I've encountered this error as well, this can temporarily sovle the problem, but still need to figure out what is the reason.