LLVM Discussion Forums

Generating FIR (MLIR's Fortran dialect) using FLANG

How can I generate FIR code given a Fortran program using llvm?

It seems that there currently no way to do this with the current flang frontend in the latest release candidate (llvmorg-11.0.0-rc1). If so, is there an easy alternative to do so?

Summary of What I Found:

I’ve currently been trying to play a little with MLIR, more specifically with FIR, which is the Fortran dialect in MLIR.

From what I’ve read it seems like there are (or were) multiple Fortran frontends related to LLVM.

It appears that the one which is currently under the llvm-project GitHub is the former flang-compiler/f18 that was merged into llvm-project under the name flang. But this issue appears to indicate that the codegen part from the F18 frontend has not been merged yet and is only available on a currently active fork of the llvm-project.

There’s also a more recent Fortran frontend, entitled FC, being developed for LLVM which appears to support FIR generation despite not being officially a part of the llvm-project. I’ve tried compiling this project but had some issues with MLIR dependencies.

Thanks @sitio-couto for your interest in Flang and FIR.

Currently llvm/flang cannot generate FIR because the lowering portion (parse-tree to FIR) is not yet upstreamed. The code currently sits in a development branch in the “fir-dev” branch of https://github.com/flang-compiler/f18-llvm-project. I believe you can build this branch using normal build instructions and if you enable flang in the list of projects to build. You can use the tool “bbc” to generate FIR from Fortran source. See the following test for an example.
The current focus is on getting this working for F77 code. So don’t expect it to work for any/newer versions of Fortran code. However, if you find an issue with F77 code please feel free to file a bug.

There is currently a discussion underway on how to upstream the lowering code to llvm-project/flang. Hopefully it should be in before the next release.

FIR is the Fortran dialect designed using MLIR for llvm-project/flang. I believe FC has its own dialect for Fortran.

1 Like