Skip to content

Commit 3236637

Browse files
committed
Auto merge of #159064 - sgasho:offload-dist, r=<try>
Add offload support to the dist-x86_64-linux CI job try-job: dist-x86_64-linux
2 parents 29e68fe + 2f451af commit 3236637

8 files changed

Lines changed: 123 additions & 12 deletions

File tree

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2278,15 +2278,14 @@ impl Step for Assemble {
22782278
if let Some(_llvm_config) = builder.llvm_config(builder.config.host_target) {
22792279
let target_libdir =
22802280
builder.sysroot_target_libdir(target_compiler, target_compiler.host);
2281-
for p in offload_install.offload_paths() {
2282-
let libname = p.file_name().unwrap();
2283-
let dst_lib = target_libdir.join(libname);
2281+
let offload_libdir = builder.offload_out(build_compiler.host).join("lib");
2282+
2283+
for p in offload_install.artifact_paths_with_symlink_targets() {
2284+
let relative = t!(p.strip_prefix(&offload_libdir));
2285+
let dst_lib = target_libdir.join(relative);
2286+
t!(fs::create_dir_all(dst_lib.parent().unwrap()));
22842287
builder.resolve_symlink_and_copy(&p, &dst_lib);
22852288
}
2286-
// FIXME(offload): Add amdgcn-amd-amdhsa and nvptx64-nvidia-cuda folder
2287-
// This one is slightly more tricky, since we have the same file twice, in two
2288-
// subfolders for amdgcn and nvptx64. We'll likely find two more in the future, once
2289-
// Intel and Spir-V support lands in offload.
22902289
}
22912290
}
22922291

src/bootstrap/src/core/build_steps/dist.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2811,6 +2811,59 @@ impl Step for Enzyme {
28112811
}
28122812
}
28132813

2814+
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
2815+
pub struct Offload {
2816+
pub target: TargetSelection,
2817+
}
2818+
2819+
impl Step for Offload {
2820+
type Output = Option<GeneratedTarball>;
2821+
const IS_HOST: bool = true;
2822+
2823+
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
2824+
run.alias("offload")
2825+
}
2826+
2827+
fn is_default_step(builder: &Builder<'_>) -> bool {
2828+
builder.config.llvm_offload
2829+
}
2830+
2831+
fn make_run(run: RunConfig<'_>) {
2832+
run.builder.ensure(Offload { target: run.target });
2833+
}
2834+
2835+
fn run(self, builder: &Builder<'_>) -> Self::Output {
2836+
if !builder.unstable_features() {
2837+
return None;
2838+
}
2839+
2840+
let target = self.target;
2841+
2842+
let offload = builder.ensure(llvm::OmpOffload { target });
2843+
2844+
if builder.config.dry_run() {
2845+
return None;
2846+
}
2847+
2848+
let target_libdir = format!("lib/rustlib/{}/lib", target.triple);
2849+
2850+
let mut tarball = Tarball::new(builder, "offload", &target.triple);
2851+
tarball.set_overlay(OverlayKind::Offload);
2852+
tarball.is_preview(true);
2853+
2854+
let offload_libdir = builder.offload_out(target).join("lib");
2855+
2856+
for path in offload.artifact_paths_with_symlink_targets() {
2857+
let relative = t!(path.strip_prefix(&offload_libdir));
2858+
let destdir = Path::new(&target_libdir).join(relative.parent().unwrap());
2859+
2860+
tarball.add_file(path, destdir, FileType::NativeLibrary);
2861+
}
2862+
2863+
Some(tarball.generate())
2864+
}
2865+
}
2866+
28142867
/// Tarball intended for internal consumption to ease rustc/std development.
28152868
///
28162869
/// Should not be considered stable by end users.

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

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -941,8 +941,25 @@ pub struct BuiltOmpOffload {
941941
}
942942

