homebrew-core/Formula/enzyme.rb

59 lines
2.2 KiB
Ruby

class Enzyme < Formula
desc "High-performance automatic differentiation of LLVM"
homepage "https://enzyme.mit.edu"
url "https://github.com/wsmoses/Enzyme/archive/v0.0.25.tar.gz"
sha256 "127dbb1654e977b8f6c48d8b44e0b8bf132900abe8334271130b9aa2a89bceaf"
license "Apache-2.0" => { with: "LLVM-exception" }
head "https://github.com/wsmoses/Enzyme.git", branch: "main"
bottle do
sha256 cellar: :any, arm64_monterey: "451d0f8bcef9e7b5cc7e545668419ff00a595553245aad3c109b9915420f78ea"
sha256 cellar: :any, arm64_big_sur: "ac762826b245870f415b8ed52c16510e1314e50e03a4d883ab676a11430adb3a"
sha256 cellar: :any, monterey: "f42892ea3bec8e2dfb2fb159dbdeb35233ae20c40cf089ac4683962335bee739"
sha256 cellar: :any, big_sur: "ba668a8e18962e15c16da345474af3115923976b9663e32fcbcc3f23487fdf3a"
sha256 cellar: :any, catalina: "d2d29607093765496d474e21022ced424b9dec5116fb7e138351aa49a777648e"
end
depends_on "cmake" => :build
depends_on "llvm"
def llvm
deps.map(&:to_formula).find { |f| f.name.match? "^llvm" }
end
def install
system "cmake", "-S", "enzyme", "-B", "build", *std_cmake_args, "-DLLVM_DIR=#{llvm.opt_lib}/cmake/llvm"
system "cmake", "--build", "build"
system "cmake", "--install", "build"
end
test do
(testpath/"test.c").write <<~EOS
#include <stdio.h>
extern double __enzyme_autodiff(void*, double);
double square(double x) {
return x * x;
}
double dsquare(double x) {
return __enzyme_autodiff(square, x);
}
int main() {
double i = 21.0;
printf("square(%.0f)=%.0f, dsquare(%.0f)=%.0f\\n", i, square(i), i, dsquare(i));
}
EOS
opt = llvm.opt_bin/"opt"
ENV["CC"] = llvm.opt_bin/"clang"
system ENV.cc, testpath/"test.c", "-S", "-emit-llvm", "-o", "input.ll", "-O2",
"-fno-vectorize", "-fno-slp-vectorize", "-fno-unroll-loops"
system opt, "input.ll", "--enable-new-pm=0",
"-load=#{opt_lib/shared_library("LLVMEnzyme-#{llvm.version.major}")}",
"--enzyme-attributor=0", "-enzyme", "-o", "output.ll", "-S"
system ENV.cc, "output.ll", "-O3", "-o", "test"
assert_equal "square(21)=441, dsquare(21)=42\n", shell_output("./test")
end
end