@@ -288,11 +288,19 @@ cnri_program ProgramManager::loadProgram(OSModuleHandle M,
288
288
debugDumpBinaryImage (Img);
289
289
}
290
290
}
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
+ }
292
298
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 );
293
301
294
302
// Determine the format of the image if not set already
295
- if (Img-> Format == CNRI_IMG_NONE) {
303
+ if (Format == CNRI_IMG_NONE) {
296
304
struct {
297
305
cnri_device_image_format Fmt;
298
306
const uint32_t Magic;
@@ -305,11 +313,22 @@ cnri_program ProgramManager::loadProgram(OSModuleHandle M,
305
313
306
314
for (const auto &Fmt : Fmts) {
307
315
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
+ }
310
330
if (DbgProgMgr > 1 ) {
311
- std::cerr << " determined image format: " << (int )Img->Format
312
- << " \n " ;
331
+ std::cerr << " determined image format: " << (int )Format << " \n " ;
313
332
}
314
333
break ;
315
334
}
@@ -322,9 +341,9 @@ cnri_program ProgramManager::loadProgram(OSModuleHandle M,
322
341
Fname += Img->DeviceTargetSpec ;
323
342
std::string Ext;
324
343
325
- if (Img-> Format == CNRI_IMG_SPIRV) {
344
+ if (Format == CNRI_IMG_SPIRV) {
326
345
Ext = " .spv" ;
327
- } else if (Img-> Format == CNRI_IMG_LLVMIR_BITCODE) {
346
+ } else if (Format == CNRI_IMG_LLVMIR_BITCODE) {
328
347
Ext = " .bc" ;
329
348
} else {
330
349
Ext = " .bin" ;
@@ -342,7 +361,7 @@ cnri_program ProgramManager::loadProgram(OSModuleHandle M,
342
361
// Load the selected image
343
362
const cnri_context &Ctx = getRawSyclObjImpl (Context)->getHandleRef ();
344
363
cnri_program Res = nullptr ;
345
- Res = Img-> Format == CNRI_IMG_SPIRV
364
+ Res = Format == CNRI_IMG_SPIRV
346
365
? createSpirvProgram (Ctx, Img->ImageStart , ImgSize)
347
366
: createBinaryProgram (Ctx, Img->ImageStart , ImgSize);
348
367
0 commit comments