|
13 | 13 | :local:
|
14 | 14 | ```
|
15 | 15 |
|
| 16 | + |
| 17 | +> **_NOTE:_** This document assumes that Flang's drivers can already generate code and |
| 18 | +> produce executables. However, this is still work-in-progress. By making this |
| 19 | +> assumption, we are able to prepare this document ahead-of-time and to provide |
| 20 | +> an overview of the design that we are working towards. |
| 21 | +
|
16 | 22 | There are two main drivers in Flang:
|
17 | 23 | * the compiler driver, `flang-new`
|
18 | 24 | * the frontend driver, `flang-new -fc1`
|
19 | 25 |
|
20 |
| -The compiler driver will allow you to control all compilation phases (i.e. |
21 |
| -preprocessing, frontend code-generation, middlend/backend code-optimisation and |
22 |
| -lowering, linking). For frontend specific tasks, the compiler driver creates a |
23 |
| -Fortran compilation job and delegates it to `flang-new -fc1`, the frontend driver. |
| 26 | +> **_NOTE:_** The diagrams in this document refer to `flang` as opposed to |
| 27 | +> `flang-new`. This is because the diagrams reflect the final design that we |
| 28 | +> are still working towards. See the note on [the flang script](https://github.com/llvm/llvm-project/blob/main/flang/docs/FlangDriver.md#the-flang-script) |
| 29 | +> below for more context. |
| 30 | +
|
| 31 | +The **compiler driver** will allow you to control all compilation phases (e.g. |
| 32 | +preprocessing, semantic checks, code-generation, code-optimisation, lowering |
| 33 | +and linking). For frontend specific tasks, the compiler driver creates a |
| 34 | +Fortran compilation job and delegates it to `flang-new -fc1`, the frontend |
| 35 | +driver. For linking, it creates a linker job and calls an external linker (e.g. |
| 36 | +LLVM's [`lld`](https://lld.llvm.org/)). It can also call other tools such as |
| 37 | +external assemblers (e.g. [`as`](https://www.gnu.org/software/binutils/)). In |
| 38 | +Clang, the compiler driver can also link the generated binaries with LLVM's |
| 39 | +static analysis/sanitizer libraries (e.g. |
| 40 | +[MemorySanitizer](https://clang.llvm.org/docs/MemorySanitizer.html)). This is |
| 41 | +not yet available in Flang, but will be relatively easy to support once such |
| 42 | +libraries become available. Flang's compiler driver is intended for Flang's |
| 43 | +end-users - its interface needs to remain stable. Otherwise, Flang's users will |
| 44 | +have to adjust their build scripts every time a compiler flag is changed. |
| 45 | + |
| 46 | +|  | |
| 47 | +|:--:| |
| 48 | +| *Flang’s compiler driver and the **tools** that it runs* | |
| 49 | + |
| 50 | +The **frontend driver** glues together and drives all of the Flang's frontend |
| 51 | +libraries. As such, it provides an easy-to-use and intuitive interface to the |
| 52 | +frontend. It uses MLIR and LLVM for code-generation and can be viewed as a |
| 53 | +driver for Flang, LLVM and MLIR libraries. Contrary to the compiler driver, it |
| 54 | +is not capable of calling any external tools (including linkers). It is aware |
| 55 | +of all the frontend internals that are "hidden" from the compiler driver. It |
| 56 | +accepts many frontend-specific options not available in `flang-new` and as such |
| 57 | +it provides a finer control over the frontend. Note that this tool is mostly |
| 58 | +intended for Flang developers. In particular, there are no guarantees about the |
| 59 | +stability of its interface and compiler developers can use it to experiment |
| 60 | +with new flags. |
| 61 | + |
| 62 | +|  | |
| 63 | +|:-:| |
| 64 | +| *Flang's frontend driver and the **libraries** that it drives* | |
| 65 | + |
| 66 | +Note that similarly to `-Xclang` in `clang`, you can use `-Xflang` to forward a |
| 67 | +frontend specific flag from the _compiler_ directly to the _frontend_ driver, |
| 68 | +e.g.: |
| 69 | + |
| 70 | +```lang=bash |
| 71 | +flang-new -Xflang -fdebug-dump-parse-tree input.f95 |
| 72 | +``` |
24 | 73 |
|
25 |
| -The frontend driver glues all of the frontend libraries together and provides |
26 |
| -an easy-to-use and intuitive interface to the frontend. It accepts many |
27 |
| -frontend-specific options not available in `flang-new` and as such it provides a |
28 |
| -finer control over the frontend. Similarly to `-Xclang` in `clang`, you can use |
29 |
| -`-Xflang` to forward the frontend specific flags from the compiler directly to |
30 |
| -the frontend driver. |
| 74 | +In the invocation above, `-fdebug-dump-parse-tree` is forwarded to `flang-new |
| 75 | +-fc1`. Without the forwarding flag, `-Xflang`, you would see the following |
| 76 | +warning: |
31 | 77 |
|
32 |
| -## Compiler Driver |
| 78 | +```lang=bash |
| 79 | +flang-new: warning: argument unused during compilation: |
| 80 | +``` |
| 81 | + |
| 82 | +As `-fdebug-dump-parse-tree` is only supported by `flang-new -fc1`, `flang-new` |
| 83 | +will ignore it when used without `Xflang`. |
| 84 | + |
| 85 | +## Why Do We Need Two Drivers? |
| 86 | +As hinted above, `flang-new` and `flang-new -fc1` are two separate tools. The |
| 87 | +fact that these tools are accessed through one binary, `flang-new`, is just an |
| 88 | +implementation detail. Each tool has a separate list of options, albeit defined |
| 89 | +in the same file: `clang/include/clang/Driver/Options.td`. |
| 90 | + |
| 91 | +The separation helps us split various tasks and allows us to implement more |
| 92 | +specialised tools. In particular, `flang-new` is not aware of various |
| 93 | +compilation phases within the frontend (e.g. scanning, parsing or semantic |
| 94 | +checks). It does not have to be. Conversely, the frontend driver, `flang-new |
| 95 | +-fc1`, needs not to be concerned with linkers or other external tools like |
| 96 | +assemblers. Nor does it need to know where to look for various systems |
| 97 | +libraries, which is usually OS and platform specific. |
| 98 | + |
| 99 | +One helpful way of differentiating these tools is to keep in mind that: |
| 100 | + |
| 101 | +* the compiler driver is an end-user tool |
| 102 | +* frontend driver is a compiler developer tool with many additional options, |
| 103 | + |
| 104 | +Also, Since the compiler driver can call external tools, e.g. linkers, it can |
| 105 | +be used to generate **executables**. The frontend driver cannot call external |
| 106 | +tools and hence can only generate **object files**. A similar model is |
| 107 | +implemented in Clang (`clang` vs `clang -cc1` vs `clang -cc1as`), which is |
| 108 | +based on the [architecture of |
| 109 | +GCC](https://en.wikibooks.org/wiki/GNU_C_Compiler_Internals/GNU_C_Compiler_Architecture). |
| 110 | +In fact, Flang needs to adhere to this model in order to be able to re-use |
| 111 | +Clang's driver library. If you are more familiar with the [architecture of |
| 112 | +GFortran](https://gcc.gnu.org/onlinedocs/gcc-4.7.4/gfortran/About-GNU-Fortran.html) |
| 113 | +than Clang, then `flang-new` corresponds to `gfortran` and `flang-new -fc1` to |
| 114 | +`f951`. |
33 | 115 |
|
| 116 | +## Compiler Driver |
34 | 117 | The main entry point for Flang's compiler driver is implemented in
|
35 | 118 | `flang/tools/flang-driver/driver.cpp`. Flang's compiler driver is implemented
|
36 | 119 | in terms of Clang's driver library, `clangDriver`. This approach allows us to:
|
@@ -92,9 +175,9 @@ You can read more on the design of `clangDriver` in Clang's [Driver Design &
|
92 | 175 | Internals](https://clang.llvm.org/docs/DriverInternals.html).
|
93 | 176 |
|
94 | 177 | ## Frontend Driver
|
95 |
| -Flang's frontend driver is the main interface between end-users and the Flang |
96 |
| -frontend. The high-level design is similar to Clang's frontend driver, `clang |
97 |
| --cc1` and consists of the following classes: |
| 178 | +Flang's frontend driver is the main interface between compiler developers and |
| 179 | +the Flang frontend. The high-level design is similar to Clang's frontend |
| 180 | +driver, `clang -cc1` and consists of the following classes: |
98 | 181 | * `CompilerInstance`, which is a helper class that encapsulates and manages
|
99 | 182 | various objects that are always required by the frontend (e.g. `AllSources`,
|
100 | 183 | `AllCookedSources, `Parsing`, `CompilerInvocation`, etc.). In most cases
|
|
0 commit comments