Error while building llvm and mlir

While trying to buils llvm and mlir using instructions listed on mlir’s getting statred page, I am encountering following error:

cmake -G Ninja ../llvm \
>    -DLLVM_ENABLE_PROJECTS=mlir \
>    -DLLVM_BUILD_EXAMPLES=ON \
>    -DLLVM_TARGETS_TO_BUILD="RISCV" \
>    -DCMAKE_BUILD_TYPE=Release \
>    -DLLVM_ENABLE_ASSERTIONS=ON
-- bolt project is disabled
-- clang project is disabled
-- clang-tools-extra project is disabled
-- compiler-rt project is disabled
-- cross-project-tests project is disabled
-- libc project is disabled
-- libclc project is disabled
-- lld project is disabled
-- lldb project is disabled
-- mlir project is enabled
-- openmp project is disabled
-- polly project is disabled
-- pstl project is disabled
-- flang project is disabled
-- Could NOT find ZLIB (missing: ZLIB_LIBRARY) (found version "1.2.11")
-- Could NOT find LibXml2 (missing: LIBXML2_LIBRARY LIBXML2_INCLUDE_DIR) 
-- Could NOT find Terminfo (missing: Terminfo_LIBRARIES Terminfo_LINKABLE) 
-- Could NOT find Backtrace (missing: Backtrace_LIBRARY) 
-- Native target X86 is not selected; lli will not JIT code
-- Threads enabled.
-- Doxygen disabled.
-- Ninja version: 1.10.0
-- OCaml bindings disabled, need ctypes >=0.4.
-- LLVM host triple: x86_64-unknown-linux-gnu
-- LLVM default target triple: riscv64-unknown-elf
CMake Error at cmake/modules/HandleLLVMOptions.cmake:309 (message):
  Host compiler does not support '-fuse-ld=lld'
Call Stack (most recent call first):
  CMakeLists.txt:875 (include)


-- Configuring incomplete, errors occurred!
See also "/home/user3/MLIR/llvm-project/build/CMakeFiles/CMakeOutput.log".
See also "/home/user3/MLIR/llvm-project/build/CMakeFiles/CMakeError.log".

Please help.

This is because the host compiler either does not understand the option, or lld (llvm’s linker) is not installed. The name of that compiler will be printed somewhere at the start of the cmake output.

Assuming that is some gcc or clang, it’s unlikely to not handle the option at all. More likely that lld is not installed.

So you can either remove the cmake option that enabled -fuse-ld=, which is usually LLVM_USE_LINKER or ENABLE_LLD. Though I don’t see either in your cmake options. Perhaps your compiler already defaults to using lld but lld is not installed?

To install it, you can get it from apt if you are on something like Ubuntu. A quick way to test this is working is to compile a simple hello world with the -fuse-ld= option.

Before I installed lld:

$ gcc /tmp/test.c -fuse-ld=lld -o /dev/null
collect2: fatal error: cannot find ‘ld’
compilation terminated.

Then I install it and try again:

$ sudo apt install lld
<...>
The following additional packages will be installed:
  lld-10
The following NEW packages will be installed:
  lld lld-10
<...>
$ gcc /tmp/test.c -fuse-ld=lld -o /dev/null

If you’re on some other Linux or OS, we can figure out the equivalent.

Ah, this means it must have come from a cmake option. Perhaps MLIR defaults to it not sure.

Or they’re in CMakeCache.txt from a previous attempt

1 Like

It does not as far as I know.

Much more likely, the instruction page link in the original post has:

# Using clang and lld speeds up the build, we recomment adding:
#  -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DLLVM_ENABLE_LLD=ON

This is a suggestion and commented out, but if they tried it and then re-invoked cmake without it but without wiping out the build directory, it can lead to the reported issue.