Compiling Fortran

Hello, is there a frontend that can generate LLVM bytecode from Fortran?

There’s this: GitHub - flang-compiler/flang: Flang is a Fortran language front-end designed for integration with LLVM., which is an open sourced compiler from nVidia & PGI. I believe it fprintfs LLVM IR rather than generating bitcode directly, but you can feed that into llvm-as if that difference matters to you. I’ve never used it personally I’m afraid.

People are also working on a modern Fortran compiler in the main LLVM tree, but I don’t think it can generate any code yet. They decided to implement it by completing the parser layer before moving on to CodeGen.

2 Likes

The fork that is working on a modern compiler can be found here:

It is being merged into llvm/main, but at present the code-generation is far from complete, so unless your fortran program is quite simple.

It can generate LLVM-IR “assembler” with flang-new -fc1 -emit-llvm myfile.f90 (with myfile.ll as output), but I don’t think it generates binary LLVM files at this point - although shouldn’t be hard to translate - llvm-as myfile.ll -o myfile.bc or opt -O0 myfile.ll -o myfile.bc should do roughly what you want.

If your source is Fortran-95 compatible, it should compile. If you are using later versions of Fortran, it may not generate code.

FYI, I’ve just uploaded a patch that adds support for -emit-llvm-bc in LLVM Flang: ⚙ D123211 [flang][driver] Add support for generating LLVM bytecode files.

Note that this functionality is already available in the fork linked by @Leporacanthicus .

1 Like

In other words, a simpler version is to use the f18-llvm-project branch fir-dev, and then use flang-new -fc1 -emit-llvm-bc myfile.f90 to produce myfile.bc.