Adding optimization reporting to Flang

Hello,

I have been working on the Flang driver for the past 3 months as part of a rotation project. My focus was on adding options to the driver similar to Clang that will enable reporting of optimizations performed by the LLVM Optimizer.

The -fsave-optimization-record reports this in a YAML file while the R flags, i.e. -Rpass, -Rpass-missed and -Rpass-analysis report this in the commandline.

Descriptions of each option
-f[no-]save-optimization-record[=yaml]
This flag controls whether Flang writes optimization reports to a separate file. When enabled, optimization diagnostics are recorded in a YAML file by default. User can only specify a YAML extension when using the variant of the flag with ‘=’.

The default file name used will be the input file name without the extension but with ‘.opt.yaml’ appended at the end. e.g. “my_fortran_test.f90” will give “my_fortran_test.opt.yaml”

$ bin/flang-new -fsave-optimization-record ~/test/forttest.f90
$ ls
forttest.opt.yaml

-foptimization-record-file=[FILE_NAME]
A variant of the same flag that accepts a specific file name to use.

$ bin/flang-new -foptimization-record-file=hello.yaml ~/test/forttest.f90
$ ls
hello.yaml

-Rpass[=regex], -Rpass-missed[=regex] and -Rpass-analysis[=regex]
These flags print to the command line the optimization report rather than writing to a separate file.

-Rpass prints all successful middle-end and backend passes applied. One can provided a regex expression with the “=” variant of the flag to filter what passes to report. The regex is applied to the pass names e.g. “loop-delete”.

$ bin/flang-new -O1 -Rpass ~/test/forttest.f90
home/root/test/forttest.f90:6:5: remark: Loop deleted because it is invariant [-Rpass=loop-delete]
$ 

-Rpass-missed[=regex] reports missed optimization passes

$ bin/flang-new -O1 -Rpass-missed=reg ~/test/forttest.f90
home/root/test/forttest.f90:1:1: remark: 3 virtual registers copies 3.000000e+00 total copies cost generated in function [-Rpass-missed=regalloc] 
$ 

-Rpass-analysis[=regex] reports when LLVM determines to apply a pass or not.

$ bin/flang-new -O1 -Rpass-analysis=pro ~/test/forttest.f90
home/root/test/forttest.f90:1:0: remark: 16 stack bytes in function [-Rpass-analysis=prologepilog]
$

Hope this will be useful to the community.

Big Thanks to @banach-space for the reviews!

6 Likes