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 GitHub - flang-compiler/f18-llvm-project: Fork of llvm/llvm-project for f18. In sync with f18-mlir and f18.. 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.
f18-llvm-project/control-flow.f90 at fir-dev · flang-compiler/f18-llvm-project · GitHub
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.

2 Likes

@kaitingwang Thanks for liking the post. But the post is not accurate since a lot of progress have been made in the last two years. llvm-project/flang can generate FIR now.

You can use the following instructions for building flang.

git clone https://github.com/llvm/llvm-project.git
cd llvm-project
mkdir build
cd build
cmake -G Ninja ../llvm -DLLVM_ENABLE_PROJECTS="mlir;flang;clang;openmp" -DLLVM_TARGETS_TO_BUILD=host -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_ASSERTIONS=ON
ninja check-flang

We are mostly Fortran 95 compatible with a few issues pending in character lowering and FORALL. We are awaiting full coformance with the Fortran standard before opening up to users. If you want to try out then you can use the flang-experimental-exec flag to generate executables.

flang-new -flang-experimental-exec program.f90
1 Like

Hi,

I looked at the tests and attempted to run bbc to emit FIR for my F90 code, but I got a lot of errors with “bad character in Fortran token”. Is that a sign that something in my code is not yet supported?

This error comes from the parsing side. Parser is mostly Fortran 2018 complete. I think the error probably means there is some invalid character in the code. Would you have a small reproducer?

It’s a fairly big legacy application and I have to spend some time getting familiar with it before I can isolate a small example to post here. My hunch is that there is something wrong/unexpected in the modules that are used by the file I am trying to lower, but maybe I can make without those. Thanks anyway for answering!