Skip to content

Commit 6229003

Browse files
committed
Replace the first of 4 binary invocations for offload
1 parent 5f7653d commit 6229003

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

compiler/rustc_codegen_llvm/src/back/write.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,23 @@ pub(crate) unsafe fn llvm_optimize(
765765
llvm_plugins.len(),
766766
)
767767
};
768+
769+
if cgcx.target_is_like_gpu && config.offload.contains(&config::Offload::Enable) {
770+
//let triple = SmallCStr::new(&versioned_llvm_target(sess));
771+
//let arch = SmallCStr::new(llvm_util::target_cpu(sess));
772+
//let arch = c"gfx90a";
773+
//let triple = c"amdgcn-amd-amdhsa";
774+
unsafe {
775+
llvm::rustBundleImages(
776+
module.module_llvm.llmod(),
777+
module.module_llvm.tm.raw(), //triple.as_ptr(),
778+
//triple.count_bytes(),
779+
//arch.as_ptr(),
780+
//arch.count_bytes(),
781+
);
782+
}
783+
}
784+
768785
result.into_result().unwrap_or_else(|()| llvm_err(dcx, LlvmError::RunLlvmPasses))
769786
}
770787

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2026,6 +2026,14 @@ unsafe extern "C" {
20262026

20272027
// Operations on functions
20282028
pub(crate) fn LLVMRustOffloadMapper<'a>(Fn: &'a Value, Fn: &'a Value);
2029+
pub(crate) fn rustBundleImages<'a>(
2030+
M: &'a Module,
2031+
TM: &'a TargetMachine,
2032+
//triple: *const c_char,
2033+
//tripleLen: size_t,
2034+
//arch: *const c_char,
2035+
//archLen: size_t,
2036+
) -> bool;
20292037
pub(crate) fn LLVMRustGetOrInsertFunction<'a>(
20302038
M: &'a Module,
20312039
Name: *const c_char,

compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@
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;
5766
using namespace llvm::sys;
5867
using 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.
62121
static_assert(dwarf::DW_OP_LLVM_fragment == 0x1000);

0 commit comments

Comments
 (0)