I have had an issue for some time which have prevented me from enabling OpenMP offloading in my main application (this is/was blocking issue 1 of 2). I had some big hopes that this would now be fixed when mod-files are built per target (thanks @Meinersbur - I have been looking forward to this for a long time!), but I am still struggling.
I now configure and build LLVM and Flang like this:
cmake -GNinja \
-DCMAKE_BUILD_TYPE="Release" \
-DCMAKE_INSTALL_PREFIX="/opt/llvm/llvm-project/install" \
-DCMAKE_CUDA_COMPILER=${CUDACXX} \
-DCMAKE_CUDA_HOST_COMPILER="clang++-20" \
-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" \
-DLLVM_RUNTIME_TARGETS='default;nvptx64-nvidia-cuda' \
-DOPENMP_ENABLE_LIBOMPTARGET="ON" \
-DFLANG_RT_EXPERIMENTAL_OFFLOAD_SUPPORT="OpenMP" \
-DFLANG_RT_DEVICE_ARCHITECTURES="sm_89;sm_120" \
-DFLANG_RT_ENABLE_STATIC=ON \
../llvm 2>&1 | tee cmake.log
nice ninja -j 12 install
Building the reproducer from issue 146876 with offloading enabled:
program real10
use ieee_arithmetic
implicit none
end program
now gives a series of linker errors:
$ flang --version
flang version 22.0.0git (https://github.com/llvm/llvm-project.git 7675fc79c802cf7f6a95660f6ee59bf6cb62102f)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /opt/llvm/llvm-project/install/bin
$ flang -fopenmp -fopenmp-targets=nvptx64-nvidia-cuda real10.F90
nvlink error : Undefined reference to '_QM__fortran_builtinsEC__builtin_ieee_away' in '/tmp/real10-054f3c-nvptx64-nvidia-cuda-sm_89-f50675-0bef9e.cubin'
nvlink error : Undefined reference to '_QM__fortran_builtinsEC__builtin_ieee_denorm' in '/tmp/real10-054f3c-nvptx64-nvidia-cuda-sm_89-f50675-0bef9e.cubin'
nvlink error : Undefined reference to '_QM__fortran_builtinsEC__builtin_ieee_divide_by_zero' in '/tmp/real10-054f3c-nvptx64-nvidia-cuda-sm_89-f50675-0bef9e.cubin'
nvlink error : Undefined reference to '_QM__fortran_builtinsEC__builtin_ieee_down' in '/tmp/real10-054f3c-nvptx64-nvidia-cuda-sm_89-f50675-0bef9e.cubin'
nvlink error : Undefined reference to '_QM__fortran_builtinsEC__builtin_ieee_inexact' in '/tmp/real10-054f3c-nvptx64-nvidia-cuda-sm_89-f50675-0bef9e.cubin'
nvlink error : Undefined reference to '_QM__fortran_builtinsEC__builtin_ieee_invalid' in '/tmp/real10-054f3c-nvptx64-nvidia-cuda-sm_89-f50675-0bef9e.cubin'
nvlink error : Undefined reference to '_QM__fortran_builtinsEC__builtin_ieee_nearest' in '/tmp/real10-054f3c-nvptx64-nvidia-cuda-sm_89-f50675-0bef9e.cubin'
nvlink error : Undefined reference to '_QMieee_arithmeticECieee_negative_denormal' in '/tmp/real10-054f3c-nvptx64-nvidia-cuda-sm_89-f50675-0bef9e.cubin'
nvlink error : Undefined reference to '_QMieee_arithmeticECieee_negative_inf' in '/tmp/real10-054f3c-nvptx64-nvidia-cuda-sm_89-f50675-0bef9e.cubin'
nvlink error : Undefined reference to '_QMieee_arithmeticECieee_negative_normal' in '/tmp/real10-054f3c-nvptx64-nvidia-cuda-sm_89-f50675-0bef9e.cubin'
nvlink error : Undefined reference to '_QMieee_arithmeticECieee_negative_subnormal' in '/tmp/real10-054f3c-nvptx64-nvidia-cuda-sm_89-f50675-0bef9e.cubin'
nvlink error : Undefined reference to '_QMieee_arithmeticECieee_negative_zero' in '/tmp/real10-054f3c-nvptx64-nvidia-cuda-sm_89-f50675-0bef9e.cubin'
nvlink error : Undefined reference to '_QM__fortran_builtinsEC__builtin_ieee_other' in '/tmp/real10-054f3c-nvptx64-nvidia-cuda-sm_89-f50675-0bef9e.cubin'
nvlink error : Undefined reference to '_QMieee_arithmeticECieee_other_value' in '/tmp/real10-054f3c-nvptx64-nvidia-cuda-sm_89-f50675-0bef9e.cubin'
nvlink error : Undefined reference to '_QM__fortran_builtinsEC__builtin_ieee_overflow' in '/tmp/real10-054f3c-nvptx64-nvidia-cuda-sm_89-f50675-0bef9e.cubin'
nvlink error : Undefined reference to '_QMieee_arithmeticECieee_positive_denormal' in '/tmp/real10-054f3c-nvptx64-nvidia-cuda-sm_89-f50675-0bef9e.cubin'
nvlink error : Undefined reference to '_QMieee_arithmeticECieee_positive_inf' in '/tmp/real10-054f3c-nvptx64-nvidia-cuda-sm_89-f50675-0bef9e.cubin'
nvlink error : Undefined reference to '_QMieee_arithmeticECieee_positive_normal' in '/tmp/real10-054f3c-nvptx64-nvidia-cuda-sm_89-f50675-0bef9e.cubin'
nvlink error : Undefined reference to '_QMieee_arithmeticECieee_positive_subnormal' in '/tmp/real10-054f3c-nvptx64-nvidia-cuda-sm_89-f50675-0bef9e.cubin'
nvlink error : Undefined reference to '_QMieee_arithmeticECieee_positive_zero' in '/tmp/real10-054f3c-nvptx64-nvidia-cuda-sm_89-f50675-0bef9e.cubin'
nvlink error : Undefined reference to '_QMieee_arithmeticECieee_quiet_nan' in '/tmp/real10-054f3c-nvptx64-nvidia-cuda-sm_89-f50675-0bef9e.cubin'
nvlink error : Undefined reference to '_QMieee_arithmeticECieee_signaling_nan' in '/tmp/real10-054f3c-nvptx64-nvidia-cuda-sm_89-f50675-0bef9e.cubin'
nvlink error : Undefined reference to '_QM__fortran_builtinsEC__builtin_ieee_to_zero' in '/tmp/real10-054f3c-nvptx64-nvidia-cuda-sm_89-f50675-0bef9e.cubin'
nvlink error : Undefined reference to '_QM__fortran_builtinsEC__builtin_ieee_underflow' in '/tmp/real10-054f3c-nvptx64-nvidia-cuda-sm_89-f50675-0bef9e.cubin'
nvlink error : Undefined reference to '_QM__fortran_builtinsEC__builtin_ieee_up' in '/tmp/real10-054f3c-nvptx64-nvidia-cuda-sm_89-f50675-0bef9e.cubin'
/opt/llvm/llvm-project/install/bin/clang-nvlink-wrapper: error: 'nvlink' failed
clang: error: nvlink command failed with exit code 1 (use -v to see invocation)
/opt/llvm/llvm-project/install/bin/clang-linker-wrapper: error: 'clang' failed
flang-22: error: linker command failed with exit code 1 (use -v to see invocation)
I suspect that the problem is that no runtime library for the target/device architecture is being built. I have tried to set -DRUNTIMES_nvptx64-nvidia-cuda_LLVM_ENABLE_RUNTIMES="compiler-rt;flang-rt;openmp", but this breaks every small example using offloading by introducing additional linker errors, i.e. every small OpenMP example suddenly do not link with the error:
$ flang -fopenmp -fopenmp-targets=nvptx64-nvidia-cuda loop-01-index.F90
/opt/llvm/llvm-project/install/bin/clang-nvlink-wrapper: error: unable to find library -lflang_rt.runtime
clang: error: nvlink command failed with exit code 1 (use -v to see invocation)
/opt/llvm/llvm-project/install/bin/clang-linker-wrapper: error: 'clang' failed
flang-22: error: linker command failed with exit code 1 (use -v to see invocation)
So this option really only introduce additional problems. Is there an updated documentation on how to build LLVM+Flang with as much offloading support as possible? Do I do something wrong when building Flang? Or is what I try to do still just not supported/working?
Thanks in advance for any hints.