# Enzyme with Fortran

**URL:** <https://fortran-lang.discourse.group/t/enzyme-with-fortran/2273>\
**Category:** Uncategorized\
**Created:** [November 15, 2021, 2:57pm UTC](https://fortran-lang.discourse.group/t/enzyme-with-fortran/2273 "2021-11-15T14:57:47Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [November 15, 2021, 2:57pm UTC](https://fortran-lang.discourse.group/t/enzyme-with-fortran/2273/1 "2021-11-15T14:57:47Z")

</div>

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

> [@2021 LLVM Developer's Meeting](https://fortran-lang.discourse.group/t/2021-llvm-developers-meeting/2256/4):
>
> Hey, one of the Enzyme co-authors here. We are already doing a good bit with Fortran and have made it a point to preserve the use of Fortran in papers. But I fully agree that more usage of Enzyme with Fortran poses some interesting problems, as well as possibilities. To be completely frank, the one thing making it quite difficult, if not to say an art form, to use Fortran at the moment is the state of the LLVM Fortran stack. If you want to have a look at how it manifests in practice please feel…

---

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [November 15, 2021, 3:01pm UTC](https://fortran-lang.discourse.group/t/enzyme-with-fortran/2273/2 "2021-11-15T15:01:47Z")

</div>

@lpaehler, regarding this:

> [@2021 LLVM Developer's Meeting](https://fortran-lang.discourse.group/t/2021-llvm-developers-meeting/2256/4):
>
> Hey, one of the Enzyme co-authors here. We are already doing a good bit with Fortran and have made it a point to preserve the use of Fortran in papers. But I fully agree that more usage of Enzyme with Fortran poses some interesting problems, as well as possibilities.
> 
> To be completely frank, the one thing making it quite difficult, if not to say an art form, to use Fortran at the moment is the state of the LLVM Fortran stack. If you want to have a look at how it manifests in practice please feel free to have a look at the Makefile at one of the more recent benchmarks.
> 
> [https://github.com/ludgerpaehler/LULESH-Fortran/blob/main/Makefile](https://github.com/ludgerpaehler/LULESH-Fortran/blob/main/Makefile)
> 
> There also exists quite a bit of stuff with Fortran which is still in the works and will probably come through the publication pipeline in the next few months or so…

I think you are using Flang in there.

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

```auto
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:

```auto
lfortran -o executable some_file.o

```

What are your ideas to simplify this workflow?

---

<div class="post-metadata">

**Author:** ![lpaehler](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/lpaehler/32/1026_2.png) [@lpaehler](https://fortran-lang.discourse.group/u/lpaehler)\
**Post date:** [November 15, 2021, 9:47pm UTC](https://fortran-lang.discourse.group/t/enzyme-with-fortran/2273/3 "2021-11-15T21:47:29Z")

</div>

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):

```bash
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

```bash
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**

```bash
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

```bash
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.

---

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [November 16, 2021, 12:30am UTC](https://fortran-lang.discourse.group/t/enzyme-with-fortran/2273/4 "2021-11-16T00:30:51Z")

</div>

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](https://gitlab.com/lfortran/lfortran/-/issues/635)

---

<div class="post-metadata">

**Author:** ![lpaehler](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/lpaehler/32/1026_2.png) [@lpaehler](https://fortran-lang.discourse.group/u/lpaehler)\
**Post date:** [November 16, 2021, 4:04pm UTC](https://fortran-lang.discourse.group/t/enzyme-with-fortran/2273/5 "2021-11-16T16:04:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lpaehler](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/lpaehler/32/1026_2.png) [@lpaehler](https://fortran-lang.discourse.group/u/lpaehler)\
**Post date:** [November 17, 2021, 3:24am UTC](https://fortran-lang.discourse.group/t/enzyme-with-fortran/2273/6 "2021-11-17T03:24:00Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [November 17, 2021, 3:49am UTC](https://fortran-lang.discourse.group/t/enzyme-with-fortran/2273/7 "2021-11-17T03:49:14Z")

</div>

Can you send information how to attend?

---

<div class="post-metadata">

**Author:** ![lpaehler](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/lpaehler/32/1026_2.png) [@lpaehler](https://fortran-lang.discourse.group/u/lpaehler)\
**Post date:** [November 17, 2021, 12:55pm UTC](https://fortran-lang.discourse.group/t/enzyme-with-fortran/2273/8 "2021-11-17T12:55:26Z")

</div>

Of course, the eventpage can be found [here](https://llvm.swoogo.com/2021devmtg/) and and [this](https://llvm.swoogo.com/2021devmtg/begin) is a direct link to the registration page.

---

<div class="post-metadata">

**Author:** ![lpaehler](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/lpaehler/32/1026_2.png) [@lpaehler](https://fortran-lang.discourse.group/u/lpaehler)\
**Post date:** [November 30, 2021, 6:57pm UTC](https://fortran-lang.discourse.group/t/enzyme-with-fortran/2273/9 "2021-11-30T18:57:34Z")

</div>

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

> **[Enzyme](https://llvm.discourse.group/c/projects-that-want-to-become-official-llvm-projects/enzyme/45)**

Feel free to ask as many questions re the integration 🙂

---

<div class="post-metadata">

**Author:** ![general\_rishkin](https://avatars.discourse-cdn.com/v4/letter/g/a9a28c/32.png) [@general\_rishkin](https://fortran-lang.discourse.group/u/general_rishkin)\
**Post date:** [June 3, 2026, 11:05am UTC](https://fortran-lang.discourse.group/t/enzyme-with-fortran/2273/10 "2026-06-03T11:05:36Z")

</div>

Hi @certik

Just wanted to ask if anything became of this with respect to LFortran and Enzyme as a plugin?
