diff --git a/opencl/test/unit_test/offline_compiler/ocloc_api_tests.cpp b/opencl/test/unit_test/offline_compiler/ocloc_api_tests.cpp index 7ec0bb00bc..2d82b9c3b1 100644 --- a/opencl/test/unit_test/offline_compiler/ocloc_api_tests.cpp +++ b/opencl/test/unit_test/offline_compiler/ocloc_api_tests.cpp @@ -272,3 +272,37 @@ __kernel void k(){ EXPECT_EQ(sourcesLen[0], sourceSection->data.size()); EXPECT_STREQ(headerB, reinterpret_cast(headerBSection->data.begin())); } + +TEST(OclocApiTests, GivenHelpParameterWhenDecodingThenHelpMsgIsPrintedAndSuccessIsReturned) { + const char *argv[] = { + "ocloc", + "disasm", + "--help"}; + 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_FALSE(output.empty()); + EXPECT_EQ(retVal, NEO::OfflineCompiler::ErrorCode::SUCCESS); +} + +TEST(OclocApiTests, GivenHelpParameterWhenEncodingThenHelpMsgIsPrintedAndSuccessIsReturned) { + const char *argv[] = { + "ocloc", + "asm", + "--help"}; + 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_FALSE(output.empty()); + EXPECT_EQ(retVal, NEO::OfflineCompiler::ErrorCode::SUCCESS); +} diff --git a/opencl/test/unit_test/offline_compiler/offline_compiler_tests.cpp b/opencl/test/unit_test/offline_compiler/offline_compiler_tests.cpp index 1427cd5cce..b62bf47341 100644 --- a/opencl/test/unit_test/offline_compiler/offline_compiler_tests.cpp +++ b/opencl/test/unit_test/offline_compiler/offline_compiler_tests.cpp @@ -583,9 +583,8 @@ TEST_F(OfflineCompilerTests, GivenHelpOptionThenBuildDoesNotOccur) { testing::internal::CaptureStdout(); pOfflineCompiler = OfflineCompiler::create(argv.size(), argv, true, retVal, oclocArgHelperWithoutInput.get()); std::string output = testing::internal::GetCapturedStdout(); - EXPECT_EQ(nullptr, pOfflineCompiler); EXPECT_STRNE("", output.c_str()); - EXPECT_EQ(OfflineCompiler::ErrorCode::PRINT_USAGE, retVal); + EXPECT_EQ(OfflineCompiler::ErrorCode::SUCCESS, retVal); delete pOfflineCompiler; } diff --git a/shared/offline_compiler/source/decoder/binary_decoder.cpp b/shared/offline_compiler/source/decoder/binary_decoder.cpp index 225586f5ae..b6e1153a02 100644 --- a/shared/offline_compiler/source/decoder/binary_decoder.cpp +++ b/shared/offline_compiler/source/decoder/binary_decoder.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2020 Intel Corporation + * Copyright (C) 2018-2021 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -503,11 +503,6 @@ uint32_t BinaryDecoder::readStructFields(const std::vector &patchLi } int BinaryDecoder::validateInput(const std::vector &args) { - if (args[args.size() - 1] == "-help") { - printHelp(); - return -1; - } - for (size_t argIndex = 2; argIndex < args.size(); ++argIndex) { const auto &currArg = args[argIndex]; const bool hasMoreArgs = (argIndex + 1 < args.size()); @@ -521,6 +516,9 @@ int BinaryDecoder::validateInput(const std::vector &args) { } else if ("-dump" == currArg && hasMoreArgs) { pathToDump = args[++argIndex]; addSlash(pathToDump); + } else if ("--help" == currArg) { + showHelp = true; + return 0; } else if ("-ignore_isa_padding" == currArg) { ignoreIsaPadding = true; } else if ("-q" == currArg) { @@ -528,13 +526,11 @@ int BinaryDecoder::validateInput(const std::vector &args) { iga->setMessagePrinter(argHelper->getPrinterRef()); } else { argHelper->printf("Unknown argument %s\n", currArg.c_str()); - printHelp(); return -1; } } if (binaryFile.find(".bin") == std::string::npos) { argHelper->printf(".bin extension is expected for binary file.\n"); - printHelp(); return -1; } if (false == iga->isKnownPlatform()) { diff --git a/shared/offline_compiler/source/decoder/binary_decoder.h b/shared/offline_compiler/source/decoder/binary_decoder.h index c31c9760c5..5d5a761e26 100644 --- a/shared/offline_compiler/source/decoder/binary_decoder.h +++ b/shared/offline_compiler/source/decoder/binary_decoder.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2020 Intel Corporation + * Copyright (C) 2018-2021 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -40,6 +40,9 @@ class BinaryDecoder { int decode(); int validateInput(const std::vector &args); + bool showHelp = false; + void printHelp(); + protected: OclocArgHelper *argHelper = nullptr; bool ignoreIsaPadding = false; @@ -54,7 +57,6 @@ class BinaryDecoder { const void *getDevBinary(); std::vector loadPatchList(); void parseTokens(); - void printHelp(); int processBinary(const void *&ptr, std::ostream &ptmFile); void processKernel(const void *&ptr, std::ostream &ptmFile); void readPatchTokens(const void *&patchListPtr, uint32_t patchListSize, std::ostream &ptmFile); diff --git a/shared/offline_compiler/source/decoder/binary_encoder.cpp b/shared/offline_compiler/source/decoder/binary_encoder.cpp index 735f9cb603..13da83de58 100644 --- a/shared/offline_compiler/source/decoder/binary_encoder.cpp +++ b/shared/offline_compiler/source/decoder/binary_encoder.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2020 Intel Corporation + * Copyright (C) 2018-2021 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -305,11 +305,6 @@ int BinaryEncoder::processKernel(size_t &line, const std::vector &p } int BinaryEncoder::validateInput(const std::vector &args) { - if ("-help" == args[args.size() - 1]) { - printHelp(); - return -1; - } - for (size_t argIndex = 2; argIndex < args.size(); ++argIndex) { const auto &currArg = args[argIndex]; const bool hasMoreArgs = (argIndex + 1 < args.size()); @@ -320,6 +315,9 @@ int BinaryEncoder::validateInput(const std::vector &args) { iga->setProductFamily(getProductFamilyFromDeviceName(args[++argIndex])); } else if ("-out" == currArg && hasMoreArgs) { elfName = args[++argIndex]; + } else if ("--help" == currArg) { + showHelp = true; + return 0; } else if ("-ignore_isa_padding" == currArg) { ignoreIsaPadding = true; } else if ("-q" == currArg) { @@ -327,7 +325,6 @@ int BinaryEncoder::validateInput(const std::vector &args) { iga->setMessagePrinter(argHelper->getPrinterRef()); } else { argHelper->printf("Unknown argument %s\n", currArg.c_str()); - printHelp(); return -1; } } @@ -340,7 +337,6 @@ int BinaryEncoder::validateInput(const std::vector &args) { } if (elfName.find(".bin") == std::string::npos) { argHelper->printf(".bin extension is expected for binary file.\n"); - printHelp(); return -1; } diff --git a/shared/offline_compiler/source/decoder/binary_encoder.h b/shared/offline_compiler/source/decoder/binary_encoder.h index 550e83460f..fea35ae0a9 100644 --- a/shared/offline_compiler/source/decoder/binary_encoder.h +++ b/shared/offline_compiler/source/decoder/binary_encoder.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2020 Intel Corporation + * Copyright (C) 2018-2021 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -25,18 +25,21 @@ class BinaryEncoder { int encode(); int validateInput(const std::vector &args); + bool showHelp = false; + void printHelp(); + protected: OclocArgHelper *argHelper = nullptr; bool ignoreIsaPadding = false; std::string pathToDump, elfName; std::unique_ptr iga; + void calculatePatchListSizes(std::vector &ptmFile); MOCKABLE_VIRTUAL bool copyBinaryToBinary(const std::string &srcFileName, std::ostream &outBinary, uint32_t *binaryLength); bool copyBinaryToBinary(const std::string &srcFileName, std::ostream &outBinary) { return copyBinaryToBinary(srcFileName, outBinary, nullptr); } int createElf(std::stringstream &deviceBinary); - void printHelp(); int processBinary(const std::vector &ptmFile, std::ostream &deviceBinary); int processKernel(size_t &i, const std::vector &ptmFileLines, std::ostream &deviceBinary); template diff --git a/shared/offline_compiler/source/ocloc_api.cpp b/shared/offline_compiler/source/ocloc_api.cpp index e5b4a53cb5..6089e527b6 100644 --- a/shared/offline_compiler/source/ocloc_api.cpp +++ b/shared/offline_compiler/source/ocloc_api.cpp @@ -89,6 +89,12 @@ int oclocInvoke(unsigned int numArgs, const char *argv[], } else if (numArgs > 1 && ConstStringRef("disasm") == allArgs[1]) { BinaryDecoder disasm(helper.get()); int retVal = disasm.validateInput(allArgs); + + if (disasm.showHelp) { + disasm.printHelp(); + return retVal; + } + if (retVal == 0) { return disasm.decode(); } else { @@ -97,6 +103,12 @@ int oclocInvoke(unsigned int numArgs, const char *argv[], } else if (numArgs > 1 && ConstStringRef("asm") == allArgs[1]) { BinaryEncoder assembler(helper.get()); int retVal = assembler.validateInput(allArgs); + + if (assembler.showHelp) { + assembler.printHelp(); + return retVal; + } + if (retVal == 0) { return assembler.encode(); } else { diff --git a/shared/offline_compiler/source/offline_compiler.cpp b/shared/offline_compiler/source/offline_compiler.cpp index 0ffc3bbca6..3b807bb47b 100644 --- a/shared/offline_compiler/source/offline_compiler.cpp +++ b/shared/offline_compiler/source/offline_compiler.cpp @@ -391,7 +391,10 @@ int OfflineCompiler::initialize(size_t numArgs, const std::vector & size_t sourceFromFileSize = 0; this->pBuildInfo = std::make_unique(); retVal = parseCommandLine(numArgs, allArgs); - if (retVal != SUCCESS) { + if (showHelp) { + printUsage(); + return retVal; + } else if (retVal != SUCCESS) { return retVal; } @@ -622,8 +625,8 @@ int OfflineCompiler::parseCommandLine(size_t numArgs, const std::vectorprintf("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)) { + retVal |= INVALID_COMMAND_LINE; + } + + if (deviceName.empty() && (false == onlySpirV)) { argHelper->printf("Error: Device name missing.\n"); retVal = INVALID_COMMAND_LINE; + } + + if (inputFile.empty()) { + argHelper->printf("Error: Input file 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; @@ -895,7 +902,7 @@ Usage: ocloc [compile] -file -device [-output &allArgs, OclocArgHelper *helper); @@ -126,8 +125,8 @@ class OfflineCompiler { std::string internalOptions; std::string sourceCode; std::string buildLog; - bool dumpFiles = true; + bool dumpFiles = true; bool useLlvmText = false; bool useLlvmBc = false; bool useCppFile = false; @@ -138,13 +137,14 @@ class OfflineCompiler { bool inputFileSpirV = false; bool outputNoSuffix = false; bool forceStatelessToStatefulOptimization = false; + bool isSpirV = false; + bool showHelp = false; std::vector elfBinary; char *genBinary = nullptr; size_t genBinarySize = 0; char *irBinary = nullptr; size_t irBinarySize = 0; - bool isSpirV = false; char *debugDataBinary = nullptr; size_t debugDataBinarySize = 0; struct buildInfo;