From 664b33787d6cc3383fbb746682c4339c0bee6ba5 Mon Sep 17 00:00:00 2001 From: Mateusz Jablonski Date: Tue, 19 Oct 2021 19:21:38 +0000 Subject: [PATCH] Move appending compiler extensions to compiler parser Signed-off-by: Mateusz Jablonski --- .../compiler_interface/CMakeLists.txt | 1 + .../cl_compiler_interface_tests.cpp | 83 +++++++++++++++++++ .../source/offline_compiler.cpp | 23 +---- .../helpers/compiler_options_parser.cpp | 23 ++++- .../source/helpers/compiler_options_parser.h | 5 +- .../compiler_interface_tests.cpp | 36 +------- 6 files changed, 117 insertions(+), 54 deletions(-) create mode 100644 opencl/test/unit_test/compiler_interface/cl_compiler_interface_tests.cpp diff --git a/opencl/test/unit_test/compiler_interface/CMakeLists.txt b/opencl/test/unit_test/compiler_interface/CMakeLists.txt index 0e3bf28839..64baed3ea7 100644 --- a/opencl/test/unit_test/compiler_interface/CMakeLists.txt +++ b/opencl/test/unit_test/compiler_interface/CMakeLists.txt @@ -6,6 +6,7 @@ set(IGDRCL_SRCS_tests_compiler_interface ${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt + ${CMAKE_CURRENT_SOURCE_DIR}/cl_compiler_interface_tests.cpp ${CMAKE_CURRENT_SOURCE_DIR}/default_cl_cache_config_tests.cpp ) diff --git a/opencl/test/unit_test/compiler_interface/cl_compiler_interface_tests.cpp b/opencl/test/unit_test/compiler_interface/cl_compiler_interface_tests.cpp new file mode 100644 index 0000000000..47dca4f706 --- /dev/null +++ b/opencl/test/unit_test/compiler_interface/cl_compiler_interface_tests.cpp @@ -0,0 +1,83 @@ +/* + * Copyright (C) 2021 Intel Corporation + * + * SPDX-License-Identifier: MIT + * + */ + +#include "shared/source/helpers/file_io.h" +#include "shared/test/common/helpers/test_files.h" +#include "shared/test/common/libult/global_environment.h" +#include "shared/test/common/mocks/mock_compiler_interface.h" + +#include "opencl/test/unit_test/fixtures/cl_device_fixture.h" +#include "test.h" + +using namespace NEO; + +class ClCompilerInterfaceTest : public ClDeviceFixture, + public ::testing::Test { + public: + void SetUp() override { + ClDeviceFixture::SetUp(); + + // create the compiler interface + this->pCompilerInterface = new MockCompilerInterface(); + bool initRet = pCompilerInterface->initialize(std::make_unique(CompilerCacheConfig{}), true); + ASSERT_TRUE(initRet); + pDevice->getExecutionEnvironment()->rootDeviceEnvironments[pDevice->getRootDeviceIndex()]->compilerInterface.reset(pCompilerInterface); + + std::string testFile; + + testFile.append(clFiles); + testFile.append("CopyBuffer_simd16.cl"); + + pSource = loadDataFromFile( + testFile.c_str(), + sourceSize); + + ASSERT_NE(0u, sourceSize); + ASSERT_NE(nullptr, pSource); + + inputArgs.src = ArrayRef(pSource.get(), sourceSize); + inputArgs.internalOptions = ArrayRef(pClDevice->peekCompilerExtensions().c_str(), pClDevice->peekCompilerExtensions().size()); + } + + void TearDown() override { + pSource.reset(); + + ClDeviceFixture::TearDown(); + } + + MockCompilerInterface *pCompilerInterface; + TranslationInput inputArgs = {IGC::CodeType::oclC, IGC::CodeType::oclGenBin}; + std::unique_ptr pSource = nullptr; + size_t sourceSize = 0; +}; + +TEST_F(ClCompilerInterfaceTest, WhenBuildIsInvokedThenFclReceivesListOfExtensionsInInternalOptions) { + std::string receivedInternalOptions; + + auto debugVars = NEO::getFclDebugVars(); + debugVars.receivedInternalOptionsOutput = &receivedInternalOptions; + gEnvironment->fclPushDebugVars(debugVars); + TranslationOutput translationOutput = {}; + auto err = pCompilerInterface->build(*pDevice, inputArgs, translationOutput); + EXPECT_EQ(TranslationOutput::ErrorCode::Success, err); + EXPECT_THAT(receivedInternalOptions, testing::HasSubstr(pClDevice->peekCompilerExtensions())); + gEnvironment->fclPopDebugVars(); +} + +TEST_F(ClCompilerInterfaceTest, WhenCompileIsInvokedThenFclReceivesListOfExtensionsInInternalOptions) { + std::string receivedInternalOptions; + + MockCompilerDebugVars fclDebugVars; + retrieveBinaryKernelFilename(fclDebugVars.fileName, "CopyBuffer_simd16_", ".bc"); + fclDebugVars.receivedInternalOptionsOutput = &receivedInternalOptions; + gEnvironment->fclPushDebugVars(fclDebugVars); + TranslationOutput translationOutput = {}; + auto err = pCompilerInterface->compile(*pDevice, inputArgs, translationOutput); + EXPECT_EQ(TranslationOutput::ErrorCode::Success, err); + EXPECT_THAT(receivedInternalOptions, testing::HasSubstr(pClDevice->peekCompilerExtensions())); + gEnvironment->fclPopDebugVars(); +} diff --git a/shared/offline_compiler/source/offline_compiler.cpp b/shared/offline_compiler/source/offline_compiler.cpp index c317ca1ec1..6d040b41ed 100644 --- a/shared/offline_compiler/source/offline_compiler.cpp +++ b/shared/offline_compiler/source/offline_compiler.cpp @@ -25,8 +25,6 @@ #include "shared/source/os_interface/os_inc_base.h" #include "shared/source/os_interface/os_library.h" -#include "opencl/source/platform/extensions.h" - #include "cif/common/cif_main.h" #include "cif/helpers/error.h" #include "cif/import/library_api.h" @@ -172,7 +170,7 @@ int OfflineCompiler::buildIrBinary() { if (true == NEO::areNotNullptr(err->GetMemory())) { updateBuildLog(err->GetMemory(), err->GetSizeRaw()); - retVal = CL_BUILD_PROGRAM_FAILURE; + retVal = BUILD_PROGRAM_FAILURE; return retVal; } @@ -451,22 +449,7 @@ int OfflineCompiler::initialize(size_t numArgs, const std::vector & internalOptions = CompilerOptions::concatenate("-ocl-version=300 -cl-ext=-all,+cl_khr_3d_image_writes", internalOptions); CompilerOptions::concatenateAppend(internalOptions, CompilerOptions::enableImageSupport); } else { - std::string extensionsList = getExtensionsList(hwInfo); - if (requiresAdditionalExtensions(options)) { - extensionsList += "cl_khr_3d_image_writes "; - } - OpenClCFeaturesContainer openclCFeatures; - if (requiresOpenClCFeatures(options)) { - getOpenclCFeaturesList(hwInfo, openclCFeatures); - } - - auto compilerExtensions = convertEnabledExtensionsToCompilerInternalOptions(extensionsList.c_str(), openclCFeatures); - auto oclVersion = getOclVersionCompilerInternalOption(hwInfo.capabilityTable.clVersionSupport); - internalOptions = CompilerOptions::concatenate(oclVersion, compilerExtensions, internalOptions); - - if (hwInfo.capabilityTable.supportsImages) { - CompilerOptions::concatenateAppend(internalOptions, CompilerOptions::enableImageSupport); - } + appendExtensionsToInternalOptions(hwInfo, options, internalOptions); } parseDebugSettings(); @@ -967,7 +950,7 @@ void OfflineCompiler::storeBinary( delete[] pDst; pDst = new char[srcSize]; - dstSize = (cl_uint)srcSize; + dstSize = static_cast(srcSize); memcpy_s(pDst, dstSize, pSrc, srcSize); } diff --git a/shared/source/helpers/compiler_options_parser.cpp b/shared/source/helpers/compiler_options_parser.cpp index 3dce3f253e..294ea6ef3e 100644 --- a/shared/source/helpers/compiler_options_parser.cpp +++ b/shared/source/helpers/compiler_options_parser.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2020 Intel Corporation + * Copyright (C) 2020-2021 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -7,6 +7,10 @@ #include "shared/source/helpers/compiler_options_parser.h" +#include "shared/source/compiler_interface/compiler_options/compiler_options_base.h" + +#include "opencl/source/platform/extensions.h" + #include #include @@ -32,5 +36,22 @@ bool requiresOpenClCFeatures(const std::string &compileOptions) { bool requiresAdditionalExtensions(const std::string &compileOptions) { return (getMajorVersion(compileOptions) == 2); } +void appendExtensionsToInternalOptions(const HardwareInfo &hwInfo, const std::string &options, std::string &internalOptions) { + std::string extensionsList = getExtensionsList(hwInfo); + if (requiresAdditionalExtensions(options)) { + extensionsList += "cl_khr_3d_image_writes "; + } + OpenClCFeaturesContainer openclCFeatures; + if (requiresOpenClCFeatures(options)) { + getOpenclCFeaturesList(hwInfo, openclCFeatures); + } + + auto compilerExtensions = convertEnabledExtensionsToCompilerInternalOptions(extensionsList.c_str(), openclCFeatures); + auto oclVersion = getOclVersionCompilerInternalOption(hwInfo.capabilityTable.clVersionSupport); + internalOptions = CompilerOptions::concatenate(oclVersion, compilerExtensions, internalOptions); + if (hwInfo.capabilityTable.supportsImages) { + CompilerOptions::concatenateAppend(internalOptions, CompilerOptions::enableImageSupport); + } +} } // namespace NEO diff --git a/shared/source/helpers/compiler_options_parser.h b/shared/source/helpers/compiler_options_parser.h index ee5b4b7c90..1b0fbe93a0 100644 --- a/shared/source/helpers/compiler_options_parser.h +++ b/shared/source/helpers/compiler_options_parser.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2020 Intel Corporation + * Copyright (C) 2020-2021 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -12,8 +12,11 @@ namespace NEO { extern const std::string clStdOptionName; +struct HardwareInfo; bool requiresOpenClCFeatures(const std::string &compileOptions); bool requiresAdditionalExtensions(const std::string &compileOptions); +void appendExtensionsToInternalOptions(const HardwareInfo &hwInfo, const std::string &options, std::string &internalOptions); + } // namespace NEO diff --git a/shared/test/unit_test/compiler_interface/compiler_interface_tests.cpp b/shared/test/unit_test/compiler_interface/compiler_interface_tests.cpp index 837c62cea4..24ca803a69 100644 --- a/shared/test/unit_test/compiler_interface/compiler_interface_tests.cpp +++ b/shared/test/unit_test/compiler_interface/compiler_interface_tests.cpp @@ -9,6 +9,7 @@ #include "shared/source/compiler_interface/compiler_interface.inl" #include "shared/source/helpers/file_io.h" #include "shared/source/helpers/hw_info.h" +#include "shared/test/common/fixtures/device_fixture.h" #include "shared/test/common/helpers/debug_manager_state_restore.h" #include "shared/test/common/helpers/test_files.h" #include "shared/test/common/helpers/unit_test_helper.h" @@ -17,7 +18,6 @@ #include "shared/test/common/mocks/mock_compiler_interface.h" #include "shared/test/common/mocks/mock_compilers.h" -#include "opencl/test/unit_test/fixtures/cl_device_fixture.h" #include "test.h" #include "gmock/gmock.h" @@ -35,11 +35,11 @@ const char *gCBadDompilerDllName = "libbad_compiler.so"; #error "Unknown OS!" #endif -class CompilerInterfaceTest : public ClDeviceFixture, +class CompilerInterfaceTest : public DeviceFixture, public ::testing::Test { public: void SetUp() override { - ClDeviceFixture::SetUp(); + DeviceFixture::SetUp(); // create the compiler interface this->pCompilerInterface = new MockCompilerInterface(); @@ -60,13 +60,12 @@ class CompilerInterfaceTest : public ClDeviceFixture, ASSERT_NE(nullptr, pSource); inputArgs.src = ArrayRef(pSource.get(), sourceSize); - inputArgs.internalOptions = ArrayRef(pClDevice->peekCompilerExtensions().c_str(), pClDevice->peekCompilerExtensions().size()); } void TearDown() override { pSource.reset(); - ClDeviceFixture::TearDown(); + DeviceFixture::TearDown(); } MockCompilerInterface *pCompilerInterface; @@ -161,19 +160,6 @@ TEST_F(CompilerInterfaceTest, WhenPreferredIntermediateRepresentationSpecifiedTh EXPECT_EQ(TranslationOutput::ErrorCode::Success, err); } -TEST_F(CompilerInterfaceTest, WhenBuildIsInvokedThenFclReceivesListOfExtensionsInInternalOptions) { - std::string receivedInternalOptions; - - auto debugVars = NEO::getFclDebugVars(); - debugVars.receivedInternalOptionsOutput = &receivedInternalOptions; - gEnvironment->fclPushDebugVars(debugVars); - TranslationOutput translationOutput = {}; - auto err = pCompilerInterface->build(*pDevice, inputArgs, translationOutput); - EXPECT_EQ(TranslationOutput::ErrorCode::Success, err); - EXPECT_THAT(receivedInternalOptions, testing::HasSubstr(pClDevice->peekCompilerExtensions())); - gEnvironment->fclPopDebugVars(); -} - TEST_F(CompilerInterfaceTest, whenCompilerIsNotAvailableThenBuildFailsGracefully) { pCompilerInterface->igcMain.reset(nullptr); TranslationOutput translationOutput = {}; @@ -250,20 +236,6 @@ TEST_F(CompilerInterfaceTest, GivenProgramCreatedFromIrWhenCompileIsCalledThenDo EXPECT_EQ(TranslationOutput::ErrorCode::AlreadyCompiled, err); } -TEST_F(CompilerInterfaceTest, WhenCompileIsInvokedThenFclReceivesListOfExtensionsInInternalOptions) { - std::string receivedInternalOptions; - - MockCompilerDebugVars fclDebugVars; - retrieveBinaryKernelFilename(fclDebugVars.fileName, "CopyBuffer_simd16_", ".bc"); - fclDebugVars.receivedInternalOptionsOutput = &receivedInternalOptions; - gEnvironment->fclPushDebugVars(fclDebugVars); - TranslationOutput translationOutput = {}; - auto err = pCompilerInterface->compile(*pDevice, inputArgs, translationOutput); - EXPECT_EQ(TranslationOutput::ErrorCode::Success, err); - EXPECT_THAT(receivedInternalOptions, testing::HasSubstr(pClDevice->peekCompilerExtensions())); - gEnvironment->fclPopDebugVars(); -} - TEST_F(CompilerInterfaceTest, whenCompilerIsNotAvailableThenCompileFailsGracefully) { MockCompilerDebugVars fclDebugVars; fclDebugVars.fileName = clFiles + "copybuffer.elf";