Skip to content

Commit ad8e079

Browse files
kbobrovsvladimirlaz
authored andcommitted
[SYCL] Fix auto-deduction of device binary image format when extracting from a fat binary.
The format field is now not updated if image is part of loaded OS module. Signed-off-by: Vladimir Lazarev <[email protected]> Signed-off-by: Konstantin S Bobrovsky <[email protected]>
1 parent 1b8c008 commit ad8e079

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

sycl/source/detail/program_manager/program_manager.cpp

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -288,11 +288,19 @@ cnri_program ProgramManager::loadProgram(OSModuleHandle M,
288288
debugDumpBinaryImage(Img);
289289
}
290290
}
291-
assert(Img->ImageEnd >= Img->ImageStart);
291+
// perform minimal sanity checks on the device image and the descriptor
292+
if (Img->ImageEnd < Img->ImageStart) {
293+
throw runtime_error("Malformed device program image descriptor");
294+
}
295+
if (Img->ImageEnd == Img->ImageStart) {
296+
throw runtime_error("Invalid device program image: size is zero");
297+
}
292298
size_t ImgSize = static_cast<size_t>(Img->ImageEnd - Img->ImageStart);
299+
cnri_device_image_format Format =
300+
static_cast<cnri_device_image_format>(Img->Format);
293301

294302
// Determine the format of the image if not set already
295-
if (Img->Format == CNRI_IMG_NONE) {
303+
if (Format == CNRI_IMG_NONE) {
296304
struct {
297305
cnri_device_image_format Fmt;
298306
const uint32_t Magic;
@@ -305,11 +313,22 @@ cnri_program ProgramManager::loadProgram(OSModuleHandle M,
305313

306314
for (const auto &Fmt : Fmts) {
307315
if (Hdr == Fmt.Magic) {
308-
Img->Format = Fmt.Fmt;
309-
316+
Format = Fmt.Fmt;
317+
318+
// Image binary format wasn't set but determined above - update it;
319+
if (UseKernelSpv) {
320+
Img->Format = Format;
321+
} else {
322+
// TODO the binary image is a part of the fat binary, the clang
323+
// driver should have set proper format option to the
324+
// clang-offload-wrapper. The fix depends on AOT compilation
325+
// implementation, so will be implemented together with it.
326+
// Img->Format can't be updated as it is inside of the in-memory
327+
// OS module binary.
328+
// throw runtime_error("Image format not set");
329+
}
310330
if (DbgProgMgr > 1) {
311-
std::cerr << "determined image format: " << (int)Img->Format
312-
<< "\n";
331+
std::cerr << "determined image format: " << (int)Format << "\n";
313332
}
314333
break;
315334
}
@@ -322,9 +341,9 @@ cnri_program ProgramManager::loadProgram(OSModuleHandle M,
322341
Fname += Img->DeviceTargetSpec;
323342
std::string Ext;
324343

325-
if (Img->Format == CNRI_IMG_SPIRV) {
344+
if (Format == CNRI_IMG_SPIRV) {
326345
Ext = ".spv";
327-
} else if (Img->Format == CNRI_IMG_LLVMIR_BITCODE) {
346+
} else if (Format == CNRI_IMG_LLVMIR_BITCODE) {
328347
Ext = ".bc";
329348
} else {
330349
Ext = ".bin";
@@ -342,7 +361,7 @@ cnri_program ProgramManager::loadProgram(OSModuleHandle M,
342361
// Load the selected image
343362
const cnri_context &Ctx = getRawSyclObjImpl(Context)->getHandleRef();
344363
cnri_program Res = nullptr;
345-
Res = Img->Format == CNRI_IMG_SPIRV
364+
Res = Format == CNRI_IMG_SPIRV
346365
? createSpirvProgram(Ctx, Img->ImageStart, ImgSize)
347366
: createBinaryProgram(Ctx, Img->ImageStart, ImgSize);
348367

0 commit comments

Comments
 (0)