Skip to content

Commit f6d9f37

Browse files
committed
Add documentation
1 parent 5c2ae6b commit f6d9f37

File tree

3 files changed

+142
-14
lines changed

3 files changed

+142
-14
lines changed

FortranRuntime/README.md

Lines changed: 128 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,142 @@
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+
19
# Fortran Runtime
210

311
Fortran Runtime is the runtime library for code emitted by the Flang
412
compiler (https://flang.llvm.org).
513

14+
615
## Getting Started
716

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
922

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+
...
1239
```
1340

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).
1542

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+
...
1850
```
1951

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"`)
20141

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.

flang/docs/GettingStarted.md

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ https://llvm.org/docs/GettingStarted.html.
3030
All of the examples below use GCC as the C/C++ compilers and ninja as the build
3131
tool.
3232

33-
### Building flang in tree
33+
### Building flang in tree (bootstrap build)
3434
Building flang in tree means building flang along with all of the projects on
35-
which it depends. These projects include mlir, clang, flang, openmp, and
36-
compiler-rt. Note that compiler-rt is only needed to access libraries that
35+
which it depends. These projects include mlir, clang, flang, openmp,
36+
compiler-rt, and FortranRuntime. Note that compiler-rt is only needed to access libraries that
3737
support 16 bit floating point numbers. It's not needed to run the automated
3838
tests. You can use several different C++ compilers for most of the build,
3939
includig GNU and clang. But building compiler-rt requres using the clang
@@ -82,7 +82,7 @@ cmake \
8282
-DLLVM_TARGETS_TO_BUILD=host \
8383
-DLLVM_LIT_ARGS=-v \
8484
-DLLVM_ENABLE_PROJECTS="clang;mlir;flang;openmp" \
85-
-DLLVM_ENABLE_RUNTIMES="compiler-rt" \
85+
-DLLVM_ENABLE_RUNTIMES="compiler-rt;FortranRuntime" \
8686
../llvm-project/llvm
8787

8888
ninja
@@ -209,13 +209,14 @@ mkdir build_flang_runtime
209209
cd build_flang_runtime
210210

211211
cmake \
212+
-DLLVM_ENABLE_RUNTIMES=FortranRuntime \
212213
-DFORTRANRUNTIME_EXPERIMENTAL_OFFLOAD_SUPPORT=CUDA \
213214
-DCMAKE_CUDA_ARCHITECTURES=80 \
214215
-DCMAKE_C_COMPILER=clang \
215216
-DCMAKE_CXX_COMPILER=clang++ \
216217
-DCMAKE_CUDA_COMPILER=clang \
217218
-DCMAKE_CUDA_HOST_COMPILER=clang++ \
218-
../runtime/
219+
../runtimes/
219220
make -j FortranRuntime
220221
```
221222

@@ -231,13 +232,14 @@ mkdir build_flang_runtime
231232
cd build_flang_runtime
232233

233234
cmake \
235+
-DLLVM_ENABLE_RUNTIMES=FortranRuntime \
234236
-DFORTRANRUNTIME_EXPERIMENTAL_OFFLOAD_SUPPORT=CUDA \
235237
-DCMAKE_CUDA_ARCHITECTURES=80 \
236238
-DCMAKE_C_COMPILER=clang \
237239
-DCMAKE_CXX_COMPILER=clang++ \
238240
-DCMAKE_CUDA_COMPILER=nvcc \
239241
-DCMAKE_CUDA_HOST_COMPILER=clang++ \
240-
../runtime/
242+
../runtimes/
241243

242244
make -j FortranRuntime
243245
```
@@ -251,13 +253,14 @@ code. Note that the packaging of the libraries is different
251253
between [Clang](https://clang.llvm.org/docs/OffloadingDesign.html#linking-target-device-code) and NVCC, so the library must be linked using
252254
compatible compiler drivers.
253255

254-
#### Building in-tree
256+
#### Building in-tree (bootstrap build)
255257
One may build Flang runtime library along with building Flang itself
256258
by providing these additional CMake variables on top of the Flang in-tree
257259
build config:
258260

259261
For example:
260262
```bash
263+
-DLLVM_ENABLE_RUNTIMES=FortranRuntime \
261264
-DFORTRANRUNTIME_EXPERIMENTAL_OFFLOAD_SUPPORT=CUDA \
262265
-DCMAKE_CUDA_ARCHITECTURES=80 \
263266
-DCMAKE_C_COMPILER=clang \
@@ -268,6 +271,7 @@ For example:
268271

269272
Or:
270273
```bash
274+
-DLLVM_ENABLE_RUNTIMES=FortranRuntime \
271275
-DFORTRANRUNTIME_EXPERIMENTAL_OFFLOAD_SUPPORT=CUDA \
272276
-DCMAKE_CUDA_ARCHITECTURES=80 \
273277
-DCMAKE_C_COMPILER=gcc \
@@ -288,11 +292,12 @@ mkdir build_flang_runtime
288292
cd build_flang_runtime
289293

290294
cmake \
295+
-DLLVM_ENABLE_RUNTIMES=FortranRuntime \
291296
-DFORTRANRUNTIME_EXPERIMENTAL_OFFLOAD_SUPPORT="OpenMP" \
292297
-DCMAKE_C_COMPILER=clang \
293298
-DCMAKE_CXX_COMPILER=clang++ \
294299
-DFORTRANRUNTIME_DEVICE_ARCHITECTURES="all" \
295-
../runtime/
300+
../runtimes/
296301

297302
make -j FortranRuntime
298303
```

llvm/runtimes/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ function(runtime_default_target)
281281
PASSTHROUGH_PREFIXES LLVM_ENABLE_RUNTIMES
282282
LLVM_USE_LINKER
283283
CUDA # For runtimes that may look for the CUDA SDK (libc, offload)
284+
FLANG_RUNTIME # Shared between Flang and FortranRuntime
284285
${ARG_PREFIXES}
285286
EXTRA_TARGETS ${extra_targets}
286287
${test_targets}

0 commit comments

Comments
 (0)