1<!--===- docs/FlangDriver.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 9# Flang drivers 10 11```eval_rst 12.. contents:: 13 :local: 14``` 15 16There are two main drivers in Flang: 17* the compiler driver, `flang-new` 18* the frontend driver, `flang-new -fc1` 19 20> **_NOTE:_** The diagrams in this document refer to `flang` as opposed to 21> `flang-new`. Eventually, `flang-new` will be renamed as `flang` and the 22> diagrams reflect the final design that we are still working towards. 23 24The **compiler driver** will allow you to control all compilation phases (e.g. 25preprocessing, semantic checks, code-generation, code-optimisation, lowering 26and linking). For frontend specific tasks, the compiler driver creates a 27Fortran compilation job and delegates it to `flang-new -fc1`, the frontend 28driver. For linking, it creates a linker job and calls an external linker (e.g. 29LLVM's [`lld`](https://lld.llvm.org/)). It can also call other tools such as 30external assemblers (e.g. [`as`](https://www.gnu.org/software/binutils/)). In 31Clang, the compiler driver can also link the generated binaries with LLVM's 32static analysis/sanitizer libraries (e.g. 33[MemorySanitizer](https://clang.llvm.org/docs/MemorySanitizer.html)). This is 34not yet available in Flang, but will be relatively easy to support once such 35libraries become available. Flang's compiler driver is intended for Flang's 36end-users - its interface needs to remain stable. Otherwise, Flang's users will 37have to adjust their build scripts every time a compiler flag is changed. 38 39|  | 40|:--:| 41| *Flang’s compiler driver and the **tools** that it runs* | 42 43The **frontend driver** glues together and drives all of the Flang's frontend 44libraries. As such, it provides an easy-to-use and intuitive interface to the 45frontend. It uses MLIR and LLVM for code-generation and can be viewed as a 46driver for Flang, LLVM and MLIR libraries. Contrary to the compiler driver, it 47is not capable of calling any external tools (including linkers). It is aware 48of all the frontend internals that are "hidden" from the compiler driver. It 49accepts many frontend-specific options not available in `flang-new` and as such 50it provides a finer control over the frontend. Note that this tool is mostly 51intended for Flang developers. In particular, there are no guarantees about the 52stability of its interface and compiler developers can use it to experiment 53with new flags. 54 55|  | 56|:-:| 57| *Flang's frontend driver and the **libraries** that it drives* | 58 59Note that similarly to `-Xclang` in `clang`, you can use `-Xflang` to forward a 60frontend specific flag from the _compiler_ directly to the _frontend_ driver, 61e.g.: 62 63```lang=bash 64flang-new -Xflang -fdebug-dump-parse-tree input.f95 65``` 66 67In the invocation above, `-fdebug-dump-parse-tree` is forwarded to `flang-new 68-fc1`. Without the forwarding flag, `-Xflang`, you would see the following 69warning: 70 71```lang=bash 72flang-new: warning: argument unused during compilation: 73``` 74 75As `-fdebug-dump-parse-tree` is only supported by `flang-new -fc1`, `flang-new` 76will ignore it when used without `Xflang`. 77 78## Why Do We Need Two Drivers? 79As hinted above, `flang-new` and `flang-new -fc1` are two separate tools. The 80fact that these tools are accessed through one binary, `flang-new`, is just an 81implementation detail. Each tool has a separate list of options, albeit defined 82in the same file: `clang/include/clang/Driver/Options.td`. 83 84The separation helps us split various tasks and allows us to implement more 85specialised tools. In particular, `flang-new` is not aware of various 86compilation phases within the frontend (e.g. scanning, parsing or semantic 87checks). It does not have to be. Conversely, the frontend driver, `flang-new 88-fc1`, needs not to be concerned with linkers or other external tools like 89assemblers. Nor does it need to know where to look for various systems 90libraries, which is usually OS and platform specific. 91 92One helpful way of differentiating these tools is to keep in mind that: 93 94* the compiler driver is an end-user tool 95* frontend driver is a compiler developer tool with many additional options, 96 97Also, Since the compiler driver can call external tools, e.g. linkers, it can 98be used to generate **executables**. The frontend driver cannot call external 99tools and hence can only generate **object files**. A similar model is 100implemented in Clang (`clang` vs `clang -cc1` vs `clang -cc1as`), which is 101based on the [architecture of 102GCC](https://en.wikibooks.org/wiki/GNU_C_Compiler_Internals/GNU_C_Compiler_Architecture). 103In fact, Flang needs to adhere to this model in order to be able to re-use 104Clang's driver library. If you are more familiar with the [architecture of 105GFortran](https://gcc.gnu.org/onlinedocs/gcc-4.7.4/gfortran/About-GNU-Fortran.html) 106than Clang, then `flang-new` corresponds to `gfortran` and `flang-new -fc1` to 107`f951`. 108 109## Compiler Driver 110The main entry point for Flang's compiler driver is implemented in 111`flang/tools/flang-driver/driver.cpp`. Flang's compiler driver is implemented 112in terms of Clang's driver library, `clangDriver`. This approach allows us to: 113* benefit from Clang's support for various targets, platforms and operating systems 114* leverage Clang's ability to drive various backends available in LLVM, as well 115 as linkers and assemblers. 116One implication of this dependency on Clang is that all of Flang's compiler 117options are defined alongside Clang's options in 118`clang/include/clang/Driver/Options.td`. For options that are common for both 119Flang and Clang, the corresponding definitions are shared. 120 121Internally, a `clangDriver` based compiler driver works by creating actions 122that correspond to various compilation phases, e.g. `PreprocessJobClass`, 123`CompileJobClass`, `BackendJobClass` or `LinkJobClass` from the 124`clang::driver::Action::ActionClass` enum. There are also other, more 125specialised actions, e.g. `MigrateJobClass` or `InputClass`, that do not map 126directly to common compilation steps. The actions to run are determined from 127the supplied compiler flags, e.g. 128 129* `-E` for `PreprocessJobClass`, 130* `-c` for `CompileJobClass`. 131 132In most cases, the driver creates a chain of actions/jobs/phases where the 133output from one action is the input for the subsequent one. You can use the 134`-ccc-print-phases` flag to see the sequence of actions that the driver will 135create for your compiler invocation: 136```bash 137flang-new -ccc-print-phases -E file.f 138+- 0: input, "file.f", f95-cpp-input 1391: preprocessor, {0}, f95 140``` 141As you can see, for `-E` the driver creates only two jobs and stops immediately 142after preprocessing. The first job simply prepares the input. For `-c`, the 143pipeline of the created jobs is more complex: 144```bash 145flang-new -ccc-print-phases -c file.f 146 +- 0: input, "file.f", f95-cpp-input 147 +- 1: preprocessor, {0}, f95 148 +- 2: compiler, {1}, ir 149+- 3: backend, {2}, assembler 1504: assembler, {3}, object 151``` 152Note that currently Flang does not support code-generation and `flang-new` will 153fail during the second step above with the following error: 154```bash 155error: code-generation is not available yet 156``` 157The other phases are printed nonetheless when using `-ccc-print-phases`, as 158that reflects what `clangDriver`, the library, will try to create and run. 159 160For actions specific to the frontend (e.g. preprocessing or code generation), a 161command to call the frontend driver is generated (more specifically, an 162instance of `clang::driver::Command`). Every command is bound to an instance of 163`clang::driver::Tool`. For Flang we introduced a specialisation of this class: 164`clang::driver::Flang`. This class implements the logic to either translate or 165forward compiler options to the frontend driver, `flang-new -fc1`. 166 167You can read more on the design of `clangDriver` in Clang's [Driver Design & 168Internals](https://clang.llvm.org/docs/DriverInternals.html). 169 170## Frontend Driver 171Flang's frontend driver is the main interface between compiler developers and 172the Flang frontend. The high-level design is similar to Clang's frontend 173driver, `clang -cc1` and consists of the following classes: 174* `CompilerInstance`, which is a helper class that encapsulates and manages 175 various objects that are always required by the frontend (e.g. `AllSources`, 176 `AllCookedSources, `Parsing`, `CompilerInvocation`, etc.). In most cases 177 `CompilerInstance` owns these objects, but it also can share them with its 178 clients when required. It also implements utility methods to construct and 179 manipulate them. 180* `CompilerInvocation` encapsulates the configuration of the current 181 invocation of the compiler as derived from the command-line options and the 182 input files (in particular, file extensions). Among other things, it holds an 183 instance of `FrontendOptions`. Like `CompilerInstance`, it owns the objects 184 that it manages. It can share them with its clients that want to access them 185 even after the corresponding `CompilerInvocation` has been destructed. 186* `FrontendOptions` holds options that control the behaviour of the frontend, 187 as well as e.g. the list of the input files. These options come either 188 directly from the users (through command-line flags) or are derived from 189 e.g. the host system configuration. 190* `FrontendAction` and `FrontendActions` (the former being the base class for 191 the latter) implement the actual actions to perform by the frontend. Usually 192 there is one specialisation of `FrontendActions` for every compiler action flag 193 (e.g. `-E`, `-fdebug-unparse`). These classes also contain various hooks that 194 allow you to e.g. fine-tune the configuration of the frontend based on the 195 input. 196 197This list is not exhaustive and only covers the main classes that implement the 198driver. The main entry point for the frontend driver, `fc1_main`, is 199implemented in `flang/tools/flang-driver/driver.cpp`. It can be accessed by 200invoking the compiler driver, `flang-new`, with the `-fc1` flag. 201 202The frontend driver will only run one action at a time. If you specify multiple 203action flags, only the last one will be taken into account. The default action 204is `ParseSyntaxOnlyAction`, which corresponds to `-fsyntax-only`. In other 205words, `flang-new -fc1 <input-file>` is equivalent to `flang-new -fc1 -fsyntax-only 206<input-file>`. 207 208## The `flang-to-external-fc` script 209The `flang-to-external-fc` wrapper script for `flang-new` was introduced as a 210development tool and to facilitate testing. The `flang-to-external-fc` wrapper 211script will: 212* use `flang-new` to unparse the input source file (i.e. it will run `flang-new 213 -fc1 -fdebug-unparse <input-file>`), and then 214* call a host Fortran compiler, e.g. `gfortran`, to compile the unparsed file. 215 216Here's a basic breakdown of what happens inside `flang-to-external-fc` when you 217run `flang-to-external-fc file.f90`: 218```bash 219flang-new -fc1 -fdebug-unparse file.f90 -o file-unparsed.f90 220gfortran file-unparsed.f90 221``` 222This is a simplified version for illustration purposes only. In practice, 223`flang-to-external-fc` adds a few more frontend options and it also supports 224various other use cases (e.g. compiling C files, linking existing object 225files). `gfortran` is the default host compiler used by `flang-to-external-fc`. 226You can change it by setting the `FLANG_FC` environment variable. 227 228## Adding new Compiler Options 229Adding a new compiler option in Flang consists of two steps: 230* define the new option in a dedicated TableGen file, 231* parse and implement the option in the relevant drivers that support it. 232 233### Option Definition 234All of Flang's compiler and frontend driver options are defined in 235`clang/include/clang/Driver/Options.td` in Clang. When adding a new option to 236Flang, you will either: 237 * extend the existing definition for an option that is already available 238 in one of Clang's drivers (e.g. `clang`), but not yet available in Flang, or 239 * add a completely new definition if the option that you are adding has not 240 been defined yet. 241 242There are many predefined TableGen classes and records that you can use to fine 243tune your new option. The list of available configurations can be overwhelming 244at times. Sometimes the easiest approach is to find an existing option that has 245similar semantics to your new option and start by copying that. 246 247For every new option, you will also have to define the visibility of the new 248option. This is controlled through the `Flags` field. You can use the following 249Flang specific option flags to control this: 250 * `FlangOption` - this option will be available in the `flang-new` compiler driver, 251 * `FC1Option` - this option will be available in the `flang-new -fc1` frontend driver, 252 * `FlangOnlyOption` - this option will not be visible in Clang drivers. 253 254Please make sure that options that you add are only visible in drivers that can 255support it. For example, options that only make sense for Fortran input files 256(e.g. `-ffree-form`) should not be visible in Clang and be marked as 257`FlangOnlyOption`. 258 259When deciding what `OptionGroup` to use when defining a new option in the 260`Options.td` file, many new options fall into one of the following two 261categories: 262 * `Action_Group` - options that define an action to run (e.g. 263 `-fsyntax-only`, `-E`) 264 * `f_Group` - target independent compiler flags (e.g. `-ffixed-form`, 265 `-fopenmp`) 266There are also other groups and occasionally you will use them instead of the 267groups listed above. 268 269### Option Implementation 270First, every option needs to be parsed. Flang compiler options are parsed in 271two different places, depending on which driver they belong to: 272 273* frontend driver: `flang/lib/Frontend/CompilerInvocation.cpp`, 274* compiler driver: `clang/lib/Driver/ToolChains/Flang.cpp`. 275 276The parsing will depend on the semantics encoded in the TableGen definition. 277 278When adding a compiler driver option (i.e. an option that contains 279`FlangOption` among its `Flags`) that you also intend to be understood by the 280frontend, make sure that it is either forwarded to `flang-new -fc1` or translated 281into some other option that is accepted by the frontend driver. In the case of 282options that contain both `FlangOption` and `FC1Option` among its flags, we 283usually just forward from `flang-new` to `flang-new -fc1`. This is then tested in 284`flang/test/Driver/frontend-forward.F90`. 285 286What follows is usually very dependant on the meaning of the corresponding 287option. In general, regular compiler flags (e.g. `-ffree-form`) are mapped to 288some state within the driver. A lot of this state is stored within an instance 289of `FrontendOptions`, but there are other more specialised classes too. Action 290flags (e.g. `-fsyntax-only`) are usually more complex overall, but also more 291structured in terms of the implementation. 292 293### Action Options 294For options that correspond to an action (i.e. marked as `Action_Group`), you 295will have to define a dedicated instance of `FrontendActions` in 296`flang/include/flang/Frontend/FrontendOptions.h`. For example, for 297`-fsyntax-only` we defined: 298```cpp 299class ParseSyntaxOnlyAction : public PrescanAndSemaAction { 300 void ExecuteAction() override; 301}; 302``` 303Command line options are mapped to frontend actions through the 304`Fortran::frontend::ActionKind` enum. For every new action option that you 305add, you will have to add a dedicated entry in that enum (e.g. 306`ParseSyntaxOnly` for `-fsyntax-only`) and a corresponding `case` in 307`ParseFrontendArgs` function in the `CompilerInvocation.cpp` file, e.g.: 308```cpp 309 case clang::driver::options::OPT_fsyntax_only: 310 opts.programAction = ParseSyntaxOnly; 311 break; 312``` 313Note that this simply sets the program/frontend action within the frontend 314driver. You still have make sure that the corresponding frontend action class 315is instantiated when your new action option is used. The relevant `switch` 316statement is implemented in `Fortran::frontend::CreatedFrontendBaseAction` in 317the `ExecuteCompilerInvocation.cpp` file. Here's an example for 318`-fsyntax-only`: 319```cpp 320 case ParseSyntaxOnly: 321 return std::make_unique<ParseSyntaxOnlyAction>(); 322``` 323At this point you should be able to trigger that frontend action that you have 324just added using your new frontend option. 325 326 327# CMake Support 328As of [#7246](https://gitlab.kitware.com/cmake/cmake/-/merge_requests/7246) 329(and soon to be released CMake 3.24.0), `cmake` can detect `flang-new` as a 330supported Fortran compiler. You can configure your CMake projects to use 331`flang-new` as follows: 332```bash 333cmake -DCMAKE_Fortran_FLAGS="-flang-experimental-exec" -DCMAKE_Fortran_COMPILER=<path/to/flang-new> <src/dir> 334``` 335You should see the following in the output: 336``` 337-- The Fortran compiler identification is LLVMFlang <version> 338``` 339where `<version>` corresponds to the LLVM Flang version. Note that while 340generating executables remains experimental, you will need to inform CMake to 341use the `-flang-experimental-exec` flag when invoking `flang-new` as in the 342example above. 343 344# Testing 345In LIT, we define two variables that you can use to invoke Flang's drivers: 346* `%flang` is expanded as `flang-new` (i.e. the compiler driver) 347* `%flang_fc1` is expanded as `flang-new -fc1` (i.e. the frontend driver) 348 349For most regression tests for the frontend, you will want to use `%flang_fc1`. 350In some cases, the observable behaviour will be identical regardless of whether 351`%flang` or `%flang_fc1` is used. However, when you are using `%flang` instead 352of `%flang_fc1`, the compiler driver will add extra flags to the frontend 353driver invocation (i.e. `flang-new -fc1 -<extra-flags>`). In some cases that might 354be exactly what you want to test. In fact, you can check these additional 355flags by using the `-###` compiler driver command line option. 356 357Lastly, you can use `! REQUIRES: <feature>` for tests that will only work when 358`<feature>` is available. For example, you can use`! REQUIRES: shell` to mark a 359test as only available on Unix-like systems (i.e. systems that contain a Unix 360shell). In practice this means that the corresponding test is skipped on 361Windows. 362 363# Frontend Driver Plugins 364Plugins are an extension to the frontend driver that make it possible to run 365extra user defined frontend actions, in the form of a specialization of a 366`PluginParseTreeAction`. These actions are run during compilation, after 367semantic checks. Similarly to Clang, Flang leverages `LoadLibraryPermanently` 368from LLVM's `llvm::sys::DynamicLibrary` to load dynamic objects that implement 369plugins. The process for using plugins includes: 370* [Creating a plugin](#creating-a-plugin) 371* [Loading and running a plugin](#loading-and-running-a-plugin) 372 373Flang plugins are limited to `flang-new -fc1` and are currently only available / 374been tested on Linux. 375 376## Creating a Plugin 377There are three parts required for plugins to work: 3781. [`PluginParseTreeAction` subclass](#a-pluginparsetreeaction-subclass) 3791. [Implementation of `ExecuteAction`](#implementation-of-executeaction) 3801. [Plugin registration](#plugin-registration) 381 382There is an example plugin located in `flang/example/PrintFlangFunctionNames` 383that demonstrates these points by using the `ParseTree` API to print out 384function and subroutine names declared in the input file. 385 386### A `PluginParseTreeAction` Subclass 387This subclass will wrap everything together and represent the `FrontendAction` 388corresponding to your plugin. It will need to inherit from 389`PluginParseTreeAction` (defined in `flang/include/flang/FrontendActions.h`), in 390order to have access to the parse tree post semantic checks, and also so that it 391can be registered, e.g. 392```cpp 393class PrintFunctionNamesAction : public PluginParseTreeAction 394``` 395 396### Implementation of `ExecuteAction` 397Like in other frontend actions, the driver looks for an `ExecuteAction` function 398to run, so in order for your plugin to do something, you will need to implement 399the `ExecuteAction` method in your plugin class. This method will contain the 400implementation of what the plugin actually does, for example: 401```cpp 402// Forward declaration 403struct ParseTreeVisitor; 404 405void ExecuteAction() override { 406 ParseTreeVisitor visitor; 407 Fortran::parser::Walk(getParsing().parseTree(), visitor); 408} 409``` 410In the example plugin, the `ExecuteAction` method first creates an instance of 411`visitor` struct, before passing it together with the parse tree to the 412`Fortran::parser::Walk` function that will traverse the parse tree. The parse 413tree will normally be generated by the frontend driver and can be retrieved in 414your plugin through the `getParsing()` member method. Implementation and 415details of the `Walk` function can be found in 416`flang/include/flang/Parser/parse-tree-visitor.h`. 417 418You will have to define your own `visitor` struct. It should define different 419`Pre` and `Post` functions that take the type of a specific `ParseTree` node as 420an argument. When the `Walk` function is traversing the parse tree, these 421functions will be run before/after a node of that type is visited. Template 422functions for `Pre`/`Post` are defined so that when a node is visited that you 423have not defined a function for, it will still be able to continue. `Pre` 424returns a `bool` indicating whether to visit that node's children or not. For 425example: 426```cpp 427struct ParseTreeVisitor { 428 template <typename A> bool Pre(const A&) { return true; } 429 template <typename A> void Post(const A&) {} 430 void Post(const Fortran::parser::FunctionStmt &f) { 431 llvm::outs() << std::get<Fortran::parser::Name>(f.t).ToString() << "\n" ; 432 } 433} 434``` 435The different types of nodes and also what each node structure contains are 436defined in `flang/include/flang/Parser/parse-tree.h`. In the example, there is a 437`Post` function, with a line that gets the `Name` element from a tuple `t` in 438the `FunctionStmt` struct and prints it. This function will be run after every 439`FunctionStmt` node is visited in the parse tree. 440 441### Plugin Registration 442A plugin registry is used to store names and descriptions of a collection of 443plugins. The Flang plugin registry, defined in 444`flang/include/flang/Frontend/FrontendPluginRegistry.h`, is an alias of 445`llvm::Registry` of type `PluginParseTreeAction`. 446 447The plugin will need to be registered, which will add the Plugin to the registry 448and allow it to be used. The format is as follows, with `print-fns` being the 449plugin name that is used later to call the plugin and `Print Function names` 450being the description: 451```cpp 452static FrontendPluginRegistry::Add<PrintFunctionNamesAction> X( 453 "print-fns", "Print Function names"); 454``` 455 456## Loading and Running a Plugin 457In order to use plugins, there are 2 command line options made available to the 458frontend driver, `flang-new -fc1`: 459* [`-load <dsopath>`](#the--load-dsopath-option) for loading the dynamic shared 460 object of the plugin 461* [`-plugin <name>`](#the--plugin-name-option) for calling the registered plugin 462 463Invocation of the example plugin is done through: 464```bash 465flang-new -fc1 -load flangPrintFunctionNames.so -plugin print-fns file.f90 466``` 467 468Both these options are parsed in `flang/lib/Frontend/CompilerInvocation.cpp` and 469fulfil their actions in 470`flang/lib/FrontendTool/ExecuteCompilerInvocation.cpp` 471 472### The `-load <dsopath>` option 473This loads the plugin shared object library, with the path given at `<dsopath>`, 474using `LoadLibraryPermantly` from LLVM's `llvm::sys::DynamicLibrary`, which 475itself uses `dlopen`. During this stage, the plugin is registered with the 476registration line from the plugin, storing the name and description. 477 478### The `-plugin <name>` option 479This sets `frontend::ActionKind programAction` in `FrontendOptions` to 480`PluginAction`, through which it searches the plugin registry for the plugin 481name from `<name>`. If found, it returns the instantiated plugin, otherwise it 482reports an error diagnostic and returns `nullptr`. 483 484## Enabling In-Tree Plugins 485For in-tree plugins, there is the CMake flag `FLANG_PLUGIN_SUPPORT`, enabled by 486default, that controls the exporting of executable symbols from `flang-new`, 487which plugins need access to. Additionally, there is the CMake flag 488`FLANG_BUILD_EXAMPLES`, turned off by default, that is used to control if the 489example programs are built. This includes plugins that are in the 490`flang/example` directory and added as a `sub_directory` to the 491`flang/examples/CMakeLists.txt`, for example, the `PrintFlangFunctionNames` 492plugin. It is also possible to develop plugins out-of-tree. 493 494## Limitations 495Note that the traversal API presented here is under active development and 496might change in the future. We expect it to evolve as support for new 497language features are added. This document and the examples will be updated 498accordingly. 499 500The current `ParseTree` structure is not suitable for modifications. The 501copy constructors are not available and hence duplicating code might not be 502trivial. Please take this into consideration when designing your plugin. In 503particular, creating a transformation plugin will be noticeably harder than 504analysis plugins that just consume (rather than edit) `ParseTree`. 505 506Lastly, if `ParseTree` modifications are performed, then it might be necessary 507to re-analyze expressions and modify scope or symbols. You can check 508[Semantics.md](Semantics.md) for more details on how `ParseTree` is edited 509e.g. during the semantic checks. 510