Skip to content

Commit e43f8c5

Browse files
Rollup merge of #163179 - ZuseZ4:cleanup-offload, r=bjorn3
Cleanup offload build steps We used to generate a couple of artifacts only because clang offload tooling required them. Now that we fully control the build steps on the Rust side without clang tooling, we can remove most of them. I'll make a follow-up PR afterwards to also remove device.bin artifacts and pass the normal rustc outputs of the device compilation pass. r? oli-obk
2 parents c80dce5 + 4f20be3 commit e43f8c5

5 files changed

Lines changed: 1 addition & 73 deletions

File tree

‎compiler/rustc_codegen_llvm/src/back/write.rs‎

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -825,9 +825,7 @@ pub(crate) unsafe fn llvm_optimize(
825825

826826
// This assumes that we previously compiled our kernels for a gpu target, which created a
827827
// `device.bin` artifact. The user is supposed to provide us with a path to this artifact, we
828-
// don't need any other artifacts from the previous run. We will embed this artifact into our
829-
// LLVM-IR host module, to create a `host.o` ObjectFile, which we will write to disk.
830-
// The last, not yet automated steps uses the `clang-linker-wrapper` to process `host.o`.
828+
// don't need any other artifacts from the previous run.
831829
if !cgcx.target_is_like_gpu && is_final_stage {
832830
if let Some(device_path) = config
833831
.offload
@@ -846,38 +844,7 @@ pub(crate) unsafe fn llvm_optimize(
846844
} else if !device_pathbuf.exists() {
847845
dcx.emit_err(crate::diagnostics::OffloadNonexistingPath);
848846
}
849-
let host_path = cgcx.output_filenames.path(OutputType::Object);
850-
let host_dir = host_path.parent().unwrap();
851-
let out_obj = host_dir.join("host.o");
852847
let device_bin_c = path_to_c_string(device_pathbuf.as_path());
853-
854-
// 2) Finalize host: lib.bc + device.bin -> host.o (host TM)
855-
// We create a full clone of our LLVM host module, since we will embed the device IR
856-
// into it, and this might break caching or incremental compilation otherwise.
857-
let ok = unsafe {
858-
llvm::RustOffloadWrapper::get_instance().llvm_rust_offload_embed_buffer_in_module(
859-
module.module_llvm.llmod(),
860-
device_bin_c.as_c_str(),
861-
)
862-
};
863-
if !ok {
864-
dcx.emit_err(crate::diagnostics::OffloadEmbedFailed);
865-
}
866-
write_output_file(
867-
dcx,
868-
module.module_llvm.tm.raw(),
869-
config.no_builtins,
870-
module.module_llvm.llmod(),
871-
&out_obj,
872-
None,
873-
llvm::FileType::ObjectFile,
874-
prof,
875-
true,
876-
);
877-
// We ignore cgcx.save_temps here and unconditionally always keep our `device.bin` artifact.
878-
// Otherwise, recompiling the host code would fail since we deleted that device artifact
879-
// in the previous host compilation, which would be confusing at best.
880-
881848
let ok = unsafe {
882849
llvm::RustOffloadWrapper::get_instance().llvm_rust_offload_wrap_images(
883850
module.module_llvm.llmod(),

‎compiler/rustc_codegen_llvm/src/diagnostics.rs‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,6 @@ pub(crate) struct OffloadNonexistingPath;
101101
#[diag("call to BundleImages failed, `device.bin` was not created")]
102102
pub(crate) struct OffloadBundleImagesFailed;
103103

104-
#[derive(Diagnostic)]
105-
#[diag("call to EmbedBufferInModule failed, `host.o` was not created")]
106-
pub(crate) struct OffloadEmbedFailed;
107-
108104
#[derive(Diagnostic)]
109105
#[diag("call to WrapImages failed, device image was not wrapped into the host module")]
110106
pub(crate) struct OffloadWrapImagesFailed;

‎compiler/rustc_codegen_llvm/src/llvm/offload_ffi.rs‎

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use std::sync::OnceLock;
55
use super::ffi::{Module, TargetMachine, Value};
66

77
type LLVMRustBundleImagesFn = unsafe extern "C" fn(&Module, &TargetMachine, *const c_char) -> bool;
8-
type LLVMRustOffloadEmbedBufferInModuleFn = unsafe extern "C" fn(&Module, *const c_char) -> bool;
98
type LLVMRustOffloadMapperFn = unsafe extern "C" fn(&Value, &Value, *const &Value);
109
type LLVMRustOffloadWrapImagesFn =
1110
unsafe extern "C" fn(&Module, *const c_char, *const c_char) -> bool;
@@ -18,7 +17,6 @@ use crate::llvm;
1817

1918
pub(crate) struct RustOffloadWrapper {
2019
LLVMRustBundleImages: LLVMRustBundleImagesFn,
21-
LLVMRustOffloadEmbedBufferInModule: LLVMRustOffloadEmbedBufferInModuleFn,
2220
LLVMRustOffloadMapper: LLVMRustOffloadMapperFn,
2321
LLVMRustOffloadWrapImages: LLVMRustOffloadWrapImagesFn,
2422
lld_path: Option<PathBuf>,
@@ -65,14 +63,6 @@ impl RustOffloadWrapper {
6563
unsafe { (self.LLVMRustBundleImages)(m, tm, c.as_ptr()) }
6664
}
6765

68-
pub(crate) unsafe fn llvm_rust_offload_embed_buffer_in_module(
69-
&self,
70-
m: &Module,
71-
i: &CStr,
72-
) -> bool {
73-
unsafe { (self.LLVMRustOffloadEmbedBufferInModule)(m, i.as_ptr()) }
74-
}
75-
7666
pub(crate) unsafe fn llvm_rust_offload_wrapper(&self, v1: &Value, v2: &Value, vs: &[&Value]) {
7767
unsafe { (self.LLVMRustOffloadMapper)(v1, v2, vs.as_ptr()) }
7868
}
@@ -96,19 +86,13 @@ impl RustOffloadWrapper {
9686

9787
let llvm_rust_bundle_images =
9888
*unsafe { lib.get::<LLVMRustBundleImagesFn>(b"LLVMRustBundleImages\0")? };
99-
let llvm_rust_offload_embed_buffer_in_module = *unsafe {
100-
lib.get::<LLVMRustOffloadEmbedBufferInModuleFn>(
101-
b"LLVMRustOffloadEmbedBufferInModule\0",
102-
)?
103-
};
10489
let llvm_rust_offload_wrapper =
10590
*unsafe { lib.get::<LLVMRustOffloadMapperFn>(b"LLVMRustOffloadMapper\0")? };
10691
let llvm_rust_offload_wrap_images =
10792
*unsafe { lib.get::<LLVMRustOffloadWrapImagesFn>(b"LLVMRustOffloadWrapImages\0")? };
10893

10994
Ok(Self {
11095
LLVMRustBundleImages: llvm_rust_bundle_images,
111-
LLVMRustOffloadEmbedBufferInModule: llvm_rust_offload_embed_buffer_in_module,
11296
LLVMRustOffloadMapper: llvm_rust_offload_wrapper,
11397
LLVMRustOffloadWrapImages: llvm_rust_offload_wrap_images,
11498
lld_path,

‎compiler/rustc_llvm/llvm-wrapper/offload/OffloadWrapper.cpp‎

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
#include "llvm/Target/TargetOptions.h"
2929
#include "llvm/TargetParser/Triple.h"
3030
#include "llvm/Transforms/Utils/Cloning.h"
31-
#include "llvm/Transforms/Utils/ModuleUtils.h"
3231
#include "llvm/Transforms/Utils/ValueMapper.h"
3332

3433
#include <optional>
@@ -88,22 +87,6 @@ extern "C" bool LLVMRustBundleImages(LLVMModuleRef M, TargetMachine &TM,
8887
return true;
8988
}
9089

91-
extern "C" bool LLVMRustOffloadEmbedBufferInModule(LLVMModuleRef HostM,
92-
const char *HostOutPath) {
93-
auto MBOrErr = MemoryBuffer::getFile(HostOutPath);
94-
if (!MBOrErr) {
95-
auto E = MBOrErr.getError();
96-
auto _B = errorCodeToError(E);
97-
return false;
98-
}
99-
MemoryBufferRef Buf = (*MBOrErr)->getMemBufferRef();
100-
Module *M = unwrap(HostM);
101-
StringRef SectionName = ".llvm.offloading";
102-
Align Alignment = Align(8);
103-
llvm::embedBufferInModule(*M, Buf, SectionName, Alignment);
104-
return true;
105-
}
106-
10790
// Clone OldFn into NewFn, remapping its arguments to RebuiltArgs.
10891
// Each arg of OldFn is replaced with the corresponding value in RebuiltArgs.
10992
// For scalars, RebuiltArgs contains the value cast and/or truncated to the

‎tests/run-make/offload/host-std-device-nostd/rmake.rs‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,4 @@ fn main() {
3535
.arg("-Clto=fat")
3636
.emit("obj")
3737
.run();
38-
39-
assert!(cwd().join("host.o").exists());
4038
}

0 commit comments

Comments
 (0)