From 44342e167aa062dec8b94c6c221feafe997dae81 Mon Sep 17 00:00:00 2001 From: bit-aloo Date: Thu, 24 Apr 2025 14:55:41 +0530 Subject: [PATCH 1/4] add enzyme distribution step --- src/bootstrap/src/core/build_steps/dist.rs | 52 ++++++++++++++++++++++ src/bootstrap/src/core/builder/mod.rs | 3 +- src/bootstrap/src/utils/tarball.rs | 3 ++ 3 files changed, 57 insertions(+), 1 deletion(-) diff --git a/src/bootstrap/src/core/build_steps/dist.rs b/src/bootstrap/src/core/build_steps/dist.rs index 3c412683b9492..98e7df8206b19 100644 --- a/src/bootstrap/src/core/build_steps/dist.rs +++ b/src/bootstrap/src/core/build_steps/dist.rs @@ -2599,3 +2599,55 @@ impl Step for Gcc { tarball.generate() } } + +#[derive(Clone, Debug, Eq, Hash, PartialEq)] +pub struct Enzyme { + pub target: TargetSelection, +} + +impl Step for Enzyme { + type Output = Option; + const DEFAULT: bool = false; + const ONLY_HOSTS: bool = true; + + fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> { + run.alias("enzyme") + } + + fn make_run(run: RunConfig<'_>) { + run.builder.ensure(Enzyme { target: run.target }); + } + + fn run(self, builder: &Builder<'_>) -> Self::Output { + let mut tarball = Tarball::new(builder, "enzyme", &self.target.triple); + let enzyme_dir = + builder.ensure(super::llvm::Enzyme { target: self.target }).join("build/Enzyme"); + + tarball.set_overlay(OverlayKind::Enzyme); + tarball.is_preview(true); + + if let Some(llvm_config) = builder.llvm_config(builder.config.build) { + let major = llvm::get_llvm_version_major(builder, &llvm_config); + let prefix = format!("libEnzyme-{major}"); + let mut found = false; + + for entry in std::fs::read_dir(&enzyme_dir) + .unwrap_or_else(|_| panic!("Failed to read {:?}", enzyme_dir)) + { + let path = entry.unwrap().path(); + if let Some(name) = path.file_name().and_then(|n| n.to_str()) { + if name.starts_with(&prefix) && is_dylib(&path) { + tarball.add_file(path, "", FileType::NativeLibrary); + found = true; + } + } + } + + assert!(found, "Enzyme library starting with '{}' not found", prefix); + tarball.add_legal_and_readme_to("share/doc/enzyme"); + return Some(tarball.generate()); + } + + None + } +} diff --git a/src/bootstrap/src/core/builder/mod.rs b/src/bootstrap/src/core/builder/mod.rs index c32d9c2870cfa..e1767a1b1cdac 100644 --- a/src/bootstrap/src/core/builder/mod.rs +++ b/src/bootstrap/src/core/builder/mod.rs @@ -1084,7 +1084,8 @@ impl<'a> Builder<'a> { dist::PlainSourceTarball, dist::BuildManifest, dist::ReproducibleArtifacts, - dist::Gcc + dist::Gcc, + dist::Enzyme ), Kind::Install => describe!( install::Docs, diff --git a/src/bootstrap/src/utils/tarball.rs b/src/bootstrap/src/utils/tarball.rs index 7b77b21293413..ff2b1e0f996d2 100644 --- a/src/bootstrap/src/utils/tarball.rs +++ b/src/bootstrap/src/utils/tarball.rs @@ -26,6 +26,7 @@ pub(crate) enum OverlayKind { RustAnalyzer, RustcCodegenCranelift, LlvmBitcodeLinker, + Enzyme, } impl OverlayKind { @@ -72,6 +73,7 @@ impl OverlayKind { "LICENSE-MIT", "src/tools/llvm-bitcode-linker/README.md", ], + OverlayKind::Enzyme => &["src/tools/enzyme/Readme.md", "src/tools/enzyme/LICENSE"], } } @@ -94,6 +96,7 @@ impl OverlayKind { .version(builder, &builder.release_num("rust-analyzer/crates/rust-analyzer")), OverlayKind::RustcCodegenCranelift => builder.rust_version(), OverlayKind::LlvmBitcodeLinker => builder.rust_version(), + OverlayKind::Enzyme => builder.rust_version(), } } } From 1ad7a8bf55798dc9694cd6636c5f6f6072114815 Mon Sep 17 00:00:00 2001 From: bit-aloo Date: Wed, 30 Apr 2025 09:39:41 +0530 Subject: [PATCH 2/4] Add hard error against building enzyme via downloaded-llvm-ci and add a comment explaining LLVM_DIR flag in cfg in enzyme build step --- src/bootstrap/src/core/build_steps/llvm.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/bootstrap/src/core/build_steps/llvm.rs b/src/bootstrap/src/core/build_steps/llvm.rs index 86af956535e5e..f3c2aaf8ef8f5 100644 --- a/src/bootstrap/src/core/build_steps/llvm.rs +++ b/src/bootstrap/src/core/build_steps/llvm.rs @@ -903,6 +903,14 @@ impl Step for Enzyme { ), )] fn run(self, builder: &Builder<'_>) -> PathBuf { + // FIXME: This can be removed once we start shipping the CMake files needed by Enzyme + // to build against a standalone LLVM binary. + // For now, Enzyme must be built with the in-tree LLVM and shared LLVM (`llvm.link-shared = true`), not one downloaded from CI. + if builder.config.llvm_from_ci || !builder.config.llvm_link_shared() { + panic!( + "Enzyme must be built with an in-tree LLVM and requires `llvm.link-shared = true`.Set `llvm.download-ci-llvm = false` and `llvm.link-shared = true` to build Enzyme." + ); + } builder.require_submodule( "src/tools/enzyme", Some("The Enzyme sources are required for autodiff."), @@ -970,6 +978,7 @@ impl Step for Enzyme { .env("LLVM_CONFIG_REAL", &llvm_config) .define("LLVM_ENABLE_ASSERTIONS", "ON") .define("ENZYME_EXTERNAL_SHARED_LIB", "ON") + // Enzyme must be built against this exact LLVM build — mixing versions breaks compatibility. .define("LLVM_DIR", builder.llvm_out(target)); cfg.build(); From 11ddc6bf40670a418b9ecfdb0da8ce5ba498e220 Mon Sep 17 00:00:00 2001 From: bit-aloo Date: Fri, 2 May 2025 01:08:21 +0530 Subject: [PATCH 3/4] add enzyme dist step in x86-64-linux ci dockerfile --- src/ci/docker/host-x86_64/dist-x86_64-linux/Dockerfile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ci/docker/host-x86_64/dist-x86_64-linux/Dockerfile b/src/ci/docker/host-x86_64/dist-x86_64-linux/Dockerfile index bedf45c8630cf..bd173adf5eadf 100644 --- a/src/ci/docker/host-x86_64/dist-x86_64-linux/Dockerfile +++ b/src/ci/docker/host-x86_64/dist-x86_64-linux/Dockerfile @@ -103,7 +103,8 @@ ENV SCRIPT python3 ../x.py build --set rust.debug=true opt-dist && \ --include-default-paths \ build-manifest bootstrap && \ # Use GCC for building GCC, as it seems to behave badly when built with Clang - CC=/rustroot/bin/cc CXX=/rustroot/bin/c++ python3 ../x.py dist gcc + # Also build Enzyme only on the x64 Linux dist runner + CC=/rustroot/bin/cc CXX=/rustroot/bin/c++ python3 ../x.py dist gcc && ../x.py dist enzyme ENV CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER=clang # This is the only builder which will create source tarballs From aef04ebe1f86dff2ab2fbd94876c43cb7d5cd325 Mon Sep 17 00:00:00 2001 From: bit-aloo Date: Tue, 6 May 2025 07:36:35 +0530 Subject: [PATCH 4/4] update enzyme --- src/tools/enzyme | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tools/enzyme b/src/tools/enzyme index a35f4f773118c..5abddac685fd0 160000 --- a/src/tools/enzyme +++ b/src/tools/enzyme @@ -1 +1 @@ -Subproject commit a35f4f773118ccfbd8d05102eb12a34097b1ee55 +Subproject commit 5abddac685fd0143697274e95a93b6a06875feb8