Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion frontend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ COPY --from=build /src/compiler-explorer/examples /app/examples
COPY --from=build /src/compiler-explorer/etc /app/etc
COPY --from=build /src/compiler-explorer/views /app/views
COPY --from=build /src/compiler-explorer/public /app/public
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

ENV JULIA_DEPOT_PATH=/local/juliapackages:/opt/compiler-explorer/juliapackages
ENV JULIA_PKG_SERVER=http://package-proxy
Expand All @@ -73,4 +75,4 @@ ENV PATH="$PATH:/opt/compiler-explorer/cuda/12.9.1/bin"
LABEL org.opencontainers.image.source=https://github.com/EnzymeAD/compiler-explorer-builder
LABEL org.opencontainers.image.description="Enzyme Explorer"
WORKDIR /app
ENTRYPOINT ["node", "./out/dist/app.js", "--static", "./out/webpack/static", "--env", "enzyme"]
ENTRYPOINT ["/entrypoint.sh"]
32 changes: 32 additions & 0 deletions frontend/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/bash
set -e

# Refresh Fortran module files from the live Enzyme checkout if available so
# module checksums always match each installed Flang binary and stay up to date.
if [ -f /opt/compiler-explorer/main/fortran/enzyme_function_hooks.f90 ] && \
[ -f /opt/compiler-explorer/main/fortran/enzyme.f90 ]; then
for flang_bin in /usr/bin/flang-[0-9]*; do
[ -x "$flang_bin" ] || continue
v="${flang_bin##*/flang-}"
[[ "$v" =~ ^[0-9]+$ ]] || continue
mod_dir="/var/cache/enzyme-fortran-modules/${v}"
tmp_dir=$(mktemp -d)
if "$flang_bin" -fsyntax-only -J"$tmp_dir" -I"$tmp_dir" \
/opt/compiler-explorer/main/fortran/enzyme_function_hooks.f90 && \
"$flang_bin" -fsyntax-only -J"$tmp_dir" -I"$tmp_dir" \
/opt/compiler-explorer/main/fortran/enzyme.f90; then
mkdir -p "$mod_dir"
cp "$tmp_dir"/*.mod "$mod_dir"/
else
echo "Warning: failed to recompile Fortran modules with $flang_bin; keeping build-time modules" >&2
fi
rm -rf "$tmp_dir"
done
fi

# Sync builder-rendered properties (with live commit hashes) if present
if [ -f /app/compiler-explorer/etc/config/fortran.enzyme.properties ]; then
cp /app/compiler-explorer/etc/config/*.enzyme.properties /app/etc/config/
fi

exec node ./out/dist/app.js --static ./out/webpack/static --env enzyme "$@"
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
urlShortenService=tinyurl
httpRoot=/explorer
restrictToLanguages=c,c++,llvm,julia,mlir,cuda,rust
restrictToLanguages=c,c++,fortran,llvm,julia,mlir,cuda,rust

compileTimeoutMs=300000
binaryExecTimeoutMs=100000
Expand Down
60 changes: 60 additions & 0 deletions frontend/files/examples/fortran/custom_derivative.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
! Custom reverse-mode derivative registration in pure Fortran.
module custom_rule_mod
implicit none
private
public :: evaluate

type :: gradient_registration
procedure(scaled_op), pointer, nopass :: primal => scaled_op
procedure(augment_scaled_op), pointer, nopass :: augmented => augment_scaled_op
procedure(reverse_scaled_op), pointer, nopass :: reverse => reverse_scaled_op
end type gradient_registration

! Enzyme reads this static initializer at compile time to register the custom rule.
type(gradient_registration) :: f__enzyme_register_gradient_scaled_op

contains

subroutine scaled_op(x, y)
real, intent(in) :: x
real, intent(out) :: y
y = 2.0 * x
end subroutine scaled_op

subroutine augment_scaled_op(x, dx, y, dy)
real, intent(in) :: x, dx
real, intent(out) :: y
real, intent(inout) :: dy
call scaled_op(x, y)
end subroutine augment_scaled_op

subroutine reverse_scaled_op(x, dx, y, dy)
real, intent(in) :: x, y
real, intent(inout) :: dx, dy
! Custom rule: supply d/dx log(1 + x) = 1 / (1 + x)
dx = dx + dy / (1.0 + x)
dy = 0.0
end subroutine reverse_scaled_op

function evaluate(x) result(y)
real, intent(in) :: x
real :: y
call scaled_op(x, y)
end function evaluate

end module custom_rule_mod

program main
use enzyme, only: enzyme_autodiff
use custom_rule_mod, only: evaluate
implicit none
real :: x, dx, y

x = 2.0
dx = 0.0
y = evaluate(x)
call enzyme_autodiff(evaluate, x, dx)

write(*, '(a,f6.4)') 'y = ', y
write(*, '(a,f6.4)') 'dx = ', dx
end program main
36 changes: 36 additions & 0 deletions frontend/files/examples/fortran/default.f90

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.

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module square_mod
use enzyme, only: enzyme_autodiff
implicit none
private
public :: square, dsquare

contains

real function square(x)
real, intent(in) :: x
square = x * x
end function square

real function dsquare(x)
real, intent(in) :: x
real :: dx
dx = 0.0
! Returns the derivative of square(x), i.e. 2 * x
call enzyme_autodiff(square, x, dx)
dsquare = dx
end function dsquare

end module square_mod

program main
use square_mod, only: square, dsquare
implicit none
real :: x
integer :: i

do i = 1, 4
x = real(i)
write(*, '(2(a,f3.1,a,f4.1))') &
'square(', x, ')=', square(x), ', dsquare(', x, ')=', dsquare(x)
end do
end program main
41 changes: 41 additions & 0 deletions frontend/files/examples/fortran/dot_product.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
! Reverse-mode gradient of a vector dot product using activity descriptors.
program main
use enzyme, only: enzyme_autodiff, enzyme_const, enzyme_dup
implicit none
integer, parameter :: n = 4
real, dimension(n) :: a, da, b, db
real :: res, dres
integer :: i

a = [1.0, 2.0, 3.0, 4.0]
b = [10.0, 20.0, 30.0, 40.0]
da = 0.0
db = 0.0
dres = 1.0

call enzyme_autodiff(vec_dot, enzyme_const, n, &
enzyme_dup, a, da, &
enzyme_dup, b, db, &
enzyme_dup, res, dres)

write(*, '(a,f6.1)') 'dot(a, b) =', res
do i = 1, n
write(*, '(2(a,i1,a,f5.1))') &
'd(dot)/da(', i, ') =', da(i), ', d(dot)/db(', i, ') =', db(i)
end do

contains

subroutine vec_dot(n, a, b, res)
integer, intent(in) :: n
real, dimension(n), intent(in) :: a, b
real, intent(out) :: res
integer :: i

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

end program main
33 changes: 33 additions & 0 deletions frontend/files/examples/fortran/forward_mode.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
! Forward-mode directional derivative using enzyme_fwddiff.
program main
use enzyme, only: enzyme_fwddiff, enzyme_const, enzyme_dup
implicit none
integer, parameter :: n = 3
real, dimension(n) :: x, dx
real :: y, dy

x = [2.0, 3.0, 6.0]
dx = [1.0, 0.5, -1.0]

call enzyme_fwddiff(l2_norm_sq, enzyme_const, n, &
enzyme_dup, x, dx, &
enzyme_dup, y, dy)

write(*, '(a,f6.2)') 'y = ||x||^2 =', y
write(*, '(a,f6.2)') 'dy = grad(y) . dx =', dy

contains

subroutine l2_norm_sq(n, x, y)
integer, intent(in) :: n
real, dimension(n), intent(in) :: x
real, intent(out) :: y
integer :: i

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

end program main
38 changes: 38 additions & 0 deletions frontend/files/examples/fortran/function_like.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
! Registering a function-like mathematical derivative rule via procedure pointer.
module function_like_mod
implicit none
private
public :: wrapper

procedure(approx_log1p), pointer, private :: &
fn__enzyme_function_like__log1p => approx_log1p

contains

function approx_log1p(x) result(y)
real, value :: x
real :: y
y = x - 0.5 * x * x
end function approx_log1p

function wrapper(x) result(y)
real, intent(in) :: x
real :: y
y = approx_log1p(x)
end function wrapper

end module function_like_mod

program main
use enzyme, only: enzyme_autodiff
use function_like_mod, only: wrapper
implicit none
real :: x, dx

x = 2.0
dx = 0.0
call enzyme_autodiff(wrapper, x, dx)

write(*, '(a,f7.4)') 'wrapper(2.0) =', wrapper(x)
write(*, '(a,f7.4)') 'd/dx via rule =', dx
end program main
15 changes: 14 additions & 1 deletion frontend/install_clang.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
curl -fsSL https://apt.llvm.org/llvm-snapshot.gpg.key | apt-key add -

mkdir -p /etc/fortran
curl -fsSL https://raw.githubusercontent.com/EnzymeAD/Enzyme/main/enzyme/Fortran/enzyme_function_hooks.f90 -o /etc/fortran/enzyme_function_hooks.f90
curl -fsSL https://raw.githubusercontent.com/EnzymeAD/Enzyme/main/enzyme/Fortran/enzyme.f90 -o /etc/fortran/enzyme.f90

# Read versions from file available in Docker volume
while read -r line || [ -n "$line" ]; do
# format is like clang-15.0.0
Expand All @@ -19,6 +23,15 @@ while read -r line || [ -n "$line" ]; do
# Use the variable to construct the repository string
apt-add-repository "deb http://apt.llvm.org/${distro}/ llvm-toolchain-${distro}-${v} main"

apt-get install -y llvm-${v}-dev libomp-${v}-dev lld-${v} clang-${v} libclang-${v}-dev
if [ "$v" -ge 20 ]; then
apt-get install -y llvm-${v}-dev libomp-${v}-dev lld-${v} clang-${v} libclang-${v}-dev flang-${v}
mkdir -p /var/cache/enzyme-fortran-modules/${v}
/usr/bin/flang-${v} -fsyntax-only -J/var/cache/enzyme-fortran-modules/${v} -I/var/cache/enzyme-fortran-modules/${v} /etc/fortran/enzyme_function_hooks.f90
/usr/bin/flang-${v} -fsyntax-only -J/var/cache/enzyme-fortran-modules/${v} -I/var/cache/enzyme-fortran-modules/${v} /etc/fortran/enzyme.f90
else
apt-get install -y llvm-${v}-dev libomp-${v}-dev lld-${v} clang-${v} libclang-${v}-dev
fi
fi
done < /ver_data/clang-versions.txt

rm -rf /etc/fortran
35 changes: 29 additions & 6 deletions scripts/update-versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,17 @@ def main():

if commit is not None:
os.makedirs(f"/opt/compiler-explorer/{branch}", exist_ok=True)
for lib in ["ClangEnzyme", "LLVMEnzyme"]:
filename = f"{lib}-{version}.so"
artifacts = [
f"ClangEnzyme-{version}.so",
f"LLVMEnzyme-{version}.so",
f"LLDEnzyme-{version}.so",
]
if version >= 20:
artifacts.extend([
f"FlangEnzyme-{version}.so",
f"FlangEnzyme-{version}.cfg",
])
for filename in artifacts:
url = f"https://github.com/EnzymeAD/Enzyme/releases/download/nightly/{filename}"
tmp_path = Path("/tmp") / filename
dest_path = Path(f"/opt/compiler-explorer/{branch}") / filename
Expand Down Expand Up @@ -133,8 +142,21 @@ def main():
except Exception as e:
print(f"Error handling {filename}: {e}")

if commit is not None:
fortran_src_dir = Path("/app/Enzyme/enzyme/Fortran")
if fortran_src_dir.is_dir():
fortran_dest_dir = Path(f"/opt/compiler-explorer/{branch}/fortran")
fortran_dest_dir.mkdir(parents=True, exist_ok=True)
for f90_name in ["enzyme_function_hooks.f90", "enzyme.f90"]:
src_f90 = fortran_src_dir / f90_name
if src_f90.is_file():
shutil.copy2(str(src_f90), str(fortran_dest_dir / f90_name))

flang_compilers = [c for c in compiler_data if c["version"] >= 20]

context = {
"compilers": compiler_data,
"flang_compilers": flang_compilers,
"julia_versions": julia_versions,
"trunk_val": trunk_val,
"enzymejax_commit": ejaxcommit if ejaxcommit is not None else "<unknown>",
Expand All @@ -146,10 +168,11 @@ def main():
FILES_MAP = []

if commit is not None or reactant_commit is not None or len(sys.argv) == 4:
FILES_MAP.append(("cuda", "cuda.enzyme.properties"))
FILES_MAP.append(("cpp", "c++.enzyme.properties"))
FILES_MAP.append(("c", "c.enzyme.properties"))
FILES_MAP.append(("llvm", "llvm.enzyme.properties"))
FILES_MAP.append(("cuda", "cuda.enzyme.properties"))
FILES_MAP.append(("cpp", "c++.enzyme.properties"))
FILES_MAP.append(("c", "c.enzyme.properties"))
FILES_MAP.append(("fortran", "fortran.enzyme.properties"))
FILES_MAP.append(("llvm", "llvm.enzyme.properties"))

if ejaxcommit is not None or len(sys.argv) == 4:
FILES_MAP.append(("mlir", "mlir.enzyme.properties"))
Expand Down
25 changes: 25 additions & 0 deletions template_files/etc/config/fortran.enzyme.properties.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Default settings for Fortran
compilers=&flang-enzyme-main
defaultCompiler=flang{{ flang_compilers[-1].version }}-enzyme-main
demangler=c++filt
objdumper=objdump
postProcess=
supportsBinary=true
supportsExecute=true
supportsLibraryCodeFilter=true
binaryHideFuncRe=^(__.*|_(init|start|fini)|(de)?register_tm_clones|call_gmon_start|frame_dummy|\.plt.*|.*@plt.*|_dl_relocate_static_pie|_GLOBAL__sub_I_.*|_Z.*|_FortranA.*|CFI_.*|atexit|backtrace_|(Convert|Validate|AllocateValidated).*|.*\(.*)$

group.flang-enzyme-main.compilers={% for compiler in flang_compilers %}flang{{ compiler.version }}-enzyme-main{% if not loop.last %}:{% endif %}{% endfor %}
group.flang-enzyme-main.intelAsm=-masm=intel
group.flang-enzyme-main.compilerType=flang
group.flang-enzyme-main.compilerCategories=flang
group.flang-enzyme-main.supportsExecute=true
group.flang-enzyme-main.isSemVer=true
group.flang-enzyme-main.groupName=FLANG + ENZYME (main)

{% for compiler in flang_compilers %}
compiler.flang{{ compiler.version }}-enzyme-main.exe=/usr/bin/flang-{{ compiler.version }}
compiler.flang{{ compiler.version }}-enzyme-main.options=--config=/opt/compiler-explorer/main/FlangEnzyme-{{ compiler.version }}.cfg -Xflang -load -Xflang /opt/compiler-explorer/main/FlangEnzyme-{{ compiler.version }}.so -I/var/cache/enzyme-fortran-modules/{{ compiler.version }}
compiler.flang{{ compiler.version }}-enzyme-main.semver={{ compiler.semver }}
compiler.flang{{ compiler.version }}-enzyme-main.name=flang {{ compiler.version }} ({{ compiler.enzyme_commit }})
{% endfor %}
Loading