Skip to content

Commit 59d4114

Browse files
committed
Auto merge of #129176 - EnzymeAD:enzyme-backend, r=albertlarsan68
Autodiff Upstreaming - enzyme backend Tracking issue: #124509 Part of #129175 This PR should allow building Enzyme from source on Tier 1 targets (when also building LLVM), except MSVC. It's only a small fraction (~200 lines) of the whole upstream PR, but due to bootstrapping and the number of configurations in which rustc can be build I assume that this will be the hardest to merge, so I'm starting with it. Happy to hear what changes are required to be able to upstream this code. **Content:** It contains a new configure flag `--enable-llvm-enzyme`, and will build the new Enzyme submodule when it is set. **Discussion:** Apparently Rust CI isn't able to clone repositories outside the rust-lang org? At least I'm seeing this error in CI: ``` git@github.com: Permission denied (publickey). fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists. ``` Does that mean we would need to mirror github.com/EnzymeAD/Enzyme in rust-lang, until LLVM upgrades Enzyme from an Incubator project to something that ships as part of the monorepo? Tracking: - #124509
2 parents a3af208 + 4f5c16d commit 59d4114

File tree

13 files changed

+160
-0
lines changed

13 files changed

+160
-0
lines changed

.gitmodules

+4
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,7 @@
4747
path = src/tools/rustc-perf
4848
url = https://github.com/rust-lang/rustc-perf.git
4949
shallow = true
50+
[submodule "src/tools/enzyme"]
51+
path = src/tools/enzyme
52+
url = https://github.com/EnzymeAD/Enzyme.git
53+
shallow = true

config.example.toml

+3
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@
7878
# Indicates whether the LLVM plugin is enabled or not
7979
#plugins = false
8080

81+
# Wheter to build Enzyme as AutoDiff backend.
82+
#enzyme = false
83+
8184
# Indicates whether ccache is used when building LLVM. Set to `true` to use the first `ccache` in
8285
# PATH, or set an absolute path to use a specific version.
8386
#ccache = false

src/bootstrap/configure.py

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ def v(*args):
7171
# channel, etc.
7272
o("optimize-llvm", "llvm.optimize", "build optimized LLVM")
7373
o("llvm-assertions", "llvm.assertions", "build LLVM with assertions")
74+
o("llvm-enzyme", "llvm.enzyme", "build LLVM with enzyme")
7475
o("llvm-plugins", "llvm.plugins", "build LLVM with plugin interface")
7576
o("debug-assertions", "rust.debug-assertions", "build with debugging assertions")
7677
o("debug-assertions-std", "rust.debug-assertions-std", "build the standard library with debugging assertions")

src/bootstrap/src/core/build_steps/compile.rs

+22
Original file line numberDiff line numberDiff line change
@@ -1195,6 +1195,10 @@ pub fn rustc_cargo_env(
11951195
cargo.env("RUSTC_VERIFY_LLVM_IR", "1");
11961196
}
11971197

1198+
if builder.config.llvm_enzyme {
1199+
cargo.rustflag("--cfg=llvm_enzyme");
1200+
}
1201+
11981202
// Note that this is disabled if LLVM itself is disabled or we're in a check
11991203
// build. If we are in a check build we still go ahead here presuming we've
12001204
// detected that LLVM is already built and good to go which helps prevent
@@ -1790,6 +1794,24 @@ impl Step for Assemble {
17901794
// use that to bootstrap this compiler forward.
17911795
let mut build_compiler = builder.compiler(target_compiler.stage - 1, builder.config.build);
17921796

1797+
// Build enzyme
1798+
let enzyme_install = if builder.config.llvm_enzyme {
1799+
Some(builder.ensure(llvm::Enzyme { target: build_compiler.host }))
1800+
} else {
1801+
None
1802+
};
1803+
1804+
if let Some(enzyme_install) = enzyme_install {
1805+
let lib_ext = std::env::consts::DLL_EXTENSION;
1806+
let src_lib = enzyme_install.join("build/Enzyme/libEnzyme-19").with_extension(lib_ext);
1807+
let libdir = builder.sysroot_libdir(build_compiler, build_compiler.host);
1808+
let target_libdir = builder.sysroot_libdir(target_compiler, target_compiler.host);
1809+
let dst_lib = libdir.join("libEnzyme-19").with_extension(lib_ext);
1810+
let target_dst_lib = target_libdir.join("libEnzyme-19").with_extension(lib_ext);
1811+
builder.copy_link(&src_lib, &dst_lib);
1812+
builder.copy_link(&src_lib, &target_dst_lib);
1813+
}
1814+
17931815
// Build the libraries for this compiler to link to (i.e., the libraries
17941816
// it uses at runtime). NOTE: Crates the target compiler compiles don't
17951817
// link to these. (FIXME: Is that correct? It seems to be correct most

src/bootstrap/src/core/build_steps/llvm.rs

+95
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,7 @@ impl Step for Llvm {
529529
}
530530
};
531531

