|
8 | 8 |
|
9 | 9 | #include "OffloadWrapper.h"
|
10 | 10 | #include "llvm/ADT/ArrayRef.h"
|
| 11 | +#include "llvm/BinaryFormat/Magic.h" |
11 | 12 | #include "llvm/Frontend/Offloading/Utility.h"
|
12 | 13 | #include "llvm/IR/Constants.h"
|
13 | 14 | #include "llvm/IR/GlobalVariable.h"
|
@@ -121,19 +122,36 @@ GlobalVariable *createBinDesc(Module &M, ArrayRef<ArrayRef<char>> Bufs) {
|
121 | 122 | SmallVector<Constant *, 4u> ImagesInits;
|
122 | 123 | ImagesInits.reserve(Bufs.size());
|
123 | 124 | for (ArrayRef<char> Buf : Bufs) {
|
| 125 | + // We embed the full offloading entry so the binary utilities can parse it. |
124 | 126 | auto *Data = ConstantDataArray::get(C, Buf);
|
125 |
| - auto *Image = new GlobalVariable(M, Data->getType(), /*isConstant*/ true, |
| 127 | + auto *Image = new GlobalVariable(M, Data->getType(), /*isConstant=*/true, |
126 | 128 | GlobalVariable::InternalLinkage, Data,
|
127 | 129 | ".omp_offloading.device_image");
|
128 | 130 | Image->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
|
129 | 131 | Image->setSection(".llvm.offloading");
|
130 | 132 | Image->setAlignment(Align(object::OffloadBinary::getAlignment()));
|
131 | 133 |
|
132 |
| - auto *Size = ConstantInt::get(getSizeTTy(M), Buf.size()); |
| 134 | + StringRef Binary(Buf.data(), Buf.size()); |
| 135 | + assert(identify_magic(Binary) == file_magic::offload_binary && |
| 136 | + "Invalid binary format"); |
| 137 | + |
| 138 | + // The device image struct contains the pointer to the beginning and end of |
| 139 | + // the image stored inside of the offload binary. There should only be one |
| 140 | + // of these for each buffer so we parse it out manually. |
| 141 | + const auto *Header = |
| 142 | + reinterpret_cast<const object::OffloadBinary::Header *>( |
| 143 | + Binary.bytes_begin()); |
| 144 | + const auto *Entry = reinterpret_cast<const object::OffloadBinary::Entry *>( |
| 145 | + Binary.bytes_begin() + Header->EntryOffset); |
| 146 | + |
| 147 | + auto *Begin = ConstantInt::get(getSizeTTy(M), Entry->ImageOffset); |
| 148 | + auto *Size = |
| 149 | + ConstantInt::get(getSizeTTy(M), Entry->ImageOffset + Entry->ImageSize); |
| 150 | + Constant *ZeroBegin[] = {Zero, Begin}; |
133 | 151 | Constant *ZeroSize[] = {Zero, Size};
|
134 | 152 |
|
135 | 153 | auto *ImageB =
|
136 |
| - ConstantExpr::getGetElementPtr(Image->getValueType(), Image, ZeroZero); |
| 154 | + ConstantExpr::getGetElementPtr(Image->getValueType(), Image, ZeroBegin); |
137 | 155 | auto *ImageE =
|
138 | 156 | ConstantExpr::getGetElementPtr(Image->getValueType(), Image, ZeroSize);
|
139 | 157 |
|
|
0 commit comments