From 8052bde17c187fdf1aa59e79f449ebb55e3c840a Mon Sep 17 00:00:00 2001 From: Joe Wallwork Date: Thu, 7 May 2026 13:03:31 +0100 Subject: [PATCH 1/5] Add enzyme_mod containing activity descriptor bindings --- enzyme/Fortran/enzyme_mod.f90 | 36 +++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 enzyme/Fortran/enzyme_mod.f90 diff --git a/enzyme/Fortran/enzyme_mod.f90 b/enzyme/Fortran/enzyme_mod.f90 new file mode 100644 index 00000000000..a28de48bb36 --- /dev/null +++ b/enzyme/Fortran/enzyme_mod.f90 @@ -0,0 +1,36 @@ +! ===- enzyme_mod.f90 - Fortran bindings for Enzyme -----------------------=== ! +! +! Enzyme Project +! +! Part of the Enzyme Project, under the Apache License v2.0 with LLVM +! Exceptions. See https://llvm.org/LICENSE.txt for license information. +! SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +! +! If using this code in an academic setting, please cite the following: +! @misc{enzymeGithub, +! author = {William S. Moses and Valentin Churavy}, +! title = {Enzyme: High Performance Automatic Differentiation of LLVM}, +! year = {2020}, +! howpublished = {\url{https://github.com/wsmoses/Enzyme}}, +! note = {commit xxxxxxx} +! } +! +! ===----------------------------------------------------------------------=== ! +! +! This file provides Fortran bindings for Enzyme. +! +! ===----------------------------------------------------------------------=== ! +module enzyme_mod + use iso_c_binding, only: c_int + implicit none + private + + ! Bindings for activity descriptors + integer(c_int), public, bind(C, name="enzyme_const") :: enzyme_const + integer(c_int), public, bind(C, name="enzyme_dup") :: enzyme_dup + integer(c_int), public, bind(C, name="enzyme_dupnoneed") :: enzyme_dupnoneed + integer(c_int), public, bind(C, name="enzyme_out") :: enzyme_out + integer(c_int), public, bind(C, name="enzyme_scalar") :: enzyme_scalar + integer(c_int), public, bind(C, name="enzyme_width") :: enzyme_width + integer(c_int), public, bind(C, name="enzyme_vector") :: enzyme_vector +end module enzyme_mod From 47c59bd689eff51c08be939a613e0507f2953203 Mon Sep 17 00:00:00 2001 From: Joe Wallwork Date: Thu, 7 May 2026 13:46:07 +0100 Subject: [PATCH 2/5] Hook Fortran bindings up in build system --- enzyme/CMakeLists.txt | 3 ++- enzyme/Fortran/CMakeLists.txt | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 enzyme/Fortran/CMakeLists.txt diff --git a/enzyme/CMakeLists.txt b/enzyme/CMakeLists.txt index 25a6a78940d..b489f43195d 100644 --- a/enzyme/CMakeLists.txt +++ b/enzyme/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.13) -project(Enzyme) +project(Enzyme LANGUAGES C CXX Fortran) include(CMakePackageConfigHelpers) include(CheckIncludeFile) @@ -299,6 +299,7 @@ endif() if (ENZYME_ENABLE_PLUGINS) add_subdirectory(test) endif() +add_subdirectory(Fortran) # The benchmarks data are not in git-exported source archives to minimize size. # Only add the benchmarks if the directory exists. diff --git a/enzyme/Fortran/CMakeLists.txt b/enzyme/Fortran/CMakeLists.txt new file mode 100644 index 00000000000..cd98bc6f162 --- /dev/null +++ b/enzyme/Fortran/CMakeLists.txt @@ -0,0 +1,25 @@ +include(FortranCInterface) +FortranCInterface_VERIFY(QUIET) + +add_library(EnzymeFortran enzyme_mod.f90) + +# Install library, create target file +install( + TARGETS EnzymeFortran + EXPORT EnzymeFortranTargets + LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" + PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} + PRIVATE_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} + INCLUDES + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/EnzymeFortran) + +# Install Fortran module file +if(NOT DEFINED CMAKE_INSTALL_MODULEDIR) + set( + CMAKE_INSTALL_MODULEDIR + "${CMAKE_INSTALL_INCLUDEDIR}/EnzymeFortran" + CACHE STRING "Directory in prefix to install generated module files" + ) +endif() +install(FILES "${CMAKE_Fortran_MODULE_DIRECTORY}/enzyme_mod.mod" + DESTINATION "${CMAKE_INSTALL_MODULEDIR}") From 27b7edc9658da36fcfc8881a97b73aa68d4b986a Mon Sep 17 00:00:00 2001 From: Joe Wallwork Date: Thu, 7 May 2026 15:21:33 +0100 Subject: [PATCH 3/5] Rename enzyme_mod->enzyme --- enzyme/Fortran/CMakeLists.txt | 4 ++-- enzyme/Fortran/{enzyme_mod.f90 => enzyme.f90} | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) rename enzyme/Fortran/{enzyme_mod.f90 => enzyme.f90} (93%) diff --git a/enzyme/Fortran/CMakeLists.txt b/enzyme/Fortran/CMakeLists.txt index cd98bc6f162..fb6b62454f0 100644 --- a/enzyme/Fortran/CMakeLists.txt +++ b/enzyme/Fortran/CMakeLists.txt @@ -1,7 +1,7 @@ include(FortranCInterface) FortranCInterface_VERIFY(QUIET) -add_library(EnzymeFortran enzyme_mod.f90) +add_library(EnzymeFortran enzyme.f90) # Install library, create target file install( @@ -21,5 +21,5 @@ if(NOT DEFINED CMAKE_INSTALL_MODULEDIR) CACHE STRING "Directory in prefix to install generated module files" ) endif() -install(FILES "${CMAKE_Fortran_MODULE_DIRECTORY}/enzyme_mod.mod" +install(FILES "${CMAKE_Fortran_MODULE_DIRECTORY}/enzyme.mod" DESTINATION "${CMAKE_INSTALL_MODULEDIR}") diff --git a/enzyme/Fortran/enzyme_mod.f90 b/enzyme/Fortran/enzyme.f90 similarity index 93% rename from enzyme/Fortran/enzyme_mod.f90 rename to enzyme/Fortran/enzyme.f90 index a28de48bb36..1e999ff1004 100644 --- a/enzyme/Fortran/enzyme_mod.f90 +++ b/enzyme/Fortran/enzyme.f90 @@ -1,4 +1,4 @@ -! ===- enzyme_mod.f90 - Fortran bindings for Enzyme -----------------------=== ! +! ===- enzyme.f90 - Fortran bindings for Enzyme ---------------------------=== ! ! ! Enzyme Project ! @@ -20,7 +20,7 @@ ! This file provides Fortran bindings for Enzyme. ! ! ===----------------------------------------------------------------------=== ! -module enzyme_mod +module enzyme use iso_c_binding, only: c_int implicit none private @@ -33,4 +33,4 @@ module enzyme_mod integer(c_int), public, bind(C, name="enzyme_scalar") :: enzyme_scalar integer(c_int), public, bind(C, name="enzyme_width") :: enzyme_width integer(c_int), public, bind(C, name="enzyme_vector") :: enzyme_vector -end module enzyme_mod +end module enzyme From 0479c37962c5d24c379213ea820be0f79f38efab Mon Sep 17 00:00:00 2001 From: Joe Wallwork Date: Mon, 11 May 2026 17:07:12 +0100 Subject: [PATCH 4/5] Use enable_language in Fortran subdir --- enzyme/CMakeLists.txt | 2 +- enzyme/Fortran/CMakeLists.txt | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/enzyme/CMakeLists.txt b/enzyme/CMakeLists.txt index b489f43195d..22a573c6e0c 100644 --- a/enzyme/CMakeLists.txt +++ b/enzyme/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.13) -project(Enzyme LANGUAGES C CXX Fortran) +project(Enzyme) include(CMakePackageConfigHelpers) include(CheckIncludeFile) diff --git a/enzyme/Fortran/CMakeLists.txt b/enzyme/Fortran/CMakeLists.txt index fb6b62454f0..d3e6fdaa139 100644 --- a/enzyme/Fortran/CMakeLists.txt +++ b/enzyme/Fortran/CMakeLists.txt @@ -1,3 +1,4 @@ +enable_language(Fortran) include(FortranCInterface) FortranCInterface_VERIFY(QUIET) From 827e439a404abcca9508feb2fd80e8ad9a89e864 Mon Sep 17 00:00:00 2001 From: Joe Wallwork Date: Mon, 11 May 2026 17:09:30 +0100 Subject: [PATCH 5/5] Add ENZYME_FORTRAN option for building bindings --- enzyme/CMakeLists.txt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/enzyme/CMakeLists.txt b/enzyme/CMakeLists.txt index 22a573c6e0c..ce533e69cfb 100644 --- a/enzyme/CMakeLists.txt +++ b/enzyme/CMakeLists.txt @@ -34,10 +34,12 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) option(ENZYME_ENABLE_PLUGINS "Enable Clang/LLD/Opt plugins" ON) option(ENZYME_ENABLE_BENCHMARKS "Enable benchmarks" OFF) option(ENZYME_BC_LOADER "Enable bitcode loader" ON) +option(ENZYME_FORTRAN "Build Enzyme Fortran bindings" OFF) option(ENZYME_CLANG "Build enzyme clang plugin" ON) option(ENZYME_FLANG "Build enzyme flang symlink" OFF) option(ENZYME_MLIR "Build enzyme mlir plugin" OFF) option(ENZYME_IFX "Enable enzyme support for the Intel Fortran compiler IFX" OFF) +option(ENZYME_IFX "Enable enzyme support for the Intel Fortran compiler IFX" OFF) option(ENZYME_EXTERNAL_SHARED_LIB "Build external shared library" OFF) option(ENZYME_APPLE_DYNAMIC_LOOKUP "On Apple, link Enzyme shared library with -flat_namespace/-undefined,dynamic_lookup instead of linking LLVM" @@ -299,7 +301,9 @@ endif() if (ENZYME_ENABLE_PLUGINS) add_subdirectory(test) endif() -add_subdirectory(Fortran) +if (ENZYME_FORTRAN) + add_subdirectory(Fortran) +endif() # The benchmarks data are not in git-exported source archives to minimize size. # Only add the benchmarks if the directory exists.