Skip to content

Commit 9e18416

Browse files
Add interface to extract versioning info
Appened ocloc interface with new 'query' feature. Using this feature now one can extract HEAD hash and version of neo. Signed-off-by: Bushev, Dmitry <dmitry.bushev@intel.com>
1 parent 4dda709 commit 9e18416

File tree

10 files changed

+271
-15
lines changed

10 files changed

+271
-15
lines changed

driver_version.h.in

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2018-2020 Intel Corporation
2+
* Copyright (C) 2018-2021 Intel Corporation
33
*
44
* SPDX-License-Identifier: MIT
55
*
@@ -9,5 +9,6 @@
99
#define DRIVER_VERSION_H
1010

1111
#cmakedefine NEO_OCL_DRIVER_VERSION ${NEO_OCL_DRIVER_VERSION}
12+
#cmakedefine NEO_REVISION "${NEO_REVISION}"
1213

1314
#endif /* DRIVER_VERSION_H */

opencl/test/unit_test/offline_compiler/ocloc_api_tests.cpp

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include "shared/offline_compiler/source/ocloc_api.h"
99
#include "shared/offline_compiler/source/offline_compiler.h"
10+
#include "shared/offline_compiler/source/utilities/get_git_version_info.h"
1011
#include "shared/source/device_binary_format/elf/elf_decoder.h"
1112
#include "shared/source/device_binary_format/elf/ocl_elf.h"
1213

@@ -44,6 +45,126 @@ TEST(OclocApiTests, WhenGoodArgsAreGivenThenSuccessIsReturned) {
4445
EXPECT_EQ(std::string::npos, output.find("Command was: ocloc -file test_files/copybuffer.cl -device "s + argv[4]));
4546
}
4647

