Enzyme with Fortran

I am creating a dedicated thread for the Enzyme + Fortran integration, which started here:

3 Likes

@lpaehler, regarding this:

I think you are using Flang in there.

We can also use LFortran. To get LLVM out of it, one can do:

lfortran --show-llvm some_file.f90 > some_file.ll

Then you work on the LLVM as you do, and then you compile to object file (some_file.o) using LLVM tools and link it:

lfortran -o executable some_file.o

What are your ideas to simplify this workflow?

Just had a very brief look and think it should be possible to make LFortran interact smoothly with Enzyme, the question I am asking myself here is whether it would be possible to make LFortran accept Enzyme as a plugin?

In such a case Enzyme’s usage in Fortran could be simplified to something like this (just guessing at the syntax here right now, orienting on similar C++ code):

lfortran -Xfortran -load -Xfortran /temp/Enzyme/build/LFortranEnzyme-13.so some_file.f90 -I ./ -c -o some_file.o
lfortran -Xfortran -load -Xfortran /temp/Enzyme/build/LFortranEnzyme-13.so some_file.f90 -o executable some_file.o

Right now, as is, one could quite probably run Enzyme with LFortran as follows:

  1. Use LFortran to compile code into LLVM IR
lfortran --show-llvm some_file.f90 > some_file.ll
  1. Use the opt of the LLVM Enzyme was built against, this needs to be the same LLVM version as the one provided by LFortran
opt some_file.ll -load=/path/to/Enzyme/enzyme/build/Enzyme/LLVMEnzyme-<VERSION>.so -enzyme -o some_file_differentiated.ll -S
  1. Run an optimization pass and compile this into the final binary
LFortran -O3 -o executable some_file_differentiated.ll

Which is pretty much what you summarized above, just with the written out intermediate step. The much easier step would be if one could load Enzyme like an LLVM-Plugin into LFortran, and then we could use the simplified workflow outlined above.

1 Like

Currently we don’t do plugins, but we should. Is Enzyme just an LLVM plugin? I think we need to implement loading LLVM plugins in the main lfortran driver. Then it would be simple to use. Great idea. I created an issue for this: Allow to load LLVM plugins (#635) · Issues · lfortran / lfortran · GitLab

1 Like

Enzyme is loaded into LLVM as a LLVM plugin. When you enable the loading of LLVM plugins it should mostly just work.

1 Like

@certik I would also highly recommend attending our LLVM tutorial on Friday. We have a whole section on the how-to of including Enzyme in a new compiler front-end, the integration of LLVM generation, optimization with opt, the use of an LLVM-plugin, link-time optimization, and Enzyme’s API.

3 Likes

Can you send information how to attend?

1 Like

Of course, the eventpage can be found here and and this is a direct link to the registration page.

2 Likes

With Enzyme being moved to official LLVM Incubator Project status, Enyzme now also has its own Subthread on the LLVM Discourse:

Feel free to ask as many questions re the integration :slight_smile:

2 Likes