[flang][driver] Add support for `-O{0|1|2|3}`
authorAndrzej Warzynski <andrzej.warzynski@arm.com>
Mon, 6 Jun 2022 09:44:21 +0000 (09:44 +0000)
committerAndrzej Warzynski <andrzej.warzynski@arm.com>
Mon, 27 Jun 2022 10:06:14 +0000 (10:06 +0000)
commit869385b11c32032d02f5f24fcd21c694fb073850
tree3c53524bb7124932109ccda3aa7ff5833028afe7
parentbdfe556dd837007c5671f33384d26e9ea315db53
[flang][driver] Add support for `-O{0|1|2|3}`

This patch adds support for most common optimisation compiler flags:
`-O{0|1|2|3}`. This is implemented in both the compiler and frontend
drivers. At this point, these options are only used to configure the
LLVM optimisation pipelines (aka middle-end). LLVM backend or MLIR/FIR
optimisations are not supported yet.

Previously, the middle-end pass manager was only required when
generating LLVM bitcode (i.e. for `flang-new -c -emit-llvm <file>` or
`flang-new -fc1 -emit-llvm-bc <file>`). With this change, it becomes
required for all frontend actions that are represented as
`CodeGenAction` and `CodeGenAction::executeAction` is refactored
accordingly (in the spirit of better code re-use).

Additionally, the `-fdebug-pass-manager` option is enabled to facilitate
testing. This flag can be used to configure the pass manager to print
the middle-end passes that are being run. Similar option exists in Clang
and the semantics in Flang are identical. This option translates to
extra configuration when setting up the pass manager. This is
implemented in `CodeGenAction::runOptimizationPipeline`.

This patch also adds some bolier plate code to manage code-gen options
("code-gen" refers to generating machine code in LLVM in this context).
This was extracted from Clang. In Clang, it simplifies defining code-gen
options and enables option marshalling. In Flang, option marshalling is
not yet supported (we might do at some point), but being able to
auto-generate some code with macros is beneficial. This will become
particularly apparent when we start adding more options (at least in
Clang, the list of code-gen options is rather long).

Differential Revision: https://reviews.llvm.org/D128043
13 files changed:
clang/include/clang/Driver/Options.td
clang/lib/Driver/ToolChains/Flang.cpp
flang/include/flang/Frontend/CodeGenOptions.def [new file with mode: 0644]
flang/include/flang/Frontend/CodeGenOptions.h [new file with mode: 0644]
flang/include/flang/Frontend/CompilerInvocation.h
flang/include/flang/Frontend/FrontendActions.h
flang/lib/Frontend/CMakeLists.txt
flang/lib/Frontend/CodeGenOptions.cpp [new file with mode: 0644]
flang/lib/Frontend/CompilerInvocation.cpp
flang/lib/Frontend/FrontendActions.cpp
flang/test/Driver/default-optimization-pipelines.f90 [new file with mode: 0644]
flang/test/Driver/driver-help.f90
flang/test/Driver/flang_f_opts.f90 [new file with mode: 0644]