diff --git a/offline_compiler/decoder/binary_decoder.cpp b/offline_compiler/decoder/binary_decoder.cpp index c1fb26a572..0f3e29c861 100644 --- a/offline_compiler/decoder/binary_decoder.cpp +++ b/offline_compiler/decoder/binary_decoder.cpp @@ -16,6 +16,10 @@ #include #include +void BinaryDecoder::setMessagePrinter(const MessagePrinter &messagePrinter) { + this->messagePrinter = messagePrinter; +} + template T readUnaligned(void *ptr) { T retVal = 0; @@ -32,7 +36,7 @@ int BinaryDecoder::decode() { std::ofstream ptmFile(pathToDump + "PTM.txt"); auto devBinPtr = getDevBinary(); if (devBinPtr == nullptr) { - printf("Error! Device Binary section was not found.\n"); + messagePrinter.printf("Error! Device Binary section was not found.\n"); exit(1); } return processBinary(devBinPtr, ptmFile); @@ -62,7 +66,7 @@ void BinaryDecoder::dumpField(void *&binaryPtr, const PTField &field, std::ostre break; } default: - printf("Error! Unknown size.\n"); + messagePrinter.printf("Error! Unknown size.\n"); exit(1); } binaryPtr = ptrOffset(binaryPtr, field.size); @@ -110,7 +114,7 @@ uint8_t BinaryDecoder::getSize(const std::string &typeStr) { } else if (typeStr == "uint64_t") { return 8; } else { - printf("Unhandled type : %s\n", typeStr.c_str()); + messagePrinter.printf("Unhandled type : %s\n", typeStr.c_str()); exit(1); } } @@ -128,22 +132,22 @@ void BinaryDecoder::parseTokens() { size_t pos = findPos(patchList, "struct SProgramBinaryHeader"); if (pos == patchList.size()) { - printf("While parsing patchtoken definitions: couldn't find SProgramBinaryHeader."); + messagePrinter.printf("While parsing patchtoken definitions: couldn't find SProgramBinaryHeader."); exit(1); } pos = findPos(patchList, "enum PATCH_TOKEN"); if (pos == patchList.size()) { - printf("While parsing patchtoken definitions: couldn't find enum PATCH_TOKEN."); + messagePrinter.printf("While parsing patchtoken definitions: couldn't find enum PATCH_TOKEN."); exit(1); } pos = findPos(patchList, "struct SKernelBinaryHeader"); if (pos == patchList.size()) { - printf("While parsing patchtoken definitions: couldn't find SKernelBinaryHeader."); + messagePrinter.printf("While parsing patchtoken definitions: couldn't find SKernelBinaryHeader."); exit(1); } pos = findPos(patchList, "struct SKernelBinaryHeaderCommon :"); if (pos == patchList.size()) { - printf("While parsing patchtoken definitions: couldn't find SKernelBinaryHeaderCommon."); + messagePrinter.printf("While parsing patchtoken definitions: couldn't find SKernelBinaryHeaderCommon."); exit(1); } @@ -200,8 +204,8 @@ void BinaryDecoder::parseTokens() { } void BinaryDecoder::printHelp() { - printf("Usage:\n-file -patch -dump \n"); - printf("e.g. -file C:/my_folder/my_binary.bin -patch C:/igc/inc -dump C:/my_folder/dump\n"); + messagePrinter.printf("Usage:\n-file -patch -dump \n"); + messagePrinter.printf("e.g. -file C:/my_folder/my_binary.bin -patch C:/igc/inc -dump C:/my_folder/dump\n"); } int BinaryDecoder::processBinary(void *&ptr, std::ostream &ptmFile) { @@ -216,10 +220,10 @@ int BinaryDecoder::processBinary(void *&ptr, std::ostream &ptmFile) { dumpField(ptr, v, ptmFile); } if (patchListSize == 0) { - printf("Warning! Program's patch list size is 0.\n"); + messagePrinter.printf("Warning! Program's patch list size is 0.\n"); } if (numberOfKernels == 0) { - printf("Warning! Number of Kernels is 0.\n"); + messagePrinter.printf("Warning! Number of Kernels is 0.\n"); } readPatchTokens(ptr, patchListSize, ptmFile); @@ -254,7 +258,7 @@ void BinaryDecoder::processKernel(void *&ptr, std::ostream &ptmFile) { } if (KernelNameSize == 0) { - printf("Error! KernelNameSize was 0.\n"); + messagePrinter.printf("Error! KernelNameSize was 0.\n"); exit(1); } @@ -268,7 +272,7 @@ void BinaryDecoder::processKernel(void *&ptr, std::ostream &ptmFile) { ptr = ptrOffset(ptr, KernelHeapSize); if (GeneralStateHeapSize != 0) { - printf("Warning! GeneralStateHeapSize wasn't 0.\n"); + messagePrinter.printf("Warning! GeneralStateHeapSize wasn't 0.\n"); fileName = pathToDump + kernelName + "_GeneralStateHeap.bin"; writeDataToFile(fileName.c_str(), ptr, DynamicStateHeapSize); ptr = ptrOffset(ptr, GeneralStateHeapSize); @@ -283,7 +287,7 @@ void BinaryDecoder::processKernel(void *&ptr, std::ostream &ptmFile) { ptr = ptrOffset(ptr, SurfaceStateHeapSize); if (KernelPatchListSize == 0) { - printf("Warning! Kernel's patch list size was 0.\n"); + messagePrinter.printf("Warning! Kernel's patch list size was 0.\n"); } readPatchTokens(ptr, KernelPatchListSize, ptmFile); } @@ -379,21 +383,21 @@ int BinaryDecoder::validateInput(uint32_t argc, const char **argv) { pathToDump = std::string(argv[++i]); addSlash(pathToDump); } else { - printf("Unknown argument %s\n", argv[i]); + messagePrinter.printf("Unknown argument %s\n", argv[i]); printHelp(); return -1; } } if (binaryFile.find(".bin") == std::string::npos) { - printf(".bin extension is expected for binary file.\n"); + messagePrinter.printf(".bin extension is expected for binary file.\n"); printHelp(); return -1; } else if (pathToPatch.empty()) { - printf("Path to patch list folder can't be empty.\n"); + messagePrinter.printf("Path to patch list folder can't be empty.\n"); printHelp(); return -1; } else if (pathToDump.empty()) { - printf("Path to dump folder can't be empty.\n"); + messagePrinter.printf("Path to dump folder can't be empty.\n"); printHelp(); return -1; } diff --git a/offline_compiler/decoder/binary_decoder.h b/offline_compiler/decoder/binary_decoder.h index 8948a2a3e9..6e4ee7fa20 100644 --- a/offline_compiler/decoder/binary_decoder.h +++ b/offline_compiler/decoder/binary_decoder.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018 Intel Corporation + * Copyright (C) 2018-2019 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -8,6 +8,8 @@ #pragma once #include "elf/types.h" +#include "helper.h" + #include #include #include @@ -36,11 +38,15 @@ class BinaryDecoder { int decode(); int validateInput(uint32_t argc, const char **argv); + void setMessagePrinter(const MessagePrinter &messagePrinter); + protected: BinaryHeader programHeader, kernelHeader; CLElfLib::ElfBinaryStorage binary; PTMap patchTokens; std::string binaryFile, pathToPatch, pathToDump; + MessagePrinter messagePrinter; + void dumpField(void *&binaryPtr, const PTField &field, std::ostream &ptmFile); uint8_t getSize(const std::string &typeStr); void *getDevBinary(); diff --git a/offline_compiler/decoder/binary_encoder.cpp b/offline_compiler/decoder/binary_encoder.cpp index 168cfafe34..8fd8a50943 100644 --- a/offline_compiler/decoder/binary_encoder.cpp +++ b/offline_compiler/decoder/binary_encoder.cpp @@ -17,6 +17,10 @@ #include #include +void BinaryEncoder::setMessagePrinter(const MessagePrinter &messagePrinter) { + this->messagePrinter = messagePrinter; +} + void BinaryEncoder::calculatePatchListSizes(std::vector &ptmFile) { size_t patchListPos = 0; for (size_t i = 0; i < ptmFile.size(); ++i) { @@ -37,7 +41,7 @@ void BinaryEncoder::calculatePatchListSizes(std::vector &ptmFile) { } uint32_t size = static_cast(std::stoul(ptmFile[patchListPos].substr(ptmFile[patchListPos].find_last_of(' ') + 1))); if (size != calcSize) { - printf("Warning! Calculated PatchListSize ( %u ) differs from file ( %u ) - changing it. Line %d\n", calcSize, size, static_cast(patchListPos + 1)); + messagePrinter.printf("Warning! Calculated PatchListSize ( %u ) differs from file ( %u ) - changing it. Line %d\n", calcSize, size, static_cast(patchListPos + 1)); ptmFile[patchListPos] = ptmFile[patchListPos].substr(0, ptmFile[patchListPos].find_last_of(' ') + 1); ptmFile[patchListPos] += std::to_string(calcSize); } @@ -48,7 +52,7 @@ void BinaryEncoder::calculatePatchListSizes(std::vector &ptmFile) { int BinaryEncoder::copyBinaryToBinary(const std::string &srcFileName, std::ostream &outBinary) { std::ifstream ifs(srcFileName, std::ios::binary); if (!ifs.good()) { - printf("Cannot open %s.\n", srcFileName.c_str()); + messagePrinter.printf("Cannot open %s.\n", srcFileName.c_str()); return -1; } ifs.seekg(0, ifs.end); @@ -73,7 +77,7 @@ int BinaryEncoder::createElf() { data, static_cast(data.size()))); } else { - printf("Warning! Missing build section.\n"); + messagePrinter.printf("Warning! Missing build section.\n"); } //LLVM or SPIRV @@ -94,7 +98,7 @@ int BinaryEncoder::createElf() { data, static_cast(data.size()))); } else { - printf("Warning! Missing llvm/spirv section.\n"); + messagePrinter.printf("Warning! Missing llvm/spirv section.\n"); } //Device Binary @@ -107,7 +111,7 @@ int BinaryEncoder::createElf() { data, static_cast(data.size()))); } else { - printf("Missing device_binary.bin\n"); + messagePrinter.printf("Missing device_binary.bin\n"); return -1; } @@ -117,7 +121,7 @@ int BinaryEncoder::createElf() { std::ofstream elfFile(elfName, std::ios::binary); if (!elfFile.good()) { - printf("Couldn't create %s.\n", elfName.c_str()); + messagePrinter.printf("Couldn't create %s.\n", elfName.c_str()); return -1; } @@ -126,8 +130,8 @@ int BinaryEncoder::createElf() { } void BinaryEncoder::printHelp() { - printf("Usage:\n-dump -out \n"); - printf("e.g. -dump C:/my_folder/dump -out C:/my_folder/new_binary.bin\n"); + messagePrinter.printf("Usage:\n-dump -out \n"); + messagePrinter.printf("e.g. -dump C:/my_folder/dump -out C:/my_folder/new_binary.bin\n"); } int BinaryEncoder::encode() { @@ -137,7 +141,7 @@ int BinaryEncoder::encode() { std::ofstream deviceBinary(pathToDump + "device_binary.bin", std::ios::binary); if (!deviceBinary.good()) { - printf("Error! Couldn't create device_binary.bin.\n"); + messagePrinter.printf("Error! Couldn't create device_binary.bin.\n"); return -1; } int retVal = processBinary(ptmFile, deviceBinary); @@ -154,11 +158,11 @@ int BinaryEncoder::processBinary(const std::vector &ptmFile, std::o while (i < ptmFile.size()) { if (ptmFile[i].find("Kernel #") != std::string::npos) { if (processKernel(++i, ptmFile, deviceBinary)) { - printf("Error while processing kernel!\n"); + messagePrinter.printf("Warning while processing kernel!\n"); return -1; } } else if (writeDeviceBinary(ptmFile[i++], deviceBinary)) { - printf("Error while writing to binary!\n"); + messagePrinter.printf("Error while writing to binary!\n"); return -1; } } @@ -177,13 +181,13 @@ int BinaryEncoder::processKernel(size_t &i, const std::vector &ptmF ss >> kernelNameSize; } if (writeDeviceBinary(ptmFile[i++], deviceBinary)) { - printf("Error while writing to binary.\n"); + messagePrinter.printf("Error while writing to binary.\n"); return -1; } } //KernelName if (i == ptmFile.size()) { - printf("Couldn't find KernelName line.\n"); + messagePrinter.printf("Couldn't find KernelName line.\n"); return -1; } std::string kernelName(ptmFile[i], ptmFile[i].find(' ') + 1); @@ -197,22 +201,22 @@ int BinaryEncoder::processKernel(size_t &i, const std::vector &ptmF // Writing KernelHeap, DynamicStateHeap, SurfaceStateHeap if (fileExists(pathToDump + kernelName + "_GeneralStateHeap.bin")) { - printf("Warning! Adding GeneralStateHeap.\n"); + messagePrinter.printf("Warning! Adding GeneralStateHeap.\n"); if (copyBinaryToBinary(pathToDump + kernelName + "_GeneralStateHeap.bin", deviceBinary)) { - printf("Error! Couldn't copy %s_GeneralStateHeap.bin\n", kernelName.c_str()); + messagePrinter.printf("Couldn't copy %s_GeneralStateHeap.bin\n", kernelName.c_str()); return -1; } } if (copyBinaryToBinary(pathToDump + kernelName + "_KernelHeap.bin", deviceBinary)) { - printf("Error! Couldn't copy %s_KernelHeap.bin\n", kernelName.c_str()); + messagePrinter.printf("Couldn't copy %s_KernelHeap.bin\n", kernelName.c_str()); return -1; } if (copyBinaryToBinary(pathToDump + kernelName + "_DynamicStateHeap.bin", deviceBinary)) { - printf("Error! Couldn't copy %s_DynamicStateHeap.bin\n", kernelName.c_str()); + messagePrinter.printf("Couldn't copy %s_DynamicStateHeap.bin\n", kernelName.c_str()); return -1; } if (copyBinaryToBinary(pathToDump + kernelName + "_SurfaceStateHeap.bin", deviceBinary)) { - printf("Error! Couldn't copy %s_SurfaceStateHeap.bin\n", kernelName.c_str()); + messagePrinter.printf("Couldn't copy %s_SurfaceStateHeap.bin\n", kernelName.c_str()); return -1; } return 0; @@ -230,17 +234,17 @@ int BinaryEncoder::validateInput(uint32_t argc, const char **argv) { } else if (!strcmp(argv[i], "-out")) { elfName = std::string(argv[++i]); } else { - printf("Unknown argument %s\n", argv[i]); + messagePrinter.printf("Unknown argument %s\n", argv[i]); printHelp(); return -1; } } if (pathToDump.empty()) { - printf("Path to dump folder can't be empty.\n"); + messagePrinter.printf("Path to dump folder can't be empty.\n"); printHelp(); return -1; } else if (elfName.find(".bin") == std::string::npos) { - printf(".bin extension is expected for binary file.\n"); + messagePrinter.printf(".bin extension is expected for binary file.\n"); printHelp(); return -1; } @@ -299,7 +303,7 @@ int BinaryEncoder::writeDeviceBinary(const std::string &line, std::ostream &devi write(ss, deviceBinary); break; default: - printf("Unknown size in line: %s\n", line.c_str()); + messagePrinter.printf("Unknown size in line: %s\n", line.c_str()); return -1; } } diff --git a/offline_compiler/decoder/binary_encoder.h b/offline_compiler/decoder/binary_encoder.h index f74504fe0a..160939f3fd 100644 --- a/offline_compiler/decoder/binary_encoder.h +++ b/offline_compiler/decoder/binary_encoder.h @@ -6,6 +6,8 @@ */ #pragma once +#include "helper.h" + #include #include #include @@ -18,8 +20,12 @@ class BinaryEncoder { int encode(); int validateInput(uint32_t argc, const char **argv); + void setMessagePrinter(const MessagePrinter &messagePrinter); + protected: std::string pathToDump, elfName; + MessagePrinter messagePrinter; + void calculatePatchListSizes(std::vector &ptmFile); int copyBinaryToBinary(const std::string &srcFileName, std::ostream &outBinary); int createElf(); diff --git a/offline_compiler/decoder/helper.h b/offline_compiler/decoder/helper.h index 0a0a1756cf..bd66924342 100644 --- a/offline_compiler/decoder/helper.h +++ b/offline_compiler/decoder/helper.h @@ -17,3 +17,25 @@ std::vector readBinaryFile(const std::string &fileName); void readFileToVectorOfStrings(std::vector &lines, const std::string &fileName, bool replaceTabs = false); size_t findPos(const std::vector &lines, const std::string &whatToFind); + +class MessagePrinter { + public: + MessagePrinter() = default; + MessagePrinter(bool suppressMessages) : suppressMessages(suppressMessages) {} + + void printf(const char *message) { + if (!suppressMessages) { + ::printf("%s", message); + } + } + + template + void printf(const char *format, Args... args) { + if (!suppressMessages) { + ::printf(format, std::forward(args)...); + } + } + + private: + bool suppressMessages = false; +}; diff --git a/unit_tests/offline_compiler/decoder/mock/mock_decoder.h b/unit_tests/offline_compiler/decoder/mock/mock_decoder.h index a70afdcc40..5df35503a2 100644 --- a/unit_tests/offline_compiler/decoder/mock/mock_decoder.h +++ b/unit_tests/offline_compiler/decoder/mock/mock_decoder.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018 Intel Corporation + * Copyright (C) 2018-2019 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -9,9 +9,11 @@ #include "offline_compiler/decoder/binary_decoder.h" struct MockDecoder : public BinaryDecoder { - MockDecoder() : BinaryDecoder("", "", ""){}; + MockDecoder() : MockDecoder("", "", ""){}; MockDecoder(const std::string &file, const std::string &patch, const std::string &dump) - : BinaryDecoder(file, patch, dump){}; + : BinaryDecoder(file, patch, dump) { + setMessagePrinter(MessagePrinter{true}); + }; using BinaryDecoder::binaryFile; using BinaryDecoder::decode; using BinaryDecoder::getSize; diff --git a/unit_tests/offline_compiler/decoder/mock/mock_encoder.h b/unit_tests/offline_compiler/decoder/mock/mock_encoder.h index 6c956183dd..7b1f7a2b7b 100644 --- a/unit_tests/offline_compiler/decoder/mock/mock_encoder.h +++ b/unit_tests/offline_compiler/decoder/mock/mock_encoder.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018 Intel Corporation + * Copyright (C) 2018-2019 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -9,9 +9,11 @@ #include "offline_compiler/decoder/binary_encoder.h" struct MockEncoder : public BinaryEncoder { - MockEncoder() : BinaryEncoder("", ""){}; + MockEncoder() : MockEncoder("", ""){}; MockEncoder(const std::string &dump, const std::string &elf) - : BinaryEncoder(dump, elf){}; + : BinaryEncoder(dump, elf) { + setMessagePrinter(MessagePrinter{true}); + }; using BinaryEncoder::calculatePatchListSizes; using BinaryEncoder::copyBinaryToBinary; using BinaryEncoder::createElf;