|
| 1 | +<!--===- README.md |
| 2 | +
|
| 3 | + Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | + See https://llvm.org/LICENSE.txt for license information. |
| 5 | + SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +
|
| 7 | +--> |
| 8 | + |
1 | 9 | # Fortran Runtime |
2 | 10 |
|
3 | 11 | Fortran Runtime is the runtime library for code emitted by the Flang |
4 | 12 | compiler (https://flang.llvm.org). |
5 | 13 |
|
| 14 | + |
6 | 15 | ## Getting Started |
7 | 16 |
|
8 | | -### Bootstrap Build |
| 17 | +There are two build modes for the FortranRuntime. The bootstrap build, also called the in-tree build, and the runtime-only build, also called the out-of-tree build. |
| 18 | +Not to be confused with the terms [in-source and out-of-source](https://cmake.org/cmake/help/latest/manual/cmake.1.html#introduction-to-cmake-buildsystems) builds as defined by CMake. In an in-source build, the source directory and the build directory are identical, whereas with an out-of-source build the build artifacts are stored somewhere else, possibly in a subdirectory of the source directory. LLVM does not support in-source builds. |
| 19 | + |
| 20 | + |
| 21 | +### Requirements |
9 | 22 |
|
10 | | -```sh |
11 | | -cmake -S <path-to-llvm-project>/llvm -DLLVM_ENABLE_PROJECTS=flang -DLLVM_ENABLE_RUNTIMES=FortranRuntime |
| 23 | +Requirements: |
| 24 | + * [Same as LLVM](https://llvm.org/docs/GettingStarted.html#requirements). |
| 25 | + * While for LLVM C++14 suffices, Flang and FortranRuntime require a C++17-capable C++ compiler. |
| 26 | + |
| 27 | + |
| 28 | +### Bootstrap/In-Tree Build |
| 29 | + |
| 30 | +The bootstrap build will first build Clang and Flang, then use these compilers to compile the FortranRuntime. |
| 31 | +CMake will create a secondary build tree in configured with these just-built compilers. The secondary build will reuse the same build options (Flags, Debug/Release, ...) as the primary build. It will also ensure that once built, the FortranRuntime is found by Flang from either the build- or install-prefix. To enable, add `FortranRuntime` to `LLVM_ENABLE_RUNTIMES`: |
| 32 | + |
| 33 | +```bash |
| 34 | +cmake -S <path-to-llvm-project>/llvm \ |
| 35 | + -DNinja \ |
| 36 | + -DLLVM_ENABLE_PROJECTS=flang \ |
| 37 | + -DLLVM_ENABLE_RUNTIMES=FortranRuntime \ |
| 38 | + ... |
12 | 39 | ``` |
13 | 40 |
|
14 | | -### Runtime-only build |
| 41 | +It is recommended to enable building OpenMP alongside Flang and FortranRuntime as well. This will build `omp_lib.mod` required to use OpenMP from Fortran. Building Compiler-RT may also be required, particularly on platforms that do not provide all C-ABI functionality (such as Windows). |
15 | 42 |
|
16 | | -```sh |
17 | | -cmake -S <path-to-llvm-project>/runtimes -DLLVM_ENABLE_RUNTIMES=FortranRuntime -DCMAKE_Fortran_COMPILER=<path-to-llvm-builddir-or-installprefix>/bin/flang-new |
| 43 | +```bash |
| 44 | +cmake -S <path-to-llvm-project>/llvm \ |
| 45 | + -DNinja \ |
| 46 | + -DCMAKE_BUILD_TYPE=Release \ |
| 47 | + -DLLVM_ENABLE_PROJECTS="flang;openmp" \ |
| 48 | + -DLLVM_ENABLE_RUNTIMES="compiler-rt;FortranRuntime" \ |
| 49 | + ... |
18 | 50 | ``` |
19 | 51 |
|
| 52 | +By default, the enabled runtimes will only be built for the host platform (`-DLLVM_RUNTIME_TARGETS=default`). To add additional targets to support cross-compilation via `flang-new --target=<target-triple>`, add more triples to `LLVM_RUNTIME_TARGETS`, such as `-DLLVM_RUNTIME_TARGETS="default;aarch64-linux-gnu"`. |
| 53 | + |
| 54 | +After configuration, build, test, and install the runtime via |
| 55 | + |
| 56 | +```shell |
| 57 | +$ ninja FortranRuntime |
| 58 | +$ ninja check-FortranRuntime |
| 59 | +$ ninja install-FortranRuntime |
| 60 | +``` |
| 61 | + |
| 62 | + |
| 63 | +### Runtime-only/Out-of-Tree Build |
| 64 | + |
| 65 | +Instead of building Clang and Flang from scratch, the Runtime-only build uses CMake's environment introspection to find a C, C++, and Fortran compiler. The compiler to be used can be controlled using CMake's standard mechanisms such as `CMAKE_CXX_COMPILER`, `CMAKE_CXX_COMPILER`, and `CMAKE_Fortran_COMPILER`. `CMAKE_Fortran_COMPILER` must be `flang-new` built from the same Git commit as FortranRuntime to ensure they are using the same ABI. The C and C++ compiler can be any compiler supporting the same ABI. |
| 66 | + |
| 67 | +In addition to the compiler, the build be able to find LLVM development tools such as `lit` and `FileCheck` that are not found in an LLVM's install directory. Use `CMAKE_BINARY_DIR` to point to directory where LLVM has been built. |
| 68 | +A simple build configuration maight look like the following: |
| 69 | + |
| 70 | +```bash |
| 71 | +cmake -S <path-to-llvm-project>/runtimes \ |
| 72 | + -GNinja \ |
| 73 | + -DLLVM_BINARY_DIR=<path-to-llvm-builddir> \ |
| 74 | + -DCMAKE_Fortran_COMPILER=<path-to-llvm-builddir>/bin/flang-new \ |
| 75 | + -DLLVM_ENABLE_RUNTIMES=FortranRuntime \ |
| 76 | + ... |
| 77 | +``` |
| 78 | + |
| 79 | +uilding the FortranRuntime for cross-compilation triple, the target triple can be selected using `LLVM_DEFAULT_TARGET_TRIPLE` AND `LLVM_RUNTIMES_TARGET`. Of course, FortranRuntime can be built multiple times with different build configurations, but have to be located manually when using with the Flang driver using the `-L` option. |
| 80 | + |
| 81 | +A more complete build configuration could be the following: |
| 82 | + |
| 83 | +```bash |
| 84 | +cmake -S <path-to-llvm-project>/runtimes \ |
| 85 | + -GNinja \ |
| 86 | + -DCMAKE_BUILD_TYPE=Release \ |
| 87 | + -DCMAKE_INSTALL_PREFIX="${HOME}/local" \ |
| 88 | + -DLLVM_ENABLE_RUNTIMES="compiler-rt;FortranRuntime" \ |
| 89 | + -DCMAKE_C_COMPILER=gcc \ |
| 90 | + -DCMAKE_CXX_COMPILER=g++ \ |
| 91 | + -DLLVM_BINARY_DIR=<path-to-llvm-builddir> \ |
| 92 | + -DLLVM_DIR=<path-to-llvm-builddir>/lib/cmake/llvm \ |
| 93 | + -DClang_DIR=<path-to-llvm-builddir>/lib/cmake/clang \ |
| 94 | + -DCMAKE_Fortran_COMPILER=<path-to-llvm-builddir-or-installprefix>/bin/flang-new \ |
| 95 | + -DLLVM_DEFAULT_TARGET_TRIPLE=x86_64-linux-gnu \ |
| 96 | + -DLLVM_RUNTIMES_TARGET=x86_64-linux-gnu \ |
| 97 | + ... |
| 98 | +``` |
| 99 | + |
| 100 | +## Configuration Option Reference |
| 101 | + |
| 102 | +The FortranRuntime has the followign configuration options. This is in addition to the build options the LLVM_ENABLE_RUNTIMES mechanism and CMake itself provide. |
| 103 | + |
| 104 | + * `FORTRANRUNTIME_INCLUDE_TESTS` (boolean; default: `ON`) |
| 105 | + |
| 106 | + When `OFF`, does not add any tests, unittests, and omits the `check-FortranRuntime` build target. |
| 107 | + |
| 108 | + * `FLANG_RUNTIME_F128_MATH_LIB` (default: `""`) |
| 109 | + |
| 110 | + Determines the implementation of `REAL(16)` math functions. If set to `libquadmath`, uses `quadmath.h` and `-lquadmath` typically distributed with gcc. If empty, disables `REAL(16)` support. For any other value, introspects the compiler for `__float128` or 128-bit `long double` support. [More details](Real16MathSupport.md). |
| 111 | + |
| 112 | + * `FORTRANRUNTIME_EXPERIMENTAL_OFFLOAD_SUPPORT` (values: `"CUDA"`,`"OpenMP"`, `""` default: `""`) |
| 113 | + |
| 114 | + When set to `CUDA`, builds a FortranRuntime with support for GPU accelerators using CUDA. `CMAKE_CUDA_COMPILER` must be set if not automatically detected by CMake. `nvcc` as well as `clang` are supported. |
| 115 | + |
| 116 | + When set to `OpenMP`, builds a FortranRuntime with support for GPU accelerators using OpenMP offloading. Only Clang is supported for `CMAKE_C_COMPILER` and `CMAKE_CXX_COMPILER`. |
| 117 | + |
| 118 | + * `FORTRANRUNTIME_ENABLE_CUF` (bool, default: `OFF`) |
| 119 | + |
| 120 | + Compiles the `libCufRuntime_cuda_<CUDA-version>.a/.so` library. This is independent of `FORTRANRUNTIME_EXPERIMENTAL_OFFLOAD_SUPPORT=CUDA` and only requires a [CUDA Toolkit installation](https://cmake.org/cmake/help/latest/module/FindCUDAToolkit.html) found (not `CMAKE_CUDA_COMPILER`). |
| 121 | + |
| 122 | + |
| 123 | +### CUDA Support |
| 124 | + |
| 125 | +With `-DFORTRANRUNTIME_EXPERIMENTAL_OFFLOAD_SUPPORT=CUDA`, the following additional configuration options become available. |
| 126 | + |
| 127 | + * `FORTRANRUNTIME_LIBCUDACXX_PATH` (path, default: `""`) |
| 128 | + |
| 129 | + Path to libcu++ package installation. |
| 130 | + |
| 131 | + * `FORTRANRUNTIME_CUDA_RUNTIME_PTX_WITHOUT_GLOBAL_VARS` (boolean, default: `OFF`) |
| 132 | + |
| 133 | + Do not compile global variables' definitions when producing PTX library. Default is `OFF`, meaning global variable definitions are compiled by default. |
| 134 | + |
| 135 | + |
| 136 | +### OpenMP Offload Support |
| 137 | + |
| 138 | +With `-DFORTRANRUNTIME_EXPERIMENTAL_OFFLOAD_SUPPORT=OpenMP`, the following additional configuration options become available. |
| 139 | + |
| 140 | + * `FORTRANRUNTIME_DEVICE_ARCHITECTURES` (default: `"all"`) |
20 | 141 |
|
| 142 | + A list of device architectures that the FortranRuntime is supporting. If `"all"` uses a pre-defined list of architectures. Same purpose as `LIBOMPTARGET_DEVICE_ARCHITECTURES` from liboffload. |
0 commit comments