Skip to content

Commit 45078e5

Browse files
committed
[UR][L0] FIx UR_PROGRAM_INFO_BINARIES query
1 parent 3d6647e commit 45078e5

File tree

1 file changed

+19
-40
lines changed

1 file changed

+19
-40
lines changed

unified-runtime/source/adapters/level_zero/program.cpp

Lines changed: 19 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -746,62 +746,41 @@ ur_result_t urProgramGetInfo(
746746
return ReturnValue(binarySizes.data(), binarySizes.size());
747747
}
748748
case UR_PROGRAM_INFO_BINARIES: {
749-
// The caller sets "ParamValue" to an array of pointers, one for each
750-
// device.
751-
uint8_t **PBinary = nullptr;
752-
if (ProgramInfo) {
753-
PBinary = ur_cast<uint8_t **>(ProgramInfo);
754-
if (!PBinary[0]) {
755-
break;
756-
}
757-
}
758749
std::shared_lock<ur_shared_mutex> Guard(Program->Mutex);
759-
uint8_t *NativeBinaryPtr = nullptr;
760-
if (PBinary) {
761-
NativeBinaryPtr = PBinary[0];
750+
size_t NumDevices = Program->AssociatedDevices.size();
751+
if (PropSizeRet) {
752+
// Return the size of the array of pointers to binaries (for each device).
753+
*PropSizeRet = NumDevices * sizeof(uint8_t *);
762754
}
763755

764-
size_t SzBinary = 0;
756+
// If the caller did not provide an array of pointers to copy binaries into, return early.
757+
if (!ProgramInfo)
758+
break;
759+
760+
// If the caller provided an array of pointers, copy the binaries.
761+
uint8_t **DestBinPtrs = ur_cast<uint8_t **>(ProgramInfo);
765762
for (uint32_t deviceIndex = 0;
766-
deviceIndex < Program->AssociatedDevices.size(); deviceIndex++) {
763+
deviceIndex < NumDevices; deviceIndex++) {
764+
uint8_t *DestBinPtr = DestBinPtrs[deviceIndex];
765+
if (!DestBinPtr)
766+
continue;
767+
767768
auto ZeDevice = Program->AssociatedDevices[deviceIndex]->ZeDevice;
768769
auto State = Program->getState(ZeDevice);
769770
if (State == ur_program_handle_t_::Native) {
770-
// If Program was created from Native code then return that code.
771-
if (PBinary) {
772-
std::memcpy(PBinary[deviceIndex], Program->getCode(ZeDevice),
771+
// If Program was created from Native code then return that code.
772+
std::memcpy(DestBinPtr, Program->getCode(ZeDevice),
773773
Program->getCodeSize(ZeDevice));
774-
}
775-
SzBinary += Program->getCodeSize(ZeDevice);
776-
continue;
777-
}
778-
if (State == ur_program_handle_t_::IL ||
779-
State == ur_program_handle_t_::Object) {
780-
// We don't have a binary for this device, so don't update the output
781-
// pointer to the binary, only set return size to 0.
782-
if (PropSizeRet)
783-
*PropSizeRet = 0;
784774
} else if (State == ur_program_handle_t_::Exe) {
785775
auto ZeModule = Program->getZeModuleHandle(ZeDevice);
786776
if (!ZeModule) {
787777
return UR_RESULT_ERROR_INVALID_PROGRAM;
788778
}
789-
size_t binarySize = 0;
790-
if (PBinary) {
791-
NativeBinaryPtr = PBinary[deviceIndex];
792-
}
793-
// If the caller is using a Program which is a built binary, then
794-
// the program returned will either be a single module if this is a
795-
// native binary or the native binary for each device will be returned.
779+
size_t DummySize;
796780
ZE2UR_CALL(zeModuleGetNativeBinary,
797-
(ZeModule, &binarySize, NativeBinaryPtr));
798-
SzBinary += binarySize;
799-
} else {
800-
return UR_RESULT_ERROR_INVALID_PROGRAM;
781+
(ZeModule, &DummySize, DestBinPtr));
801782
}
802783
}
803-
if (PropSizeRet)
804-
*PropSizeRet = SzBinary;
805784
break;
806785
}
807786
case UR_PROGRAM_INFO_NUM_KERNELS: {

0 commit comments

Comments
 (0)