Hi all,
currently I am trying to compile clang/flang with offloading support. So far, I have tried numerous combinations of CMake configurations and environment variables with no success. I am using Ubuntu, the latest NVIDIA drivers with CUDA 12.9 and a RTX 5070ti (but I think this does not matter for compiling LLVM). After consulting the discourse here, I took a step back and tried the recommended approach to get an offloading capable compiler:
cd llvm-project
mkdir build
cd build
cmake ../llvm -G Ninja \
-C ../offload/cmake/caches/Offload.cmake \
-DCMAKE_BUILD_TYPE="Release" \
-DCMAKE_INSTALL_PREFIX="/opt/llvm/llvm-project/install" \
-DCMAKE_C_COMPILER="clang-19" \
-DCMAKE_CXX_COMPILER="clang++-19"
ninja -j 24 install
Using this approach I get errors during building various objects in libcxx, all with the same error pattern. E.g. for chrono.cpp
:
[2562/2580] Building CXX object libcxx/src/CMakeFiles/cxx_static.dir/chrono.cpp.o
FAILED: libcxx/src/CMakeFiles/cxx_static.dir/chrono.cpp.o
/home/jfuchs/tools/llvm-compilation/llvm-project/build/./bin/clang++ --target=nvptx64-nvidia-cuda -DLIBCXX_BUILDING_LIBCXXABI -DLIBC_NAMESPACE=__llvm_libc_common_utils -D_LIBCPP_BUILDING_LIBRARY -D_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER -D_LIBCPP_LINK_PTHREAD_LIB -D_LIBCPP_LINK_RT_LIB -D_LIBCPP_REMOVE_TRANSITIVE_INCLUDES -I/home/jfuchs/tools/llvm-compilation/llvm-project/libcxx/src -I/home/jfuchs/tools/llvm-compilation/llvm-project/build/include/nvptx64-nvidia-cuda/c++/v1 -I/home/jfuchs/tools/llvm-compilation/llvm-project/build/include/c++/v1 -I/home/jfuchs/tools/llvm-compilation/llvm-project/libcxxabi/include -I/home/jfuchs/tools/llvm-compilation/llvm-project/cmake/Modules/../../libc -O3 -DNDEBUG -std=c++23 -faligned-allocation -nostdinc++ -fvisibility-inlines-hidden -fvisibility=hidden -fsized-deallocation -nogpulib -flto -fconvergent-functions --cuda-feature=+ptx63 -Wall -Wextra -Wnewline-eof -Wshadow -Wwrite-strings -Wno-unused-parameter -Wno-long-long -Werror=return-type -Wextra-semi -Wundef -Wunused-template -Wformat-nonliteral -Wzero-length-array -Wdeprecated-redundant-constexpr-static-def -Wno-nullability-completeness -Wno-user-defined-literals -Wno-covered-switch-default -Wno-suggest-override -Wno-error -fno-exceptions -fno-rtti -fdebug-prefix-map=/home/jfuchs/tools/llvm-compilation/llvm-project/build/include/c++/v1=/home/jfuchs/tools/llvm-compilation/llvm-project/libcxx/include -MD -MT libcxx/src/CMakeFiles/cxx_static.dir/chrono.cpp.o -MF libcxx/src/CMakeFiles/cxx_static.dir/chrono.cpp.o.d -o libcxx/src/CMakeFiles/cxx_static.dir/chrono.cpp.o -c /home/jfuchs/tools/llvm-compilation/llvm-project/libcxx/src/chrono.cpp
In file included from /home/jfuchs/tools/llvm-compilation/llvm-project/libcxx/src/chrono.cpp:27:
/usr/include/unistd.h:27:1: error: unknown type name '__BEGIN_DECLS'
27 | __BEGIN_DECLS
| ^
/usr/include/unistd.h:220:19: error: typedef redefinition with different types ('__ssize_t' (aka 'int') vs 'long')
220 | typedef __ssize_t ssize_t;
| ^
/home/jfuchs/tools/llvm-compilation/llvm-project/build/include/nvptx64-nvidia-cuda/llvm-libc-types/ssize_t.h:12:26: note: previous definition is here
12 | typedef __PTRDIFF_TYPE__ ssize_t;
| ^
In file included from /home/jfuchs/tools/llvm-compilation/llvm-project/libcxx/src/chrono.cpp:27:
/usr/include/unistd.h:287:52: error: expected function body after function declarator
287 | extern int access (const char *__name, int __type) __THROW __nonnull ((1));
| ^
/usr/include/unistd.h:339:65: error: expected function body after function declarator
339 | extern __off_t lseek (int __fd, __off_t __offset, int __whence) __THROW;
| ^
/usr/include/unistd.h:371:62: error: expected function body after function declarator
371 | extern ssize_t read (int __fd, void *__buf, size_t __nbytes) __wur
| ^
/usr/include/unistd.h:378:64: error: expected function body after function declarator
378 | extern ssize_t write (int __fd, const void *__buf, size_t __n) __wur
| ^
/usr/include/unistd.h:437:36: error: expected function body after function declarator
437 | extern int pipe (int __pipedes[2]) __THROW __wur;
| ^
/usr/include/unistd.h:452:52: error: expected function body after function declarator
452 | extern unsigned int alarm (unsigned int __seconds) __THROW;
| ^
/usr/include/unistd.h:494:6: error: expected function body after function declarator
494 | __THROW __nonnull ((1)) __wur;
| ^
/usr/include/unistd.h:517:39: error: expected function body after function declarator
517 | extern int chdir (const char *__path) __THROW __nonnull ((1)) __wur;
| ^
/usr/include/unistd.h:531:50: error: expected function body after function declarator
531 | extern char *getcwd (char *__buf, size_t __size) __THROW __wur;
| ^
/usr/include/unistd.h:552:27: error: expected function body after function declarator
552 | extern int dup (int __fd) __THROW __wur;
| ^
/usr/include/unistd.h:555:39: error: expected function body after function declarator
555 | extern int dup2 (int __fd, int __fd2) __THROW;
| ^
/usr/include/unistd.h:573:28: error: expected function body after function declarator
573 | char *const __envp[]) __THROW __nonnull ((1, 2));
| ^
/usr/include/unistd.h:585:6: error: expected function body after function declarator
585 | __THROW __nonnull ((1, 2));
| ^
/usr/include/unistd.h:590:6: error: expected function body after function declarator
590 | __THROW __nonnull ((1, 2));
| ^
/usr/include/unistd.h:595:6: error: expected function body after function declarator
595 | __THROW __nonnull ((1, 2));
| ^
/usr/include/unistd.h:600:6: error: expected function body after function declarator
600 | __THROW __nonnull ((1, 2));
| ^
/usr/include/unistd.h:606:6: error: expected function body after function declarator
606 | __THROW __nonnull ((1, 2));
| ^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.
More confusingly, if I use a custom CMake command (which works for my colleague using a 4080; see below), I can successfully compile clang/flang. I can even compile my code with this flang build that uses OpenMP offloading and target sections. However, this approach fails when I run my code. If I force OMP_TARGET_OFFLOAD=mandatory
, then I get the error:
omptarget error: Consult https://openmp.llvm.org/design/Runtimes.html for debugging options.
omptarget error: No images found compatible with the installed hardware. Segmentation fault (core dumped)
export CC="clang-19"
export CXX="clang++-19"
export CUDACXX="/usr/local/cuda-12.9/bin/nvcc"
cd $BUILD_DIR
cmake -GNinja \
-DCMAKE_BUILD_TYPE="Release" \
-DCMAKE_INSTALL_PREFIX=${INSTALL_DIR} \
-DCMAKE_CUDA_COMPILER=${CUDACXX} \
-DCMAKE_CUDA_HOST_COMPILER="clang++-19" \
-DCMAKE_CUDA_ARCHITECTURES="89;120" \
-DLLVM_ENABLE_PROJECTS="clang;lld;flang;lldb" \
-DLLVM_ENABLE_RUNTIMES="compiler-rt;openmp;offload;flang-rt" \
-DLLVM_TARGETS_TO_BUILD="X86;NVPTX" \
-DLLVM_ENABLE_RTTI="ON" \
-DOPENMP_ENABLE_LIBOMPTARGET="ON" \
-DFLANG_RT_EXPERIMENTAL_OFFLOAD_SUPPORT="OpenMP" \
-DFLANG_RT_DEVICE_ARCHITECTURES="sm_89;sm_120" \
-DFLANG_EXPERIMENTAL_OMP_OFFLOAD_BUILD="host_device" \
${LLVM_DIR} 2>&1 | tee cmake.log
nice ninja -j 24 install 2>&1 | tee ninja.log
I suspect that there is some issue regarding my environment that I am not aware of. Additionally, there were some resources online that have done some kind of bootstrapping. Is this something to consider here? I am grateful for any hints or guidance in the right direction.