Skip to content

Commit 7ce5776

Browse files
[SYCL] Select compatible binary in piextDeviceSelectBinary (#2567)
Signed-off-by: Sergey V Maslov <[email protected]>
1 parent c5f0766 commit 7ce5776

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

sycl/plugins/level_zero/pi_level_zero.cpp

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,7 +1441,16 @@ piextDeviceSelectBinary(pi_device Device, // TODO: does this need to be context?
14411441
pi_device_binary *Binaries, pi_uint32 NumBinaries,
14421442
pi_uint32 *SelectedBinaryInd) {
14431443

1444-
// TODO dummy implementation.
1444+
assert(Device);
1445+
assert(SelectedBinaryInd);
1446+
assert(NumBinaries == 0 || Binaries);
1447+
1448+
// TODO: this is a bare-bones implementation for choosing a device image
1449+
// that would be compatible with the targeted device. An AOT-compiled
1450+
// image is preferred over SPIR-V for known devices (i.e. Intel devices)
1451+
// The implementation makes no effort to differentiate between multiple images
1452+
// for the given device, and simply picks the first one compatible.
1453+
//
14451454
// Real implementation will use the same mechanism OpenCL ICD dispatcher
14461455
// uses. Something like:
14471456
// PI_VALIDATE_HANDLE_RETURN_HANDLE(ctx, PI_INVALID_CONTEXT);
@@ -1450,9 +1459,28 @@ piextDeviceSelectBinary(pi_device Device, // TODO: does this need to be context?
14501459
// where context->dispatch is set to the dispatch table provided by PI
14511460
// plugin for platform/device the ctx was created for.
14521461

1462+
// Look for GEN binary, which we known can only be handled by Level-Zero now.
1463+
const char *BinaryTarget = PI_DEVICE_BINARY_TARGET_SPIRV64_GEN;
1464+
1465+
// Find the appropriate device image, fallback to spirv if not found
14531466
constexpr pi_uint32 InvalidInd = std::numeric_limits<pi_uint32>::max();
1454-
*SelectedBinaryInd = NumBinaries > 0 ? 0 : InvalidInd;
1455-
return PI_SUCCESS;
1467+
pi_uint32 Spirv = InvalidInd;
1468+
1469+
for (pi_uint32 i = 0; i < NumBinaries; ++i) {
1470+
if (strcmp(Binaries[i]->DeviceTargetSpec, BinaryTarget) == 0) {
1471+
*SelectedBinaryInd = i;
1472+
return PI_SUCCESS;
1473+
}
1474+
if (strcmp(Binaries[i]->DeviceTargetSpec,
1475+
PI_DEVICE_BINARY_TARGET_SPIRV64) == 0)
1476+
Spirv = i;
1477+
}
1478+
// Points to a spirv image, if such indeed was found
1479+
if ((*SelectedBinaryInd = Spirv) != InvalidInd)
1480+
return PI_SUCCESS;
1481+
1482+
// No image can be loaded for the given device
1483+
return PI_INVALID_BINARY;
14561484
}
14571485

14581486
pi_result piextDeviceGetNativeHandle(pi_device Device,

0 commit comments

Comments
 (0)