Skip to content

Commit 93bc6ea

Browse files
committed
Change how we build offload as a single Step
1 parent f392ed5 commit 93bc6ea

File tree

7 files changed

+206
-11
lines changed

7 files changed

+206
-11
lines changed

bootstrap.example.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@
106106
# Whether to build LLVM with support for it's gpu offload runtime.
107107
#llvm.offload = false
108108

109+
# absolute path to ClangConfig.cmake
110+
#llvm.offload-clang-dir = ""
111+
109112
# When true, link libstdc++ statically into the rustc_llvm.
110113
# This is useful if you don't want to use the dynamic version of that
111114
# library provided by LLVM.

src/bootstrap/configure.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ def v(*args):
120120
o("llvm-assertions", "llvm.assertions", "build LLVM with assertions")
121121
o("llvm-enzyme", "llvm.enzyme", "build LLVM with enzyme")
122122
o("llvm-offload", "llvm.offload", "build LLVM with gpu offload support")
123+
o(
124+
"llvm-offload-clang-dir",
125+
"llvm.offload-clang-dir",
126+
"pass the absolute directory of ClangConfig.cmake",
127+
)
123128
o("llvm-plugins", "llvm.plugins", "build LLVM with plugin interface")
124129
o("debug-assertions", "rust.debug-assertions", "build with debugging assertions")
125130
o(

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

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1440,10 +1440,12 @@ fn rustc_llvm_env(builder: &Builder<'_>, cargo: &mut Cargo, target: TargetSelect
14401440
if builder.config.llvm_enzyme {
14411441
cargo.env("LLVM_ENZYME", "1");
14421442
}
1443+
let llvm::LlvmResult { host_llvm_config, .. } = builder.ensure(llvm::Llvm { target });
14431444
if builder.config.llvm_offload {
1445+
builder.ensure(llvm::OmpOffload { target });
14441446
cargo.env("LLVM_OFFLOAD", "1");
14451447
}
1446-
let llvm::LlvmResult { host_llvm_config, .. } = builder.ensure(llvm::Llvm { target });
1448+
14471449
cargo.env("LLVM_CONFIG", &host_llvm_config);
14481450

14491451
// Some LLVM linker flags (-L and -l) may be needed to link `rustc_llvm`. Its build script
@@ -2214,6 +2216,45 @@ impl Step for Assemble {
22142216
}
22152217
}
22162218

2219+
if builder.config.llvm_offload && !builder.config.dry_run() {
2220+
debug!("`llvm_offload` requested");
2221+
let offload_install = builder.ensure(llvm::OmpOffload { target: build_compiler.host });
2222+
if let Some(_llvm_config) = builder.llvm_config(builder.config.host_target) {
2223+
let src_dir = offload_install.join("lib");
2224+
let libdir = builder.sysroot_target_libdir(build_compiler, build_compiler.host);
2225+
let target_libdir =
2226+
builder.sysroot_target_libdir(target_compiler, target_compiler.host);
2227+
let lib_ext = std::env::consts::DLL_EXTENSION;
2228+
2229+
let libenzyme = "libLLVMOffload";
2230+
let src_lib = src_dir.join(&libenzyme).with_extension(lib_ext);
2231+
let dst_lib = libdir.join(&libenzyme).with_extension(lib_ext);
2232+
let target_dst_lib = target_libdir.join(&libenzyme).with_extension(lib_ext);
2233+
builder.resolve_symlink_and_copy(&src_lib, &dst_lib);
2234+
builder.resolve_symlink_and_copy(&src_lib, &target_dst_lib);
2235+
2236+
// FIXME(offload): With LLVM-22, we should be able to drop everything below here.
2237+
let omp = "libomp";
2238+
let src_omp = src_dir.join(&omp).with_extension(lib_ext);
2239+
let dst_omp_lib = libdir.join(&omp).with_extension(lib_ext);
2240+
let target_omp_dst_lib = target_libdir.join(&omp).with_extension(lib_ext);
2241+
builder.resolve_symlink_and_copy(&src_omp, &dst_omp_lib);
2242+
builder.resolve_symlink_and_copy(&src_omp, &target_omp_dst_lib);
2243+
2244+
let tgt = "libomptarget";
2245+
let src_tgt = src_dir.join(&tgt).with_extension(lib_ext);
2246+
let dst_tgt_lib = libdir.join(&tgt).with_extension(lib_ext);
2247+
let target_tgt_dst_lib = target_libdir.join(&tgt).with_extension(lib_ext);
2248+
builder.resolve_symlink_and_copy(&src_tgt, &dst_tgt_lib);
2249+
builder.resolve_symlink_and_copy(&src_tgt, &target_tgt_dst_lib);
2250+
2251+
// FIXME(offload): Add amdgcn-amd-amdhsa and nvptx64-nvidia-cuda folder
2252+
// This one is slightly more tricky, since we have the same file twice, in two
2253+
// subfolders for amdgcn and nvptx64. We'll likely find two more in the future, once
2254+
// Intel and Spir-V support lands in offload.
2255+
}
2256+
}
2257+
22172258
// Build the libraries for this compiler to link to (i.e., the libraries
22182259
// it uses at runtime).
22192260
debug!(

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

Lines changed: 142 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -455,16 +455,6 @@ impl Step for Llvm {
455455
enabled_llvm_runtimes.push("compiler-rt");
456456
}
457457

458-
// This is an experimental flag, which likely builds more than necessary.
459-
// We will optimize it when we get closer to releasing it on nightly.
460-
if builder.config.llvm_offload {
461-
enabled_llvm_runtimes.push("offload");
462-
//FIXME(ZuseZ4): LLVM intends to drop the offload dependency on openmp.
463-
//Remove this line once they achieved it.
464-
enabled_llvm_runtimes.push("openmp");
465-
enabled_llvm_projects.push("compiler-rt");
466-
}
467-
468458
if !enabled_llvm_projects.is_empty() {
469459
enabled_llvm_projects.sort();
470460
enabled_llvm_projects.dedup();
@@ -779,6 +769,17 @@ fn configure_cmake(
779769
.collect::<Vec<String>>()
780770
.join(" ")
781771
.into();
772+
773+
// If we use an external clang as opposed to building our own llvm_clang, than that clang will
774+
// come with it's own set of default include directories, which are based on a potentially older
775+
// LLVM. This can cause issues, so we overwrite it to include headers based on our
776+
// `src/llvm-project` submodule instead.
777+
// FIXME(offload): With LLVM-22 we hopefully won't need an external clang anymore.
778+
let base = builder.llvm_out(target).join("include");
779+
let inc_dir = base.display();
780+
if builder.config.llvm_offload && !builder.config.llvm_clang {
781+
cflags.push(format!(" -I {inc_dir}"));
782+
}
782783
if let Some(ref s) = builder.config.llvm_cflags {
783784
cflags.push(" ");
784785
cflags.push(s);
@@ -790,6 +791,7 @@ fn configure_cmake(
790791
cflags.push(format!(" --target={target}"));
791792
}
792793
cfg.define("CMAKE_C_FLAGS", cflags);
794+
793795
let mut cxxflags: OsString = builder
794796
.cc_handled_clags(target, CLang::Cxx)
795797
.into_iter()
@@ -802,6 +804,9 @@ fn configure_cmake(
802804
.collect::<Vec<String>>()
803805
.join(" ")
804806
.into();
807+
if builder.config.llvm_offload && !builder.config.llvm_clang {
808+
cxxflags.push(format!(" -I {inc_dir}"));
809+
}
805810
if let Some(ref s) = builder.config.llvm_cxxflags {
806811
cxxflags.push(" ");
807812
cxxflags.push(s);
@@ -897,6 +902,133 @@ fn get_var(var_base: &str, host: &str, target: &str) -> Option<OsString> {
897902
.or_else(|| env::var_os(var_base))
898903
}
899904

905+
// FIXME(offload): In an ideal world, we would just enable the offload runtime in our previous LLVM
906+
// build step. For now, we still depend on the openmp runtime since we use some of it's API, so we
907+
// build both. However, when building those runtimes as part of the LLVM step, then LLVM's cmake
908+
// implicitly assumes that Clang has also been build and will try to use it. In the Rust CI, we
909+
// don't always build clang (due to compile times), but instead use a slightly older external clang.
910+
// LLVM tries to remove this build dependency of offload/openmp on Clang for LLVM-22, so in the
911+
// future we might be able to integrate this step into the LLVM step. For now, we instead introduce
912+
// a Clang_DIR bootstrap option, which allows us tell CMake to use an external clang for these two
913+
// runtimes. This external clang will try to use it's own (older) include dirs when building our
914+
// in-tree LLVM submodule, which will cause build failures. To prevent those, we now also
915+
// explicitly set our include dirs.
916+
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
917+
pub struct OmpOffload {
918+
pub target: TargetSelection,
919+
}
920+
921+
impl Step for OmpOffload {
922+
type Output = PathBuf;
923+
const IS_HOST: bool = true;
924+
925+
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
926+
run.path("src/llvm-project/offload")
927+
}
928+
929+
fn make_run(run: RunConfig<'_>) {
930+
run.builder.ensure(OmpOffload { target: run.target });
931+
}
932+
933+
/// Compile OpenMP offload runtimes for `target`.
934+
#[allow(unused)]
935+
fn run(self, builder: &Builder<'_>) -> PathBuf {
936+
if builder.config.dry_run() {
937+
return builder.config.tempdir().join("llvm-offload-dry-run");
938+
}
939+
let target = self.target;
940+
941+
let LlvmResult { host_llvm_config, .. } = builder.ensure(Llvm { target: self.target });
942+
943+
// Running cmake twice in the same folder is known to cause issues, like deleting existing
944+
// binaries. We therefore write our offload artifacts into it's own subfolder. We use a
945+
// subfolder, so that all the logic that processes our build artifacts (hopefully) also
946+
// automatically manages our artifacts in the subfolder.
947+
let out_dir = builder.llvm_out(target).join("offload-outdir");
948+
if std::fs::exists(&out_dir).is_ok_and(|x| !x) {
949+
std::fs::DirBuilder::new().create(&out_dir).unwrap();
950+
}
951+
952+
// Offload/OpenMP are just subfolders of LLVM, so we can use the LLVM sha.
953+
static STAMP_HASH_MEMO: OnceLock<String> = OnceLock::new();
954+
let smart_stamp_hash = STAMP_HASH_MEMO.get_or_init(|| {
955+
generate_smart_stamp_hash(
956+
builder,
957+
&builder.config.src.join("src/llvm-project/offload"),
958+
builder.in_tree_llvm_info.sha().unwrap_or_default(),
959+
)
960+
});
961+
let stamp = BuildStamp::new(&out_dir).with_prefix("offload").add_stamp(smart_stamp_hash);
962+
963+
trace!("checking build stamp to see if we need to rebuild offload/openmp artifacts");
964+
if stamp.is_up_to_date() {
965+
trace!(?out_dir, "offload/openmp build artifacts are up to date");
966+
if stamp.stamp().is_empty() {
967+
builder.info(
968+
"Could not determine the Offload submodule commit hash. \
969+
Assuming that an Offload rebuild is not necessary.",
970+
);
971+
builder.info(&format!(
972+
"To force Offload/OpenMP to rebuild, remove the file `{}`",
973+
stamp.path().display()
974+
));
975+
}
976+
return out_dir;
977+
}
978+
979+
trace!(?target, "(re)building offload/openmp artifacts");
980+
builder.info(&format!("Building OpenMP/Offload for {target}"));
981+
t!(stamp.remove());
982+
let _time = helpers::timeit(builder);
983+
t!(fs::create_dir_all(&out_dir));
984+
985+
builder.config.update_submodule("src/llvm-project");
986+
let mut cfg = cmake::Config::new(builder.src.join("src/llvm-project/runtimes/"));
987+
configure_cmake(builder, target, &mut cfg, true, LdFlags::default(), &[]);
988+
989+
// Re-use the same flags as llvm to control the level of debug information
990+
// generated for offload.
991+
let profile = match (builder.config.llvm_optimize, builder.config.llvm_release_debuginfo) {
992+
(false, _) => "Debug",
993+
(true, false) => "Release",
994+
(true, true) => "RelWithDebInfo",
995+
};
996+
trace!(?profile);
997+
998+
// OpenMP/Offload builds currently (LLVM-21) still depend on Clang, although there are
999+
// intentions to loosen this requirement for LLVM-22. If we were to
1000+
let clang_dir = if !builder.config.llvm_clang {
1001+
// We must have an external clang to use.
1002+
assert!(&builder.build.config.llvm_clang_dir.is_some());
1003+
builder.build.config.llvm_clang_dir.clone()
1004+
} else {
1005+
// No need to specify it, since we use the in-tree clang
1006+
None
1007+
};
1008+
1009+
// FIXME(offload): Once we move from OMP to Offload (Ol) APIs, we should drop the openmp
1010+
// runtime to simplify our build. We should also re-evaluate the LLVM_Root and try to get
1011+
// rid of the Clang_DIR, once we upgrade to LLVM-22.
1012+
cfg.out_dir(&out_dir)
1013+
.profile(profile)
1014+
.env("LLVM_CONFIG_REAL", &host_llvm_config)
1015+
.define("LLVM_ENABLE_ASSERTIONS", "ON")
1016+
.define("LLVM_ENABLE_RUNTIMES", "openmp;offload")
1017+
.define("LLVM_INCLUDE_TESTS", "OFF")
1018+
.define("OFFLOAD_INCLUDE_TESTS", "OFF")
1019+
.define("OPENMP_STANDALONE_BUILD", "ON")
1020+
.define("LLVM_ROOT", builder.llvm_out(target).join("build"))
1021+
.define("LLVM_DIR", builder.llvm_out(target).join("lib").join("cmake").join("llvm"));
1022+
if let Some(p) = clang_dir {
1023+
cfg.define("Clang_DIR", p);
1024+
}
1025+
cfg.build();
1026+
1027+
t!(stamp.write());
1028+
out_dir
1029+
}
1030+
}
1031+
9001032
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
9011033
pub struct Enzyme {
9021034
pub target: TargetSelection,

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ pub struct Config {
169169
pub llvm_link_jobs: Option<u32>,
170170
pub llvm_version_suffix: Option<String>,
171171
pub llvm_use_linker: Option<String>,
172+
pub llvm_clang_dir: Option<PathBuf>,
172173
pub llvm_allow_old_toolchain: bool,
173174
pub llvm_polly: bool,
174175
pub llvm_clang: bool,
@@ -290,6 +291,7 @@ pub struct Config {
290291
pub miri_info: channel::GitInfo,
291292
pub rustfmt_info: channel::GitInfo,
292293
pub enzyme_info: channel::GitInfo,
294+
pub offload_info: channel::GitInfo,
293295
pub in_tree_llvm_info: channel::GitInfo,
294296
pub in_tree_gcc_info: channel::GitInfo,
295297

@@ -601,6 +603,7 @@ impl Config {
601603
ldflags: llvm_ldflags,
602604
use_libcxx: llvm_use_libcxx,
603605
use_linker: llvm_use_linker,
606+
clang_dir: llvm_clang_dir,
604607
allow_old_toolchain: llvm_allow_old_toolchain,
605608
offload: llvm_offload,
606609
polly: llvm_polly,
@@ -1257,6 +1260,8 @@ impl Config {
12571260
let in_tree_gcc_info = git_info(&exec_ctx, false, &src.join("src/gcc"));
12581261
let in_tree_llvm_info = git_info(&exec_ctx, false, &src.join("src/llvm-project"));
12591262
let enzyme_info = git_info(&exec_ctx, omit_git_hash, &src.join("src/tools/enzyme"));
1263+
let offload_info =
1264+
git_info(&exec_ctx, omit_git_hash, &src.join("src/llvm-project/offload"));
12601265
let miri_info = git_info(&exec_ctx, omit_git_hash, &src.join("src/tools/miri"));
12611266
let rust_analyzer_info =
12621267
git_info(&exec_ctx, omit_git_hash, &src.join("src/tools/rust-analyzer"));
@@ -1356,6 +1361,7 @@ impl Config {
13561361
llvm_cflags,
13571362
llvm_clang: llvm_clang.unwrap_or(false),
13581363
llvm_clang_cl,
1364+
llvm_clang_dir: llvm_clang_dir.map(PathBuf::from),
13591365
llvm_cxxflags,
13601366
llvm_enable_warnings: llvm_enable_warnings.unwrap_or(false),
13611367
llvm_enzyme: llvm_enzyme.unwrap_or(false),
@@ -1396,6 +1402,7 @@ impl Config {
13961402
musl_root: rust_musl_root.map(PathBuf::from),
13971403
ninja_in_file: llvm_ninja.unwrap_or(true),
13981404
nodejs: build_nodejs.map(PathBuf::from),
1405+
offload_info,
13991406
omit_git_hash,
14001407
on_fail: flags_on_fail,
14011408
optimized_compiler_builtins,

src/bootstrap/src/core/config/toml/llvm.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ define_config! {
3232
ldflags: Option<String> = "ldflags",
3333
use_libcxx: Option<bool> = "use-libcxx",
3434
use_linker: Option<String> = "use-linker",
35+
clang_dir: Option<String> = "offload-clang-dir",
3536
allow_old_toolchain: Option<bool> = "allow-old-toolchain",
3637
offload: Option<bool> = "offload",
3738
polly: Option<bool> = "polly",
@@ -110,6 +111,7 @@ pub fn check_incompatible_options_for_ci_llvm(
110111
ldflags,
111112
use_libcxx,
112113
use_linker,
114+
clang_dir: _,
113115
allow_old_toolchain,
114116
offload,
115117
polly,

src/bootstrap/src/utils/change_tracker.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,4 +601,9 @@ pub const CONFIG_CHANGE_HISTORY: &[ChangeInfo] = &[
601601
severity: ChangeSeverity::Info,
602602
summary: "New options `rust.rustflags` for all targets and per-target `rustflags` that will pass specified flags to rustc for all stages. Target-specific flags override global `rust.rustflags` ones.",
603603
},
604+
ChangeInfo {
605+
change_id: 148671,
606+
severity: ChangeSeverity::Info,
607+
summary: "New option `llvm.offload-clang-dir` to allow building an in-tree llvm offload and openmp runtime with an external clang.",
608+
},
604609
];

0 commit comments

Comments
 (0)