This is an archive of the discontinued LLVM Phabricator instance.

[flang][runtime] Support in-tree device build of Flang runtime.
ClosedPublic

Authored by vzakhari on Jul 11 2023, 4:20 PM.

Details

Summary

I changed the set of files that are built for experimental CUDA/OMP
builds, i.e. the files with enabled device support are built
as such and the rest of the files are built just for the host target.
With this change we can build Flang runtime library that is fully functional
on the host target, so in-tree targets like check-flang become operational.

Diff Detail

Event Timeline

vzakhari created this revision.Jul 11 2023, 4:20 PM
Herald added a project: Restricted Project. · View Herald Transcript
vzakhari requested review of this revision.Jul 11 2023, 4:20 PM
klausler accepted this revision.Jul 11 2023, 4:23 PM
This revision is now accepted and ready to land.Jul 11 2023, 4:23 PM

I tried building using the instructions in GettingStarted.md, but I was not successful. Here's the invocation of cmake I used:

cmake \
  -G Ninja \
  ../llvm \
  -DCMAKE_BUILD_TYPE=Release \
  -DFLANG_EXPERIMENTAL_CUDA_RUNTIME=on \
  -DCMAKE_CUDA_ARCHITECTURES=80 \
  -DLLVM_LIT_ARGS=-v \
  -DFLANG_ENABLE_WERROR=On \
  -DCMAKE_CXX_STANDARD=17 \
  -DCMAKE_C_COMPILER=clang \
  -DCMAKE_CXX_COMPILER=clang++ \
  -DCMAKE_CUDA_COMPILER=clang \
  -DCMAKE_CUDA_HOST_COMPILER=clang++ \
  -DLLVM_ENABLE_PROJECTS="clang;mlir;flang;compiler-rt;openmp" \
  -DLLVM_BUILD_TOOLS=On \
  -DLLVM_INSTALL_UTILS=On \
  -DLLVM_TARGETS_TO_BUILD=host \
  -DLLVM_ENABLE_ASSERTIONS=ON \
  -DCMAKE_CXX_LINK_FLAGS="-Wl,-rpath,$LD_LIBRARY_PATH" \
  -DCMAKE_INSTALL_PREFIX=$INSTALLDIR

But my build failed. Here's an excerpt from the log file of the build:

-- Performing Test CXX_SUPPORTS_NO_CTAD_MAYBE_UNSUPPORTED_FLAG - Success
-- Performing Test FLANG_RUNTIME_HAS_FNO_LTO_FLAG
-- Performing Test FLANG_RUNTIME_HAS_FNO_LTO_FLAG - Success
-- The CUDA compiler identification is unknown
-- Detecting CUDA compiler ABI info
CMake Error: Error required internal CMake variable not set, cmake may not be built correctly.
Missing variable is:
_CMAKE_CUDA_WHOLE_FLAG
CMake Error at /usr/local/share/cmake-3.26/Modules/CMakeDetermineCompilerABI.cmake:57 (try_compile):
  Failed to generate test project build system.
Call Stack (most recent call first):
  /usr/local/share/cmake-3.26/Modules/CMakeTestCUDACompiler.cmake:19 (CMAKE_DETERMINE_COMPILER_ABI)
  /local/home/psteinfeld/main/029/flang/runtime/CMakeLists.txt:163 (enable_language)


-- Configuring incomplete, errors occurred!
Exit with error 0: cmake

Hi @PeteSteinfeld, thank you for trying it! I guess the clang compiler that you have in your path does not support NVPTX target. For my local builds I used specially built clang. You may try building with nvcc.

PeteSteinfeld accepted this revision.Jul 11 2023, 6:51 PM

Thanks, @vzakhari.

I tried building with nvcc, and I ran into problems because it doesn't support the -Wall option. I did succeed using gcc version 9.3.0. I also tried building with a version of the clang compiler that I built based on llvm release 16.0. I could not get this to work, even with using nvcc as my CMAKE_CUDA_COMPILER. You might want to mention that the version of clang that you use to configure the build must support the NVPTX target.

@vzakhari, here's more information on what I did.

Here's the script that I used that was successful. Note that the versio of gcc that's on my path is 9.3.0:

