Add multi command line option for ocloc

- now ocloc is able to make multi build, all options and parameters
of build are listed in separate .txt file, each line in this file
is new build.

Change-Id: Id74af826e8c1a4fe14c46ed6024efe2041a22fd0
Signed-off-by: Marcin Naczk <marcin.naczk@intel.com>
This commit is contained in:
Marcin Naczk
2019-05-14 16:47:35 +02:00
committed by sys_ocldev
parent 2d02435fb9
commit ce8d24d124
10 changed files with 582 additions and 78 deletions

View File

@@ -16,6 +16,8 @@ ${IGDRCL_SOURCE_DIR}/offline_compiler/decoder/helper.h
${IGDRCL_SOURCE_DIR}/offline_compiler/helper.cpp
${IGDRCL_SOURCE_DIR}/offline_compiler/offline_compiler.cpp
${IGDRCL_SOURCE_DIR}/offline_compiler/offline_compiler.h
${IGDRCL_SOURCE_DIR}/offline_compiler/multi_command.cpp
${IGDRCL_SOURCE_DIR}/offline_compiler/multi_command.h
${IGDRCL_SOURCE_DIR}/offline_compiler/options.cpp
${IGDRCL_SOURCE_DIR}/runtime/compiler_interface/create_main.cpp
${IGDRCL_SOURCE_DIR}/runtime/helpers/abort.cpp

View File

