From 311b0b00201978850ccb79b9888e14fc68ebd547 Mon Sep 17 00:00:00 2001 From: Krystian Chmielewski Date: Wed, 21 Sep 2022 17:34:18 +0000 Subject: [PATCH] Create input for linker during zebin decoding Remove code duplication. Parsing zebin elf for relocations and symbols is moved to decodeSingleDeviceBinary. Signed-off-by: Krystian Chmielewski --- level_zero/core/source/module/module_imp.cpp | 12 ------ .../source/program/process_device_binary.cpp | 12 ------ shared/offline_compiler/source/CMakeLists.txt | 7 ++-- .../source/ocloc_validator.cpp | 10 ++--- .../device_binary_format_zebin.cpp | 37 +++++++++++++------ .../device_binary_format/zebin_decoder.cpp | 23 ++++++------ .../device_binary_format/zebin_decoder.h | 2 + shared/source/program/program_info.h | 1 - 8 files changed, 49 insertions(+), 55 deletions(-) diff --git a/level_zero/core/source/module/module_imp.cpp b/level_zero/core/source/module/module_imp.cpp index b379015d28..6f2463168d 100644 --- a/level_zero/core/source/module/module_imp.cpp +++ b/level_zero/core/source/module/module_imp.cpp @@ -396,18 +396,6 @@ bool ModuleTranslationUnit::processUnpackedBinary() { return false; } - if (programInfo.decodedElf.elfFileHeader) { - NEO::LinkerInput::SectionNameToSegmentIdMap nameToKernelId; - - uint32_t id = 0; - for (auto &kernelInfo : this->programInfo.kernelInfos) { - nameToKernelId[kernelInfo->kernelDescriptor.kernelMetadata.kernelName] = id; - id++; - } - programInfo.prepareLinkerInputStorage(); - programInfo.linkerInput->decodeElfSymbolTableAndRelocations(programInfo.decodedElf, nameToKernelId); - } - processDebugData(); size_t slmNeeded = NEO::getMaxInlineSlmNeeded(programInfo); diff --git a/opencl/source/program/process_device_binary.cpp b/opencl/source/program/process_device_binary.cpp index 27e1cdeec8..9d2176eafd 100644 --- a/opencl/source/program/process_device_binary.cpp +++ b/opencl/source/program/process_device_binary.cpp @@ -186,18 +186,6 @@ cl_int Program::processGenBinary(const ClDevice &clDevice) { DeviceBinaryFormat singleDeviceBinaryFormat; std::tie(decodeError, singleDeviceBinaryFormat) = NEO::decodeSingleDeviceBinary(programInfo, binary, decodeErrors, decodeWarnings); - if (isDeviceBinaryFormat(binary.deviceBinary)) { - NEO::LinkerInput::SectionNameToSegmentIdMap nameToKernelId; - - uint32_t id = 0; - for (auto &kernelInfo : programInfo.kernelInfos) { - nameToKernelId[kernelInfo->kernelDescriptor.kernelMetadata.kernelName] = id; - id++; - } - programInfo.prepareLinkerInputStorage(); - programInfo.linkerInput->decodeElfSymbolTableAndRelocations(programInfo.decodedElf, nameToKernelId); - } - if (decodeWarnings.empty() == false) { PRINT_DEBUG_STRING(DebugManager.flags.PrintDebugMessages.get(), stderr, "%s\n", decodeWarnings.c_str()); } diff --git a/shared/offline_compiler/source/CMakeLists.txt b/shared/offline_compiler/source/CMakeLists.txt index 6f5612cec8..418362e3b2 100644 --- a/shared/offline_compiler/source/CMakeLists.txt +++ b/shared/offline_compiler/source/CMakeLists.txt @@ -26,6 +26,10 @@ set(CLOC_LIB_SRCS_LIB ${NEO_SHARED_DIRECTORY}/device_binary_format/elf/elf_encoder.cpp ${NEO_SHARED_DIRECTORY}/device_binary_format/elf/elf_encoder.h ${NEO_SHARED_DIRECTORY}/device_binary_format/elf/ocl_elf.h + ${NEO_SHARED_DIRECTORY}/device_binary_format/device_binary_formats.h + ${NEO_SHARED_DIRECTORY}/device_binary_format/yaml/yaml_parser.cpp + ${NEO_SHARED_DIRECTORY}/device_binary_format/zebin_decoder.cpp + ${NEO_SHARED_DIRECTORY}/device_binary_format/zebin_decoder.h ${NEO_SHARED_DIRECTORY}/dll/devices${BRANCH_DIR_SUFFIX}devices.inl ${NEO_SHARED_DIRECTORY}/dll/devices${BRANCH_DIR_SUFFIX}devices_additional.inl ${NEO_SHARED_DIRECTORY}/dll/devices/devices_base.inl @@ -88,9 +92,6 @@ set(CLOC_LIB_SRCS_LIB ${OCLOC_DIRECTORY}/source/queries.h ${OCLOC_DIRECTORY}/source/utilities/get_git_version_info.h ${OCLOC_DIRECTORY}/source/utilities/get_git_version_info.cpp - ${NEO_SOURCE_DIR}/shared/source/device_binary_format/device_binary_format_zebin.cpp - ${NEO_SOURCE_DIR}/shared/source/device_binary_format/zebin_decoder.cpp - ${NEO_SOURCE_DIR}/shared/source/device_binary_format/yaml/yaml_parser.cpp ${NEO_SOURCE_DIR}/third_party${BRANCH_DIR_SUFFIX}aot_config_headers/platforms.h ) diff --git a/shared/offline_compiler/source/ocloc_validator.cpp b/shared/offline_compiler/source/ocloc_validator.cpp index 2e45ab436b..328d91843e 100644 --- a/shared/offline_compiler/source/ocloc_validator.cpp +++ b/shared/offline_compiler/source/ocloc_validator.cpp @@ -8,7 +8,7 @@ #include "shared/offline_compiler/source/ocloc_validator.h" #include "shared/offline_compiler/source/ocloc_arg_helper.h" -#include "shared/source/device_binary_format/device_binary_formats.h" +#include "shared/source/device_binary_format/zebin_decoder.h" #include "shared/source/program/kernel_info.h" #include "shared/source/program/program_info.h" @@ -28,7 +28,6 @@ namespace Ocloc { int validate(const std::vector &args, OclocArgHelper *argHelper) { NEO::ProgramInfo programInfo; - NEO::SingleDeviceBinary deviceBinary; std::string errors; std::string warnings; UNRECOVERABLE_IF(nullptr == argHelper) @@ -51,13 +50,14 @@ int validate(const std::vector &args, OclocArgHelper *argHelper) { auto fileData = argHelper->readBinaryFile(fileName); argHelper->printf("Validating : %s (%zd bytes).\n", fileName.c_str(), fileData.size()); - deviceBinary.deviceBinary = deviceBinary.deviceBinary.fromAny(fileData.data(), fileData.size()); - if (false == NEO::isDeviceBinaryFormat(deviceBinary.deviceBinary)) { + auto deviceBinary = ArrayRef::fromAny(fileData.data(), fileData.size()); + if (false == NEO::isDeviceBinaryFormat(deviceBinary)) { argHelper->printf("Input is not a Zebin file (not elf or wrong elf object file type)\n"); return -2; } - auto decodeResult = NEO::decodeSingleDeviceBinary(programInfo, deviceBinary, errors, warnings); + auto elf = NEO::Elf::decodeElf(deviceBinary, errors, warnings); + auto decodeResult = NEO::decodeZebin(programInfo, elf, errors, warnings); if (false == warnings.empty()) { argHelper->printf("Validator detected potential problems :\n%s\n", warnings.c_str()); } 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 b6335c706c..964e249ea9 100644 --- a/shared/source/device_binary_format/device_binary_format_zebin.cpp +++ b/shared/source/device_binary_format/device_binary_format_zebin.cpp @@ -11,23 +11,13 @@ #include "shared/source/device_binary_format/elf/elf_encoder.h" #include "shared/source/device_binary_format/elf/zebin_elf.h" #include "shared/source/device_binary_format/zebin_decoder.h" +#include "shared/source/program/kernel_info.h" #include "shared/source/program/program_info.h" #include namespace NEO { -template <> -bool isDeviceBinaryFormat(const ArrayRef binary) { - auto header = Elf::decodeElfFileHeader(binary); - if (nullptr == header) { - return false; - } - - return header->type == NEO::Elf::ET_REL || - header->type == NEO::Elf::ET_ZEBIN_EXE; -} - template <> SingleDeviceBinary unpackSingleDeviceBinary(const ArrayRef archive, const ConstStringRef requestedProductAbbreviation, const TargetDevice &requestedTargetDevice, std::string &outErrReason, std::string &outWarning) { @@ -86,4 +76,29 @@ SingleDeviceBinary unpackSingleDeviceBinary(cons return ret; } +void prepareLinkerInputForZebin(ProgramInfo &programInfo, Elf::Elf &elf) { + programInfo.prepareLinkerInputStorage(); + + LinkerInput::SectionNameToSegmentIdMap nameToKernelId; + for (uint32_t id = 0; id < static_cast(programInfo.kernelInfos.size()); id++) { + nameToKernelId[programInfo.kernelInfos[id]->kernelDescriptor.kernelMetadata.kernelName] = id; + } + programInfo.linkerInput->decodeElfSymbolTableAndRelocations(elf, nameToKernelId); +} + +template <> +DecodeError decodeSingleDeviceBinary(ProgramInfo &dst, const SingleDeviceBinary &src, std::string &outErrReason, std::string &outWarning) { + auto elf = Elf::decodeElf(src.deviceBinary, outErrReason, outWarning); + if (nullptr == elf.elfFileHeader) { + return DecodeError::InvalidBinary; + } + + dst.grfSize = src.targetDevice.grfSize; + dst.minScratchSpaceSize = src.targetDevice.minScratchSpaceSize; + auto decodeError = decodeZebin(dst, elf, outErrReason, outWarning); + prepareLinkerInputForZebin(dst, elf); + + return decodeError; +} + } // namespace NEO diff --git a/shared/source/device_binary_format/zebin_decoder.cpp b/shared/source/device_binary_format/zebin_decoder.cpp index c9e1939e4a..00ad5b0747 100644 --- a/shared/source/device_binary_format/zebin_decoder.cpp +++ b/shared/source/device_binary_format/zebin_decoder.cpp @@ -26,6 +26,17 @@ namespace NEO { +template <> +bool isDeviceBinaryFormat(const ArrayRef binary) { + auto header = Elf::decodeElfFileHeader(binary); + if (nullptr == header) { + return false; + } + + return header->type == NEO::Elf::ET_REL || + header->type == NEO::Elf::ET_ZEBIN_EXE; +} + bool validateTargetDevice(const Elf::Elf &elf, const TargetDevice &targetDevice, std::string &outErrReason, std::string &outWarning) { GFXCORE_FAMILY gfxCore = IGFX_UNKNOWN_CORE; PRODUCT_FAMILY productFamily = IGFX_UNKNOWN; @@ -1427,13 +1438,7 @@ NEO::DecodeError populateKernelSourceAttributes(NEO::KernelDescriptor &dst, NEO: return DecodeError::Success; } -template <> -DecodeError decodeSingleDeviceBinary(ProgramInfo &dst, const SingleDeviceBinary &src, std::string &outErrReason, std::string &outWarning) { - auto elf = Elf::decodeElf(src.deviceBinary, outErrReason, outWarning); - if (nullptr == elf.elfFileHeader) { - return DecodeError::InvalidBinary; - } - +NEO::DecodeError decodeZebin(ProgramInfo &dst, NEO::Elf::Elf &elf, std::string &outErrReason, std::string &outWarning) { ZebinSections zebinSections; auto extractError = extractZebinSections(elf, zebinSections, outErrReason, outWarning); if (DecodeError::Success != extractError) { @@ -1445,10 +1450,6 @@ DecodeError decodeSingleDeviceBinary(ProgramInfo return extractError; } - dst.decodedElf = elf; - dst.grfSize = src.targetDevice.grfSize; - dst.minScratchSpaceSize = src.targetDevice.minScratchSpaceSize; - if (false == zebinSections.globalDataSections.empty()) { dst.globalVariables.initData = zebinSections.globalDataSections[0]->data.begin(); dst.globalVariables.size = zebinSections.globalDataSections[0]->data.size(); diff --git a/shared/source/device_binary_format/zebin_decoder.h b/shared/source/device_binary_format/zebin_decoder.h index 07482f1aa4..6ef900c85d 100644 --- a/shared/source/device_binary_format/zebin_decoder.h +++ b/shared/source/device_binary_format/zebin_decoder.h @@ -121,4 +121,6 @@ NEO::DecodeError populateZeInfoVersion(NEO::Elf::ZebinKernelMetadata::Types::Ver NEO::DecodeError populateExternalFunctionsMetadata(NEO::ProgramInfo &dst, NEO::Yaml::YamlParser &yamlParser, const NEO::Yaml::Node &functionNd, std::string &outErrReason, std::string &outWarning); NEO::DecodeError populateKernelSourceAttributes(NEO::KernelDescriptor &dst, NEO::Elf::ZebinKernelMetadata::Types::Kernel::Attributes::AttributesBaseT &attributes); + +NEO::DecodeError decodeZebin(ProgramInfo &dst, NEO::Elf::Elf &elf, std::string &outErrReason, std::string &outWarning); } // namespace NEO diff --git a/shared/source/program/program_info.h b/shared/source/program/program_info.h index da23c2f8c1..88eb444fa8 100644 --- a/shared/source/program/program_info.h +++ b/shared/source/program/program_info.h @@ -45,7 +45,6 @@ struct ProgramInfo { std::vector externalFunctions; std::vector kernelInfos; - Elf::Elf decodedElf; uint32_t grfSize = 32U; uint32_t minScratchSpaceSize = 0U; };