Skip to content

Commit 9b4ad11

Browse files
committed
working standalone openmp build
1 parent 27afca6 commit 9b4ad11

File tree

2 files changed

+131
-3
lines changed

2 files changed

+131
-3
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,6 +1437,11 @@ fn rustc_llvm_env(builder: &Builder<'_>, cargo: &mut Cargo, target: TargetSelect
14371437
cargo.env("LLVM_ENZYME", "1");
14381438
}
14391439
let llvm::LlvmResult { host_llvm_config, .. } = builder.ensure(llvm::Llvm { target });
1440+
if builder.config.llvm_enzyme {
1441+
builder.ensure(llvm::Offload { target });
1442+
cargo.env("LLVM_OFFLOAD", "1");
1443+
}
1444+
14401445
cargo.env("LLVM_CONFIG", &host_llvm_config);
14411446

14421447
// Some LLVM linker flags (-L and -l) may be needed to link `rustc_llvm`. Its build script

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

Lines changed: 126 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ impl Step for Llvm {
457457
// We will optimize it when we get closer to releasing it on nightly.
458458
if builder.config.llvm_offload {
459459
//enabled_llvm_projects.push("lld");
460-
enabled_llvm_runtimes.push("offload");
460+
//enabled_llvm_runtimes.push("offload");
461461
//FIXME(ZuseZ4): LLVM intends to drop the offload dependency on openmp.
462462
//Remove this line once they achieved it.
463463
//enabled_llvm_runtimes.push("openmp");
@@ -470,8 +470,8 @@ impl Step for Llvm {
470470
//let runtime_targets = vec!["default", "amdgcn-amd-amdhsa", "nvptx64-nvidia-cuda"];
471471
//cfg.define("LLVM_RUNTIME_TARGETS", runtime_targets.join(";"));
472472

473-
cfg.define("LLVM_INCLUDE_TESTS", "OFF");
474-
cfg.define("LLVM_BUILD_TESTS", "OFF");
473+
//cfg.define("LLVM_INCLUDE_TESTS", "OFF");
474+
//cfg.define("LLVM_BUILD_TESTS", "OFF");
475475
//cfg.define("RUNTIMES_nvptx64-nvidia-cuda_LLVM_ENABLE_RUNTIMES", "openmp");
476476
//cfg.define("RUNTIMES_amdgcn-amd-amdhsa_LLVM_ENABLE_RUNTIMES", "openmp");
477477
}
@@ -908,6 +908,129 @@ fn get_var(var_base: &str, host: &str, target: &str) -> Option<OsString> {
908908
.or_else(|| env::var_os(var_base))
909909
}
910910

911+
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
912+
pub struct Offload {
913+
pub target: TargetSelection,
914+
}
915+
916+
impl Step for Offload {
917+
type Output = PathBuf;
918+
const IS_HOST: bool = true;
919+
920+
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
921+
run.path("src/llvm-project/openmp")
922+
}
923+
924+
fn make_run(run: RunConfig<'_>) {
925+
run.builder.ensure(Offload { target: run.target });
926+
}
927+
928+
/// Compile OpenMP offload runtimes for `target`.
929+
fn run(self, builder: &Builder<'_>) -> PathBuf {
930+
if builder.config.dry_run() {
931+
return PathBuf::from("/");
932+
}
933+
let target = self.target;
934+
935+
let LlvmResult { host_llvm_config, .. } = builder.ensure(Llvm { target: self.target });
936+
937+
let out_dir = builder.llvm_out(target);
938+
static STAMP_HASH_MEMO: OnceLock<String> = OnceLock::new();
939+
//let smart_stamp_hash = STAMP_HASH_MEMO.get_or_init(|| {
940+
// generate_smart_stamp_hash(
941+
// builder,
942+
// &builder.config.src.join("src/llvm-project/openmp"),
943+
// builder.offload_info.sha().unwrap_or_default(),
944+
// )
945+
//});
946+
//let stamp = BuildStamp::new(&out_dir).with_prefix("enzyme").add_stamp(smart_stamp_hash);
947+
948+
trace!("checking build stamp to see if we need to rebuild offload/openmp artifacts");
949+
//if stamp.is_up_to_date() {
950+
// trace!(?out_dir, "offload/openmp build artifacts are up to date");
951+
// if stamp.stamp().is_empty() {
952+
// builder.info(
953+
// "Could not determine the Offload submodule commit hash. \
954+
// Assuming that an Offload rebuild is not necessary.",
955+
// );
956+
// builder.info(&format!(
957+
// "To force Offload to rebuild, remove the file `{}`",
958+
// stamp.path().display()
959+
// ));
960+
// }
961+
// return out_dir;
962+
//}
963+
964+
trace!(?target, "(re)building openmp artifacts");
965+
builder.info(&format!("Building OpenMP for {target}"));
966+
//t!(stamp.remove());
967+
let _time = helpers::timeit(builder);
968+
t!(fs::create_dir_all(&out_dir));
969+
970+
builder.config.update_submodule(Path::new("src").join("llvm-project").to_str().unwrap());
971+
let mut cfg = cmake::Config::new(builder.src.join("src/llvm-project/runtimes/"));
972+
configure_cmake(builder, target, &mut cfg, true, LdFlags::default(), &[]);
973+
974+
// Re-use the same flags as llvm to control the level of debug information
975+
// generated by Enzyme.
976+
// FIXME(ZuseZ4): Find a nicer way to use Enzyme Debug builds.
977+
let profile = match (builder.config.llvm_optimize, builder.config.llvm_release_debuginfo) {
978+
(false, _) => "Debug",
979+
(true, false) => "Release",
980+
(true, true) => "RelWithDebInfo",
981+
};
982+
trace!(?profile);
983+
984+
cfg.out_dir(&out_dir)
985+
.profile(profile)
986+
.env("LLVM_CONFIG_REAL", &host_llvm_config)
987+
.define("LLVM_ENABLE_ASSERTIONS", "ON")
988+
.define("TARGET_TRIPLE", &target.triple)
989+
.define("LLVM_ENABLE_RUNTIMES", "openmp")
990+
.define("CMAKE_C_COMPILER", builder.cc(target))
991+
.define("CMAKE_CXX_COMPILER", builder.cxx(target).unwrap())
992+
.define("LLVM_DEFAULT_TARGET_TRIPLE", &target.triple)
993+
.define("OPENMP_STANDALONE_BUILD", "ON")
994+
.define(
995+
"LLVM_ROOT",
996+
"/tmp/drehwald1/prog/rust/build/x86_64-unknown-linux-gnu/llvm/build/",
997+
)
998+
.define("LLVM_DIR", builder.llvm_out(target).join("lib").join("cmake").join("llvm"));
999+
///tmp/drehwald1/prog/rust/build/x86_64-unknown-linux-gnu/llvm/lib/cmake/llvm
1000+
//$> cmake ../runtimes \ # Point to the runtimes build
1001+
// -G Ninja \
1002+
// -DLLVM_ENABLE_RUNTIMES=openmp \
1003+
// -DCMAKE_C_COMPILER=$TARGET_C_COMPILER \
1004+
// -DCMAKE_CXX_COMPILER=$TARGET_CXX_COMPILER \
1005+
// -DLLVM_DEFAULT_TARGET_TRIPLE=$TARGET_TRIPLE \
1006+
// -DCMAKE_BUILD_TYPE=Release
1007+
cfg.build();
1008+
1009+
//t!(stamp.write());
1010+
out_dir
1011+
1012+
//$> cd llvm-project # The llvm-project checkout
1013+
//$> mkdir build # A different build directory for the build tools
1014+
//$> cd build
1015+
//$> TARGET_TRIPLE=<amdgcn-amd-amdhsa or nvptx64-nvidia-cuda>
1016+
//$> TARGET_C_COMPILER=</path/to/clang>
1017+
//$> TARGET_CXX_COMPILER=</path/to/clang++>
1018+
//$> ninja install
1019+
//
1020+
//You can do the same thing for the offload project.
1021+
//
1022+
//$> TARGET_C_COMPILER=</path/to/clang>
1023+
//$> TARGET_CXX_COMPILER=</path/to/clang++>
1024+
//$> cmake ../runtimes \ # Point to the runtimes build
1025+
// -G Ninja \
1026+
// -DLLVM_ENABLE_RUNTIMES=openmp \
1027+
// -DCMAKE_C_COMPILER=$TARGET_C_COMPILER \
1028+
// -DCMAKE_CXX_COMPILER=$TARGET_CXX_COMPILER \
1029+
// -DCMAKE_BUILD_TYPE=Release
1030+
//$> ninja install
1031+
}
1032+
}
1033+
9111034
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
9121035
pub struct Enzyme {
9131036
pub target: TargetSelection,

0 commit comments

Comments
 (0)