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 68d46339f4..1a8d9fddf7 100644 --- a/opencl/test/unit_test/offline_compiler/offline_compiler_tests.cpp +++ b/opencl/test/unit_test/offline_compiler/offline_compiler_tests.cpp @@ -1242,6 +1242,101 @@ TEST_F(OfflineCompilerTests, givenDebugOptionThenInternalOptionShouldContainKern EXPECT_TRUE(hasSubstr(internalOptions, "-cl-kernel-debug-enable")); } +TEST_F(OfflineCompilerTests, givenDebugOptionAndNonSpirvInputThenOptionsShouldContainDashSOptionAppendedAutomatically) { + auto mockOfflineCompiler = std::unique_ptr(new MockOfflineCompiler()); + mockOfflineCompiler->uniqueHelper->callBaseFileExists = false; + mockOfflineCompiler->uniqueHelper->callBaseLoadDataFromFile = false; + mockOfflineCompiler->uniqueHelper->filesMap["some_input.cl"] = ""; + + std::vector argv = { + "ocloc", + "-q", + "-options", + "-g", + "-file", + "some_input.cl", + "-device", + gEnvironment->devicePrefix.c_str()}; + + mockOfflineCompiler->initialize(argv.size(), argv); + const auto &options = mockOfflineCompiler->options; + std::string appendedOption{"-s \"some_input.cl\""}; + EXPECT_TRUE(hasSubstr(options, appendedOption)); +} + +TEST_F(OfflineCompilerTests, givenDebugOptionAndNonSpirvInputWhenFilenameIsSeparatedWithSpacesThenAppendedSourcePathIsSetCorrectly) { + auto mockOfflineCompiler = std::unique_ptr(new MockOfflineCompiler()); + mockOfflineCompiler->uniqueHelper->callBaseFileExists = false; + mockOfflineCompiler->uniqueHelper->callBaseLoadDataFromFile = false; + mockOfflineCompiler->uniqueHelper->filesMap["filename with spaces.cl"] = ""; + + std::vector argv = { + "ocloc", + "-q", + "-options", + "-g", + "-file", + "filename with spaces.cl", + "-device", + gEnvironment->devicePrefix.c_str()}; + + const auto result = mockOfflineCompiler->initialize(argv.size(), argv); + EXPECT_EQ(OclocErrorCode::SUCCESS, result); + const auto &options = mockOfflineCompiler->options; + std::string appendedOption{"-s \"filename with spaces.cl\""}; + EXPECT_TRUE(hasSubstr(options, appendedOption)); +} + +TEST_F(OfflineCompilerTests, givenDebugOptionAndSpirvInputThenDoNotAppendDashSOptionAutomatically) { + auto mockOfflineCompiler = std::unique_ptr(new MockOfflineCompiler()); + mockOfflineCompiler->uniqueHelper->callBaseFileExists = false; + mockOfflineCompiler->uniqueHelper->callBaseLoadDataFromFile = false; + mockOfflineCompiler->uniqueHelper->filesMap["some_input.spirv"] = ""; + + std::vector argvSpirvInput = { + "ocloc", + "-q", + "-spirv_input", + "-options", + "-g", + "-file", + "some_input.spirv", + "-device", + gEnvironment->devicePrefix.c_str()}; + + mockOfflineCompiler->initialize(argvSpirvInput.size(), argvSpirvInput); + const auto &options = mockOfflineCompiler->options; + std::string notAppendedOption{"-s \"some_input.spirv\""}; + EXPECT_FALSE(hasSubstr(options, notAppendedOption)); +} + +TEST_F(OfflineCompilerTests, givenDebugOptionAndDashSOptionPassedManuallyThenDoNotAppendDashSOptionAutomatically) { + auto mockOfflineCompiler = std::unique_ptr(new MockOfflineCompiler()); + mockOfflineCompiler->uniqueHelper->callBaseFileExists = false; + mockOfflineCompiler->uniqueHelper->callBaseLoadDataFromFile = false; + mockOfflineCompiler->uniqueHelper->filesMap["some_input.cl"] = ""; + std::vector argvDashSPassed = { + "ocloc", + "-q", + "-options", + "-g -s \"mockPath/some_input.cl\"", + "-file", + "some_input.cl", + "-device", + gEnvironment->devicePrefix.c_str()}; + + mockOfflineCompiler->initialize(argvDashSPassed.size(), argvDashSPassed); + const auto &options = mockOfflineCompiler->options; + std::string appendedOption{"-s"}; + auto occurrences = 0u; + size_t pos = 0u; + while ((pos = options.find(appendedOption, pos)) != std::string::npos) { + occurrences++; + pos++; + } + EXPECT_EQ(1u, occurrences); +} + TEST_F(OfflineCompilerTests, givenDashGInBiggerOptionStringWhenInitializingThenInternalOptionsShouldNotContainKernelDebugEnable) { std::vector argv = { diff --git a/shared/offline_compiler/source/offline_compiler.cpp b/shared/offline_compiler/source/offline_compiler.cpp index a5d298a25b..cd6ea6dde7 100644 --- a/shared/offline_compiler/source/offline_compiler.cpp +++ b/shared/offline_compiler/source/offline_compiler.cpp @@ -576,6 +576,12 @@ int OfflineCompiler::initialize(size_t numArgs, const std::vector & if (hwInfo.platform.eRenderCoreFamily >= IGFX_GEN9_CORE) { internalOptions = CompilerOptions::concatenate(internalOptions, CompilerOptions::debugKernelEnable); } + if (false == inputFileSpirV && false == CompilerOptions::contains(options, CompilerOptions::generateSourcePath)) { + auto sourcePathStringOption = CompilerOptions::generateSourcePath.str(); + sourcePathStringOption.append(" "); + sourcePathStringOption.append(CompilerOptions::wrapInQuotes(inputFile)); + options = CompilerOptions::concatenate(options, sourcePathStringOption); + } } if (deviceName.empty()) { diff --git a/shared/source/compiler_interface/compiler_options.cpp b/shared/source/compiler_interface/compiler_options.cpp index a183ceab87..0c9ba054fe 100644 --- a/shared/source/compiler_interface/compiler_options.cpp +++ b/shared/source/compiler_interface/compiler_options.cpp @@ -30,6 +30,11 @@ bool contains(const std::string &options, ConstStringRef optionToFind) { return contains(options.c_str(), optionToFind); } +std::string wrapInQuotes(const std::string &stringToWrap) { + std::string quoteEscape{"\""}; + return std::string{quoteEscape + stringToWrap + quoteEscape}; +} + TokenizedString tokenize(ConstStringRef src, char sperator) { TokenizedString ret; const char *it = src.begin(); diff --git a/shared/source/compiler_interface/compiler_options.h b/shared/source/compiler_interface/compiler_options.h index c65474d4f1..5821814fb2 100644 --- a/shared/source/compiler_interface/compiler_options.h +++ b/shared/source/compiler_interface/compiler_options.h @@ -29,6 +29,7 @@ constexpr ConstStringRef fastRelaxedMath = "-cl-fast-relaxed-math"; constexpr ConstStringRef preserveVec3Type = "-fpreserve-vec3-type"; constexpr ConstStringRef createLibrary = "-create-library"; constexpr ConstStringRef generateDebugInfo = "-g"; +constexpr ConstStringRef generateSourcePath = "-s"; constexpr ConstStringRef bindlessMode = "-cl-intel-use-bindless-mode -cl-intel-use-bindless-advanced-mode"; constexpr ConstStringRef uniformWorkgroupSize = "-cl-uniform-work-group-size"; constexpr ConstStringRef forceEmuInt32DivRem = "-cl-intel-force-emu-int32divrem"; @@ -176,6 +177,8 @@ bool contains(const char *options, ConstStringRef optionToFind); bool contains(const std::string &options, ConstStringRef optionToFind); +std::string wrapInQuotes(const std::string &stringToWrap); + using TokenizedString = StackVec; TokenizedString tokenize(ConstStringRef src, char sperator = ' '); } // namespace CompilerOptions