Do not print error messages from BinaryEncoder/BinaryDecoder in tests

Change-Id: I45d339c233f9a45724db8d84bf5c5bd66233dcab
Signed-off-by: Maciej Dziuban <maciej.dziuban@intel.com>
This commit is contained in:
Maciej Dziuban
2019-03-26 15:11:49 +01:00
committed by sys_ocldev
parent 9bbf1daa0e
commit 0b5f810142
7 changed files with 93 additions and 47 deletions

View File

@@ -16,6 +16,10 @@
#include <cstring>
#include <fstream>
void BinaryDecoder::setMessagePrinter(const MessagePrinter &messagePrinter) {
this->messagePrinter = messagePrinter;
}
template <typename T>
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 <Opencl elf binary file> -patch <path to folder containing patchlist> -dump <path to dumping folder>\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 <Opencl elf binary file> -patch <path to folder containing patchlist> -dump <path to dumping folder>\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;
}

View File

@@ -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 <memory>
#include <string>
#include <unordered_map>
@@ -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();

View File

@@ -17,6 +17,10 @@
#include <cstring>
#include <fstream>
void BinaryEncoder::setMessagePrinter(const MessagePrinter &messagePrinter) {
this->messagePrinter = messagePrinter;
}
void BinaryEncoder::calculatePatchListSizes(std::vector<std::string> &ptmFile) {
size_t patchListPos = 0;
for (size_t i = 0; i < ptmFile.size(); ++i) {
@@ -37,7 +41,7 @@ void BinaryEncoder::calculatePatchListSizes(std::vector<std::string> &ptmFile) {
}
uint32_t size = static_cast<uint32_t>(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<int>(patchListPos + 1));
messagePrinter.printf("Warning! Calculated PatchListSize ( %u ) differs from file ( %u ) - changing it. Line %d\n", calcSize, size, static_cast<int>(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<std::string> &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<uint32_t>(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<uint32_t>(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<uint32_t>(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 <path to dumping folder> -out <new elf file>\n");
printf("e.g. -dump C:/my_folder/dump -out C:/my_folder/new_binary.bin\n");
messagePrinter.printf("Usage:\n-dump <path to dumping folder> -out <new elf file>\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<std::string> &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<std::string> &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<std::string> &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<uint64_t>(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;
}
}

View File

@@ -6,6 +6,8 @@
*/
#pragma once
#include "helper.h"
#include <sstream>
#include <string>
#include <vector>
@@ -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<std::string> &ptmFile);
int copyBinaryToBinary(const std::string &srcFileName, std::ostream &outBinary);
int createElf();

View File

@@ -17,3 +17,25 @@ std::vector<char> readBinaryFile(const std::string &fileName);
void readFileToVectorOfStrings(std::vector<std::string> &lines, const std::string &fileName, bool replaceTabs = false);
size_t findPos(const std::vector<std::string> &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 <typename... Args>
void printf(const char *format, Args... args) {
if (!suppressMessages) {
::printf(format, std::forward<Args>(args)...);
}
}
private:
bool suppressMessages = false;
};