David Truby | 413467f | 2020-04-09 21:12:54 +0100 | [diff] [blame] | 1 | # Flang |
Steve Scalpone | 282358f | 2020-03-27 09:23:32 -0700 | [diff] [blame] | 2 | |
David Truby | 413467f | 2020-04-09 21:12:54 +0100 | [diff] [blame] | 3 | Flang is a ground-up implementation of a Fortran front end written in modern |
| 4 | C++. It started off as the f18 project (https://github.com/flang-compiler/f18) |
| 5 | with an aim to replace the previous flang project |
| 6 | (https://github.com/flang-compiler/flang) and address its various deficiencies. |
| 7 | F18 was subsequently accepted into the LLVM project and rechristened as Flang. |
Stephane Chauveau | 63141a0 | 2018-05-03 14:54:53 +0200 | [diff] [blame] | 8 | |
Steve Scalpone | c90ce54 | 2019-03-19 23:47:18 -0700 | [diff] [blame] | 9 | ## Getting Started |
Stephane Chauveau | 63141a0 | 2018-05-03 14:54:53 +0200 | [diff] [blame] | 10 | |
sameeran joshi | eaff200 | 2020-08-18 15:05:51 +0530 | [diff] [blame] | 11 | Read more about flang in the [docs directory](docs). |
| 12 | Start with the [compiler overview](docs/Overview.md). |
psteinfeld | 1b50ccd | 2019-02-25 09:39:27 -0800 | [diff] [blame] | 13 | |
Steve Scalpone | c90ce54 | 2019-03-19 23:47:18 -0700 | [diff] [blame] | 14 | To better understand Fortran as a language |
David Truby | 413467f | 2020-04-09 21:12:54 +0100 | [diff] [blame] | 15 | and the specific grammar accepted by flang, |
sameeran joshi | eaff200 | 2020-08-18 15:05:51 +0530 | [diff] [blame] | 16 | read [Fortran For C Programmers](docs/FortranForCProgrammers.md) |
Steve Scalpone | c90ce54 | 2019-03-19 23:47:18 -0700 | [diff] [blame] | 17 | and |
brett koonce | f09587c | 2020-10-10 20:10:53 +0000 | [diff] [blame] | 18 | flang's specifications of the [Fortran grammar](docs/f2018-grammar.md) |
Steve Scalpone | c90ce54 | 2019-03-19 23:47:18 -0700 | [diff] [blame] | 19 | and |
brett koonce | f09587c | 2020-10-10 20:10:53 +0000 | [diff] [blame] | 20 | the [OpenMP grammar](docs/OpenMP-4.5-grammar.md). |
psteinfeld | 1b50ccd | 2019-02-25 09:39:27 -0800 | [diff] [blame] | 21 | |
Steve Scalpone | c90ce54 | 2019-03-19 23:47:18 -0700 | [diff] [blame] | 22 | Treatment of language extensions is covered |
sameeran joshi | eaff200 | 2020-08-18 15:05:51 +0530 | [diff] [blame] | 23 | in [this document](docs/Extensions.md). |
Steve Scalpone | c90ce54 | 2019-03-19 23:47:18 -0700 | [diff] [blame] | 24 | |
| 25 | To understand the compilers handling of intrinsics, |
sameeran joshi | eaff200 | 2020-08-18 15:05:51 +0530 | [diff] [blame] | 26 | see the [discussion of intrinsics](docs/Intrinsics.md). |
Steve Scalpone | c90ce54 | 2019-03-19 23:47:18 -0700 | [diff] [blame] | 27 | |
David Truby | 413467f | 2020-04-09 21:12:54 +0100 | [diff] [blame] | 28 | To understand how a flang program communicates with libraries at runtime, |
sameeran joshi | eaff200 | 2020-08-18 15:05:51 +0530 | [diff] [blame] | 29 | see the discussion of [runtime descriptors](docs/RuntimeDescriptor.md). |
Steve Scalpone | c90ce54 | 2019-03-19 23:47:18 -0700 | [diff] [blame] | 30 | |
| 31 | If you're interested in contributing to the compiler, |
sameeran joshi | eaff200 | 2020-08-18 15:05:51 +0530 | [diff] [blame] | 32 | read the [style guide](docs/C++style.md) |
Steve Scalpone | c90ce54 | 2019-03-19 23:47:18 -0700 | [diff] [blame] | 33 | and |
sameeran joshi | eaff200 | 2020-08-18 15:05:51 +0530 | [diff] [blame] | 34 | also review [how flang uses modern C++ features](docs/C++17.md). |
Stephane Chauveau | 63141a0 | 2018-05-03 14:54:53 +0200 | [diff] [blame] | 35 | |
Sameeran joshi | bc9cdfa | 2020-08-24 19:51:45 +0530 | [diff] [blame] | 36 | If you are interested in writing new documentation, follow |
| 37 | [markdown style guide from LLVM](https://github.com/llvm/llvm-project/blob/master/llvm/docs/MarkdownQuickstartTemplate.md). |
| 38 | |
David Truby | 413467f | 2020-04-09 21:12:54 +0100 | [diff] [blame] | 39 | ## Supported C++ compilers |
Stephane Chauveau | 63141a0 | 2018-05-03 14:54:53 +0200 | [diff] [blame] | 40 | |
David Truby | 413467f | 2020-04-09 21:12:54 +0100 | [diff] [blame] | 41 | Flang is written in C++17. |
Stephane Chauveau | 63141a0 | 2018-05-03 14:54:53 +0200 | [diff] [blame] | 42 | |
Steve Scalpone | 2a69600 | 2018-05-18 13:02:58 -0700 | [diff] [blame] | 43 | The code has been compiled and tested with |
David Truby | 413467f | 2020-04-09 21:12:54 +0100 | [diff] [blame] | 44 | GCC versions from 7.2.0 to 9.3.0. |
Stephane Chauveau | 63141a0 | 2018-05-03 14:54:53 +0200 | [diff] [blame] | 45 | |
Steve Scalpone | c90ce54 | 2019-03-19 23:47:18 -0700 | [diff] [blame] | 46 | The code has been compiled and tested with |
David Truby | 413467f | 2020-04-09 21:12:54 +0100 | [diff] [blame] | 47 | clang version 7.0, 8.0, 9.0 and 10.0 |
Steve Scalpone | c90ce54 | 2019-03-19 23:47:18 -0700 | [diff] [blame] | 48 | using either GNU's libstdc++ or LLVM's libc++. |
| 49 | |
David Truby | 413467f | 2020-04-09 21:12:54 +0100 | [diff] [blame] | 50 | The code has been compiled on |
| 51 | AArch64, x86\_64 and ppc64le servers |
| 52 | with CentOS7, Ubuntu18.04, Rhel, MacOs, Mojave, XCode and |
| 53 | Apple Clang version 10.0.1. |
| 54 | |
| 55 | The code does not compile with Windows and a compiler that does not have |
| 56 | support for C++17. |
| 57 | |
| 58 | ## Building Flang out of tree |
| 59 | These instructions are for building Flang separately from LLVM; if you are |
| 60 | building Flang alongside LLVM then follow the standard LLVM build instructions |
| 61 | and add flang to `LLVM_ENABLE_PROJECTS` instead, as detailed there. |
| 62 | |
Steve Scalpone | c90ce54 | 2019-03-19 23:47:18 -0700 | [diff] [blame] | 63 | ### LLVM dependency |
| 64 | |
Steve Scalpone | c90ce54 | 2019-03-19 23:47:18 -0700 | [diff] [blame] | 65 | The instructions to build LLVM can be found at |
David Truby | 413467f | 2020-04-09 21:12:54 +0100 | [diff] [blame] | 66 | https://llvm.org/docs/GettingStarted.html. If you are building flang as part |
| 67 | of LLVM, follow those instructions and add flang to `LLVM_ENABLE_PROJECTS`. |
Steve Scalpone | c90ce54 | 2019-03-19 23:47:18 -0700 | [diff] [blame] | 68 | |
David Truby | 413467f | 2020-04-09 21:12:54 +0100 | [diff] [blame] | 69 | We highly recommend using the same compiler to compile both llvm and flang. |
Steve Scalpone | c90ce54 | 2019-03-19 23:47:18 -0700 | [diff] [blame] | 70 | |
David Truby | 413467f | 2020-04-09 21:12:54 +0100 | [diff] [blame] | 71 | The flang CMakeList.txt file uses |
Steve Scalpone | 282358f | 2020-03-27 09:23:32 -0700 | [diff] [blame] | 72 | the variable `LLVM_DIR` to find the installed LLVM components |
| 73 | and |
| 74 | the variable `MLIR_DIR` to find the installed MLIR components. |
Steve Scalpone | c90ce54 | 2019-03-19 23:47:18 -0700 | [diff] [blame] | 75 | |
David Truby | 413467f | 2020-04-09 21:12:54 +0100 | [diff] [blame] | 76 | To get the correct LLVM and MLIR libraries included in your flang build, |
Steve Scalpone | 282358f | 2020-03-27 09:23:32 -0700 | [diff] [blame] | 77 | define LLVM_DIR and MLIR_DIR on the cmake command line. |
Steve Scalpone | c90ce54 | 2019-03-19 23:47:18 -0700 | [diff] [blame] | 78 | ``` |
Steve Scalpone | 282358f | 2020-03-27 09:23:32 -0700 | [diff] [blame] | 79 | LLVM=<LLVM_BUILD_DIR>/lib/cmake/llvm \ |
| 80 | MLIR=<LLVM_BUILD_DIR>/lib/cmake/mlir \ |
| 81 | cmake -DLLVM_DIR=$LLVM -DMLIR_DIR=$MLIR ... |
Steve Scalpone | c90ce54 | 2019-03-19 23:47:18 -0700 | [diff] [blame] | 82 | ``` |
Steve Scalpone | a705a4a | 2020-03-05 07:12:06 -0800 | [diff] [blame] | 83 | where `LLVM_BUILD_DIR` is |
| 84 | the top-level directory where LLVM was built. |
CarolineConcatto | 0d9dd49 | 2020-01-14 16:20:49 +0000 | [diff] [blame] | 85 | |
David Truby | 413467f | 2020-04-09 21:12:54 +0100 | [diff] [blame] | 86 | ### Building flang with GCC |
peter klausler | 1e036b2 | 2018-05-07 14:39:36 -0700 | [diff] [blame] | 87 | |
Steve Scalpone | 2a69600 | 2018-05-18 13:02:58 -0700 | [diff] [blame] | 88 | By default, |
| 89 | cmake will search for g++ on your PATH. |
Steve Scalpone | c90ce54 | 2019-03-19 23:47:18 -0700 | [diff] [blame] | 90 | The g++ version must be one of the supported versions |
David Truby | 413467f | 2020-04-09 21:12:54 +0100 | [diff] [blame] | 91 | in order to build flang. |
Stephane Chauveau | 63141a0 | 2018-05-03 14:54:53 +0200 | [diff] [blame] | 92 | |
David Truby | 413467f | 2020-04-09 21:12:54 +0100 | [diff] [blame] | 93 | Or, cmake will use the variable CXX to find the C++ compiler. CXX should include |
| 94 | the full path to the compiler or a name that will be found on your PATH, e.g. |
| 95 | g++-8.3, assuming g++-8.3 is on your PATH. |
| 96 | |
Steve Scalpone | 2a69600 | 2018-05-18 13:02:58 -0700 | [diff] [blame] | 97 | ``` |
Steve Scalpone | 282358f | 2020-03-27 09:23:32 -0700 | [diff] [blame] | 98 | export CXX=g++-8.3 |
Steve Scalpone | 2a69600 | 2018-05-18 13:02:58 -0700 | [diff] [blame] | 99 | ``` |
Steve Scalpone | c90ce54 | 2019-03-19 23:47:18 -0700 | [diff] [blame] | 100 | or |
| 101 | ``` |
Steve Scalpone | 282358f | 2020-03-27 09:23:32 -0700 | [diff] [blame] | 102 | CXX=/opt/gcc-8.3/bin/g++-8.3 cmake ... |
Steve Scalpone | c90ce54 | 2019-03-19 23:47:18 -0700 | [diff] [blame] | 103 | ``` |
Steve Scalpone | c90ce54 | 2019-03-19 23:47:18 -0700 | [diff] [blame] | 104 | |
David Truby | 413467f | 2020-04-09 21:12:54 +0100 | [diff] [blame] | 105 | ### Building flang with clang |
Steve Scalpone | c90ce54 | 2019-03-19 23:47:18 -0700 | [diff] [blame] | 106 | |
David Truby | 413467f | 2020-04-09 21:12:54 +0100 | [diff] [blame] | 107 | To build flang with clang, |
Steve Scalpone | c90ce54 | 2019-03-19 23:47:18 -0700 | [diff] [blame] | 108 | cmake needs to know how to find clang++ |
| 109 | and the GCC library and tools that were used to build clang++. |
| 110 | |
Steve Scalpone | c90ce54 | 2019-03-19 23:47:18 -0700 | [diff] [blame] | 111 | CXX should include the full path to clang++ |
| 112 | or clang++ should be found on your PATH. |
| 113 | ``` |
| 114 | export CXX=clang++ |
| 115 | ``` |
Steve Scalpone | 2a69600 | 2018-05-18 13:02:58 -0700 | [diff] [blame] | 116 | |
| 117 | ### Installation Directory |
| 118 | |
| 119 | To specify a custom install location, |
| 120 | add |
| 121 | `-DCMAKE_INSTALL_PREFIX=<INSTALL_PREFIX>` |
| 122 | to the cmake command |
| 123 | where `<INSTALL_PREFIX>` |
David Truby | 413467f | 2020-04-09 21:12:54 +0100 | [diff] [blame] | 124 | is the path where flang should be installed. |
Steve Scalpone | 2a69600 | 2018-05-18 13:02:58 -0700 | [diff] [blame] | 125 | |
| 126 | ### Build Types |
| 127 | |
| 128 | To create a debug build, |
| 129 | add |
| 130 | `-DCMAKE_BUILD_TYPE=Debug` |
| 131 | to the cmake command. |
| 132 | Debug builds execute slowly. |
| 133 | |
| 134 | To create a release build, |
| 135 | add |
| 136 | `-DCMAKE_BUILD_TYPE=Release` |
| 137 | to the cmake command. |
| 138 | Release builds execute quickly. |
| 139 | |
David Truby | 413467f | 2020-04-09 21:12:54 +0100 | [diff] [blame] | 140 | ### Build Flang out of tree |
Steve Scalpone | 2a69600 | 2018-05-18 13:02:58 -0700 | [diff] [blame] | 141 | ``` |
David Truby | 413467f | 2020-04-09 21:12:54 +0100 | [diff] [blame] | 142 | cd ~/flang/build |
| 143 | cmake -DLLVM_DIR=$LLVM -DMLIR_DIR=$MLIR ~/flang/src |
Steve Scalpone | 2a69600 | 2018-05-18 13:02:58 -0700 | [diff] [blame] | 144 | make |
| 145 | ``` |
Caroline Concatto | 257b297 | 2020-09-11 10:17:31 +0100 | [diff] [blame] | 146 | |
| 147 | ### Build The New Flang Driver |
| 148 | The new Flang driver, `flang-new`, is currently under active development and |
| 149 | should be considered as an experimental feature. For this reason it is disabled |
| 150 | by default. This will change once the new driver replaces the _throwaway_ |
| 151 | driver, `flang`. |
| 152 | |
Andrzej Warzynski | 27da287 | 2020-09-24 09:34:43 +0100 | [diff] [blame] | 153 | In order to build the new driver, add `-DFLANG_BUILD_NEW_DRIVER=ON` to your |
Caroline Concatto | 257b297 | 2020-09-11 10:17:31 +0100 | [diff] [blame] | 154 | CMake invocation line. Additionally, when building out-of-tree, use `CLANG_DIR` |
| 155 | (similarly to `LLVM_DIR` and `MLIR_DIR`) to find the installed Clang |
| 156 | components. |
| 157 | |
| 158 | **Note:** `CLANG_DIR` is only required when building the new Flang driver, |
| 159 | which currently depends on Clang. |
| 160 | |
sameeran joshi | 93f602b | 2020-06-02 22:45:44 +0530 | [diff] [blame] | 161 | # How to Run Tests |
CarolineConcatto | 0d9dd49 | 2020-01-14 16:20:49 +0000 | [diff] [blame] | 162 | |
sameeran joshi | 93f602b | 2020-06-02 22:45:44 +0530 | [diff] [blame] | 163 | Flang supports 2 different categories of tests |
| 164 | 1. Regression tests (https://www.llvm.org/docs/TestingGuide.html#regression-tests) |
| 165 | 2. Unit tests (https://www.llvm.org/docs/TestingGuide.html#unit-tests) |
| 166 | |
| 167 | ## For out of tree builds |
CarolineConcatto | 0d9dd49 | 2020-01-14 16:20:49 +0000 | [diff] [blame] | 168 | To run all tests: |
| 169 | ``` |
David Truby | 413467f | 2020-04-09 21:12:54 +0100 | [diff] [blame] | 170 | cd ~/flang/build |
| 171 | cmake -DLLVM_DIR=$LLVM -DMLIR_DIR=$MLIR ~/flang/src |
Tim Keith | 6905526 | 2020-03-16 10:46:17 -0700 | [diff] [blame] | 172 | make test check-all |
CarolineConcatto | 0d9dd49 | 2020-01-14 16:20:49 +0000 | [diff] [blame] | 173 | ``` |
| 174 | |
| 175 | To run individual regression tests llvm-lit needs to know the lit |
David Truby | 413467f | 2020-04-09 21:12:54 +0100 | [diff] [blame] | 176 | configuration for flang. The parameters in charge of this are: |
Richard Barton | 7e5dab5 | 2020-09-07 16:33:55 +0100 | [diff] [blame] | 177 | flang_site_config and flang_config. And they can be set as shown below: |
CarolineConcatto | 0d9dd49 | 2020-01-14 16:20:49 +0000 | [diff] [blame] | 178 | ``` |
| 179 | <path-to-llvm-lit>/llvm-lit \ |
David Truby | 413467f | 2020-04-09 21:12:54 +0100 | [diff] [blame] | 180 | --param flang_site_config=<path-to-flang-build>/test-lit/lit.site.cfg.py \ |
| 181 | --param flang_config=<path-to-flang-build>/test-lit/lit.cfg.py \ |
CarolineConcatto | 0d9dd49 | 2020-01-14 16:20:49 +0000 | [diff] [blame] | 182 | <path-to-fortran-test> |
sameeran joshi | 93f602b | 2020-06-02 22:45:44 +0530 | [diff] [blame] | 183 | |
| 184 | ``` |
| 185 | |
| 186 | Unit tests: |
| 187 | |
| 188 | If flang was built with `-DFLANG_INCLUDE_TESTS=On` (`ON` by default), it is possible to generate unittests. |
| 189 | Note: Unit-tests will be skipped for LLVM install for an out-of-tree build as it does not include googletest related headers and libraries. |
| 190 | |
| 191 | There are various ways to run unit-tests. |
| 192 | |
| 193 | ``` |
| 194 | |
| 195 | 1. make check-flang-unit |
| 196 | 2. make check-all or make check-flang |
| 197 | 3. <path-to-llvm-lit>/llvm-lit \ |
| 198 | test/Unit |
| 199 | 4. Invoking tests from <out-of-tree flang build>/unittests/<respective unit test folder> |
| 200 | |
| 201 | ``` |
| 202 | |
| 203 | |
| 204 | ## For in tree builds |
| 205 | If flang was built with `-DFLANG_INCLUDE_TESTS=On` (`On` by default), it is possible to |
| 206 | generate unittests. |
| 207 | |
| 208 | To run all of the flang unit tests use the `check-flang-unit` target: |
| 209 | ``` |
| 210 | make check-flang-unit |
| 211 | ``` |
| 212 | To run all of the flang regression tests use the `check-flang` target: |
| 213 | ``` |
| 214 | make check-flang |
CarolineConcatto | 0d9dd49 | 2020-01-14 16:20:49 +0000 | [diff] [blame] | 215 | ``` |
jeanPerier | 57f536a | 2020-03-11 21:47:22 -0700 | [diff] [blame] | 216 | |
sameeran joshi | 807fe05 | 2020-04-27 14:52:36 +0530 | [diff] [blame] | 217 | # How to Generate Documentation |
jeanPerier | 57f536a | 2020-03-11 21:47:22 -0700 | [diff] [blame] | 218 | |
sameeran joshi | 807fe05 | 2020-04-27 14:52:36 +0530 | [diff] [blame] | 219 | ## Generate FIR Documentation |
| 220 | If flang was built with `-DLINK_WITH_FIR=On` (`On` by default), it is possible to |
jeanPerier | 57f536a | 2020-03-11 21:47:22 -0700 | [diff] [blame] | 221 | generate FIR language documentation by running `make flang-doc`. This will |
sameeran joshi | 807fe05 | 2020-04-27 14:52:36 +0530 | [diff] [blame] | 222 | create `docs/Dialect/FIRLangRef.md` in flang build directory. |
| 223 | |
| 224 | ## Generate Doxygen-based Documentation |
| 225 | To generate doxygen-style documentation from source code |
| 226 | - Pass `-DLLVM_ENABLE_DOXYGEN=ON -DFLANG_INCLUDE_DOCS=ON` to the cmake command. |
| 227 | |
| 228 | ``` |
| 229 | cd ~/llvm-project/build |
| 230 | cmake -DLLVM_ENABLE_DOXYGEN=ON -DFLANG_INCLUDE_DOCS=ON ../llvm |
| 231 | make doxygen-flang |
Richard Barton | 7e5dab5 | 2020-09-07 16:33:55 +0100 | [diff] [blame] | 232 | ``` |
sameeran joshi | 807fe05 | 2020-04-27 14:52:36 +0530 | [diff] [blame] | 233 | |
| 234 | It will generate html in |
| 235 | |
Richard Barton | 7e5dab5 | 2020-09-07 16:33:55 +0100 | [diff] [blame] | 236 | ``` |
sameeran joshi | 807fe05 | 2020-04-27 14:52:36 +0530 | [diff] [blame] | 237 | <build-dir>/tools/flang/docs/doxygen/html # for flang docs |
| 238 | ``` |
Sameeran joshi | bc9cdfa | 2020-08-24 19:51:45 +0530 | [diff] [blame] | 239 | ## Generate Sphinx-based Documentation |
| 240 | <!TODO: Add webpage once we have a website. |
| 241 | !> |
| 242 | Flang documentation should preferably be written in `markdown(.md)` syntax (they can be in `reStructuredText(.rst)` format as well but markdown is recommended in first place), it |
| 243 | is mostly meant to be processed by the Sphinx documentation generation |
| 244 | system to create HTML pages which would be hosted on the webpage of flang and |
| 245 | updated periodically. |
| 246 | |
Richard Barton | 7e5dab5 | 2020-09-07 16:33:55 +0100 | [diff] [blame] | 247 | If you would like to generate and view the HTML locally: |
| 248 | - Install [Sphinx](http://sphinx-doc.org/), including the [sphinx-markdown-tables](https://pypi.org/project/sphinx-markdown-tables/) extension. |
Sameeran joshi | bc9cdfa | 2020-08-24 19:51:45 +0530 | [diff] [blame] | 249 | - Pass `-DLLVM_ENABLE_SPHINX=ON -DSPHINX_WARNINGS_AS_ERRORS=OFF` to the cmake command. |
| 250 | |
| 251 | ``` |
| 252 | cd ~/llvm-project/build |
| 253 | cmake -DLLVM_ENABLE_SPHINX=ON -DSPHINX_WARNINGS_AS_ERRORS=OFF ../llvm |
| 254 | make docs-flang-html |
Richard Barton | 7e5dab5 | 2020-09-07 16:33:55 +0100 | [diff] [blame] | 255 | ``` |
Sameeran joshi | bc9cdfa | 2020-08-24 19:51:45 +0530 | [diff] [blame] | 256 | |
| 257 | It will generate html in |
| 258 | |
Richard Barton | 7e5dab5 | 2020-09-07 16:33:55 +0100 | [diff] [blame] | 259 | ``` |
Sameeran joshi | bc9cdfa | 2020-08-24 19:51:45 +0530 | [diff] [blame] | 260 | $BROWSER <build-dir>/tools/flang/docs/html/ |
| 261 | ``` |