Refactor ocloc query

Signed-off-by: Krystian Chmielewski <krystian.chmielewski@intel.com>
This commit is contained in:
Krystian Chmielewski
2021-08-05 12:50:37 +00:00
committed by Compute-Runtime-Automation
parent edcdd74850
commit 75f8fbf82e
5 changed files with 106 additions and 139 deletions

View File

@@ -6,7 +6,7 @@
*/
#include "shared/offline_compiler/source/ocloc_api.h"
#include "shared/offline_compiler/source/offline_compiler.h"
#include "shared/offline_compiler/source/queries.h"
#include "shared/offline_compiler/source/utilities/get_git_version_info.h"
#include "shared/source/device_binary_format/elf/elf_decoder.h"
#include "shared/source/device_binary_format/elf/ocl_elf.h"
@@ -45,124 +45,99 @@ TEST(OclocApiTests, WhenGoodArgsAreGivenThenSuccessIsReturned) {
EXPECT_EQ(std::string::npos, output.find("Command was: ocloc -file test_files/copybuffer.cl -device "s + argv[4]));
}
TEST(OclocApiTests, WhenValidQueryArgumentWithoutQueryInvokeIsGivenThenErrorMessageIsProduced) {
const char *argv[] = {
"ocloc",
"compile",
"NEO_REVISION"};
unsigned int argc = sizeof(argv) / sizeof(const char *);
TEST(OclocApiTests, GivenNeoRevisionQueryWhenQueryingThenNeoRevisionIsReturned) {
uint32_t numOutputs;
uint64_t *lenOutputs;
uint8_t **dataOutputs;
char **nameOutputs;
testing::internal::CaptureStdout();
int retVal = oclocInvoke(argc, argv,
0, nullptr, nullptr, nullptr,
0, nullptr, nullptr, nullptr,
&numOutputs, &dataOutputs, &lenOutputs, &nameOutputs);
std::string output = testing::internal::GetCapturedStdout();
EXPECT_EQ(retVal, NEO::OfflineCompiler::ErrorCode::INVALID_COMMAND_LINE);
EXPECT_NE(std::string::npos, output.find("Invalid option (arg 2): NEO_REVISION"));
}
TEST(OclocApiTests, WhenValidQueryArgumentWithExtraArgumentIsGivenThenErrorMessageIsProduced) {
const char *argv[] = {
"ocloc",
"query",
"NEO_REVISION",
"extra"};
NEO::Queries::queryNeoRevision.data()};
unsigned int argc = sizeof(argv) / sizeof(const char *);
uint32_t numOutputs;
uint64_t *lenOutputs;
uint8_t **dataOutputs;
char **nameOutputs;
testing::internal::CaptureStdout();
int retVal = oclocInvoke(argc, argv,
0, nullptr, nullptr, nullptr,
0, nullptr, nullptr, nullptr,
&numOutputs, &dataOutputs, &lenOutputs, &nameOutputs);
std::string output = testing::internal::GetCapturedStdout();
EXPECT_EQ(retVal, NEO::OfflineCompiler::ErrorCode::INVALID_COMMAND_LINE);
EXPECT_NE(std::string::npos, output.find("Invalid option (arg 2): NEO_REVISION"));
}
TEST(OclocApiTests, WhenRightArgumentsToQueryOptionAreGivenThenSuccessIsReturned) {
const char *argv[] = {
"ocloc",
"query",
"NEO_REVISION"};
unsigned int argc = sizeof(argv) / sizeof(const char *);
uint32_t numOutputs;
uint64_t *lenOutputs;
uint8_t **dataOutputs;
char **nameOutputs;
int retVal = oclocInvoke(argc, argv,
0, nullptr, nullptr, nullptr,
0, nullptr, nullptr, nullptr,
&numOutputs, &dataOutputs, &lenOutputs, &nameOutputs);
EXPECT_EQ(retVal, NEO::OfflineCompiler::ErrorCode::SUCCESS);
EXPECT_EQ(retVal, NEO::OfflineCompiler::SUCCESS);
EXPECT_EQ(numOutputs, 2u);
EXPECT_STREQ(*nameOutputs, "NEO_REVISION");
const char *revision = reinterpret_cast<const char *>(*dataOutputs);
EXPECT_STREQ(revision, NEO::getRevision().c_str());
oclocFreeOutput(&numOutputs, &dataOutputs, &lenOutputs, &nameOutputs);
const char *argv1[] = {
"ocloc",
"query",
"OCL_DRIVER_VERSION"};
retVal = oclocInvoke(argc, argv1,
0, nullptr, nullptr, nullptr,
0, nullptr, nullptr, nullptr,
&numOutputs, &dataOutputs, &lenOutputs, &nameOutputs);
EXPECT_EQ(retVal, NEO::OfflineCompiler::ErrorCode::SUCCESS);
EXPECT_EQ(numOutputs, 2u);
EXPECT_STREQ(*nameOutputs, "OCL_DRIVER_VERSION");
const char *driverVersion = reinterpret_cast<const char *>(*dataOutputs);
EXPECT_STREQ(driverVersion, NEO::getOclDriverVersion().c_str());
int queryOutputIndex = -1;
for (uint32_t i = 0; i < numOutputs; ++i) {
if (strcmp(NEO::Queries::queryNeoRevision.data(), nameOutputs[i]) == 0) {
queryOutputIndex = i;
}
}
ASSERT_NE(-1, queryOutputIndex);
NEO::ConstStringRef queryOutput(reinterpret_cast<const char *>(dataOutputs[queryOutputIndex]),
static_cast<size_t>(lenOutputs[queryOutputIndex]));
EXPECT_STREQ(NEO::getRevision().c_str(), queryOutput.data());
oclocFreeOutput(&numOutputs, &dataOutputs, &lenOutputs, &nameOutputs);
}
TEST(OclocApiTests, WhenQueryCommandPassedWithWrongArgumentThenErrorMessageIsProduced) {
const char *argv[] = {
"ocloc",
"query",
"wrong"};
unsigned int argc = sizeof(argv) / sizeof(const char *);
TEST(OclocApiTests, GivenOclDriverVersionQueryWhenQueryingThenNeoRevisionIsReturned) {
uint32_t numOutputs;
uint64_t *lenOutputs;
uint8_t **dataOutputs;
char **nameOutputs;
testing::internal::CaptureStdout();
const char *argv[] = {
"ocloc",
"query",
NEO::Queries::queryOCLDriverVersion.data()};
unsigned int argc = sizeof(argv) / sizeof(const char *);
int retVal = oclocInvoke(argc, argv,
0, nullptr, nullptr, nullptr,
0, nullptr, nullptr, nullptr,
&numOutputs, &dataOutputs, &lenOutputs, &nameOutputs);
std::string output = testing::internal::GetCapturedStdout();
EXPECT_EQ(retVal, NEO::OfflineCompiler::ErrorCode::INVALID_COMMAND_LINE);
EXPECT_NE(std::string::npos, output.find("Invalid option (arg 2): wrong"));
EXPECT_EQ(retVal, NEO::OfflineCompiler::SUCCESS);
EXPECT_EQ(numOutputs, 2u);
int queryOutputIndex = -1;
for (uint32_t i = 0; i < numOutputs; ++i) {
if (strcmp(NEO::Queries::queryOCLDriverVersion.data(), nameOutputs[i]) == 0) {
queryOutputIndex = i;
}
}
ASSERT_NE(-1, queryOutputIndex);
NEO::ConstStringRef queryOutput(reinterpret_cast<const char *>(dataOutputs[queryOutputIndex]),
static_cast<size_t>(lenOutputs[queryOutputIndex]));
EXPECT_STREQ(NEO::getOclDriverVersion().c_str(), queryOutput.data());
oclocFreeOutput(&numOutputs, &dataOutputs, &lenOutputs, &nameOutputs);
}
TEST(OclocApiTests, WhenQueryCommandPassedWithoutArgumentsThenErrorMessageIsProduced) {
TEST(OclocApiTests, GivenNoQueryWhenQueryingThenErrorIsReturned) {
const char *argv[] = {
"ocloc",
"query"};
unsigned int argc = sizeof(argv) / sizeof(const char *);
uint32_t numOutputs;
uint64_t *lenOutputs;
uint8_t **dataOutputs;
char **nameOutputs;
testing::internal::CaptureStdout();
int retVal = oclocInvoke(argc, argv,
0, nullptr, nullptr, nullptr,
0, nullptr, nullptr, nullptr,
&numOutputs, &dataOutputs, &lenOutputs, &nameOutputs);
nullptr, nullptr, nullptr, nullptr);
std::string output = testing::internal::GetCapturedStdout();
EXPECT_EQ(retVal, NEO::OfflineCompiler::ErrorCode::INVALID_COMMAND_LINE);
EXPECT_NE(std::string::npos, output.find("Error: no options for query provided."));
EXPECT_EQ(retVal, NEO::OfflineCompiler::INVALID_COMMAND_LINE);
EXPECT_STREQ("Error: Invalid command line. Expected ocloc query <argument>", output.c_str());
}
TEST(OclocApiTests, GivenInvalidQueryWhenQueryingThenErrorIsReturned) {
const char *argv[] = {
"ocloc",
"query",
"unknown_query"};
unsigned int argc = sizeof(argv) / sizeof(const char *);
testing::internal::CaptureStdout();
int retVal = oclocInvoke(argc, argv,
0, nullptr, nullptr, nullptr,
0, nullptr, nullptr, nullptr,
nullptr, nullptr, nullptr, nullptr);
std::string output = testing::internal::GetCapturedStdout();
EXPECT_EQ(retVal, NEO::OfflineCompiler::INVALID_COMMAND_LINE);
EXPECT_STREQ("Error: Invalid command line. Uknown argument unknown_query.", output.c_str());
}
TEST(OclocApiTests, WhenGoodFamilyNameIsProvidedThenSuccessIsReturned) {

View File

@@ -61,6 +61,7 @@ set(CLOC_LIB_SRCS_LIB
${OCLOC_DIRECTORY}/source/offline_compiler.h
${OCLOC_DIRECTORY}/source/offline_compiler_helper.cpp
${OCLOC_DIRECTORY}/source/offline_compiler_options.cpp
${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

View File

@@ -7,6 +7,7 @@
#include "offline_compiler.h"
#include "shared/offline_compiler/source/queries.h"
#include "shared/offline_compiler/source/utilities/get_git_version_info.h"
#include "shared/source/compiler_interface/intermediate_representations.h"
#include "shared/source/debug_settings/debug_settings_manager.h"
@@ -106,17 +107,24 @@ OfflineCompiler *OfflineCompiler::create(size_t numArgs, const std::vector<std::
}
int OfflineCompiler::query(size_t numArgs, const std::vector<std::string> &allArgs, OclocArgHelper *helper) {
int retVal = OUT_OF_HOST_MEMORY;
std::unique_ptr<OfflineCompiler> pOffCompiler{new OfflineCompiler()};
if (allArgs.size() != 3) {
helper->printf("Error: Invalid command line. Expected ocloc query <argument>");
return INVALID_COMMAND_LINE;
}
pOffCompiler->queryInvoke = true;
pOffCompiler->argHelper = helper;
retVal = pOffCompiler->initialize(numArgs, allArgs, true);
auto retVal = SUCCESS;
auto &arg = allArgs[2];
if (retVal != SUCCESS)
return retVal;
retVal = pOffCompiler->performQuery();
if (Queries::queryNeoRevision == arg) {
auto revision = NEO::getRevision();
helper->saveOutput(Queries::queryNeoRevision.data(), revision.c_str(), revision.size() + 1);
} else if (Queries::queryOCLDriverVersion == arg) {
auto driverVersion = NEO::getOclDriverVersion();
helper->saveOutput(Queries::queryOCLDriverVersion.data(), driverVersion.c_str(), driverVersion.size() + 1);
} else {
helper->printf("Error: Invalid command line. Uknown argument %s.", arg.c_str());
retVal = INVALID_COMMAND_LINE;
}
return retVal;
}
@@ -377,7 +385,7 @@ int OfflineCompiler::initialize(size_t numArgs, const std::vector<std::string> &
size_t sourceFromFileSize = 0;
this->pBuildInfo = std::make_unique<buildInfo>();
retVal = parseCommandLine(numArgs, allArgs);
if (retVal != SUCCESS || queryInvoke) {
if (retVal != SUCCESS) {
return retVal;
}
@@ -602,20 +610,6 @@ int OfflineCompiler::initialize(size_t numArgs, const std::vector<std::string> &
return retVal;
}
int OfflineCompiler::performQuery() {
int retVal = SUCCESS;
if (queryOption == QUERY_NEO_REVISION) {
auto revision = NEO::getRevision();
argHelper->saveOutput("NEO_REVISION", revision.c_str(), revision.size() + 1);
} else {
auto driverVersion = NEO::getOclDriverVersion();
argHelper->saveOutput("OCL_DRIVER_VERSION", driverVersion.c_str(), driverVersion.size() + 1);
}
return retVal;
}
int OfflineCompiler::parseCommandLine(size_t numArgs, const std::vector<std::string> &argv) {
int retVal = SUCCESS;
bool compile32 = false;
@@ -629,7 +623,7 @@ int OfflineCompiler::parseCommandLine(size_t numArgs, const std::vector<std::str
for (uint32_t argIndex = 1; argIndex < numArgs; argIndex++) {
const auto &currArg = argv[argIndex];
const bool hasMoreArgs = (argIndex + 1 < numArgs);
if ("compile" == currArg || "query" == currArg) {
if ("compile" == currArg) {
//skip it
} else if (("-file" == currArg) && hasMoreArgs) {
inputFile = argv[argIndex + 1];
@@ -684,10 +678,6 @@ int OfflineCompiler::parseCommandLine(size_t numArgs, const std::vector<std::str
} else if (("-revision_id" == currArg) && hasMoreArgs) {
revisionId = std::stoi(argv[argIndex + 1]);
argIndex++;
} else if ("NEO_REVISION" == currArg && queryInvoke && !hasMoreArgs) {
queryOption = QUERY_NEO_REVISION;
} else if ("OCL_DRIVER_VERSION" == currArg && queryInvoke && !hasMoreArgs) {
queryOption = QUERY_OCL_DRIVER_VERSION;
} else {
argHelper->printf("Invalid option (arg %d): %s\n", argIndex, argv[argIndex].c_str());
retVal = INVALID_COMMAND_LINE;
@@ -696,25 +686,18 @@ int OfflineCompiler::parseCommandLine(size_t numArgs, const std::vector<std::str
}
if (retVal == SUCCESS) {
if (queryInvoke) {
if (queryOption == QUERY_LAST) {
argHelper->printf("Error: no options for query provided.\n");
retVal = INVALID_COMMAND_LINE;
}
} else {
if (compile32 && compile64) {
argHelper->printf("Error: Cannot compile for 32-bit and 64-bit, please choose one.\n");
retVal = INVALID_COMMAND_LINE;
} else if (inputFile.empty()) {
argHelper->printf("Error: Input file name missing.\n");
retVal = INVALID_COMMAND_LINE;
} else if (deviceName.empty() && (false == onlySpirV)) {
argHelper->printf("Error: Device name missing.\n");
retVal = INVALID_COMMAND_LINE;
} else if (!argHelper->fileExists(inputFile)) {
argHelper->printf("Error: Input file %s missing.\n", inputFile.c_str());
retVal = INVALID_FILE;
}
if (compile32 && compile64) {
argHelper->printf("Error: Cannot compile for 32-bit and 64-bit, please choose one.\n");
retVal = INVALID_COMMAND_LINE;
} else if (inputFile.empty()) {
argHelper->printf("Error: Input file name missing.\n");
retVal = INVALID_COMMAND_LINE;
} else if (deviceName.empty() && (false == onlySpirV)) {
argHelper->printf("Error: Device name missing.\n");
retVal = INVALID_COMMAND_LINE;
} else if (!argHelper->fileExists(inputFile)) {
argHelper->printf("Error: Input file %s missing.\n", inputFile.c_str());
retVal = INVALID_FILE;
}
}

View File

@@ -43,14 +43,10 @@ class OfflineCompiler {
INVALID_FILE = -5151,
PRINT_USAGE = -5152,
};
enum QueryOption {
QUERY_OCL_DRIVER_VERSION = 0,
QUERY_NEO_REVISION = 1,
QUERY_LAST,
};
static int query(size_t numArgs, const std::vector<std::string> &allArgs, OclocArgHelper *helper);
static OfflineCompiler *create(size_t numArgs, const std::vector<std::string> &allArgs, bool dumpFiles, int &retVal, OclocArgHelper *helper);
static int query(size_t numArgs, const std::vector<std::string> &allArgs, OclocArgHelper *helper);
int build();
std::string &getBuildLog();
void printUsage();
@@ -87,7 +83,6 @@ class OfflineCompiler {
std::string getStringWithinDelimiters(const std::string &src);
int initialize(size_t numArgs, const std::vector<std::string> &allArgs, bool dumpFiles);
int parseCommandLine(size_t numArgs, const std::vector<std::string> &allArgs);
int performQuery();
void setStatelessToStatefullBufferOffsetFlag();
void resolveExtraSettings();
void parseDebugSettings();
@@ -131,8 +126,6 @@ class OfflineCompiler {
bool inputFileSpirV = false;
bool outputNoSuffix = false;
bool forceStatelessToStatefulOptimization = false;
bool queryInvoke = false;
int queryOption = QUERY_LAST;
std::vector<uint8_t> elfBinary;
char *genBinary = nullptr;

View File

@@ -0,0 +1,15 @@
/*
* Copyright (C) 2021 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#include "shared/source/utilities/const_stringref.h"
namespace NEO {
namespace Queries {
static constexpr ConstStringRef queryNeoRevision = "NEO_REVISION";
static constexpr ConstStringRef queryOCLDriverVersion = "OCL_DRIVER_VERSION";
}; // namespace Queries
} // namespace NEO