Skip to content

Add Fortran (Flang + Enzyme) support to Enzyme Explorer - #34

Open
wsmoses wants to merge 2 commits into
mainfrom
fortran
Open

wsmoses wants to merge 2 commits into
mainfrom
fortran

Conversation

@wsmoses

@wsmoses wsmoses commented Nov 3, 2026 •

Copy link
Copy Markdown
Member

Summary

  • Language & Compiler Config:
    • Enable fortran in restrictToLanguages (frontend/files/etc/config/compiler-explorer.enzyme.properties).
    • Add template_files/etc/config/fortran.enzyme.properties.j2 configuring flang20-enzyme-main and flang21-enzyme-main (defaultCompiler=flang21-enzyme-main, compilerType=flang, Intel/AT&T asm, LLVM IR view, execution, --config=FlangEnzyme-<ver>.cfg for regular and -flto builds, -Xflang -load for -mllvm -enzyme-* options, and binaryHideFuncRe for static libFortranRuntime.a / libFortranDecimal.a symbols).
  • Toolchain & .mod Precompilation:
    • Install flang-${v} for LLVM >= 20 in frontend/install_clang.sh, fetch enzyme_function_hooks.f90 and enzyme.f90 directly from EnzymeAD/Enzyme, and precompile .mod files per Flang version so iso_c_binding.mod checksums match.
    • Add frontend/entrypoint.sh to recompile /opt/compiler-explorer/main/fortran/*.f90 (synced from Enzyme/main by builder) with each installed /usr/bin/flang-${v} on startup, and sync /app/compiler-explorer/etc/config/*.enzyme.properties into /app/etc/config/ when present so live commit hashes display instead of <unknown>.
    • Update scripts/update-versions.py to download LLDEnzyme-{version}.so, FlangEnzyme-{version}.so, and FlangEnzyme-{version}.cfg (published by Build and publish FlangEnzyme in nightly C/C++ CI Enzyme#3551), copy Enzyme/enzyme/Fortran/*.f90 into /opt/compiler-explorer/main/fortran/, and render fortran.enzyme.properties.
  • Builtin Fortran Examples:
    • Add default.f90, dot_product.f90, forward_mode.f90, custom_derivative.f90, and function_like.f90 under frontend/files/examples/fortran/.

Verification

  • Verified all 5 Fortran examples across flang-20 and flang-21 (assembly, LLVM IR, binary disassembly, and program execution) via a live local Compiler Explorer server (--env enzyme).

@wsmoses wsmoses changed the title Add Fortran (Flang + Enzyme) support and fix default MLIR example Add Fortran (Flang + Enzyme) support to Enzyme Explorer Nov 3, 2026

@joewallwork joewallwork left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this @wsmoses, it will be a very useful addition.

I have a few comments on Fortran style and formatting. Is there a reason you converted the tests to use double precision rather than real?

Also, I gather these are just examples rather than tests? Just because there doesn't seem to be any value checking for what's printed out.

Comment thread scripts/update-versions.py Outdated
Comment on lines +98 to +112
flang_filename = f"FlangEnzyme-{version}.so"
flang_url = f"https://github.com/EnzymeAD/Enzyme/releases/download/nightly/{flang_filename}"
flang_tmp = Path("/tmp") / flang_filename
flang_dest = Path(f"/opt/compiler-explorer/{branch}") / flang_filename
lld_dest = Path(f"/opt/compiler-explorer/{branch}") / f"LLDEnzyme-{version}.so"

print(f"Downloading {flang_url} to {flang_tmp}")
try:
urllib.request.urlretrieve(flang_url, flang_tmp)
print(f"Moving {flang_tmp} to {flang_dest}")
shutil.move(str(flang_tmp), str(flang_dest))
except Exception as e:
print(f"FlangEnzyme download unavailable ({e}); falling back to LLDEnzyme-{version}.so")
if lld_dest.exists():
shutil.copy2(str(lld_dest), str(flang_dest))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the additional code here is to facilitate falling back on LLDEnzyme. Is this just a temporary workaround until EnzymeAD/Enzyme#3551 is merged or do you intend to keep it around?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes — that was only a temporary bootstrap until EnzymeAD/Enzyme#3551 lands. Removed the fallback in 3a3587c so FlangEnzyme-{version}.so and FlangEnzyme-{version}.cfg use the standard nightly download loop directly (we should just merge EnzymeAD/Enzyme#3551 first).

Comment thread frontend/files/etc/fortran/enzyme.f90 Outdated

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we happy to duplicate this and enzyme_function_hooks.f90 or would it be reasonable to download these two files directly from Enzyme? I imagine that there will be more changes to these files to come and copying them here would mean having to remember to change them here, too.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call — removed both duplicated .f90 files in 3a3587c. install_clang.sh now fetches enzyme_function_hooks.f90 and enzyme.f90 directly from EnzymeAD/Enzyme (main) at Docker build time, and at runtime update-versions.py + entrypoint.sh re-sync and recompile them from /app/Enzyme/enzyme/Fortran/ whenever Enzyme updates.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is nowadays considered bad practice to have procedures that don't belong to a module or program. Please could you either put the functions in a module that gets used in the program, or put them following a contains statement in the program?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved square and dsquare into module square_mod (used in program main) in 3a3587c.

integer :: i
do i = 1, 4
x = dble(i)
write(*, '(a,f3.1,a,f4.1,a,f3.1,a,f4.1)') &

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
write(*, '(a,f3.1,a,f4.1,a,f3.1,a,f4.1)') &
write(*, '(2(a,f3.1,a,f4.1))') &

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied in 3a3587c, thanks!

Comment on lines +1 to +13
! Reverse-mode gradient of a vector dot product using activity descriptors.
subroutine vec_dot(n, a, b, res)
implicit none
integer, intent(in) :: n
double precision, dimension(n), intent(in) :: a, b
double precision, intent(out) :: res
integer :: i

res = 0.0d0
do i = 1, n
res = res + a(i) * b(i)
end do
end subroutine vec_dot

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above - put the subroutine in a module or the program.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved vec_dot under contains in program main in 3a3587c.


write(*, '(a,f6.1)') 'dot(a, b) =', res
do i = 1, n
write(*, '(a,i1,a,f5.1,a,i1,a,f5.1)') &

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
write(*, '(a,i1,a,f5.1,a,i1,a,f5.1)') &
write(*, '(2(a,i1,a,f5.1))') &

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied in 3a3587c, thanks!

Comment on lines +1 to +13
! Forward-mode directional derivative using enzyme_fwddiff.
subroutine l2_norm_sq(n, x, y)
implicit none
integer, intent(in) :: n
double precision, dimension(n), intent(in) :: x
double precision, intent(out) :: y
integer :: i

y = 0.0d0
do i = 1, n
y = y + x(i) * x(i)
end do
end subroutine l2_norm_sq

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here too.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved l2_norm_sq under contains in program main in 3a3587c.

@wsmoses

wsmoses commented Nov 4, 2026

Copy link
Copy Markdown
Member Author

Thanks for the review @joewallwork! Addressed all comments in 3a3587c:

  • real vs double precision: No special reason for double precision — switched all 5 examples to real to match Enzyme's Fortran test suite.
  • Examples vs tests: Yes, frontend/files/examples/fortran/*.f90 are the interactive starter snippets shown in Compiler Explorer's Load/Save → Examples menu on enzyme.mit.edu/explorer (like frontend/files/examples/c/default.c), rather than automated test cases, so they print outputs for the Executor pane.
  • Module / contains scoping & format strings: Scoped procedures into module square_mod (default.f90) and contains in program main (dot_product.f90, forward_mode.f90), and applied the format repeat descriptors.
  • No duplicated enzyme*.f90 or LLDEnzyme fallback: Removed the local copies of enzyme.f90 / enzyme_function_hooks.f90 (fetched directly from EnzymeAD/Enzyme at build time and synced from /app/Enzyme at runtime) and dropped the temporary LLDEnzyme fallback in update-versions.py.

@wsmoses
wsmoses requested a review from joewallwork November 4, 2026 15:27

@joewallwork joewallwork left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @wsmoses, this looks good now!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Fortran to Enzyme Explorer

2 participants