Skip to content

Commit 51e0b65

Browse files
committed
Build and link Enzyme durin bootstrap
1 parent e4e3430 commit 51e0b65

File tree

4 files changed

+95
-0
lines changed

4 files changed

+95
-0
lines changed

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1576,6 +1576,7 @@ pub struct Assemble {
15761576
pub target_compiler: Compiler,
15771577
}
15781578

1579+
#[allow(unreachable_code)]
15791580
impl Step for Assemble {
15801581
type Output = Compiler;
15811582
const ONLY_HOSTS: bool = true;
@@ -1636,6 +1637,26 @@ impl Step for Assemble {
16361637
return target_compiler;
16371638
}
16381639

1640+
// Build enzyme
1641+
let enzyme_install =
1642+
Some(builder.ensure(llvm::Enzyme { target: build_compiler.host }));
1643+
//let enzyme_install = if builder.config.llvm_enzyme {
1644+
// Some(builder.ensure(llvm::Enzyme { target: build_compiler.host }))
1645+
//} else {
1646+
// None
1647+
//};
1648+
1649+
if let Some(enzyme_install) = enzyme_install {
1650+
let src_lib = enzyme_install.join("build/Enzyme/LLVMEnzyme-17.so");
1651+
1652+
let libdir = builder.sysroot_libdir(build_compiler, build_compiler.host);
1653+
let target_libdir = builder.sysroot_libdir(target_compiler, target_compiler.host);
1654+
let dst_lib = libdir.join("libLLVMEnzyme-17.so");
1655+
let target_dst_lib = target_libdir.join("libLLVMEnzyme-17.so");
1656+
builder.copy(&src_lib, &dst_lib);
1657+
builder.copy(&src_lib, &target_dst_lib);
1658+
}
1659+
16391660
// Build the libraries for this compiler to link to (i.e., the libraries
16401661
// it uses at runtime). NOTE: Crates the target compiler compiles don't
16411662
// link to these. (FIXME: Is that correct? It seems to be correct most

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

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,72 @@ fn get_var(var_base: &str, host: &str, target: &str) -> Option<OsString> {
817817
.or_else(|| env::var_os(var_base))
818818
}
819819

820+
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
821+
pub struct Enzyme {
822+
pub target: TargetSelection,
823+
}
824+
825+
impl Step for Enzyme {
826+
type Output = PathBuf;
827+
const ONLY_HOSTS: bool = true;
828+
829+
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
830+
run.path("src/tools/enzyme/enzyme")
831+
}
832+
833+
fn make_run(run: RunConfig<'_>) {
834+
run.builder.ensure(Enzyme { target: run.target });
835+
}
836+
837+
/// Compile Enzyme for `target`.
838+
fn run(self, builder: &Builder<'_>) -> PathBuf {
839+
if builder.config.dry_run() {
840+
let out_dir = builder.enzyme_out(self.target);
841+
return out_dir;
842+
}
843+
let target = self.target;
844+
845+
let LlvmResult { llvm_config, .. } = builder.ensure(Llvm { target: self.target });
846+
847+
let out_dir = builder.enzyme_out(target);
848+
let done_stamp = out_dir.join("enzyme-finished-building");
849+
if done_stamp.exists() {
850+
return out_dir;
851+
}
852+
853+
builder.info(&format!("Building Enzyme for {}", target));
854+
let _time = helpers::timeit(&builder);
855+
t!(fs::create_dir_all(&out_dir));
856+
857+
builder.update_submodule(&Path::new("src").join("tools").join("enzyme"));
858+
let mut cfg = cmake::Config::new(builder.src.join("src/tools/enzyme/enzyme/"));
859+
// TODO: Find a nicer way to use Enzyme Debug builds
860+
//cfg.profile("Debug");
861+
//cfg.define("CMAKE_BUILD_TYPE", "Debug");
862+
configure_cmake(builder, target, &mut cfg, true, LdFlags::default(), &[]);
863+
864+
// Re-use the same flags as llvm to control the level of debug information
865+
// generated for lld.
866+
let profile = match (builder.config.llvm_optimize, builder.config.llvm_release_debuginfo) {
867+
(false, _) => "Debug",
868+
(true, false) => "Release",
869+
(true, true) => "RelWithDebInfo",
870+
};
871+
872+
cfg.out_dir(&out_dir)
873+
.profile(profile)
874+
.env("LLVM_CONFIG_REAL", &llvm_config)
875+
.define("LLVM_ENABLE_ASSERTIONS", "ON")
876+
.define("ENZYME_EXTERNAL_SHARED_LIB", "OFF")
877+
.define("LLVM_DIR", builder.llvm_out(target));
878+
879+
cfg.build();
880+
881+
t!(File::create(&done_stamp));
882+
out_dir
883+
}
884+
}
885+
820886
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
821887
pub struct Lld {
822888
pub target: TargetSelection,

src/bootstrap/src/core/builder.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1416,6 +1416,10 @@ impl<'a> Builder<'a> {
14161416
rustflags.arg(sysroot_str);
14171417
}
14181418

1419+
// https://rust-lang.zulipchat.com/#narrow/stream/182449-t-compiler.2Fhelp/topic/.E2.9C.94.20link.20new.20library.20into.20stage1.2Frustc
1420+
rustflags.arg("-l");
1421+
rustflags.arg("LLVMEnzyme-17");
1422+
14191423
let use_new_symbol_mangling = match self.config.rust_new_symbol_mangling {
14201424
Some(setting) => {
14211425
// If an explicit setting is given, use that

src/bootstrap/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,10 @@ impl Build {
799799
self.out.join(&*target.triple).join("lld")
800800
}
801801

802+
fn enzyme_out(&self, target: TargetSelection) -> PathBuf {
803+
self.out.join(&*target.triple).join("enzyme")
804+
}
805+
802806
/// Output directory for all documentation for a target
803807
fn doc_out(&self, target: TargetSelection) -> PathBuf {
804808
self.out.join(&*target.triple).join("doc")

0 commit comments

Comments
 (0)