diff --git a/enzyme/CMakeLists.txt b/enzyme/CMakeLists.txt index 25a6a78940d..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,6 +301,9 @@ endif() if (ENZYME_ENABLE_PLUGINS) add_subdirectory(test) endif() +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. diff --git a/enzyme/Fortran/CMakeLists.txt b/enzyme/Fortran/CMakeLists.txt new file mode 100644 index 00000000000..d3e6fdaa139 --- /dev/null +++ b/enzyme/Fortran/CMakeLists.txt @@ -0,0 +1,26 @@ +enable_language(Fortran) +include(FortranCInterface) +FortranCInterface_VERIFY(QUIET) + +add_library(EnzymeFortran enzyme.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" + DESTINATION "${CMAKE_INSTALL_MODULEDIR}") diff --git a/enzyme/Fortran/enzyme.f90 b/enzyme/Fortran/enzyme.f90 new file mode 100644 index 00000000000..1e999ff1004 --- /dev/null +++ b/enzyme/Fortran/enzyme.f90 @@ -0,0 +1,36 @@ +! ===- enzyme.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 + 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