Skip to content

Commit dd393d1

Browse files
Ocloc: allow enforcing binary format specified by user
This commit adds ocloc option for specifying binary format. When a --format flag is passed, the internal options will be changed accordingly to the format specified (zebin/patchtokens) or a warning will be printed if an unknown format will be passed. Signed-off-by: Kacper Nowak <[email protected]>
1 parent bd6c9a2 commit dd393d1

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

opencl/test/unit_test/offline_compiler/offline_compiler_tests.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2777,6 +2777,57 @@ TEST(OclocCompile, givenSpirvInputThenDontGenerateSpirvFile) {
27772777
EXPECT_FALSE(compilerOutputExists("offline_compiler_test/binary_with_zeroes", "spv"));
27782778
}
27792779

2780+
TEST(OclocCompile, givenFormatFlagWithKnownFormatPassedThenEnforceSpecifiedFormatAccordingly) {
2781+
MockOfflineCompiler ocloc;
2782+
2783+
std::vector<std::string> argvEnforcedFormatPatchtokens = {
2784+
"ocloc",
2785+
"-q",
2786+
"-file",
2787+
"test_files/copybuffer.cl",
2788+
"-spv_only",
2789+
"--format",
2790+
"patchtokens"};
2791+
2792+
std::vector<std::string> argvEnforcedFormatZebin = {
2793+
"ocloc",
2794+
"-q",
2795+
"-file",
2796+
"test_files/copybuffer.cl",
2797+
"-spv_only",
2798+
"--format",
2799+
"zebin"};
2800+
2801+
int retVal = ocloc.initialize(argvEnforcedFormatZebin.size(), argvEnforcedFormatZebin);
2802+
ASSERT_EQ(0, retVal);
2803+
EXPECT_TRUE(hasSubstr(ocloc.internalOptions, std::string{CompilerOptions::allowZebin}));
2804+
2805+
ocloc.internalOptions.clear();
2806+
retVal = ocloc.initialize(argvEnforcedFormatPatchtokens.size(), argvEnforcedFormatPatchtokens);
2807+
ASSERT_EQ(0, retVal);
2808+
EXPECT_FALSE(hasSubstr(ocloc.internalOptions, std::string{CompilerOptions::allowZebin}));
2809+
}
2810+
2811+
TEST(OclocCompile, givenFormatFlagWithUnknownFormatPassedThenPrintWarning) {
2812+
MockOfflineCompiler ocloc;
2813+
2814+
std::vector<std::string> argvUnknownFormatEnforced = {
2815+
"ocloc",
2816+
"-file",
2817+
"test_files/copybuffer.cl",
2818+
"-spv_only",
2819+
"--format",
2820+
"banana"};
2821+
2822+
::testing::internal::CaptureStdout();
2823+
int retVal = ocloc.initialize(argvUnknownFormatEnforced.size(), argvUnknownFormatEnforced);
2824+
const auto output = testing::internal::GetCapturedStdout();
2825+
ASSERT_EQ(0, retVal);
2826+
2827+
const auto expectedOutput{"Invalid format passed: banana. Ignoring.\n"};
2828+
EXPECT_EQ(expectedOutput, output);
2829+
}
2830+
27802831
TEST(OfflineCompilerTest, GivenDebugFlagWhenSetStatelessToStatefullBufferOffsetFlagThenStatelessToStatefullOptimizationIsSetCorrectly) {
27812832
DebugManagerStateRestore stateRestore;
27822833
MockOfflineCompiler mockOfflineCompiler;

shared/offline_compiler/source/offline_compiler.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,10 @@ int OfflineCompiler::initialize(size_t numArgs, const std::vector<std::string> &
487487
return retVal;
488488
}
489489

490+
if (!formatToEnforce.empty()) {
491+
enforceFormat(formatToEnforce);
492+
}
493+
490494
if (CompilerOptions::contains(options, CompilerOptions::generateDebugInfo.str())) {
491495
if (hwInfo.platform.eRenderCoreFamily >= IGFX_GEN9_CORE) {
492496
internalOptions = CompilerOptions::concatenate(internalOptions, CompilerOptions::debugKernelEnable);
@@ -615,6 +619,9 @@ int OfflineCompiler::parseCommandLine(size_t numArgs, const std::vector<std::str
615619
argIndex++;
616620
} else if ("-exclude_ir" == currArg) {
617621
excludeIr = true;
622+
} else if ("--format" == currArg) {
623+
formatToEnforce = argv[argIndex + 1];
624+
argIndex++;
618625
} else {
619626
argHelper->printf("Invalid option (arg %d): %s\n", argIndex, argv[argIndex].c_str());
620627
retVal = INVALID_COMMAND_LINE;
@@ -947,6 +954,10 @@ Usage: ocloc [compile] -file <filename> -device <device_type> [-output <filename
947954
948955
-exclude_ir Excludes IR from the output binary file.
949956
957+
--format Enforce given binary format. The possible values are:
958+
--format zebin - Enforce generating zebin binary
959+
--format patchtokens - Enforce generating patchtokens (legacy) binary.
960+
950961
Examples :
951962
Compile file to Intel Compute GPU device binary (out = source_file_Gen9core.bin)
952963
ocloc -file source_file.cl -device skl
@@ -1124,6 +1135,17 @@ bool OfflineCompiler::readOptionsFromFile(std::string &options, const std::strin
11241135
return true;
11251136
}
11261137

1138+
void OfflineCompiler::enforceFormat(std::string &format) {
1139+
std::transform(format.begin(), format.end(), format.begin(),
1140+
[](auto c) { return std::tolower(c); });
1141+
if (format == "zebin") {
1142+
CompilerOptions::concatenateAppend(internalOptions, CompilerOptions::allowZebin);
1143+
} else if (format == "patchtokens") {
1144+
} else {
1145+
argHelper->printf("Invalid format passed: %s. Ignoring.\n", format.c_str());
1146+
}
1147+
}
1148+
11271149
std::string generateFilePath(const std::string &directory, const std::string &fileNameBase, const char *extension) {
11281150
UNRECOVERABLE_IF(extension == nullptr);
11291151

shared/offline_compiler/source/offline_compiler.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ class OfflineCompiler {
117117
MOCKABLE_VIRTUAL void writeOutAllFiles();
118118
MOCKABLE_VIRTUAL void createDir(const std::string &path);
119119
void unifyExcludeIrFlags();
120+
void enforceFormat(std::string &format);
120121
HardwareInfo hwInfo;
121122

122123
PRODUCT_CONFIG deviceConfig = UNKNOWN_ISA;
@@ -131,6 +132,7 @@ class OfflineCompiler {
131132
std::string buildLog;
132133
std::string optionsReadFromFile = "";
133134
std::string internalOptionsReadFromFile = "";
135+
std::string formatToEnforce = "";
134136

135137
bool dumpFiles = true;
136138
bool useLlvmText = false;

0 commit comments

Comments
 (0)