Skip to content

Commit ebbe811

Browse files
committed
working
1 parent af41ec8 commit ebbe811

File tree

3 files changed

+16
-26
lines changed

3 files changed

+16
-26
lines changed

compiler/rustc_codegen_llvm/src/back/write.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -779,25 +779,15 @@ pub(crate) unsafe fn llvm_optimize(
779779
let out_obj_c = CString::new("/p/lustre1/drehwald1/prog/offload/r/host.o").unwrap();
780780

781781
unsafe {
782-
//let buf = llvm::LLVMRustModuleBufferFromFile(lib_bc_c.as_ptr()).unwrap();
782+
// 1) Bundle device module into offload image host.out (device TM)
783783
let buf = llvm::LLVMRustModuleBufferCreate(module.module_llvm.llmod());
784-
785784
let ok =
786785
llvm::LLVMRustBundleImages(buf, module.module_llvm.tm.raw(), host_out_c.as_ptr());
787-
// 1) Bundle device module into offload image host.out (device TM)
788-
//let ok = llvm::LLVMRustBundleImages(
789-
// module.module_llvm.llmod(),
790-
// module.module_llvm.tm.raw(),
791-
// host_out_c.as_ptr(),
792-
//);
793786
assert!(ok, "LLVMRustBundleImages (device -> host.out) failed");
794787

795788
// 2) Finalize host: lib.bc + host.out -> host.offload.o (host TM created in C++)
796-
let ok = llvm::LLVMRustFinalizeOffload(
797-
lib_bc_c.as_ptr(),
798-
host_out_c.as_ptr(),
799-
out_obj_c.as_ptr(),
800-
);
789+
let buf = llvm::LLVMRustModuleBufferFromFile(lib_bc_c.as_ptr()).unwrap();
790+
let ok = llvm::LLVMRustFinalizeOffload(buf, host_out_c.as_ptr(), out_obj_c.as_ptr());
801791
assert!(ok, "LLVMRustFinalizeOffload (host finalize) failed");
802792
}
803793
dbg!("done");

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1724,12 +1724,12 @@ mod Offload {
17241724
/// Processes the module and writes it in an offload compatible way into a "host.out" file.
17251725
pub(crate) fn LLVMRustBundleImages<'a>(
17261726
p: &ModuleBuffer,
1727-
//M: &'a Module,
17281727
TM: &'a TargetMachine,
17291728
host_out: *const c_char,
17301729
) -> bool;
17311730
pub(crate) fn LLVMRustFinalizeOffload(
1732-
lib_bc_path: *const c_char,
1731+
p: &ModuleBuffer,
1732+
//lib_bc_path: *const c_char,
17331733
host_out_path: *const c_char,
17341734
out_obj_path: *const c_char,
17351735
) -> bool;

compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
// available. As such, we only try to build it in the first place, if
4545
// llvm.offload is enabled.
4646
#ifdef OFFLOAD
47+
#include "llvm/Bitcode/BitcodeReader.h"
4748
#include "llvm/Object/OffloadBinary.h"
4849
#include "llvm/Target/TargetMachine.h"
4950
#include "llvm/Transforms/Utils/ModuleUtils.h"
@@ -1544,12 +1545,6 @@ extern "C" bool LLVMRustBundleImages(const LLVMRustModuleBuffer *MBuf, TargetMac
15441545
// Recreate a MemoryBuffer from the bytes in LLVMRustModuleBuffer
15451546
StringRef Storage(MBuf->data.data(), MBuf->data.size());
15461547
auto MB = MemoryBuffer::getMemBufferCopy(Storage, "device.bc");
1547-
//auto MB = LLVMRustModuleBufferCreate(M);
1548-
//std::string Storage;
1549-
//llvm::raw_string_ostream OS1(Storage);
1550-
//llvm::WriteBitcodeToFile(*unwrap(M), OS1);
1551-
//OS1.flush();
1552-
//auto MB = llvm::MemoryBuffer::getMemBufferCopy(Storage, "device.bc");
15531548

15541549
SmallVector<char, 1024> BinaryData;
15551550
raw_svector_ostream OS2(BinaryData);
@@ -1575,7 +1570,6 @@ extern "C" bool LLVMRustBundleImages(const LLVMRustModuleBuffer *MBuf, TargetMac
15751570
return true;
15761571
}
15771572

1578-
#include "llvm/Bitcode/BitcodeReader.h"
15791573
Expected<std::unique_ptr<Module>>
15801574
loadHostModuleFromBitcode(LLVMContext &Ctx, StringRef LibBCPath) {
15811575
auto MBOrErr = MemoryBuffer::getFile(LibBCPath);
@@ -1706,25 +1700,31 @@ static std::unique_ptr<TargetMachine> createHostTargetMachine() {
17061700

17071701
// Top-level entry: host finalize in second rustc invocation
17081702
// lib.bc (from first rustc) + host.out (from LLVMRustBundleImages) => host.offload.o
1709-
extern "C" bool LLVMRustFinalizeOffload(const char *LibBCPath,
1703+
extern "C" bool LLVMRustFinalizeOffload(const LLVMRustModuleBuffer *MBuf,
17101704
const char *HostOutPath,
17111705
const char *OutObjPath) {
1712-
LLVMContext Ctx;
1706+
LLVMContext Ctx;
1707+
llvm::errs() << "Before A\n";
17131708

1714-
// 1. Load host lib.bc
1715-
auto ModOrErr = loadHostModuleFromBitcode(Ctx, LibBCPath);
1709+
// new:
1710+
StringRef Storage(MBuf->data.data(), MBuf->data.size());
1711+
MemoryBufferRef Ref(Storage, "host.bc");
1712+
auto ModOrErr = parseBitcodeFile(Ref, Ctx);
17161713
if (!ModOrErr)
17171714
return !errorToBool(ModOrErr.takeError());
17181715
std::unique_ptr<Module> HostM = std::move(*ModOrErr);
1716+
llvm::errs() << "A done\n";
17191717

17201718
// 2. Embed host.out
17211719
if (Error E = embedHostOutIntoHostModule(*HostM, HostOutPath))
17221720
return !errorToBool(std::move(E));
1721+
llvm::errs() << "B done\n";
17231722

17241723
// 3. Create host TM and emit host object
17251724
auto HostTM = createHostTargetMachine();
17261725
if (!HostTM)
17271726
return false;
1727+
llvm::errs() << "C done\n";
17281728

17291729
if (Error E = emitHostObjectWithTM(*HostM, *HostTM, OutObjPath))
17301730
return !errorToBool(std::move(E));

0 commit comments

Comments
 (0)