From 2ae3f3e5213aec062f9459e1e182365c756ee3e4 Mon Sep 17 00:00:00 2001 From: Krystian Chmielewski Date: Thu, 31 Mar 2022 16:30:16 +0000 Subject: [PATCH] Allow for zebin rebuild when IR is present Signed-off-by: Krystian Chmielewski --- opencl/source/program/build.cpp | 10 ++++--- .../device_binary_format_zebin.cpp | 26 ++++++++++++++----- .../device_binary_format_zebin_tests.cpp | 23 ++++++++++++++++ 3 files changed, 49 insertions(+), 10 deletions(-) diff --git a/opencl/source/program/build.cpp b/opencl/source/program/build.cpp index 6580a2a68c..3bd273236b 100644 --- a/opencl/source/program/build.cpp +++ b/opencl/source/program/build.cpp @@ -64,10 +64,12 @@ cl_int Program::build( deviceBuildInfos[device].buildStatus = CL_BUILD_IN_PROGRESS; } - if (nullptr != buildOptions) { - options = buildOptions; - } else if (this->createdFrom != CreatedFrom::BINARY) { - options = ""; + if (this->createdFrom != CreatedFrom::BINARY) { + if (nullptr != buildOptions) { + options = buildOptions; + } else { + options = ""; + } } const bool shouldSuppressRebuildWarning{CompilerOptions::extract(CompilerOptions::noRecompiledFromIr, options)}; diff --git a/shared/source/device_binary_format/device_binary_format_zebin.cpp b/shared/source/device_binary_format/device_binary_format_zebin.cpp index df8501b842..deb95af9ee 100644 --- a/shared/source/device_binary_format/device_binary_format_zebin.cpp +++ b/shared/source/device_binary_format/device_binary_format_zebin.cpp @@ -1,10 +1,11 @@ /* - * Copyright (C) 2020-2021 Intel Corporation + * Copyright (C) 2020-2022 Intel Corporation * * SPDX-License-Identifier: MIT * */ +#include "shared/source/compiler_interface/compiler_options/compiler_options.h" #include "shared/source/compiler_interface/intermediate_representations.h" #include "shared/source/device_binary_format/device_binary_formats.h" #include "shared/source/device_binary_format/elf/elf_decoder.h" @@ -46,6 +47,13 @@ SingleDeviceBinary unpackSingleDeviceBinary(cons break; } + ArrayRef intermediateRepresentation; + for (auto &elfSH : elf.sectionHeaders) { + if (elfSH.header->type == Elf::SHT_ZEBIN_SPIRV) { + intermediateRepresentation = elfSH.data; + } + } + bool validForTarget = true; if (elf.elfFileHeader->machine == Elf::ELF_MACHINE::EM_INTELGT) { validForTarget &= validateTargetDevice(elf, requestedTargetDevice); @@ -58,16 +66,22 @@ SingleDeviceBinary unpackSingleDeviceBinary(cons validForTarget &= (8U == requestedTargetDevice.maxPointerSizeInBytes); } - if (false == validForTarget) { - outErrReason = "Unhandled target device"; - return {}; - } - SingleDeviceBinary ret; ret.deviceBinary = archive; ret.format = NEO::DeviceBinaryFormat::Zebin; ret.targetDevice = requestedTargetDevice; + if (false == validForTarget) { + if (false == intermediateRepresentation.empty()) { + ret.intermediateRepresentation = intermediateRepresentation; + ret.buildOptions = NEO::CompilerOptions::allowZebin; + ret.deviceBinary = {}; + } else { + outErrReason = "Unhandled target device"; + return {}; + } + } + return ret; } diff --git a/shared/test/unit_test/device_binary_format/device_binary_format_zebin_tests.cpp b/shared/test/unit_test/device_binary_format/device_binary_format_zebin_tests.cpp index 2d5b5b5cb0..539db400d0 100644 --- a/shared/test/unit_test/device_binary_format/device_binary_format_zebin_tests.cpp +++ b/shared/test/unit_test/device_binary_format/device_binary_format_zebin_tests.cpp @@ -358,3 +358,26 @@ TEST(UnpackSingleDeviceBinaryZebin, WhenMachineIsIntelGTAndIntelGTNoteSectionIsV EXPECT_TRUE(unpackWarnings.empty()); EXPECT_TRUE(unpackErrors.empty()); } + +TEST(UnpackSingleDeviceBinaryZebin, WhenZebinIsNotValidForTargetAndHasSPIRVThenSetIRAndBuildOptions) { + ZebinTestData::ValidEmptyProgram zebin; + const uint8_t spirvData[30] = {0xd}; + zebin.appendSection(NEO::Elf::SHT_ZEBIN_SPIRV, NEO::Elf::SectionsNamesZebin::spv, spirvData); + + zebin.elfHeader->type = NEO::Elf::ET_ZEBIN_EXE; + zebin.elfHeader->machine = IGFX_UNKNOWN; + + NEO::TargetDevice targetDevice; + targetDevice.productFamily = IGFX_SKYLAKE; + targetDevice.maxPointerSizeInBytes = 8; + + std::string unpackErrors; + std::string unpackWarnings; + auto unpackResult = NEO::unpackSingleDeviceBinary(zebin.storage, "", targetDevice, unpackErrors, unpackWarnings); + EXPECT_EQ(NEO::DeviceBinaryFormat::Zebin, unpackResult.format); + EXPECT_TRUE(unpackResult.deviceBinary.empty()); + + EXPECT_FALSE(unpackResult.intermediateRepresentation.empty()); + EXPECT_EQ(0, memcmp(spirvData, unpackResult.intermediateRepresentation.begin(), sizeof(spirvData))); + EXPECT_STREQ(NEO::CompilerOptions::allowZebin.begin(), unpackResult.buildOptions.begin()); +}