cmake \
  -G Ninja \
  ../llvm \
  -DCMAKE_BUILD_TYPE=Release \
  -DFLANG_EXPERIMENTAL_CUDA_RUNTIME=ON \
  -DCMAKE_CUDA_ARCHITECTURES=80 \
  -DCMAKE_C_COMPILER=gcc \
  -DCMAKE_CXX_COMPILER=g++ \
  -DCMAKE_CUDA_COMPILER=nvcc \
  -DCMAKE_CUDA_HOST_COMPILER=g++\
  -DLLVM_LIT_ARGS=-v \
  -DFLANG_ENABLE_WERROR=On \
  -DCMAKE_CXX_STANDARD=17 \
  -DLLVM_ENABLE_PROJECTS="clang;mlir;flang;compiler-rt;openmp" \
  -DLLVM_BUILD_TOOLS=On \
  -DLLVM_INSTALL_UTILS=On \
  -DLLVM_TARGETS_TO_BUILD=host \
  -DLLVM_ENABLE_ASSERTIONS=ON \
  -DCMAKE_CXX_LINK_FLAGS="-Wl,-rpath,$LD_LIBRARY_PATH" \
  -DCMAKE_INSTALL_PREFIX=$INSTALLDIR

ninja

This build succeeded. There's a section of the log file where I can see that checking is done for the CUDA compiler:

  ...
-- Performing Test FLANG_RUNTIME_HAS_FNO_LTO_FLAG
-- Performing Test FLANG_RUNTIME_HAS_FNO_LTO_FLAG - Success
-- The CUDA compiler identification is NVIDIA 12.2.91
-- Detecting CUDA compiler ABI info
-- Detecting CUDA compiler ABI info - done
-- Check for working CUDA compiler: /proj/nv/Linux_x86_64/dev/compilers/bin/nvcc - skipped
-- Detecting CUDA compile features
-- Detecting CUDA compile features - done
-- Could NOT find Doxygen (missing: DOXYGEN_EXECUTABLE) 
  ...

I then changed the mentions of gcc and g++ to point to versions of clang that I personally built from release 16. This resulted in the following script:

cmake \
  -G Ninja \
  ../llvm \
  -DCMAKE_BUILD_TYPE=Release \
  -DFLANG_EXPERIMENTAL_CUDA_RUNTIME=ON \
  -DCMAKE_CUDA_ARCHITECTURES=80 \
  -DCMAKE_C_COMPILER=/local/home/psteinfeld/main/rel16/build/bin/clang \
  -DCMAKE_CXX_COMPILER=/local/home/psteinfeld/main/rel16/build/bin/clang++ \
  -DCMAKE_CUDA_COMPILER=nvcc \
  -DCMAKE_CUDA_HOST_COMPILER=/local/home/psteinfeld/main/rel16/build/bin/clang++
 \
  -DLLVM_LIT_ARGS=-v \
  -DFLANG_ENABLE_WERROR=On \
  -DCMAKE_CXX_STANDARD=17 \
  -DLLVM_ENABLE_PROJECTS="clang;mlir;flang;compiler-rt;openmp" \
  -DLLVM_BUILD_TOOLS=On \
  -DLLVM_INSTALL_UTILS=On \
  -DLLVM_TARGETS_TO_BUILD=host \
  -DLLVM_ENABLE_ASSERTIONS=ON \
  -DCMAKE_CXX_LINK_FLAGS="-Wl,-rpath,$LD_LIBRARY_PATH" \
  -DCMAKE_INSTALL_PREFIX=$INSTALLDIR

ninja

This build failed. Here's an excerpt from the log file:

  ...
