Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 1 addition & 34 deletions compiler/rustc_codegen_llvm/src/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -824,9 +824,7 @@ pub(crate) unsafe fn llvm_optimize(

// This assumes that we previously compiled our kernels for a gpu target, which created a
// `device.bin` artifact. The user is supposed to provide us with a path to this artifact, we
// don't need any other artifacts from the previous run. We will embed this artifact into our
// LLVM-IR host module, to create a `host.o` ObjectFile, which we will write to disk.
// The last, not yet automated steps uses the `clang-linker-wrapper` to process `host.o`.
// don't need any other artifacts from the previous run.
if !cgcx.target_is_like_gpu && is_final_stage {
if let Some(device_path) = config
.offload
Expand All @@ -845,38 +843,7 @@ pub(crate) unsafe fn llvm_optimize(
} else if !device_pathbuf.exists() {
dcx.emit_err(crate::diagnostics::OffloadNonexistingPath);
}
let host_path = cgcx.output_filenames.path(OutputType::Object);
let host_dir = host_path.parent().unwrap();
let out_obj = host_dir.join("host.o");
let device_bin_c = path_to_c_string(device_pathbuf.as_path());

// 2) Finalize host: lib.bc + device.bin -> host.o (host TM)
// We create a full clone of our LLVM host module, since we will embed the device IR
// into it, and this might break caching or incremental compilation otherwise.
let ok = unsafe {
llvm::RustOffloadWrapper::get_instance().llvm_rust_offload_embed_buffer_in_module(
module.module_llvm.llmod(),
device_bin_c.as_c_str(),
)
};
if !ok {
dcx.emit_err(crate::diagnostics::OffloadEmbedFailed);
}
write_output_file(
dcx,
module.module_llvm.tm.raw(),
config.no_builtins,
module.module_llvm.llmod(),
&out_obj,
None,
llvm::FileType::ObjectFile,
prof,
true,
);
// We ignore cgcx.save_temps here and unconditionally always keep our `device.bin` artifact.
// Otherwise, recompiling the host code would fail since we deleted that device artifact
// in the previous host compilation, which would be confusing at best.

let ok = unsafe {
llvm::RustOffloadWrapper::get_instance().llvm_rust_offload_wrap_images(
module.module_llvm.llmod(),
Expand Down
4 changes: 0 additions & 4 deletions compiler/rustc_codegen_llvm/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,6 @@ pub(crate) struct OffloadNonexistingPath;
#[diag("call to BundleImages failed, `device.bin` was not created")]
pub(crate) struct OffloadBundleImagesFailed;

#[derive(Diagnostic)]
#[diag("call to EmbedBufferInModule failed, `host.o` was not created")]
pub(crate) struct OffloadEmbedFailed;

#[derive(Diagnostic)]
#[diag("call to WrapImages failed, device image was not wrapped into the host module")]
pub(crate) struct OffloadWrapImagesFailed;
Expand Down
16 changes: 0 additions & 16 deletions compiler/rustc_codegen_llvm/src/llvm/offload_ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use std::sync::OnceLock;
use super::ffi::{Module, TargetMachine, Value};

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

pub(crate) struct RustOffloadWrapper {
LLVMRustBundleImages: LLVMRustBundleImagesFn,
LLVMRustOffloadEmbedBufferInModule: LLVMRustOffloadEmbedBufferInModuleFn,
LLVMRustOffloadMapper: LLVMRustOffloadMapperFn,
LLVMRustOffloadWrapImages: LLVMRustOffloadWrapImagesFn,
lld_path: Option<PathBuf>,
Expand Down Expand Up @@ -65,14 +63,6 @@ impl RustOffloadWrapper {
unsafe { (self.LLVMRustBundleImages)(m, tm, c.as_ptr()) }
}

pub(crate) unsafe fn llvm_rust_offload_embed_buffer_in_module(
&self,
m: &Module,
i: &CStr,
) -> bool {
unsafe { (self.LLVMRustOffloadEmbedBufferInModule)(m, i.as_ptr()) }
}

pub(crate) unsafe fn llvm_rust_offload_wrapper(&self, v1: &Value, v2: &Value, vs: &[&Value]) {
unsafe { (self.LLVMRustOffloadMapper)(v1, v2, vs.as_ptr()) }
}
Expand All @@ -96,19 +86,13 @@ impl RustOffloadWrapper {

let llvm_rust_bundle_images =
*unsafe { lib.get::<LLVMRustBundleImagesFn>(b"LLVMRustBundleImages\0")? };
let llvm_rust_offload_embed_buffer_in_module = *unsafe {
lib.get::<LLVMRustOffloadEmbedBufferInModuleFn>(
b"LLVMRustOffloadEmbedBufferInModule\0",
)?
};
let llvm_rust_offload_wrapper =
*unsafe { lib.get::<LLVMRustOffloadMapperFn>(b"LLVMRustOffloadMapper\0")? };
let llvm_rust_offload_wrap_images =
*unsafe { lib.get::<LLVMRustOffloadWrapImagesFn>(b"LLVMRustOffloadWrapImages\0")? };

Ok(Self {
LLVMRustBundleImages: llvm_rust_bundle_images,
LLVMRustOffloadEmbedBufferInModule: llvm_rust_offload_embed_buffer_in_module,
LLVMRustOffloadMapper: llvm_rust_offload_wrapper,
LLVMRustOffloadWrapImages: llvm_rust_offload_wrap_images,
lld_path,
Expand Down
17 changes: 0 additions & 17 deletions compiler/rustc_llvm/llvm-wrapper/offload/OffloadWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
#include "llvm/Target/TargetOptions.h"
#include "llvm/TargetParser/Triple.h"
#include "llvm/Transforms/Utils/Cloning.h"
#include "llvm/Transforms/Utils/ModuleUtils.h"
#include "llvm/Transforms/Utils/ValueMapper.h"

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

extern "C" bool LLVMRustOffloadEmbedBufferInModule(LLVMModuleRef HostM,
const char *HostOutPath) {
auto MBOrErr = MemoryBuffer::getFile(HostOutPath);
if (!MBOrErr) {
auto E = MBOrErr.getError();
auto _B = errorCodeToError(E);
return false;
}
MemoryBufferRef Buf = (*MBOrErr)->getMemBufferRef();
Module *M = unwrap(HostM);
StringRef SectionName = ".llvm.offloading";
Align Alignment = Align(8);
llvm::embedBufferInModule(*M, Buf, SectionName, Alignment);
return true;
}

// Clone OldFn into NewFn, remapping its arguments to RebuiltArgs.
// Each arg of OldFn is replaced with the corresponding value in RebuiltArgs.
// For scalars, RebuiltArgs contains the value cast and/or truncated to the
Expand Down
2 changes: 0 additions & 2 deletions tests/run-make/offload/host-std-device-nostd/rmake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,4 @@ fn main() {
.arg("-Clto=fat")
.emit("obj")
.run();

assert!(cwd().join("host.o").exists());
}
Loading