943943
impl BuiltOmpOffload {
944-
pub fn offload_paths(&self) -> Vec<PathBuf> {
945-
self.offload.clone()
944+
pub fn artifact_paths_with_symlink_targets(&self) -> Vec<PathBuf> {
945+
let mut paths = self.offload.clone();
946+
947+
for path in &self.offload {
948+
let mut current = path.clone();
949+
950+
while t!(fs::symlink_metadata(&current)).file_type().is_symlink() {
951+
let target = t!(fs::read_link(&current));
952+
current = current.parent().unwrap().join(target);
953+
954+
if paths.contains(&current) {
955+
break;
956+
}
957+
958+
paths.push(current.clone());
959+
}
960+
}
961+
962+
paths
946963
}
947964
}
948965

@@ -997,6 +1014,30 @@ impl Step for OmpOffload {
9971014
files.push(out_dir.join("lib").join("libLLVMOffload").with_extension(lib_ext));
9981015
files.push(out_dir.join("lib").join("libomp").with_extension(lib_ext));
9991016
files.push(out_dir.join("lib").join("libomptarget").with_extension(lib_ext));
1017+
files.push(
1018+
out_dir.join("lib").join("amdgcn-amd-amdhsa").join("libompdevice").with_extension("a"),
1019+
);
1020+
files.push(
1021+
out_dir
1022+
.join("lib")
1023+
.join("amdgcn-amd-amdhsa")
1024+
.join("libomptarget-amdgpu")
1025+
.with_extension("bc"),
1026+
);
1027+
files.push(
1028+
out_dir
1029+
.join("lib")
1030+
.join("nvptx64-nvidia-cuda")
1031+
.join("libompdevice")
1032+
.with_extension("a"),
1033+
);
1034+
files.push(
1035+
out_dir
1036+
.join("lib")
1037+
.join("nvptx64-nvidia-cuda")
1038+
.join("libomptarget-nvptx")
1039+
.with_extension("bc"),
1040+
);
10001041

10011042
// Offload/OpenMP are just subfolders of LLVM, so we can use the LLVM sha.
10021043
static STAMP_HASH_MEMO: OnceLock<String> = OnceLock::new();
@@ -1063,7 +1104,15 @@ impl Step for OmpOffload {
10631104
cflags.push_all(format!(" -I {inc_dir}"));
10641105
}
10651106

1066-
configure_cmake(builder, target, &mut cfg, true, LdFlags::default(), cflags, &[]);
1107+
// Logic copied from `configure_llvm`
1108+
// ThinLTO is only available when building with LLVM, enabling LLD is required.
1109+
// Apple's linker ld64 supports ThinLTO out of the box though, so don't use LLD on Darwin.
1110+
let mut ldflags = LdFlags::default();
1111+
if builder.config.llvm_thin_lto && !target.contains("apple") {
1112+
ldflags.push_all("-fuse-ld=lld");
1113+
}
1114+
1115+
configure_cmake(builder, target, &mut cfg, true, ldflags, cflags, &[]);
10671116

10681117
// Re-use the same flags as llvm to control the level of debug information
10691118
// generated for offload.
@@ -1097,6 +1146,7 @@ impl Step for OmpOffload {
10971146
cfg.define("LLVM_ENABLE_RUNTIMES", "openmp;offload");
10981147
} else {
10991148
// OpenMP provides some device libraries, so we also compile it for all gpu targets.
1149+
cfg.define("OPENMP_INSTALL_LIBDIR", Path::new("lib").join(omp_target));
11001150
cfg.define("LLVM_USE_LINKER", "lld");
11011151
cfg.define("LLVM_ENABLE_RUNTIMES", "openmp");
11021152
cfg.define("CMAKE_C_COMPILER_TARGET", omp_target);

src/bootstrap/src/core/builder/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,6 +1017,7 @@ impl<'a> Builder<'a> {
10171017
dist::LlvmBitcodeLinker,
10181018
dist::RustDev,
10191019
dist::Enzyme,
1020+
dist::Offload,
10201021
dist::Bootstrap,
10211022
dist::Extended,
10221023
// It seems that PlainSourceTarball somehow changes how some of the tools

src/bootstrap/src/utils/tarball.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ pub(crate) enum OverlayKind {
2929
Gcc,
3030
LlvmBitcodeLinker,
3131
Enzyme,
32+
Offload,
3233
}
3334

3435
impl OverlayKind {
@@ -39,6 +40,9 @@ impl OverlayKind {
3940
&["src/llvm-project/llvm/LICENSE.TXT", "src/llvm-project/llvm/README.txt"]
4041
}
4142
OverlayKind::Enzyme => &["src/tools/enzyme/LICENSE", "src/tools/enzyme/Readme.md"],
43+
OverlayKind::Offload => {
44+
&["src/llvm-project/openmp/LICENSE.TXT", "src/llvm-project/offload/README.md"]
45+
}
4246
OverlayKind::Cargo => &[
4347
"src/tools/cargo/README.md",
4448
"src/tools/cargo/LICENSE-MIT",
@@ -114,6 +118,7 @@ impl OverlayKind {
114118
OverlayKind::LlvmBitcodeLinker => builder.rust_version(),
115119
OverlayKind::Gcc => builder.rust_version(),
116120
OverlayKind::Enzyme => builder.rust_version(),
121+
OverlayKind::Offload => builder.rust_version(),
117122
}
118123
}
119124
}

src/ci/docker/host-x86_64/dist-x86_64-linux/Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ RUN ./cmake.sh
6565
# Now build LLVM+Clang, afterwards configuring further compilations to use the
6666
# clang/clang++ compilers.
6767
COPY scripts/build-clang.sh /tmp/
68-
ENV LLVM_BUILD_TARGETS=X86
68+
ENV LLVM_BUILD_TARGETS="X86;AMDGPU;NVPTX"
6969
RUN ./build-clang.sh
7070
ENV CC=clang CXX=clang++
7171

@@ -90,6 +90,7 @@ ENV RUST_CONFIGURE_ARGS="--enable-full-tools \
9090
--set llvm.thin-lto=true \
9191
--set llvm.ninja=false \
9292
--set llvm.libzstd=true \
93+
--set llvm.offload-clang-dir="/rustroot/lib/cmake/clang" \
9394
--set rust.override-allocator=jemalloc \
9495
--set rust.bootstrap-override-lld=true \
9596
--set rust.lto=thin \

src/ci/docker/host-x86_64/dist-x86_64-linux/dist.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ python3 ../x.py build --set rust.debug=true opt-dist
1010
build-manifest \
1111
bootstrap \
1212
enzyme \
13+
offload \
1314
rustc_codegen_gcc
1415

1516
# Use GCC for building GCC components, as it seems to behave badly when built with Clang

src/ci/docker/scripts/build-gcc.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env bash
2+
# ignore-tidy-file-linelength
23
set -eux
34

45
source shared.sh
@@ -29,7 +30,7 @@ cd gcc-$GCC
2930
# the expression will need to be updated. That new URL is available at:
3031
# https://github.com/gcc-mirror/gcc/blob/6e6e3f144a33ae504149dc992453b4f6dea12fdb/contrib/download_prerequisites#L35
3132
#
32-
sed -i'' 's|ftp://gcc\.gnu\.org/|https://gcc.gnu.org/|g' ./contrib/download_prerequisites
33+
sed -i'' 's|ftp://gcc\.gnu\.org/pub/gcc/infrastructure|https://ci-mirrors.rust-lang.org/rustc/gcc|g' ./contrib/download_prerequisites
3334

3435
./contrib/download_prerequisites
3536
mkdir ../gcc-build

0 commit comments

Comments
 (0)