Skip to content

Commit 89d5059

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

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

compiler/rustc_codegen_llvm/src/back/write.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,13 @@ 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+
unsafe {
771+
llvm::LLVMRustBundleImages(module.module_llvm.llmod(), module.module_llvm.tm.raw());
772+
}
773+
}
774+
768775
result.into_result().unwrap_or_else(|()| llvm_err(dcx, LlvmError::RunLlvmPasses))
769776
}
770777

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1641,6 +1641,9 @@ unsafe extern "C" {
16411641
Name: *const c_char,
16421642
) -> &'a Value;
16431643

1644+
/// Processes the module and writes it in an offload compatible way into a "host.out" file.
1645+
pub(crate) fn LLVMRustBundleImages<'a>(M: &'a Module, TM: &'a TargetMachine) -> bool;
1646+
16441647
/// Writes a module to the specified path. Returns 0 on success.
16451648
pub(crate) fn LLVMWriteBitcodeToFile(M: &Module, Path: *const c_char) -> c_int;
16461649

compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "llvm/IR/Module.h"
2626
#include "llvm/IR/Value.h"
2727
#include "llvm/Object/COFFImportFile.h"
28+
#include "llvm/Object/OffloadBinary.h"
2829
#include "llvm/Remarks/RemarkFormat.h"
2930
#include "llvm/Remarks/RemarkSerializer.h"
3031
#include "llvm/Remarks/RemarkStreamer.h"
@@ -35,6 +36,7 @@
3536
#include "llvm/Support/Signals.h"
3637
#include "llvm/Support/Timer.h"
3738
#include "llvm/Support/ToolOutputFile.h"
39+
#include "llvm/Target/TargetMachine.h"
3840
#include "llvm/Transforms/Utils/Cloning.h"
3941
#include "llvm/Transforms/Utils/ValueMapper.h"
4042
#include <iostream>
@@ -144,6 +146,51 @@ extern "C" void LLVMRustPrintStatistics(RustStringRef OutBuf) {
144146
llvm::PrintStatistics(OS);
145147
}
146148

149+
static Error writeFile(StringRef Filename, StringRef Data) {
150+
Expected<std::unique_ptr<FileOutputBuffer>> OutputOrErr =
151+
FileOutputBuffer::create(Filename, Data.size());
152+
if (!OutputOrErr)
153+
return OutputOrErr.takeError();
154+
std::unique_ptr<FileOutputBuffer> Output = std::move(*OutputOrErr);
155+
llvm::copy(Data, Output->getBufferStart());
156+
if (Error E = Output->commit())
157+
return E;
158+
return Error::success();
159+
}
160+
161+
// This is the first of many steps in creating a binary using llvm offload,
162+
// to run code on the gpu. Concrete, it replaces the following binary use:
163+
// clang-offload-packager -o host.out
164+
// --image=file=device.bc,triple=amdgcn-amd-amdhsa,arch=gfx90a,kind=openmp
165+
// The input module is the rust code compiled for a gpu target like amdgpu.
166+
// Based on clang/tools/clang-offload-packager/ClangOffloadPackager.cpp
167+
extern "C" bool LLVMRustBundleImages(LLVMModuleRef M, TargetMachine &TM) {
168+
std::string Storage;
169+
llvm::raw_string_ostream OS1(Storage);
170+
llvm::WriteBitcodeToFile(*unwrap(M), OS1);
171+
OS1.flush();
172+
auto MB = llvm::MemoryBuffer::getMemBufferCopy(Storage, "module.bc");
173+
174+
SmallVector<char, 1024> BinaryData;
175+
raw_svector_ostream OS2(BinaryData);
176+
177+
OffloadBinary::OffloadingImage ImageBinary{};
178+
ImageBinary.TheImageKind = object::IMG_Bitcode;
179+
ImageBinary.Image = std::move(MB);
180+
ImageBinary.TheOffloadKind = object::OFK_OpenMP;
181+
ImageBinary.StringData["triple"] = TM.getTargetTriple().str();
182+
ImageBinary.StringData["arch"] = TM.getTargetCPU();
183+
llvm::SmallString<0> Buffer = OffloadBinary::write(ImageBinary);
184+
if (Buffer.size() % OffloadBinary::getAlignment() != 0)
185+
// Offload binary has invalid size alignment
186+
return false;
187+
OS2 << Buffer;
188+
if (Error E = writeFile("host.out",
189+
StringRef(BinaryData.begin(), BinaryData.size())))
190+
return false;
191+
return true;
192+
}
193+
147194
extern "C" void LLVMRustOffloadMapper(LLVMValueRef OldFn, LLVMValueRef NewFn) {
148195
llvm::Function *oldFn = llvm::unwrap<llvm::Function>(OldFn);
149196
llvm::Function *newFn = llvm::unwrap<llvm::Function>(NewFn);

0 commit comments

Comments
 (0)