532+
// FIXME(ZuseZ4): Do we need that for Enzyme too?
532533
// When building LLVM with LLVM_LINK_LLVM_DYLIB for macOS, an unversioned
533534
// libLLVM.dylib will be built. However, llvm-config will still look
534535
// for a versioned path like libLLVM-14.dylib. Manually create a symbolic
@@ -849,6 +850,100 @@ fn get_var(var_base: &str, host: &str, target: &str) -> Option<OsString> {
849850
.or_else(|| env::var_os(var_base))
850851
}
851852

853+
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
854+
pub struct Enzyme {
855+
pub target: TargetSelection,
856+
}
857+
858+
impl Step for Enzyme {
859+
type Output = PathBuf;
860+
const ONLY_HOSTS: bool = true;
861+
862+
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
863+
run.path("src/tools/enzyme/enzyme")
864+
}
865+
866+
fn make_run(run: RunConfig<'_>) {
867+
run.builder.ensure(Enzyme { target: run.target });
868+
}
869+
870+
/// Compile Enzyme for `target`.
871+
fn run(self, builder: &Builder<'_>) -> PathBuf {
872+
builder.require_submodule(
873+
"src/tools/enzyme",
874+
Some("The Enzyme sources are required for autodiff."),
875+
);
876+
if builder.config.dry_run() {
877+
let out_dir = builder.enzyme_out(self.target);
878+
return out_dir;
879+
}
880+
let target = self.target;
881+
882+
let LlvmResult { llvm_config, .. } = builder.ensure(Llvm { target: self.target });
883+
884+
static STAMP_HASH_MEMO: OnceLock<String> = OnceLock::new();
885+
let smart_stamp_hash = STAMP_HASH_MEMO.get_or_init(|| {
886+
generate_smart_stamp_hash(
887+
builder,
888+
&builder.config.src.join("src/tools/enzyme"),
889+
builder.enzyme_info.sha().unwrap_or_default(),
890+
)
891+
});
892+
893+
let out_dir = builder.enzyme_out(target);
894+
let stamp = out_dir.join("enzyme-finished-building");
895+
let stamp = HashStamp::new(stamp, Some(smart_stamp_hash));
896+
897+
if stamp.is_done() {
898+
if stamp.hash.is_none() {
899+
builder.info(
900+
"Could not determine the Enzyme submodule commit hash. \
901+
Assuming that an Enzyme rebuild is not necessary.",
902+
);
903+
builder.info(&format!(
904+
"To force Enzyme to rebuild, remove the file `{}`",
905+
stamp.path.display()
906+
));
907+
}
908+
return out_dir;
909+
}
910+
911+
builder.info(&format!("Building Enzyme for {}", target));
912+
t!(stamp.remove());
913+
let _time = helpers::timeit(builder);
914+
t!(fs::create_dir_all(&out_dir));
915+
916+
builder
917+
.config
918+
.update_submodule(Path::new("src").join("tools").join("enzyme").to_str().unwrap());
919+
let mut cfg = cmake::Config::new(builder.src.join("src/tools/enzyme/enzyme/"));
920+
// FIXME(ZuseZ4): Find a nicer way to use Enzyme Debug builds
921+
//cfg.profile("Debug");
922+
//cfg.define("CMAKE_BUILD_TYPE", "Debug");
923+
configure_cmake(builder, target, &mut cfg, true, LdFlags::default(), &[]);
924+
925+
// Re-use the same flags as llvm to control the level of debug information
926+
// generated for lld.
927+
let profile = match (builder.config.llvm_optimize, builder.config.llvm_release_debuginfo) {
928+
(false, _) => "Debug",
929+
(true, false) => "Release",
930+
(true, true) => "RelWithDebInfo",
931+
};
932+
933+
cfg.out_dir(&out_dir)
934+
.profile(profile)
935+
.env("LLVM_CONFIG_REAL", &llvm_config)
936+
.define("LLVM_ENABLE_ASSERTIONS", "ON")
937+
.define("ENZYME_EXTERNAL_SHARED_LIB", "ON")
938+
.define("LLVM_DIR", builder.llvm_out(target));
939+
940+
cfg.build();
941+
942+
t!(stamp.write());
943+
out_dir
944+
}
945+
}
946+
852947
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
853948
pub struct Lld {
854949
pub target: TargetSelection,

src/bootstrap/src/core/builder.rs

+7
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,7 @@ impl<'a> Builder<'a> {
798798
tool::Miri,
799799
tool::CargoMiri,
800800
llvm::Lld,
801+
llvm::Enzyme,
801802
llvm::CrtBeginEnd,
802803
tool::RustdocGUITest,
803804
tool::OptimizedDist,
@@ -1588,6 +1589,12 @@ impl<'a> Builder<'a> {
15881589
rustflags.arg(sysroot_str);
15891590
}
15901591

1592+
// https://rust-lang.zulipchat.com/#narrow/stream/182449-t-compiler.2Fhelp/topic/.E2.9C.94.20link.20new.20library.20into.20stage1.2Frustc
1593+
if self.config.llvm_enzyme {
1594+
rustflags.arg("-l");
1595+
rustflags.arg("Enzyme-19");
1596+
}
1597+
15911598
let use_new_symbol_mangling = match self.config.rust_new_symbol_mangling {
15921599
Some(setting) => {
15931600
// If an explicit setting is given, use that

src/bootstrap/src/core/config/config.rs

+8
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ pub struct Config {
221221
// llvm codegen options
222222
pub llvm_assertions: bool,
223223
pub llvm_tests: bool,
224+
pub llvm_enzyme: bool,
224225
pub llvm_plugins: bool,
225226
pub llvm_optimize: bool,
226227
pub llvm_thin_lto: bool,
@@ -898,6 +899,7 @@ define_config! {
898899
release_debuginfo: Option<bool> = "release-debuginfo",
899900
assertions: Option<bool> = "assertions",
900901
tests: Option<bool> = "tests",
902+
enzyme: Option<bool> = "enzyme",
901903
plugins: Option<bool> = "plugins",
902904
ccache: Option<StringOrBool> = "ccache",
903905
static_libstdcpp: Option<bool> = "static-libstdcpp",
@@ -1603,6 +1605,7 @@ impl Config {
16031605
// we'll infer default values for them later
16041606
let mut llvm_assertions = None;
16051607
let mut llvm_tests = None;
1608+
let mut llvm_enzyme = None;
16061609
let mut llvm_plugins = None;
16071610
let mut debug = None;
16081611
let mut debug_assertions = None;
@@ -1722,6 +1725,8 @@ impl Config {
17221725
config.llvm_tools_enabled = llvm_tools.unwrap_or(true);
17231726
config.rustc_parallel =
17241727
parallel_compiler.unwrap_or(config.channel == "dev" || config.channel == "nightly");
1728+
config.llvm_enzyme =
1729+
llvm_enzyme.unwrap_or(config.channel == "dev" || config.channel == "nightly");
17251730
config.rustc_default_linker = default_linker;
17261731
config.musl_root = musl_root.map(PathBuf::from);
17271732
config.save_toolstates = save_toolstates.map(PathBuf::from);
@@ -1806,6 +1811,7 @@ impl Config {
18061811
release_debuginfo,
18071812
assertions,
18081813
tests,
1814+
enzyme,
18091815
plugins,
18101816
ccache,
18111817
static_libstdcpp,
@@ -1839,6 +1845,7 @@ impl Config {
18391845
set(&mut config.ninja_in_file, ninja);
18401846
llvm_assertions = assertions;
18411847
llvm_tests = tests;
1848+
llvm_enzyme = enzyme;
18421849
llvm_plugins = plugins;
18431850
set(&mut config.llvm_optimize, optimize_toml);
18441851
set(&mut config.llvm_thin_lto, thin_lto);
@@ -2055,6 +2062,7 @@ impl Config {
20552062

20562063
config.llvm_assertions = llvm_assertions.unwrap_or(false);
20572064
config.llvm_tests = llvm_tests.unwrap_or(false);
2065+
config.llvm_enzyme = llvm_enzyme.unwrap_or(false);
20582066
config.llvm_plugins = llvm_plugins.unwrap_or(false);
20592067
config.rust_optimize = optimize.unwrap_or(RustOptimize::Bool(true));
20602068

src/bootstrap/src/lib.rs

+10
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ const LLD_FILE_NAMES: &[&str] = &["ld.lld", "ld64.lld", "lld-link", "wasm-ld"];
7777
#[allow(clippy::type_complexity)] // It's fine for hard-coded list and type is explained above.
7878
const EXTRA_CHECK_CFGS: &[(Option<Mode>, &str, Option<&[&'static str]>)] = &[
7979
(None, "bootstrap", None),
80+
(Some(Mode::Rustc), "llvm_enzyme", None),
81+
(Some(Mode::Codegen), "llvm_enzyme", None),
82+
(Some(Mode::ToolRustc), "llvm_enzyme", None),
8083
(Some(Mode::Rustc), "parallel_compiler", None),
8184
(Some(Mode::ToolRustc), "parallel_compiler", None),
8285
(Some(Mode::ToolRustc), "rust_analyzer", None),
@@ -140,6 +143,7 @@ pub struct Build {
140143
clippy_info: GitInfo,
141144
miri_info: GitInfo,
142145
rustfmt_info: GitInfo,
146+
enzyme_info: GitInfo,
143147
in_tree_llvm_info: GitInfo,
144148
local_rebuild: bool,
145149
fail_fast: bool,
@@ -307,6 +311,7 @@ impl Build {
307311
let clippy_info = GitInfo::new(omit_git_hash, &src.join("src/tools/clippy"));
308312
let miri_info = GitInfo::new(omit_git_hash, &src.join("src/tools/miri"));
309313
let rustfmt_info = GitInfo::new(omit_git_hash, &src.join("src/tools/rustfmt"));
314+
let enzyme_info = GitInfo::new(omit_git_hash, &src.join("src/tools/enzyme"));
310315

311316
// we always try to use git for LLVM builds
312317
let in_tree_llvm_info = GitInfo::new(false, &src.join("src/llvm-project"));
@@ -400,6 +405,7 @@ impl Build {
400405
clippy_info,
401406
miri_info,
402407
rustfmt_info,
408+
enzyme_info,
403409
in_tree_llvm_info,
404410
cc: RefCell::new(HashMap::new()),
405411
cxx: RefCell::new(HashMap::new()),
@@ -755,6 +761,10 @@ impl Build {
755761
}
756762
}
757763

764+
fn enzyme_out(&self, target: TargetSelection) -> PathBuf {
765+
self.out.join(&*target.triple).join("enzyme")
766+
}
767+
758768
fn lld_out(&self, target: TargetSelection) -> PathBuf {
759769
self.out.join(target).join("lld")
760770
}

src/bootstrap/src/utils/change_tracker.rs

+5
Original file line numberDiff line numberDiff line change
@@ -245,4 +245,9 @@ pub const CONFIG_CHANGE_HISTORY: &[ChangeInfo] = &[
245245
severity: ChangeSeverity::Warning,
246246
summary: "Removed `rust.split-debuginfo` as it was deprecated long time ago.",
247247
},
248+
ChangeInfo {
249+
change_id: 129176,
250+
severity: ChangeSeverity::Info,
251+
summary: "New option `llvm.enzyme` to control whether the llvm based autodiff tool (Enzyme) is built.",
252+
},
248253
];

src/tools/enzyme

Submodule enzyme added at 2fe5164

src/tools/tidy/config/black.toml

+1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ extend-exclude = """(\
1212
src/llvm-project/|\
1313
src/doc/embedded-book/|\
1414
src/tools/rustc-perf/|\
15+
src/tools/enzyme/|\
1516
library/backtrace/
1617
)"""

src/tools/tidy/config/ruff.toml

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ extend-exclude = [
1616
"src/llvm-project/",
1717
"src/doc/embedded-book/",
1818
"library/backtrace/",
19+
"src/tools/enzyme/",
1920
"src/tools/rustc-perf/",
2021
# Hack: CI runs from a subdirectory under the main checkout
2122
"../src/doc/nomicon/",
@@ -29,6 +30,7 @@ extend-exclude = [
2930
"../src/llvm-project/",
3031
"../src/doc/embedded-book/",
3132
"../library/backtrace/",
33+
"../src/tools/enzyme/",
3234
"../src/tools/rustc-perf/",
3335
]
3436

src/tools/tidy/src/walk.rs

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pub fn filter_dirs(path: &Path) -> bool {
2424
"src/tools/rust-analyzer",
2525
"src/tools/rustc-perf",
2626
"src/tools/rustfmt",
27+
"src/tools/enzyme",
2728
"src/doc/book",
2829
"src/doc/edition-guide",
2930
"src/doc/embedded-book",

0 commit comments

Comments
 (0)