-
Notifications
You must be signed in to change notification settings - Fork 5
Enzyme support example #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
51 commits
Select commit
Hold shift + click to select a range
fb22652
Rebased.
nkoukpaizan acaaba2
Use better naming conventions
ashermancinelli 31fea4d
More verbose tests for enzyme support.
pelesh d5e410c
Extend list of LLVM/ClangEnzime libraries.
nkoukpaizan 0b10c47
int --> double in Enzyme examples.
nkoukpaizan fd75087
Minor name changes for Enzyme tests.
nkoukpaizan ff03521
Changed --enable-new-pm to directly use the new format.
nkoukpaizan 6c25725
gridkit +enzyme in CI.
nkoukpaizan d5d6f83
Update local Spack.
nkoukpaizan c00a160
Request llvm@19 in CI.
nkoukpaizan db2f158
Try using another build cache for LLVM.
nkoukpaizan 6824032
Add timeout for Github actions.
nkoukpaizan 443c18c
Try LLVM 18.
nkoukpaizan 11df3ec
Explicitly list LLVM as external Spack package with specified path.
nkoukpaizan 8460d35
Bug fix 'spec:'
nkoukpaizan 1e8930d
Bug fix in spack.yaml indents.
nkoukpaizan de1ce73
Check LLVM install.
nkoukpaizan 4a150f3
Go back to trying to build LLVM from scratch. Timeout at 500 minutes.
nkoukpaizan 5c7ef63
Remove spack clean.
nkoukpaizan 83c6f9d
Request verbose output for spack install.
nkoukpaizan 90dc064
Check storage on CI platform.
nkoukpaizan 1c381b8
Bug fix in storage-checking commands.
nkoukpaizan 425502b
Check storage after Spack setup.
nkoukpaizan 2e05f17
Check storage in /.
nkoukpaizan 94962dc
Check storage in /opt/.
nkoukpaizan ab8b9e9
Remove storage-checking step.
nkoukpaizan 5f84f6c
Try apt-get LLVM@18.
nkoukpaizan b5b682f
Try wget on llvm.sh script (should build latest stable version).
nkoukpaizan d210ddf
Revert "Try wget on llvm.sh script (should build latest stable versio…
nkoukpaizan 1b1ca43
Add apt-add-repository for llvm.
nkoukpaizan 7e4bb57
Bug fix: -->18.
nkoukpaizan d899618
wget the llvm snapshot
nkoukpaizan d2e4f6d
wget->curl
nkoukpaizan 115f7c7
Need to install wget or curl before using it.
nkoukpaizan c4405ff
'apt-get update' before 'apt-get install wget'.
nkoukpaizan 00aaf82
Improved apt-get commands for LLVM build based on Enzyme Dockerfile.
nkoukpaizan 7e06efb
Explicit request of LLVM 18.
nkoukpaizan 4af323d
Build extra LLVM components in base image.
nkoukpaizan 40b4ce7
Look for LLVM in usr/lib.
nkoukpaizan 1b7d7fb
Look for LLVM in /usr/bin.
nkoukpaizan cd104b7
Try ubuntu-22.04-->ubuntu-24.04. The latter should have llvm-18 avail…
nkoukpaizan e61f963
Check /usr/lib/cmake/
nkoukpaizan 24aec42
External Spack package for llvm explicitly set to /usr/lib/llvm-18.
nkoukpaizan ef8d8ef
Remove llvm from 'spack external find', so that it tries to use the p…
nkoukpaizan 1575920
Update local Spack submodule.
nkoukpaizan 9b3af35
Increase timeout to 30min.
nkoukpaizan f1e402f
Update local Spack submodule.
nkoukpaizan bc7dd51
Update local Spack submodule.
nkoukpaizan c3d7c0d
Update local Spack submodule.
nkoukpaizan db4d6c1
Update README with Enzyme dependency.
nkoukpaizan 20470b6
Changed Enzyme add_custom_target name to fix build issues with Ninja.
nkoukpaizan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
[submodule "Buildsystem/spack/spack"] | ||
path = Buildsystem/spack/spack | ||
url = https://github.com/nkoukpaizan/spack.git | ||
branch = Gridkit-package-dev | ||
branch = Gridkit-package-dev+enzyme |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
# | ||
# Copyright (c) 2017, Lawrence Livermore National Security, LLC. | ||
# Produced at the Lawrence Livermore National Laboratory. | ||
# Written by Slaven Peles <peles2@llnl.gov>. | ||
# LLNL-CODE-718378. | ||
# All rights reserved. | ||
# | ||
# This file is part of GridKit™. For details, see github.com/LLNL/GridKit | ||
# Please also read the LICENSE file. | ||
# | ||
# Redistribution and use in source and binary forms, with or without | ||
# modification, are permitted provided that the following conditions are met: | ||
# - Redistributions of source code must retain the above copyright notice, | ||
# this list of conditions and the disclaimer below. | ||
# - Redistributions in binary form must reproduce the above copyright notice, | ||
# this list of conditions and the disclaimer (as noted below) in the | ||
# documentation and/or other materials provided with the distribution. | ||
# - Neither the name of the LLNS/LLNL nor the names of its contributors may | ||
# be used to endorse or promote products derived from this software without | ||
# specific prior written permission. | ||
# | ||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND | ||
# CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, | ||
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | ||
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
# DISCLAIMED. IN NO EVENT SHALL LAWRENCE LIVERMORE NATIONAL | ||
# SECURITY, LLC, THE U.S. DEPARTMENT OF ENERGY OR CONTRIBUTORS BE | ||
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, | ||
# OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISINGIN ANY | ||
# WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | ||
# THE POSSIBILITY OF SUCH DAMAGE. | ||
# | ||
# Lawrence Livermore National Laboratory is operated by Lawrence Livermore | ||
# National Security, LLC, for the U.S. Department of Energy, National | ||
# Nuclear Security Administration under Contract DE-AC52-07NA27344. | ||
# | ||
# This document was prepared as an account of work sponsored by an agency | ||
# of the United States government. Neither the United States government nor | ||
# Lawrence Livermore National Security, LLC, nor any of their employees | ||
# makes any warranty, expressed or implied, or assumes any legal liability | ||
# or responsibility for the accuracy, completeness, or usefulness of any | ||
# information, apparatus, product, or process disclosed, or represents that | ||
# its use would not infringe privately owned rights. Reference herein to | ||
# any specific commercial product, process, or service by trade name, | ||
# trademark, manufacturer, or otherwise does not necessarily constitute or | ||
# imply its endorsement, recommendation, or favoring by the United States | ||
# government or Lawrence Livermore National Security, LLC. The views and | ||
# opinions of authors expressed herein do not necessarily state or reflect | ||
# those of the United States government or Lawrence Livermore National | ||
# Security, LLC, and shall not be used for advertising or product | ||
# endorsement purposes. | ||
# | ||
|
||
#[[ | ||
|
||
Finds Enzyme Clang plugin | ||
|
||
User may set: | ||
- ENZYME_DIR | ||
|
||
Author(s): | ||
- Asher Mancinelli <ashermancinelli@gmail.com> | ||
|
||
]] | ||
|
||
if(NOT ${CMAKE_C_COMPILER_ID} STREQUAL "Clang" OR NOT ${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang") | ||
message(FATAL_ERROR "Enzyme may only be enabled if building with Clang") | ||
endif() | ||
|
||
find_library(ENZYME_LLVM_PLUGIN_LIBRARY | ||
NAMES | ||
LLVMEnzyme-19.so | ||
LLVMEnzyme-18.so | ||
LLVMEnzyme-17.so | ||
LLVMEnzyme-16.so | ||
LLVMEnzyme-15.so | ||
LLVMEnzyme-14.so | ||
LLVMEnzyme-13.so | ||
LLVMEnzyme-12.so | ||
LLVMEnzyme-11.so | ||
LLVMEnzyme-10.so | ||
LLVMEnzyme-9.so | ||
PATHS | ||
${ENZYME_DIR} $ENV{ENZYME_DIR} | ||
ENV LD_LIBRARY_PATH ENV DYLD_LIBRARY_PATH | ||
PATH_SUFFIXES | ||
lib64 lib) | ||
|
||
find_library(ENZYME_CLANG_PLUGIN_LIBRARY | ||
NAMES | ||
ClangEnzyme-19.so | ||
ClangEnzyme-18.so | ||
ClangEnzyme-17.so | ||
ClangEnzyme-16.so | ||
ClangEnzyme-15.so | ||
ClangEnzyme-14.so | ||
ClangEnzyme-13.so | ||
ClangEnzyme-12.so | ||
ClangEnzyme-11.so | ||
ClangEnzyme-10.so | ||
ClangEnzyme-9.so | ||
PATHS | ||
${ENZYME_DIR} $ENV{ENZYME_DIR} | ||
ENV LD_LIBRARY_PATH ENV DYLD_LIBRARY_PATH | ||
PATH_SUFFIXES | ||
lib64 lib) | ||
|
||
find_program(GRIDKIT_LLVM_LINK llvm-link) | ||
find_program(GRIDKIT_OPT opt) | ||
|
||
if("${GRIDKIT_LLVM_LINK}" STREQUAL "GRIDKIT_LLVM_LINK-NOTFOUND") | ||
message(FATAL_ERROR "Could not find llvm-link! Will not be able to build Enzyme targets.") | ||
endif() | ||
|
||
if("${GRIDKIT_OPT}" STREQUAL "GRIDKIT_OPT-NOTFOUND") | ||
message(FATAL_ERROR "Could not find opt! Will not be able to build Enzyme targets.") | ||
endif() | ||
|
||
message(STATUS "${ENZYME_CLANG_PLUGIN_LIBRARY};${ENZYME_LLVM_PLUGIN_LIBRARY}") | ||
if((NOT ${ENZYME_LLVM_PLUGIN_LIBRARY}) OR (NOT ${ENZYME_CLANG_PLUGIN_LIBRARY})) | ||
set(ENZYME_CLANG_PLUGIN_LIBRARY CACHE FILEPATH "Path to Enzyme Clang plugin library") | ||
set(ENZYME_LLVM_PLUGIN_LIBRARY CACHE FILEPATH "Path to Enzyme LLVM plugin library") | ||
message(STATUS "Found Enzyme clang plugin: ${ENZYME_CLANG_PLUGIN_LIBRARY}") | ||
message(STATUS "Found Enzyme LLVM plugin: ${ENZYME_LLVM_PLUGIN_LIBRARY}") | ||
get_filename_component(ENZYME_LIBRARY_DIR ${ENZYME_CLANG_PLUGIN_LIBRARY} DIRECTORY CACHE "Enzyme library directory") | ||
else() | ||
message(FATAL_ERROR "Enzyme library not found!" | ||
" Set ENZYME_DIR to Enzyme's installation prefix.") | ||
endif() | ||
|
||
macro(enzyme_add_executable) | ||
set(options) | ||
set(oneValueArgs NAME) | ||
set(multiValueArgs SOURCES LINK_LIBRARIES) | ||
cmake_parse_arguments(enzyme_add_executable "${options}" "${oneValueArgs}" | ||
"${multiValueArgs}" ${ARGN}) | ||
|
||
set(PHASE2 "${CMAKE_CURRENT_BINARY_DIR}/${enzyme_add_executable_NAME}.bc") | ||
set(PHASE3 "${CMAKE_CURRENT_BINARY_DIR}/${enzyme_add_executable_NAME}_enzyme.ll") | ||
set(PHASE4 "${CMAKE_CURRENT_BINARY_DIR}/${enzyme_add_executable_NAME}_opt.ll") | ||
set(PHASE5 "${CMAKE_CURRENT_BINARY_DIR}/${enzyme_add_executable_NAME}") | ||
|
||
set(OBJS "") | ||
|
||
foreach(SRC ${enzyme_add_executable_SOURCES}) | ||
set(PHASE0 "${CMAKE_CURRENT_SOURCE_DIR}/${SRC}") | ||
set(PHASE1 "${CMAKE_CURRENT_BINARY_DIR}/${enzyme_add_executable_NAME}_${SRC}_compile.o") | ||
add_custom_command( | ||
DEPENDS ${PHASE0} | ||
OUTPUT ${PHASE1} | ||
COMMAND ${CMAKE_CXX_COMPILER} -flto -c ${PHASE0} -O2 -fno-vectorize -ffast-math -fno-unroll-loops -o ${PHASE1} | ||
COMMENT "Compiling ${SRC} to object file for target ${enzyme_add_executable_NAME}" | ||
) | ||
set(OBJS "${OBJS} ${PHASE1}") | ||
endforeach() | ||
|
||
cmake_language(EVAL CODE " | ||
add_custom_command( | ||
DEPENDS ${OBJS} | ||
OUTPUT ${PHASE2} | ||
COMMAND ${GRIDKIT_LLVM_LINK} ${OBJS} -o ${PHASE2} | ||
COMMENT \"Linking object files to LLVM bytecode for target ${enzyme_add_executable_NAME}\" | ||
) | ||
") | ||
|
||
add_custom_command( | ||
DEPENDS ${PHASE2} | ||
OUTPUT ${PHASE3} | ||
COMMAND ${GRIDKIT_OPT} ${PHASE2} -load-pass-plugin=${ENZYME_LLVM_PLUGIN_LIBRARY} -passes=enzyme -o ${PHASE3} -S | ||
pelesh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
COMMENT "Running Enzyme opt pass on target ${enzyme_add_executable_NAME}" | ||
) | ||
|
||
add_custom_command( | ||
DEPENDS ${PHASE3} | ||
OUTPUT ${PHASE4} | ||
COMMAND ${GRIDKIT_OPT} ${PHASE3} -O2 -o ${PHASE4} -S | ||
COMMENT "Running remaining opt passes on target ${enzyme_add_executable_NAME}" | ||
) | ||
|
||
add_custom_command( | ||
DEPENDS ${PHASE4} | ||
OUTPUT ${PHASE5} | ||
COMMAND ${CMAKE_CXX_COMPILER} ${PHASE4} -o ${PHASE5} | ||
) | ||
|
||
add_custom_target( | ||
"${enzyme_add_executable_NAME}_target" ALL | ||
DEPENDS ${PHASE5} | ||
) | ||
endmacro() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
add_subdirectory(Standalone) | ||
add_subdirectory(Library) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
enzyme_add_executable(NAME EnzymeLibCheck SOURCES main.cpp library.cpp) | ||
#install(TARGETS ${CMAKE_CURRENT_BINARY_DIR}/EnzymeLibCheck DESTINATION bin) | ||
|
||
Comment on lines
+2
to
+3
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we want to install Enzyme targets or this is out of scope for this PR? |
||
add_test(NAME "EnzymeLibCheck" COMMAND ${CMAKE_CURRENT_BINARY_DIR}/EnzymeLibCheck) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
double square(double x) { | ||
return x * x; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#pragma once | ||
double square(double x); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#include <iostream> | ||
#include <limits> | ||
#include "library.hpp" | ||
|
||
double __enzyme_autodiff(double (*)(double), ...); | ||
|
||
int main() { | ||
int fail = 0; | ||
double sq = square(5.0); | ||
double dsq = __enzyme_autodiff(square, 5.0); | ||
|
||
std::cout << "x = 5, x^2 = " << sq << ", d(x^2)/dx = " << dsq << "\n"; | ||
if (std::abs(dsq - 10.0) > std::numeric_limits<double>::epsilon()) | ||
{ | ||
fail++; | ||
std::cout << "Result incorrect\n"; | ||
} | ||
std::cout << "Status: " << fail << "\n"; | ||
return fail; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
enzyme_add_executable( | ||
NAME EnzymeStandaloneCheck | ||
SOURCES main.cpp | ||
) | ||
|
||
add_test(NAME "EnzymeStandaloneCheck" COMMAND ${CMAKE_CURRENT_BINARY_DIR}/EnzymeStandaloneCheck) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#include <iostream> | ||
#include <limits> | ||
|
||
double square(double x) { | ||
return x * x; | ||
} | ||
|
||
Comment on lines
+4
to
+7
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be hard-wired |
||
double __enzyme_autodiff(double(*)(double), ...); | ||
double dsquare(double x) { | ||
return __enzyme_autodiff(square, x); | ||
} | ||
|
||
double sq = square(5.0); | ||
double dsq = dsquare(5.0); | ||
|
||
int main() | ||
{ | ||
int fail = 0; | ||
std::cout << "x = 5, x^2 = " << sq << ", d(x^2)/dx = " << dsq << "\n"; | ||
if (std::abs(dsq - 10.0) > std::numeric_limits<double>::epsilon()) | ||
{ | ||
fail++; | ||
std::cout << "Result incorrect\n"; | ||
} | ||
std::cout << "Status: " << fail << "\n"; | ||
return fail; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.