homebrew-core/Formula/enzyme.rb

65 lines
2.3 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.26.tar.gz", using: :homebrew_curl
sha256 "b212bf13a50c3297fb0ca9fa3094cfd16b399f7c57a928b8eb9e293006fa0c32"
license "Apache-2.0" => { with: "LLVM-exception" }
head "https://github.com/wsmoses/Enzyme.git", branch: "main"
bottle do
sha256 cellar: :any, arm64_monterey: "c3a732216ec6777c8fdeae23f7d5f21830cfd468a114009f56ef941b0bf6aeca"
sha256 cellar: :any, arm64_big_sur: "10c7234e26194d88b5e2d4410134ef75cb37321f489cef274a1930315d302382"
sha256 cellar: :any, monterey: "2a295c1e70cf5386a82d37c96f7e5246052f3fa7abff1fed9970adeabf29d0d8"
sha256 cellar: :any, big_sur: "b42c1a22302500732113bfb7416ccf5c0ed1b6079777057a10661c6a08bd4192"
sha256 cellar: :any, catalina: "5d684f37160344c929e98970b10a04e7070eb023b606df4119e3f244ed93eef4"
end
depends_on "cmake" => :build
depends_on "llvm"
on_linux do
depends_on "gcc" => :build
end
fails_with gcc: "5"
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