@@ -5,14 +5,17 @@
*
*/
#include "offline_compiler/multi_command.h"
#include "offline_compiler/offline_compiler.h"
#include "offline_compiler/utilities/safety_caller.h"
#include "runtime/os_interface/os_library.h"
#include "decoder/binary_decoder.h"
#include "decoder/binary_encoder.h"
#include <CL/cl.h>
#include <fstream>
#include <iostream>
using namespace NEO;
int main(int numArgs, const char *argv[]) {
@@ -33,9 +36,20 @@ int main(int numArgs, const char *argv[]) {
} else {
return retVal;
}
} else if (numArgs > 1 && !strcmp(argv[1], "-multi")) {
int retValue = CL_SUCCESS;
auto pMulti = std::unique_ptr<MultiCommand>(MultiCommand::create(numArgs, argv, retValue));
return retValue;
} else {
int retVal = CL_SUCCESS;
OfflineCompiler *pCompiler = OfflineCompiler::create(numArgs, argv, retVal);
std::vector<std::string> allArgs;
if (numArgs > 1) {
allArgs.assign(argv, argv + numArgs);
}
OfflineCompiler *pCompiler = OfflineCompiler::create(numArgs, allArgs, retVal);
if (retVal == CL_SUCCESS) {
retVal = buildWithSafetyGuard(pCompiler);

View File

@@ -0,0 +1,226 @@
/*
* Copyright (C) 2019 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#include "offline_compiler/multi_command.h"
namespace NEO {
int MultiCommand::singleBuild(size_t numArgs, const std::vector<std::string> &allArgs) {
int retVal;
std::string buildLog;
OfflineCompiler *pCompiler = OfflineCompiler::create(numArgs, allArgs, retVal);
if (retVal == CL_SUCCESS) {
retVal = buildWithSafetyGuard(pCompiler);
buildLog = pCompiler->getBuildLog();
if (buildLog.empty() == false) {
printf("%s\n", buildLog.c_str());
}
if (retVal == CL_SUCCESS) {
if (!pCompiler->isQuiet())
printf("Build succeeded.\n");
} else {
printf("Build failed with error code: %d\n", retVal);
}
}
if (buildLog.empty() == false) {
singleBuilds.push_back(pCompiler);
} else {
delete pCompiler;
}
return retVal;
}
MultiCommand::MultiCommand() = default;
MultiCommand::~MultiCommand() {
deleteBuildsWithWarnigs();
}
void MultiCommand::deleteBuildsWithWarnigs() {
for (OfflineCompiler *pSingle : singleBuilds)
delete pSingle;
singleBuilds.clear();
}
MultiCommand *MultiCommand::create(int numArgs, const char *argv[], int &retVal) {
retVal = CL_SUCCESS;
auto pMultiCommand = new MultiCommand();
if (pMultiCommand) {
retVal = pMultiCommand->initialize(numArgs, argv);
}
if (retVal != CL_SUCCESS) {
delete pMultiCommand;
pMultiCommand = nullptr;
}
return pMultiCommand;
}
std::string MultiCommand::eraseExtensionFromPath(std::string &filePath) {
size_t extPos = filePath.find_last_of(".", filePath.size());
if (extPos == std::string::npos) {
extPos = filePath.size();
}
std::string fileName;
std::string fileTrunk = filePath.substr(0, extPos);
return fileTrunk;
}
void MultiCommand::addAdditionalOptionsToSingleCommandLine(std::vector<std::string> &singleLineWithArguments, int buildId) {
std::string OutFileName;
bool hasOutDir = false;
bool hasSpecificName = false;
for (auto arg : singleLineWithArguments) {
if (arg == "-out_dir") {
hasOutDir = true;
}
if (arg == "-output") {
hasSpecificName = true;
}
}
if (!hasOutDir) {
singleLineWithArguments.push_back("-out_dir");
OutDirForBuilds = eraseExtensionFromPath(pathToCMD);
singleLineWithArguments.push_back(OutDirForBuilds);
}
if (!hasSpecificName) {
singleLineWithArguments.push_back("-output");
OutFileName = "build_no_" + std::to_string(buildId + 1);
singleLineWithArguments.push_back(OutFileName);
}
if (quiet)
singleLineWithArguments.push_back("-q");
}
int MultiCommand::initialize(int numArgs, const char *argv[]) {
int retVal = CL_SUCCESS;
if (numArgs > 2)
pathToCMD = argv[2];
else {
printf("Lack of file with build arguments\n");
return INVALID_COMMAND_LINE;
}
if (numArgs > 3 && strcmp(argv[3], "-q") == 0)
quiet = true;
//save file with builds arguments to vector of strings, line by line
openFileWithBuildsArguments();
if (!lines.empty()) {
for (unsigned int i = 0; i < lines.size(); i++) {
std::vector<std::string> singleLineWithArguments;
unsigned int numberOfArg;
singleLineWithArguments.push_back(argv[0]);
retVal = splitLineInSeparateArgs(singleLineWithArguments, lines[i], i);
if (retVal != CL_SUCCESS) {
retValues.push_back(retVal);
continue;
}
addAdditionalOptionsToSingleCommandLine(singleLineWithArguments, i);
numberOfArg = static_cast<unsigned int>(singleLineWithArguments.size());
if (!quiet)
printf("\nCommand number %d: ", i + 1);
retVal = singleBuild(numberOfArg, singleLineWithArguments);
retValues.push_back(retVal);
}
return showResults();
} else
return INVALID_COMMAND_LINE;
}
int MultiCommand::splitLineInSeparateArgs(std::vector<std::string> &qargs, const std::string &command, int numberOfBuild) {
unsigned int len = static_cast<unsigned int>(command.length());
bool qot = false, sqot = false;
int arglen;
for (unsigned int i = 0; i < len; i++) {
int start = i;
if (command[i] == '\"') {
qot = true;
} else if (command[i] == '\'')
sqot = true;
if (qot) {
i++;
start++;
while (i < len && command[i] != '\"')
i++;
if (i < len)
qot = false;
arglen = i - start;
i++;
} else if (sqot) {
i++;
while (i < len && command[i] != '\'')
i++;
if (i < len)
sqot = false;
arglen = i - start;
i++;
} else {
while (i < len && command[i] != ' ')
i++;
arglen = i - start;
}
qargs.push_back(command.substr(start, arglen));
}
if (qot || sqot) {
printf("One of the quotes is open in build number %d\n", numberOfBuild + 1);
return INVALID_COMMAND_LINE;
}
return CL_SUCCESS;
}
void MultiCommand::openFileWithBuildsArguments() {
std::fstream multiCmdFile;
std::stringstream fileContent;
multiCmdFile.open(pathToCMD, std::fstream::in);
if (multiCmdFile.is_open()) {
std::string param;
fileContent << multiCmdFile.rdbuf();
multiCmdFile.close();
while (std::getline(fileContent, param, '\n')) {
param.erase(param.find_last_not_of(" \r\t") + 1);
param.erase(0, param.find_first_not_of(" \r\t"));
if (!param.empty()) {
lines.push_back(param);
}
}
} else {
printf("Can not open file with builds arguments\n");
}
}
int MultiCommand::showResults() {
int retValue = CL_SUCCESS;
int indexRetVal = 0;
for (int retVal : retValues) {
if (retVal != CL_SUCCESS) {
if (retValue == CL_SUCCESS)
retValue = retVal;
if (!quiet)
printf("Build %d: failed. Error code: %d\n", indexRetVal, retVal);
} else {
if (!quiet)
printf("Build %d: successful\n", indexRetVal);
}
indexRetVal++;
}
return retValue;
}
} // namespace NEO

View File

@@ -0,0 +1,50 @@
/*
* Copyright (C) 2019 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#include "offline_compiler/offline_compiler.h"
#include "offline_compiler/utilities/safety_caller.h"
#include "runtime/os_interface/os_library.h"
#include "decoder/binary_decoder.h"
#include "decoder/binary_encoder.h"
#include <CL/cl.h>
#include <fstream>
#include <iostream>
namespace NEO {
class MultiCommand {
public:
static MultiCommand *create(int numArgs, const char *argv[], int &retVal);
void deleteBuildsWithWarnigs();
std::vector<OfflineCompiler *> singleBuilds;
MultiCommand &operator=(const MultiCommand &) = delete;
MultiCommand(const MultiCommand &) = delete;
~MultiCommand();
std::string OutDirForBuilds;
protected:
int splitLineInSeparateArgs(std::vector<std::string> &qargs, const std::string &command, int numberOfBuild);
void openFileWithBuildsArguments();
void addAdditionalOptionsToSingleCommandLine(std::vector<std::string> &, int);
int initialize(int numArgs, const char *argv[]);
int showResults();
int singleBuild(size_t numArgs, const std::vector<std::string> &allArgs);
std::string eraseExtensionFromPath(std::string &filePath);
std::vector<int> retValues;
std::string pathToCMD;
std::vector<std::string> lines;
bool quiet = false;
MultiCommand();
};
} // namespace NEO

View File

@@ -55,6 +55,12 @@ bool stringsAreEqual(const char *string1, const char *string2) {
return (strcmp(string1, string2) == 0);
}
bool stringsAreEqual(std::string string1, std::string string2) {
if (string2.empty())
return false;
return (string1 == string2);
}
////////////////////////////////////////////////////////////////////////////////
// convertToPascalCase
////////////////////////////////////////////////////////////////////////////////
@@ -91,12 +97,12 @@ OfflineCompiler::~OfflineCompiler() {
////////////////////////////////////////////////////////////////////////////////
// Create
////////////////////////////////////////////////////////////////////////////////
OfflineCompiler *OfflineCompiler::create(size_t numArgs, const char *const *argv, int &retVal) {
OfflineCompiler *OfflineCompiler::create(size_t numArgs, const std::vector<std::string> &allArgs, int &retVal) {
retVal = CL_SUCCESS;
auto pOffCompiler = new OfflineCompiler();
if (pOffCompiler) {
retVal = pOffCompiler->initialize(numArgs, argv);
retVal = pOffCompiler->initialize(numArgs, allArgs);
}
if (retVal != CL_SUCCESS) {
@@ -270,13 +276,13 @@ std::string OfflineCompiler::getStringWithinDelimiters(const std::string &src) {
////////////////////////////////////////////////////////////////////////////////
// Initialize
////////////////////////////////////////////////////////////////////////////////
int OfflineCompiler::initialize(size_t numArgs, const char *const *argv) {
int OfflineCompiler::initialize(size_t numArgs, const std::vector<std::string> &allArgs) {
int retVal = CL_SUCCESS;
const char *pSource = nullptr;
void *pSourceFromFile = nullptr;
size_t sourceFromFileSize = 0;
retVal = parseCommandLine(numArgs, argv);
retVal = parseCommandLine(numArgs, allArgs);
if (retVal != CL_SUCCESS) {
return retVal;
}
@@ -434,7 +440,7 @@ int OfflineCompiler::initialize(size_t numArgs, const char *const *argv) {
////////////////////////////////////////////////////////////////////////////////
// ParseCommandLine
////////////////////////////////////////////////////////////////////////////////
int OfflineCompiler::parseCommandLine(size_t numArgs, const char *const *argv) {
int OfflineCompiler::parseCommandLine(size_t numArgs, const std::vector<std::string> &argv) {
int retVal = CL_SUCCESS;
bool compile32 = false;
bool compile64 = false;
@@ -493,7 +499,7 @@ int OfflineCompiler::parseCommandLine(size_t numArgs, const char *const *argv) {
printUsage();
retVal = PRINT_USAGE;
} else {
printf("Invalid option (arg %d): %s\n", argIndex, argv[argIndex]);
printf("Invalid option (arg %d): %s\n", argIndex, argv[argIndex].c_str());
retVal = INVALID_COMMAND_LINE;
break;
}
@@ -642,6 +648,13 @@ void OfflineCompiler::printUsage() {
printf(" -device <device_type> Indicates which device for which we will compile.\n");
printf(" <device_type> can be: %s\n", getDevicesTypes().c_str());
printf("\n");
printf(" -multi <filename> Indicates the txt file with multi commands\n");
printf(" where each line is a single build in format:\n");
printf(" '-file <filename> -device <device_type> [OPTIONS]'\n");
printf(" Argument '-multi' must be first argument. \n");
printf(" Result of builds will be output in directory named \n");
printf(" like .txt file with build commands.\n");
printf("\n");
printf(" -output <filename> Indicates output files core name.\n");
printf(" -out_dir <output_dir> Indicates the directory into which the compiled files\n");
printf(" will be placed.\n");

View File

@@ -33,7 +33,7 @@ std::string generateFilePath(const std::string &directory, const std::string &fi
class OfflineCompiler {
public:
static OfflineCompiler *create(size_t numArgs, const char *const *argv, int &retVal);
static OfflineCompiler *create(size_t numArgs, const std::vector<std::string> &allArgs, int &retVal);
int build();
std::string &getBuildLog();
void printUsage();
@@ -55,8 +55,8 @@ class OfflineCompiler {
int getHardwareInfo(const char *pDeviceName);
std::string getFileNameTrunk(std::string &filePath);
std::string getStringWithinDelimiters(const std::string &src);
int initialize(size_t numArgs, const char *const *argv);
int parseCommandLine(size_t numArgs, const char *const *argv);
int initialize(size_t numArgs, const std::vector<std::string> &allArgs);
int parseCommandLine(size_t numArgs, const std::vector<std::string> &allArgs);
void setStatelessToStatefullBufferOffsetFlag();
void parseDebugSettings();
void storeBinary(char *&pDst, size_t &dstSize, const void *pSrc, const size_t srcSize);