RFC: Merging FIR

Hello everyone,

This RFC proposes the process for moving FIR from its experiment branch to the flang mainline branch.

FIR is a dialect of MLIR. The adoption and evolution of both of these IRs has been discussed in many forums and generally met with acceptance and enthusiasm.

MLIR, since its unveiling in April 2019, and its adoption as an LLVM subproject late 2019, has been a rapidly evolving target. Such evolution is expected and should not be read as a criticism. The FIR experimental branch started as an exploration to gauge the suitability of MLIR as an IR for Fortran, and, as positive result accrued, this branch evolved into the work that we propose to upstream to flang.

Because the FIR branch started as an experiment branch, and because it has been chasing MLIR changes, the complete commit history is not of particular value. Also, because the sum total of the work as it stands is too large to be properly reviewed in one pull request, the patches will be presented as a series of incremental changes, each freestanding and logically organized.

The actual series of patches will depend on review comments; however, currently, the rough organization is like this:

  • Add clang-format files for FIR source code (MLIR style)

  • Add ASTBuilder structure to help lowering the parse-tree

The ASTBuilder structure is a transient data structure that

is meant to be built from the parse tree just before lowering to

FIR and that will be deleted just afterwards. It is not meant to perform

optimization analysis and transformations. It only provides temporary

information, such as label target information or parse tree parent nodes,

that is meant to be used to lower the parse tree structure into

FIR operations.

A pretty printer is available to visualize this data structure.

  • Finish adding CMake files to build f18
  • Add cmake/modules files

  • Update f18 tools CMakeLists.txt

  • Update README.md with build instructions

  • Add a Version.h and Version.inic.in files for flang

  • These CMake files are temporary; better versions are under development

  • Add Doxygen configuration to flang

  • Add Fortran IR (FIR) MLIR dialect implementation

Adds FIR library that implements an MLIR dialect to which Fortran

parse-tree will be lowered to.

FIR is documented in FIRLangRef.md added with this commit.

  • Add AbstractConverter interface and implement lowering of types to FIR
  • Add the AbstractConverter class that provides an interface to

lower expressions and types but does not provide any implementation.

  • Add an implementation of lowering of front-end types to FIR types.

  • Add lowering of parser::CharBlock to mlir::Location

  • Lowering of Fortran Expression to FIR
  • Add helpers to manipulate MLIR OpBuilder and hide

boilerplate.

  • Lower Fortran scalar intrinsic operations

  • Add a framework to describe runtime and insert runtime calls in FIR.

It allows to create a constexpr map to describe the

runtime.

  • Start Lowering of numerical intrinsics
  • Add bridge from AST to FIR
  • Add the class that implements the lowering of

the Fortran ASTs to FIR operations as described in

BurnsidetoFIR.md.

  • The lowering of IO statements is done in another file

to keep the bridge smaller.

  • Add FIR optimizer library

This library provides transformation passes that can be run of FIR

to perform several high-level optimizations.

  • Add testing tools
  • Add bbc tool that takes a Fortran input file, drives parsing,

semantic analysis, and then lowers the parse-tree to FIR. It can

then run transformation passes on the obtained MLIR before

generating LLVM IR. The bbc tool is used for testing.

  • Add tco tool that is meant to process mlir input files and

drives transformations on it. The tco tool is used for testing.

  • Update lit config for FIR tests

  • Add FIR and lowering tests

  • A FIR round-trip tests (in test/fir)

  • Add new FIR lit tests (in test-lit/fir)

  • Add expression lowering test (in test/lower)

The patches will be sent in successive PRs into f18 master because the later patches depend on the first ones.

The first patch will bring a dependency upon LLVM but not MLIR. Note that current CMake files already require LLVM package but no code was actually using it yet (apart from LIT tests). Subsequent patches will require MLIR and new CMake files.

Hi,

This is super exciting :slight_smile:

Feel free to CC River Riddle and myself on the PR!
We’re interested to keep an eye on it from the perspective of the future maintenance when we’re in the same repo (in the sense that MLIR Core changes will take care of updating FIR API usages and tests).

Cheers,

Thanks Mehdi, I will do so in the next PR (it should be MLIR related).

I believe all the comments of the first PR have now been addressed/replied.

The last commits have added some tests and have an actual link dependency to LLVM (need LLVMSupport library). This broke the cloud.drone.io integration tests at link time for clang builds.

It seems the LLVM package of clang builds depends on zlib that is not installed (“ld.lld: error: unable to find library -lz”)

@David Turby, could you install libz-dev on amd64-clang and arm64-clang or change llvm to a version version that does not depend upon zlib ?

Jean

Hi Jean,

I’ve opened a PR to add libz-dev to the drone files. However, drone still fails randomly due to out of memory errors periodically, so I’m not sure how useful it’ll be going forwards.

David Truby

Hi,

The first patch was merged last week and I have just opened a new pull request with the actual FIR implementation: https://github.com/flang-compiler/f18/pull/1035

Feel free to join the review !

Note that you will need to use an LLVM version with MLIR to build this PR. Otherwise, I have taken out the in-tree builds configurations from this patch (since Pat is working on that), so the cmake changes are minimal.

Jean