From cc0c98e974babc05d7748e5b4e3b714c592d3eef Mon Sep 17 00:00:00 2001 From: Kacper Nowak Date: Mon, 14 Mar 2022 18:01:10 +0000 Subject: [PATCH] Enable ocloc disasm for .gen files This commit allows to use "disasm" ocloc option to be used with .gen files. Signed-off-by: Kacper Nowak --- .../decoder/decoder_tests.cpp | 52 +++++++++---------- .../decoder/mock/mock_decoder.h | 4 +- .../source/decoder/binary_decoder.cpp | 19 +++++-- 3 files changed, 43 insertions(+), 32 deletions(-) diff --git a/opencl/test/unit_test/offline_compiler/decoder/decoder_tests.cpp b/opencl/test/unit_test/offline_compiler/decoder/decoder_tests.cpp index c7d80ec3d0..362925b467 100644 --- a/opencl/test/unit_test/offline_compiler/decoder/decoder_tests.cpp +++ b/opencl/test/unit_test/offline_compiler/decoder/decoder_tests.cpp @@ -6,7 +6,9 @@ */ #include "shared/source/helpers/array_count.h" +#include "shared/test/common/helpers/test_files.h" +#include "opencl/test/unit_test/offline_compiler/mock/mock_argument_helper.h" #include "opencl/test/unit_test/test_files/patch_list.h" #include "gtest/gtest.h" @@ -43,32 +45,6 @@ TEST(DecoderTests, WhenParsingValidListOfParametersThenReturnValueIsZero) { EXPECT_EQ(0, decoder.validateInput(args)); } -TEST(DecoderTests, WhenMissingParametersThenValidateInputReturnsErrorCode) { - std::vector args = { - "ocloc", - "decoder", - "-patch", - "test_files"}; - - MockDecoder decoder; - EXPECT_NE(0, decoder.validateInput(args)); -} - -TEST(DecoderTests, GivenWrongParametersWhenParsingParametersThenValidateInputReturnsErrorCode) { - std::vector args = { - "cloc", - "decoder", - "-file", - "test_files/no_extension", - "-patch", - "test_files", - "-dump", - "test_files/created"}; - - MockDecoder decoder; - EXPECT_NE(0, decoder.validateInput(args)); -} - TEST(DecoderTests, GivenValidSizeStringWhenGettingSizeThenProperOutcomeIsExpectedAndExceptionIsNotThrown) { MockDecoder decoder; EXPECT_EQ(static_cast(1), decoder.getSize("uint8_t")); @@ -301,4 +277,28 @@ TEST(DecoderTests, GivenValidBinaryWhenProcessingBinaryThenProgramAndKernelAndPa EXPECT_TRUE(decoder.getMockIga()->disasmWasCalled); EXPECT_FALSE(decoder.getMockIga()->asmWasCalled); } + +TEST(DecoderTests, givenNonPatchtokensBinaryFormatWhenTryingToGetDevBinaryFormatThenDoNotReturnRawData) { + MockDecoder decoder; + std::map files; + auto mockArgHelper = std::make_unique(files); + decoder.argHelper = mockArgHelper.get(); + files["mockgen.gen"] = "NOTMAGIC\n\n\n\n\n\n\n"; + decoder.binaryFile = "mockgen.gen"; + auto data = decoder.getDevBinary(); + EXPECT_EQ(nullptr, data); +} + +TEST(DecoderTests, givenPatchtokensBinaryFormatWhenTryingToGetDevBinaryThenRawDataIsReturned) { + MockDecoder decoder; + std::map files; + auto mockArgHelper = std::make_unique(files); + decoder.argHelper = mockArgHelper.get(); + size_t dataSize = 11u; + files["mockgen.gen"] = "CTNI\n\n\n\n\n\n\n"; + decoder.binaryFile = "mockgen.gen"; + auto data = decoder.getDevBinary(); + std::string dataString(static_cast(data), dataSize); + EXPECT_STREQ("CTNI\n\n\n\n\n\n\n", dataString.c_str()); +} } // namespace NEO diff --git a/opencl/test/unit_test/offline_compiler/decoder/mock/mock_decoder.h b/opencl/test/unit_test/offline_compiler/decoder/mock/mock_decoder.h index 3dae749f2f..e2505d2cee 100644 --- a/opencl/test/unit_test/offline_compiler/decoder/mock/mock_decoder.h +++ b/opencl/test/unit_test/offline_compiler/decoder/mock/mock_decoder.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2020 Intel Corporation + * Copyright (C) 2018-2022 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -20,8 +20,10 @@ struct MockDecoder : public BinaryDecoder { argHelper = oclocArgHelperWithoutInput.get(); argHelper->getPrinterRef() = MessagePrinter(true); }; + using BinaryDecoder::argHelper; using BinaryDecoder::binaryFile; using BinaryDecoder::decode; + using BinaryDecoder::getDevBinary; using BinaryDecoder::getSize; using BinaryDecoder::iga; using BinaryDecoder::kernelHeader; diff --git a/shared/offline_compiler/source/decoder/binary_decoder.cpp b/shared/offline_compiler/source/decoder/binary_decoder.cpp index b6e1153a02..776e10340e 100644 --- a/shared/offline_compiler/source/decoder/binary_decoder.cpp +++ b/shared/offline_compiler/source/decoder/binary_decoder.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2021 Intel Corporation + * Copyright (C) 2018-2022 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -9,6 +9,7 @@ #include "shared/offline_compiler/source/decoder/helper.h" #include "shared/offline_compiler/source/offline_compiler.h" +#include "shared/source/device_binary_format/device_binary_formats.h" #include "shared/source/device_binary_format/elf/elf_decoder.h" #include "shared/source/device_binary_format/elf/ocl_elf.h" #include "shared/source/helpers/file_io.h" @@ -78,9 +79,21 @@ void BinaryDecoder::dumpField(const void *&binaryPtr, const PTField &field, std: binaryPtr = ptrOffset(binaryPtr, field.size); } +template +bool isPatchtokensBinary(const ContainerT &data) { + static constexpr NEO::ConstStringRef intcMagic = "CTNI"; + auto binaryMagicLen = std::min(intcMagic.size(), data.size()); + NEO::ConstStringRef binaryMagic(reinterpret_cast(&*data.begin()), binaryMagicLen); + return intcMagic == binaryMagic; +} + const void *BinaryDecoder::getDevBinary() { binary = argHelper->readBinaryFile(binaryFile); const void *data = nullptr; + if (isPatchtokensBinary(binary)) { + return binary.data(); + } + std::string decoderErrors; std::string decoderWarnings; auto input = ArrayRef(reinterpret_cast(binary.data()), binary.size()); @@ -529,10 +542,6 @@ int BinaryDecoder::validateInput(const std::vector &args) { return -1; } } - if (binaryFile.find(".bin") == std::string::npos) { - argHelper->printf(".bin extension is expected for binary file.\n"); - return -1; - } if (false == iga->isKnownPlatform()) { argHelper->printf("Warning : missing or invalid -device parameter - results may be inacurate\n"); }