3939#include " llvm/Transforms/Utils/ValueMapper.h"
4040#include < iostream>
4141
42+ #include " llvm/ADT/StringExtras.h"
43+ #include " llvm/Object/ArchiveWriter.h"
44+ #include " llvm/Object/OffloadBinary.h"
45+ #include " llvm/Support/CommandLine.h"
46+ #include " llvm/Support/FileOutputBuffer.h"
47+ #include " llvm/Support/MemoryBuffer.h"
48+ #include " llvm/Support/Path.h"
49+ #include " llvm/Support/Signals.h"
50+
4251// for raw `write` in the bad-alloc handler
4352#ifdef _MSC_VER
4453#include < io.h>
@@ -57,6 +66,56 @@ using namespace llvm;
5766using namespace llvm ::sys;
5867using namespace llvm ::object;
5968
69+ static Error writeFile (StringRef Filename, StringRef Data) {
70+ Expected<std::unique_ptr<FileOutputBuffer>> OutputOrErr =
71+ FileOutputBuffer::create (Filename, Data.size ());
72+ if (!OutputOrErr)
73+ return OutputOrErr.takeError ();
74+ std::unique_ptr<FileOutputBuffer> Output = std::move (*OutputOrErr);
75+ llvm::copy (Data, Output->getBufferStart ());
76+ if (Error E = Output->commit ())
77+ return E;
78+ return Error::success ();
79+ }
80+ #include " llvm/Target/TargetMachine.h"
81+ extern " C" bool rustBundleImages (LLVMModuleRef M, TargetMachine &TM) {
82+ // Module *Mptr = unwrap(M);
83+ std::string Storage;
84+ llvm::raw_string_ostream OS1 (Storage);
85+
86+ llvm::WriteBitcodeToFile (*unwrap (M), OS1);
87+ // llvm::WriteBitcodeToFile(*Mptr, OS1);
88+ OS1.flush ();
89+ auto MB = llvm::MemoryBuffer::getMemBufferCopy (Storage, " module.bc" );
90+
91+ SmallVector<char , 1024 > BinaryData;
92+ raw_svector_ostream OS (BinaryData);
93+ BumpPtrAllocator Alloc;
94+ StringSaver Saver (Alloc);
95+
96+ DenseMap<StringRef, StringRef> Args;
97+
98+ {
99+ OffloadBinary::OffloadingImage ImageBinary{};
100+ ImageBinary.TheImageKind = object::IMG_Bitcode;
101+ ImageBinary.Image = std::move (MB);
102+ ImageBinary.TheOffloadKind = object::OFK_OpenMP;
103+ // ImageBinary.StringData["triple"] = triple;
104+ // ImageBinary.StringData["arch"] = arch;
105+ ImageBinary.StringData [" triple" ] = TM.getTargetTriple ().str ();
106+ ImageBinary.StringData [" arch" ] = TM.getTargetCPU ();
107+ llvm::SmallString<0 > Buffer = OffloadBinary::write (ImageBinary);
108+ if (Buffer.size () % OffloadBinary::getAlignment () != 0 )
109+ // Offload binary has invalid size alignment
110+ return false ;
111+ OS << Buffer;
112+ }
113+ if (Error E = writeFile (" host.out" ,
114+ StringRef (BinaryData.begin (), BinaryData.size ())))
115+ return false ;
116+ return true ;
117+ }
118+
60119// This opcode is an LLVM detail that could hypothetically change (?), so
61120// verify that the hard-coded value in `dwarf_const.rs` still agrees with LLVM.
62121static_assert (dwarf::DW_OP_LLVM_fragment == 0x1000 );
0 commit comments