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"
15791573Expected<std::unique_ptr<Module>>
15801574loadHostModuleFromBitcode (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