48+
TEST(OclocApiTests, WhenValidQueryArgumentWithoutQueryInvokeIsGivenThenErrorMessageIsProduced) {
49+
const char *argv[] = {
50+
"ocloc",
51+
"compile",
52+
"NEO_REVISION"};
53+
unsigned int argc = sizeof(argv) / sizeof(const char *);
54+
uint32_t numOutputs;
55+
uint64_t *lenOutputs;
56+
uint8_t **dataOutputs;
57+
char **nameOutputs;
58+
testing::internal::CaptureStdout();
59+
int retVal = oclocInvoke(argc, argv,
60+
0, nullptr, nullptr, nullptr,
61+
0, nullptr, nullptr, nullptr,
62+
&numOutputs, &dataOutputs, &lenOutputs, &nameOutputs);
63+
std::string output = testing::internal::GetCapturedStdout();
64+
EXPECT_EQ(retVal, NEO::OfflineCompiler::ErrorCode::INVALID_COMMAND_LINE);
65+
EXPECT_NE(std::string::npos, output.find("Invalid option (arg 2): NEO_REVISION"));
66+
}
67+
68+
TEST(OclocApiTests, WhenValidQueryArgumentWithExtraArgumentIsGivenThenErrorMessageIsProduced) {
69+
const char *argv[] = {
70+
"ocloc",
71+
"query",
72+
"NEO_REVISION",
73+
"extra"};
74+
unsigned int argc = sizeof(argv) / sizeof(const char *);
75+
uint32_t numOutputs;
76+
uint64_t *lenOutputs;
77+
uint8_t **dataOutputs;
78+
char **nameOutputs;
79+
testing::internal::CaptureStdout();
80+
int retVal = oclocInvoke(argc, argv,
81+
0, nullptr, nullptr, nullptr,
82+
0, nullptr, nullptr, nullptr,
83+
&numOutputs, &dataOutputs, &lenOutputs, &nameOutputs);
84+
std::string output = testing::internal::GetCapturedStdout();
85+
EXPECT_EQ(retVal, NEO::OfflineCompiler::ErrorCode::INVALID_COMMAND_LINE);
86+
EXPECT_NE(std::string::npos, output.find("Invalid option (arg 2): NEO_REVISION"));
87+
}
88+
89+
TEST(OclocApiTests, WhenRightArgumentsToQueryOptionAreGivenThenSuccessIsReturned) {
90+
const char *argv[] = {
91+
"ocloc",
92+
"query",
93+
"NEO_REVISION"};
94+
unsigned int argc = sizeof(argv) / sizeof(const char *);
95+
uint32_t numOutputs;
96+
uint64_t *lenOutputs;
97+
uint8_t **dataOutputs;
98+
char **nameOutputs;
99+
int retVal = oclocInvoke(argc, argv,
100+
0, nullptr, nullptr, nullptr,
101+
0, nullptr, nullptr, nullptr,
102+
&numOutputs, &dataOutputs, &lenOutputs, &nameOutputs);
103+
EXPECT_EQ(retVal, NEO::OfflineCompiler::ErrorCode::SUCCESS);
104+
EXPECT_EQ(numOutputs, 2u);
105+
EXPECT_STREQ(*nameOutputs, "NEO_REVISION");
106+
const char *revision = reinterpret_cast<const char *>(*dataOutputs);
107+
EXPECT_STREQ(revision, NEO::getRevision().c_str());
108+
109+
oclocFreeOutput(&numOutputs, &dataOutputs, &lenOutputs, &nameOutputs);
110+
111+
const char *argv1[] = {
112+
"ocloc",
113+
"query",
114+
"OCL_DRIVER_VERSION"};
115+
116+
retVal = oclocInvoke(argc, argv1,
117+
0, nullptr, nullptr, nullptr,
118+
0, nullptr, nullptr, nullptr,
119+
&numOutputs, &dataOutputs, &lenOutputs, &nameOutputs);
120+
EXPECT_EQ(retVal, NEO::OfflineCompiler::ErrorCode::SUCCESS);
121+
EXPECT_EQ(numOutputs, 2u);
122+
EXPECT_STREQ(*nameOutputs, "OCL_DRIVER_VERSION");
123+
const char *driverVersion = reinterpret_cast<const char *>(*dataOutputs);
124+
EXPECT_STREQ(driverVersion, NEO::getOclDriverVersion().c_str());
125+
126+
oclocFreeOutput(&numOutputs, &dataOutputs, &lenOutputs, &nameOutputs);
127+
}
128+
129+
TEST(OclocApiTests, WhenQueryCommandPassedWithWrongArgumentThenErrorMessageIsProduced) {
130+
const char *argv[] = {
131+
"ocloc",
132+
"query",
133+
"wrong"};
134+
unsigned int argc = sizeof(argv) / sizeof(const char *);
135+
uint32_t numOutputs;
136+
uint64_t *lenOutputs;
137+
uint8_t **dataOutputs;
138+
char **nameOutputs;
139+
testing::internal::CaptureStdout();
140+
int retVal = oclocInvoke(argc, argv,
141+
0, nullptr, nullptr, nullptr,
142+
0, nullptr, nullptr, nullptr,
143+
&numOutputs, &dataOutputs, &lenOutputs, &nameOutputs);
144+
std::string output = testing::internal::GetCapturedStdout();
145+
EXPECT_EQ(retVal, NEO::OfflineCompiler::ErrorCode::INVALID_COMMAND_LINE);
146+
EXPECT_NE(std::string::npos, output.find("Invalid option (arg 2): wrong"));
147+
}
148+
149+
TEST(OclocApiTests, WhenQueryCommandPassedWithoutArgumentsThenErrorMessageIsProduced) {
150+
const char *argv[] = {
151+
"ocloc",
152+
"query"};
153+
unsigned int argc = sizeof(argv) / sizeof(const char *);
154+
uint32_t numOutputs;
155+
uint64_t *lenOutputs;
156+
uint8_t **dataOutputs;
157+
char **nameOutputs;
158+
testing::internal::CaptureStdout();
159+
int retVal = oclocInvoke(argc, argv,
160+
0, nullptr, nullptr, nullptr,
161+
0, nullptr, nullptr, nullptr,
162+
&numOutputs, &dataOutputs, &lenOutputs, &nameOutputs);
163+
std::string output = testing::internal::GetCapturedStdout();
164+
EXPECT_EQ(retVal, NEO::OfflineCompiler::ErrorCode::INVALID_COMMAND_LINE);
165+
EXPECT_NE(std::string::npos, output.find("Error: no options for query provided."));
166+
}
167+
47168
TEST(OclocApiTests, WhenGoodFamilyNameIsProvidedThenSuccessIsReturned) {
48169
const char *argv[] = {
49170
"ocloc",

opencl/test/unit_test/offline_compiler/offline_compiler_tests.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,18 @@ TEST_F(MultiCommandTests, GivenOutputFileListFlagWhenBuildingMultiCommandThenSuc
246246
deleteOutFileList();
247247
delete pMultiCommand;
248248
}
249+
250+
TEST_F(OfflineCompilerTests, GivenArgsWhenQueryIsCalledThenSuccessIsReturned) {
251+
std::vector<std::string> argv = {
252+
"ocloc",
253+
"query",
254+
"NEO_REVISION"};
255+
256+
int retVal = OfflineCompiler::query(argv.size(), argv, oclocArgHelperWithoutInput.get());
257+
258+
EXPECT_EQ(OfflineCompiler::ErrorCode::SUCCESS, retVal);
259+
}
260+
249261
TEST_F(OfflineCompilerTests, GivenArgsWhenOfflineCompilerIsCreatedThenSuccessIsReturned) {
250262
std::vector<std::string> argv = {
251263
"ocloc",

shared/offline_compiler/source/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ set(CLOC_LIB_SRCS_LIB
5959
${OCLOC_DIRECTORY}/source/offline_compiler.h
6060
${OCLOC_DIRECTORY}/source/offline_compiler_helper.cpp
6161
${OCLOC_DIRECTORY}/source/offline_compiler_options.cpp
62+
${OCLOC_DIRECTORY}/source/utilities/get_git_version_info.h
63+
${OCLOC_DIRECTORY}/source/utilities/get_git_version_info.cpp
6264
${NEO_SOURCE_DIR}/shared/source/device_binary_format/device_binary_format_zebin.cpp
6365
${NEO_SOURCE_DIR}/shared/source/device_binary_format/zebin_decoder.cpp
6466
${NEO_SOURCE_DIR}/shared/source/device_binary_format/yaml/yaml_parser.cpp

shared/offline_compiler/source/ocloc_api.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ int oclocInvoke(unsigned int numArgs, const char *argv[],
106106
return buildFatBinary(allArgs, helper.get());
107107
} else if (numArgs > 1 && ConstStringRef("validate") == allArgs[1]) {
108108
return NEO::Ocloc::validate(allArgs, helper.get());
109+
} else if (numArgs > 1 && ConstStringRef("query") == allArgs[1]) {
110+
return OfflineCompiler::query(numArgs, allArgs, helper.get());
109111
} else {
110112
int retVal = OfflineCompiler::ErrorCode::SUCCESS;
111113

shared/offline_compiler/source/offline_compiler.cpp

Lines changed: 56 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include "offline_compiler.h"
99

10+
#include "shared/offline_compiler/source/utilities/get_git_version_info.h"
1011
#include "shared/source/compiler_interface/intermediate_representations.h"
1112
#include "shared/source/debug_settings/debug_settings_manager.h"
1213
#include "shared/source/device_binary_format/device_binary_formats.h"
@@ -104,6 +105,22 @@ OfflineCompiler *OfflineCompiler::create(size_t numArgs, const std::vector<std::
104105
return pOffCompiler;
105106
}
106107

108+
int OfflineCompiler::query(size_t numArgs, const std::vector<std::string> &allArgs, OclocArgHelper *helper) {
109+
int retVal = OUT_OF_HOST_MEMORY;
110+
std::unique_ptr<OfflineCompiler> pOffCompiler{new OfflineCompiler()};
111+
112+
pOffCompiler->queryInvoke = true;
113+
pOffCompiler->argHelper = helper;
114+
retVal = pOffCompiler->initialize(numArgs, allArgs, true);
115+
116+
if (retVal != SUCCESS)
117+
return retVal;
118+
119+
retVal = pOffCompiler->performQuery();
120+
121+
return retVal;
122+
}
123+
107124
struct OfflineCompiler::buildInfo {
108125
std::unique_ptr<CIF::Builtins::BufferLatest, CIF::RAII::ReleaseHelper<CIF::Builtins::BufferLatest>> fclOptions;
109126
std::unique_ptr<CIF::Builtins::BufferLatest, CIF::RAII::ReleaseHelper<CIF::Builtins::BufferLatest>> fclInternalOptions;
@@ -360,7 +377,7 @@ int OfflineCompiler::initialize(size_t numArgs, const std::vector<std::string> &
360377
size_t sourceFromFileSize = 0;
361378
this->pBuildInfo = std::make_unique<buildInfo>();
362379
retVal = parseCommandLine(numArgs, allArgs);
363-
if (retVal != SUCCESS) {
380+
if (retVal != SUCCESS || queryInvoke) {
364381
return retVal;
365382
}
366383

@@ -585,6 +602,20 @@ int OfflineCompiler::initialize(size_t numArgs, const std::vector<std::string> &
585602
return retVal;
586603
}
587604

605+
int OfflineCompiler::performQuery() {
606+
int retVal = SUCCESS;
607+
608+
if (queryOption == QUERY_NEO_REVISION) {
609+
auto revision = NEO::getRevision();
610+
argHelper->saveOutput("NEO_REVISION", revision.c_str(), revision.size() + 1);
611+
} else {
612+
auto driverVersion = NEO::getOclDriverVersion();
613+
argHelper->saveOutput("OCL_DRIVER_VERSION", driverVersion.c_str(), driverVersion.size() + 1);
614+
}
615+
616+
return retVal;
617+
}
618+
588619
int OfflineCompiler::parseCommandLine(size_t numArgs, const std::vector<std::string> &argv) {
589620
int retVal = SUCCESS;
590621
bool compile32 = false;
@@ -598,7 +629,7 @@ int OfflineCompiler::parseCommandLine(size_t numArgs, const std::vector<std::str
598629
for (uint32_t argIndex = 1; argIndex < numArgs; argIndex++) {
599630
const auto &currArg = argv[argIndex];
600631
const bool hasMoreArgs = (argIndex + 1 < numArgs);
601-
if ("compile" == currArg) {
632+
if ("compile" == currArg || "query" == currArg) {
602633
//skip it
603634
} else if (("-file" == currArg) && hasMoreArgs) {
604635
inputFile = argv[argIndex + 1];
@@ -653,6 +684,10 @@ int OfflineCompiler::parseCommandLine(size_t numArgs, const std::vector<std::str
653684
} else if (("-revision_id" == currArg) && hasMoreArgs) {
654685
revisionId = std::stoi(argv[argIndex + 1]);
655686
argIndex++;
687+
} else if ("NEO_REVISION" == currArg && queryInvoke && !hasMoreArgs) {
688+
queryOption = QUERY_NEO_REVISION;
689+
} else if ("OCL_DRIVER_VERSION" == currArg && queryInvoke && !hasMoreArgs) {
690+
queryOption = QUERY_OCL_DRIVER_VERSION;
656691
} else {
657692
argHelper->printf("Invalid option (arg %d): %s\n", argIndex, argv[argIndex].c_str());
658693
retVal = INVALID_COMMAND_LINE;
@@ -661,18 +696,25 @@ int OfflineCompiler::parseCommandLine(size_t numArgs, const std::vector<std::str
661696
}
662697

663698
if (retVal == SUCCESS) {
664-
if (compile32 && compile64) {
665-
argHelper->printf("Error: Cannot compile for 32-bit and 64-bit, please choose one.\n");
666-
retVal = INVALID_COMMAND_LINE;
667-
} else if (inputFile.empty()) {
668-
argHelper->printf("Error: Input file name missing.\n");
669-
retVal = INVALID_COMMAND_LINE;
670-
} else if (deviceName.empty() && (false == onlySpirV)) {
671-
argHelper->printf("Error: Device name missing.\n");
672-
retVal = INVALID_COMMAND_LINE;
673-
} else if (!argHelper->fileExists(inputFile)) {
674-
argHelper->printf("Error: Input file %s missing.\n", inputFile.c_str());
675-
retVal = INVALID_FILE;
699+
if (queryInvoke) {
700+
if (queryOption == QUERY_LAST) {
701+
argHelper->printf("Error: no options for query provided.\n");
702+
retVal = INVALID_COMMAND_LINE;
703+
}
704+
} else {
705+
if (compile32 && compile64) {
706+
argHelper->printf("Error: Cannot compile for 32-bit and 64-bit, please choose one.\n");
707+
retVal = INVALID_COMMAND_LINE;
708+
} else if (inputFile.empty()) {
709+
argHelper->printf("Error: Input file name missing.\n");
710+
retVal = INVALID_COMMAND_LINE;
711+
} else if (deviceName.empty() && (false == onlySpirV)) {
712+
argHelper->printf("Error: Device name missing.\n");
713+
retVal = INVALID_COMMAND_LINE;
714+
} else if (!argHelper->fileExists(inputFile)) {
715+
argHelper->printf("Error: Input file %s missing.\n", inputFile.c_str());
716+
retVal = INVALID_FILE;
717+
}
676718
}
677719
}
678720

shared/offline_compiler/source/offline_compiler.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,14 @@ class OfflineCompiler {
4343
INVALID_FILE = -5151,
4444
PRINT_USAGE = -5152,
4545
};
46+
enum QueryOption {
47+
QUERY_OCL_DRIVER_VERSION = 0,
48+
QUERY_NEO_REVISION = 1,
49+
QUERY_LAST,
50+
};
4651

4752
static OfflineCompiler *create(size_t numArgs, const std::vector<std::string> &allArgs, bool dumpFiles, int &retVal, OclocArgHelper *helper);
53+
static int query(size_t numArgs, const std::vector<std::string> &allArgs, OclocArgHelper *helper);
4854
int build();
4955
std::string &getBuildLog();
5056
void printUsage();
@@ -81,6 +87,7 @@ class OfflineCompiler {
8187
std::string getStringWithinDelimiters(const std::string &src);
8288
int initialize(size_t numArgs, const std::vector<std::string> &allArgs, bool dumpFiles);
8389
int parseCommandLine(size_t numArgs, const std::vector<std::string> &allArgs);
90+
int performQuery();
8491
void setStatelessToStatefullBufferOffsetFlag();
8592
void resolveExtraSettings();
8693
void parseDebugSettings();
@@ -124,6 +131,8 @@ class OfflineCompiler {
124131
bool inputFileSpirV = false;
125132
bool outputNoSuffix = false;
126133
bool forceStatelessToStatefulOptimization = false;
134+
bool queryInvoke = false;
135+
int queryOption = QUERY_LAST;
127136

128137
std::vector<uint8_t> elfBinary;
129138
char *genBinary = nullptr;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright (C) 2021 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: MIT
5+
*
6+
*/
7+
8+
#include "driver_version.h"
9+
10+
#include <string>
11+
12+
#ifdef QTR
13+
#undef QTR
14+
#endif
15+
16+
#ifdef TOSTR
17+
#undef TOSTR
18+
#endif
19+
20+
#define QTR(a) #a
21+
#define TOSTR(b) QTR(b)
22+
23+
namespace NEO {
24+
25+
std::string getRevision() {
26+
#ifdef NEO_REVISION
27+
return NEO_REVISION;
28+
#else
29+
return "";
30+
#endif
31+
}
32+
33+
std::string getOclDriverVersion() {
34+
#ifdef NEO_OCL_DRIVER_VERSION
35+
return TOSTR(NEO_OCL_DRIVER_VERSION);
36+
#else
37+
return "";
38+
#endif
39+
}
40+
41+
} // namespace NEO
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* Copyright (C) 2021 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: MIT
5+
*
6+
*/
7+
8+
#pragma once
9+
#include <string>
10+
11+
namespace NEO {
12+
extern std::string getRevision();
13+
extern std::string getOclDriverVersion();
14+
} // namespace NEO

0 commit comments

Comments
 (0)