-- Performing Test CXX_SUPPORTS_NO_CTAD_MAYBE_UNSUPPORTED_FLAG
-- Performing Test CXX_SUPPORTS_NO_CTAD_MAYBE_UNSUPPORTED_FLAG - Success
-- Performing Test FLANG_RUNTIME_HAS_FNO_LTO_FLAG
-- Performing Test FLANG_RUNTIME_HAS_FNO_LTO_FLAG - Success
CMake Error at /usr/local/share/cmake-3.26/Modules/CMakeDetermineCompilerId.cmake:751 (message):
  Compiling the CUDA compiler identification source file
  "CMakeCUDACompilerId.cu" failed.

  Compiler: /proj/nv/Linux_x86_64/dev/compilers/bin/nvcc

  Build flags:

  Id flags:
  --keep;--keep-dir;tmp;-ccbin=/local/home/psteinfeld/main/rel16/build/bin/clang++
  -v



  The output was:

  1

  #$ _NVVM_BRANCH_=nvvm

  #$ _SPACE_=

  #$ _CUDART_=cudart

  #$ _HERE_=/proj/nv/Linux_x86_64/232943-dev/cuda/12.2/bin

  #$ _THERE_=/proj/nv/Linux_x86_64/232943-dev/cuda/12.2/bin

  #$ _TARGET_SIZE_=

  #$ _TARGET_DIR_=

  #$ _TARGET_DIR_=targets/x86_64-linux

  #$ TOP=/proj/nv/Linux_x86_64/232943-dev/cuda/12.2/bin/..

  #$
  NVVMIR_LIBRARY_DIR=/proj/nv/Linux_x86_64/232943-dev/cuda/12.2/bin/../nvvm/libdevice


  #$
  LD_LIBRARY_PATH=/proj/nv/Linux_x86_64/232943-dev/cuda/12.2/bin/../lib:/home/sw/thirdparty/gcc/gcc-9.3.0/linux86-64/lib64:/opt/intel/oneapi/compiler/2023.1.0/linux/lib/oclfpga/host/linux64/lib:/opt/intel/oneapi/compiler/2023.1.0/linux/lib:/opt/intel/oneapi/compiler/2023.1.0/linux/lib/x64:/opt/intel/oneapi/compiler/2023.1.0/linux/compiler/lib/intel64_lin:/opt/intel/oneapi/tbb/2021.9.0/lib/intel64/gcc4.8:/opt/binutils/latest/lib64:/opt/binutils/latest/lib:/home/sw/envmod/modules/linux86-64/latest/lib


  #$
  PATH=/proj/nv/Linux_x86_64/232943-dev/cuda/12.2/bin/../nvvm/bin:/proj/nv/Linux_x86_64/232943-dev/cuda/12.2/bin:/home/sw/thirdparty/gcc/gcc-9.3.0/linux86-64/bin:/opt/binutils/latest/bin:/opt/intel/oneapi/compiler/2023.1.0/linux/bin/intel64:/opt/intel/oneapi/compiler/2023.1.0/linux/bin:/opt/intel/oneapi/compiler/2023.1.0/linux/lib/oclfpga/bin:/home/sw/envmod/modules/linux86-64/latest/bin:/home/sw/envmod/modules/linux86-64/4.2.5/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:.:./bin:/local/home/psteinfeld/main/rel13/install/bin:/home/psteinfeld/bin:/home/psteinfeld/bin/arcanist/arcanist/bin:/home/sw/thirdparty/llvm/linux86-64/7.1.0-f18/bin:/proj/nv/Linux_x86_64/dev/compilers/bin:/opt/nag-7.0.7048/bin:/proj/ta/bin:/opt/perforce/bin:/home/sw/thirdparty/ccollab-client:/home/psteinfeld/work/pgi/dev/bin:.:./bin:/local/home/psteinfeld/main/rel13/install/bin:/home/psteinfeld/bin:/home/psteinfeld/bin/arcanist/arcanist/bin:/home/sw/thirdparty/llvm/linux86-64/7.1.0-f18/bin:/proj/nv/Linux_x86_64/dev/compilers/bin:/opt/nag-7.0.7048/bin:/proj/ta/bin:/opt/perforce/bin:/home/sw/thirdparty/ccollab-client:/home/psteinfeld/work/pgi/dev/bin:.:./bin:/local/home/psteinfeld/main/rel13/install/bin:/home/psteinfeld/bin:/home/psteinfeld/bin/arcanist/arcanist/bin:/home/sw/thirdparty/llvm/linux86-64/7.1.0-f18/bin:/proj/nv/Linux_x86_64/dev/compilers/bin:/opt/nag-7.0.7048/bin:/proj/ta/bin:/opt/perforce/bin:/home/sw/thirdparty/ccollab-client:/home/psteinfeld/work/pgi/dev/bin:.:./bin:/local/home/psteinfeld/main/rel13/install/bin:/home/psteinfeld/bin:/home/psteinfeld/bin/arcanist/arcanist/bin:/home/sw/thirdparty/llvm/linux86-64/7.1.0-f18/bin:/proj/nv/Linux_x86_64/dev/compilers/bin:/opt/nag-7.0.7048/bin:/proj/ta/bin:/opt/perforce/bin:/home/sw/thirdparty/ccollab-client:/home/psteinfeld/work/pgi/dev/bin


  #$
  INCLUDES="-I/proj/nv/Linux_x86_64/232943-dev/cuda/12.2/bin/../targets/x86_64-linux/include"


  #$ LIBRARIES=
  "-L/proj/nv/Linux_x86_64/232943-dev/cuda/12.2/bin/../targets/x86_64-linux/lib/stubs"
  "-L/proj/nv/Linux_x86_64/232943-dev/cuda/12.2/bin/../targets/x86_64-linux/lib"


  #$ CUDAFE_FLAGS=

  #$ PTXAS_FLAGS=

  #$ rm tmp/a_dlink.reg.c

  #$ "/local/home/psteinfeld/main/rel16/build/bin"/clang++
  -D__CUDA_ARCH_LIST__=520 -E -x c++ -D__CUDACC__ -D__NVCC__
  -I"/proj/nv/Linux_x86_64/232943-dev/cuda/12.2/include"
  -I"/proj/nv/Linux_x86_64/232943-dev/math_libs/12.2/include"
  -I"/proj/nv/Linux_x86_64/232943-dev/comm_libs/12.2/nccl/include"
  -I"/proj/nv/Linux_x86_64/232943-dev/comm_libs/12.2/nvshmem/include"
  "-I/proj/nv/Linux_x86_64/232943-dev/cuda/12.2/bin/../targets/x86_64-linux/include"
  -D__CUDACC_VER_MAJOR__=12 -D__CUDACC_VER_MINOR__=2
  -D__CUDACC_VER_BUILD__=91 -D__CUDA_API_VER_MAJOR__=12
  -D__CUDA_API_VER_MINOR__=2 -D__NVCC_DIAG_PRAGMA_SUPPORT__=1 -include
  "cuda_runtime.h" -m64 "CMakeCUDACompilerId.cu" -o
  "tmp/CMakeCUDACompilerId.cpp4.ii"

  In file included from <built-in>:1:

  In file included from
  /proj/nv/Linux_x86_64/232943-dev/cuda/12.2/include/cuda_runtime.h:82:


  /proj/nv/Linux_x86_64/232943-dev/cuda/12.2/include/crt/host_config.h:144:2:
  error: -- unsupported clang version! clang version must be less than 16 and
  greater than 3.2 .  The nvcc flag '-allow-unsupported-compiler' can be used
  to override this version check; however, using an unsupported host compiler
  may cause compilation failure or incorrect run time execution.  Use at your
  own risk.

  #error -- unsupported clang version! clang version must be less than 16 and
  greater than 3.2 .  The nvcc flag '-allow-unsupported-compiler' can be used
  to override this version check; however, using an unsupported host compiler
  may cause compilation failure or incorrect run time execution.  Use at your
  own risk.

   ^

  1 error generated.

  # --error 0x1 --





Call Stack (most recent call first):
  /usr/local/share/cmake-3.26/Modules/CMakeDetermineCompilerId.cmake:8 (CMAKE_DETERMINE_COMPILER_ID_BUILD)
  /usr/local/share/cmake-3.26/Modules/CMakeDetermineCompilerId.cmake:53 (__determine_compiler_id_test)
  /usr/local/share/cmake-3.26/Modules/CMakeDetermineCUDACompiler.cmake:307 (CMAKE_DETERMINE_COMPILER_ID)
  /local/home/psteinfeld/main/029/flang/runtime/CMakeLists.txt:163 (enable_language)


-- Configuring incomplete, errors occurred!
Exit with error 0: cmake
flang/docs/GettingStarted.md
217

I think this should be rm -rf ...

vzakhari updated this revision to Diff 539702.Jul 12 2023, 1:19 PM

Thank you for the comments, Pete!

I added two notes to highlight potential tool compatibility issues:

  • NVCC only supports certain versions of clang as the host compiler.
  • Clang only supports certain versions of CUDA toolkit.
vzakhari marked an inline comment as done.Jul 12 2023, 1:25 PM