From be0306ca68afbccd55e9163077fa6250b457ce6b Mon Sep 17 00:00:00 2001 From: "Koska, Andrzej" Date: Mon, 15 Jan 2018 18:16:50 +0100 Subject: [PATCH] Adding support for passing proper extensions list from cloc to compilers Change-Id: I89249cd6ccae9996cd33d7ba26007c778207f028 --- offline_compiler/CMakeLists.txt | 4 +- offline_compiler/offline_compiler.cpp | 5 +- runtime/CMakeLists.txt | 4 +- runtime/device/device_caps.cpp | 19 +--- runtime/platform/extensions.cpp | 87 +++++++++++++++++++ runtime/platform/extensions.h | 33 +++++++ runtime/platform/platform.cpp | 18 +--- runtime/platform/platform.h | 5 +- runtime/program/build.cpp | 4 +- runtime/program/compile.cpp | 2 +- .../compiler_interface_tests.cpp | 4 +- .../offline_compiler_tests.cpp | 16 ++++ unit_tests/platform/platform_tests.cpp | 55 +++++++++++- 13 files changed, 210 insertions(+), 46 deletions(-) create mode 100644 runtime/platform/extensions.cpp create mode 100644 runtime/platform/extensions.h diff --git a/offline_compiler/CMakeLists.txt b/offline_compiler/CMakeLists.txt index 95fd4877e4..476b344664 100644 --- a/offline_compiler/CMakeLists.txt +++ b/offline_compiler/CMakeLists.txt @@ -1,4 +1,4 @@ -# Copyright (c) 2017, Intel Corporation +# Copyright (c) 2018, Intel Corporation # # Permission is hereby granted, free of charge, to any person obtaining a # copy of this software and associated documentation files (the "Software"), @@ -29,6 +29,8 @@ set(CLOC_SRCS_LIB ${IGDRCL_SOURCE_DIR}/offline_compiler/helper.cpp ${IGDRCL_SOURCE_DIR}/runtime/compiler_interface/create_main.cpp ${IGDRCL_SOURCE_DIR}/runtime/helpers/hw_info.cpp + ${IGDRCL_SOURCE_DIR}/runtime/platform/extensions.h + ${IGDRCL_SOURCE_DIR}/runtime/platform/extensions.cpp ${IGDRCL_SOURCE_DIR}/runtime/helpers/file_io.cpp ${IGDRCL_SOURCE_DIR}/runtime/helpers/abort.cpp ${IGDRCL_SOURCE_DIR}/runtime/helpers/debug_helpers.cpp diff --git a/offline_compiler/offline_compiler.cpp b/offline_compiler/offline_compiler.cpp index 7603188520..5ac483b2d1 100644 --- a/offline_compiler/offline_compiler.cpp +++ b/offline_compiler/offline_compiler.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017, Intel Corporation + * Copyright (c) 2018, Intel Corporation * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), @@ -37,6 +37,7 @@ #include "runtime/helpers/debug_helpers.h" #include "runtime/helpers/hw_info.h" #include "runtime/helpers/validators.h" +#include "runtime/platform/extensions.h" #include "elf/writer.h" #include #include @@ -489,6 +490,8 @@ int OfflineCompiler::parseCommandLine(uint32_t numArgs, const char **argv) { if (retVal != CL_SUCCESS) { printf("Error: Cannot get HW Info for device %s.\n", deviceName.c_str()); } + std::string extensionsList = getExtensionsList(*hwInfo); + internalOptions.append(convertEnabledExtensionsToCompilerInternalOptions(extensionsList.c_str())); } } diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index be700637c6..401bfd8d4b 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -1,4 +1,4 @@ -# Copyright (c) 2017, Intel Corporation +# Copyright (c) 2018, Intel Corporation # # Permission is hereby granted, free of charge, to any person obtaining a # copy of this software and associated documentation files (the "Software"), @@ -420,6 +420,8 @@ set (RUNTIME_SRCS_PLATFORM platform/platform.cpp platform/platform.h platform/platform_info.h + platform/extensions.cpp + platform/extensions.h ) set (RUNTIME_SRCS_PROGRAM diff --git a/runtime/device/device_caps.cpp b/runtime/device/device_caps.cpp index a0285b20e6..92d92f2998 100644 --- a/runtime/device/device_caps.cpp +++ b/runtime/device/device_caps.cpp @@ -30,6 +30,7 @@ #include "runtime/device/driver_info.h" #include "CL/cl_ext_intel.h" #include "runtime/os_interface/os_interface.h" +#include "runtime/platform/extensions.h" namespace OCLRT { extern const char *familyName[]; @@ -40,24 +41,6 @@ static std::string profile = "FULL_PROFILE"; static std::string spirVersions = "1.2 "; static const char *spirvVersion = "SPIR-V_1.0 "; static std::string driverVersion = NEO_DRIVER_VERSION; -static const std::string deviceExtensionsList = "cl_khr_3d_image_writes " - "cl_khr_byte_addressable_store " - "cl_khr_fp16 " - "cl_khr_depth_images " - "cl_khr_global_int32_base_atomics " - "cl_khr_global_int32_extended_atomics " - "cl_khr_icd " - "cl_khr_image2d_from_buffer " - "cl_khr_local_int32_base_atomics " - "cl_khr_local_int32_extended_atomics " - "cl_intel_subgroups " - "cl_intel_required_subgroup_size " - "cl_intel_subgroups_short " - "cl_khr_spir " - "cl_intel_accelerator " - "cl_intel_media_block_io " - "cl_intel_driver_diagnostics " - "cl_intel_device_side_avc_motion_estimation "; const char *builtInKernels = ""; // the "always available" (extension-independent) builtin kernels diff --git a/runtime/platform/extensions.cpp b/runtime/platform/extensions.cpp new file mode 100644 index 0000000000..13c193e11d --- /dev/null +++ b/runtime/platform/extensions.cpp @@ -0,0 +1,87 @@ +/* + * Copyright (c) 2018, Intel Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR + * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + */ + +#include +#include "runtime/helpers/hw_info.h" +#include "runtime/platform/extensions.h" + +namespace OCLRT { + +const char *deviceExtensionsList = "cl_khr_3d_image_writes " + "cl_khr_byte_addressable_store " + "cl_khr_fp16 " + "cl_khr_depth_images " + "cl_khr_global_int32_base_atomics " + "cl_khr_global_int32_extended_atomics " + "cl_khr_icd " + "cl_khr_image2d_from_buffer " + "cl_khr_local_int32_base_atomics " + "cl_khr_local_int32_extended_atomics " + "cl_intel_subgroups " + "cl_intel_required_subgroup_size " + "cl_intel_subgroups_short " + "cl_khr_spir " + "cl_intel_accelerator " + "cl_intel_media_block_io " + "cl_intel_driver_diagnostics " + "cl_intel_device_side_avc_motion_estimation "; + +std::string getExtensionsList(const HardwareInfo &hwInfo) { + std::string allExtensionsList; + allExtensionsList.reserve(1000); + + allExtensionsList.append(deviceExtensionsList); + + if (hwInfo.capabilityTable.clVersionSupport >= 21) { + allExtensionsList += "cl_khr_subgroups "; + allExtensionsList += "cl_khr_il_program "; + } + + if (hwInfo.capabilityTable.ftrSupportsFP64) { + allExtensionsList += "cl_khr_fp64 "; + } + + return allExtensionsList; +} + +std::string removeLastSpace(std::string &processedString) { + if (processedString.size() > 0) { + if (*processedString.rbegin() == ' ') { + processedString.pop_back(); + } + } + return processedString; +} + +std::string convertEnabledExtensionsToCompilerInternalOptions(const char *enabledExtensions) { + std::string extensionsList = enabledExtensions; + extensionsList.reserve(1000); + removeLastSpace(extensionsList); + std::string::size_type pos = 0; + while ((pos = extensionsList.find(" ", pos)) != std::string::npos) { + extensionsList.replace(pos, 1, ",+"); + } + extensionsList = "-cl-ext=-all,+" + extensionsList; + return extensionsList; +} + +} // namespace OCLRT diff --git a/runtime/platform/extensions.h b/runtime/platform/extensions.h new file mode 100644 index 0000000000..1febf59ebb --- /dev/null +++ b/runtime/platform/extensions.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2018, Intel Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR + * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + */ + +#pragma once + +namespace OCLRT { + +extern const char *deviceExtensionsList; + +std::string getExtensionsList(const HardwareInfo &hwInfo); +std::string removeLastSpace(std::string &s); +std::string convertEnabledExtensionsToCompilerInternalOptions(const char *deviceExtensions); + +} // namespace OCLRT \ No newline at end of file diff --git a/runtime/platform/platform.cpp b/runtime/platform/platform.cpp index 0a0591dbcd..4c1c45b369 100644 --- a/runtime/platform/platform.cpp +++ b/runtime/platform/platform.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017, Intel Corporation + * Copyright (c) 2018, Intel Corporation * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), @@ -32,6 +32,7 @@ #include "runtime/os_interface/device_factory.h" #include "runtime/event/async_events_handler.h" #include "runtime/sharings/sharing_factory.h" +#include "runtime/platform/extensions.h" #include "CL/cl_ext.h" namespace OCLRT { @@ -100,18 +101,7 @@ cl_int Platform::getInfo(cl_platform_info paramName, return retVal; } -std::string Platform::getCompilerExtensions(const char *deviceExtensions) { - std::string extensionsList = deviceExtensions; - extensionsList.pop_back(); - size_t pos = 0; - while ((pos = extensionsList.find(" ", pos)) != std::string::npos) { - extensionsList.replace(pos, 1, ",+"); - } - extensionsList = "-cl-ext=-all,+" + extensionsList; - return extensionsList; -} - -std::string Platform::getCompilerExtensions() { +const std::string &Platform::peekCompilerExtensions() const { return compilerExtensions; } @@ -158,7 +148,7 @@ bool Platform::initialize(size_t numDevices, break; } - compilerExtensions = getCompilerExtensions(pDevice->getDeviceInfo().deviceExtensions); + compilerExtensions = convertEnabledExtensionsToCompilerInternalOptions(pDevice->getDeviceInfo().deviceExtensions); } else { return false; } diff --git a/runtime/platform/platform.h b/runtime/platform/platform.h index c743471dc2..d453a36e4e 100644 --- a/runtime/platform/platform.h +++ b/runtime/platform/platform.h @@ -53,7 +53,7 @@ class Platform : public BaseObject<_cl_platform_id> { void *paramValue, size_t *paramValueSizeRet); - std::string getCompilerExtensions(); + const std::string &peekCompilerExtensions() const; bool initialize(size_t numDevices, const HardwareInfo **devices); @@ -75,7 +75,6 @@ class Platform : public BaseObject<_cl_platform_id> { StateInited, }; cl_uint state = StateNone; - std::string getCompilerExtensions(const char *deviceExtensions); void fillGlobalDispatchTable(); PlatformInfo *platformInfo = nullptr; @@ -85,4 +84,4 @@ class Platform : public BaseObject<_cl_platform_id> { }; Platform *platform(); -} +} // namespace OCLRT diff --git a/runtime/program/build.cpp b/runtime/program/build.cpp index 948fad8f0e..f4d8140352 100644 --- a/runtime/program/build.cpp +++ b/runtime/program/build.cpp @@ -91,7 +91,7 @@ cl_int Program::build( break; } - internalOptions.append(platform()->getCompilerExtensions()); + internalOptions.append(platform()->peekCompilerExtensions()); inputArgs.pInput = (char *)(sourceCode.c_str()); inputArgs.InputSize = (uint32_t)sourceCode.size(); inputArgs.pOptions = options.c_str(); @@ -159,4 +159,4 @@ cl_int Program::build( return retVal; } -} +} // namespace OCLRT diff --git a/runtime/program/compile.cpp b/runtime/program/compile.cpp index d0e7b5cf98..e68981296b 100644 --- a/runtime/program/compile.cpp +++ b/runtime/program/compile.cpp @@ -150,7 +150,7 @@ cl_int Program::compile( TranslationArgs inputArgs = {}; // set parameters for compilation - internalOptions.append(platform()->getCompilerExtensions()); + internalOptions.append(platform()->peekCompilerExtensions()); inputArgs.pInput = pCompileData; inputArgs.InputSize = (uint32_t)compileDataSize; inputArgs.pOptions = options.c_str(); diff --git a/unit_tests/compiler_interface/compiler_interface_tests.cpp b/unit_tests/compiler_interface/compiler_interface_tests.cpp index 18afe5c4c8..04330568a1 100644 --- a/unit_tests/compiler_interface/compiler_interface_tests.cpp +++ b/unit_tests/compiler_interface/compiler_interface_tests.cpp @@ -230,7 +230,7 @@ TEST_F(CompilerInterfaceTest, WhenBuildIsInvokedThenFclReceivesListOfExtensionsI gEnvironment->fclPushDebugVars(debugVars); retVal = pCompilerInterface->build(*pProgram, inputArgs, false); EXPECT_EQ(CL_SUCCESS, retVal); - EXPECT_THAT(receivedInternalOptions, testing::HasSubstr(platform()->getCompilerExtensions())); + EXPECT_THAT(receivedInternalOptions, testing::HasSubstr(platform()->peekCompilerExtensions())); gEnvironment->fclPopDebugVars(); } @@ -299,7 +299,7 @@ TEST_F(CompilerInterfaceTest, WhenCompileIsInvokedThenFclReceivesListOfExtension gEnvironment->fclPushDebugVars(fclDebugVars); retVal = pCompilerInterface->compile(*pProgram, inputArgs); EXPECT_EQ(CL_SUCCESS, retVal); - EXPECT_THAT(receivedInternalOptions, testing::HasSubstr(platform()->getCompilerExtensions())); + EXPECT_THAT(receivedInternalOptions, testing::HasSubstr(platform()->peekCompilerExtensions())); gEnvironment->fclPopDebugVars(); } diff --git a/unit_tests/offline_compiler/offline_compiler_tests.cpp b/unit_tests/offline_compiler/offline_compiler_tests.cpp index 7dff5657a4..e67e1c1686 100644 --- a/unit_tests/offline_compiler/offline_compiler_tests.cpp +++ b/unit_tests/offline_compiler/offline_compiler_tests.cpp @@ -28,6 +28,7 @@ #include "runtime/helpers/file_io.h" #include "runtime/helpers/options.h" #include "runtime/os_interface/debug_settings_manager.h" +#include "gmock/gmock.h" #define ARRAY_COUNT(x) (sizeof(x) / sizeof(x[0])) @@ -61,6 +62,21 @@ TEST_F(OfflineCompilerTests, GoodArgTest) { delete pOfflineCompiler; } +TEST_F(OfflineCompilerTests, TestExtensions) { + const char *argv[] = { + "cloc", + "-file", + "test_files/copybuffer.cl", + "-device", + gEnvironment->devicePrefix.c_str()}; + + auto mockOfflineCompiler = std::unique_ptr(new MockOfflineCompiler()); + ASSERT_NE(nullptr, mockOfflineCompiler); + mockOfflineCompiler->parseCommandLine(ARRAY_COUNT(argv), argv); + std::string internalOptions = mockOfflineCompiler->getInternalOptions(); + EXPECT_THAT(internalOptions, ::testing::HasSubstr(std::string("cl_khr_3d_image_writes"))); +} + TEST_F(OfflineCompilerTests, GoodBuildTest) { const char *argv[] = { "cloc", diff --git a/unit_tests/platform/platform_tests.cpp b/unit_tests/platform/platform_tests.cpp index 25bb2cfce3..6fb21fc598 100644 --- a/unit_tests/platform/platform_tests.cpp +++ b/unit_tests/platform/platform_tests.cpp @@ -22,6 +22,7 @@ #include "runtime/helpers/options.h" #include "runtime/device/device.h" +#include "runtime/platform/extensions.h" #include "runtime/sharings/sharing_factory.h" #include "unit_tests/fixtures/memory_management_fixture.h" #include "unit_tests/fixtures/platform_fixture.h" @@ -75,12 +76,12 @@ TEST_F(PlatformTest, getDevices) { EXPECT_EQ(nullptr, allDevices); } -TEST_F(PlatformTest, PlatformGetCompilerExtensions) { - std::string compilerExtensions = pPlatform->getCompilerExtensions(); +TEST_F(PlatformTest, PlatformgetAsCompilerEnabledExtensionsString) { + std::string compilerExtensions = pPlatform->peekCompilerExtensions(); EXPECT_EQ(std::string(""), compilerExtensions); pPlatform->initialize(numPlatformDevices, platformDevices); - compilerExtensions = pPlatform->getCompilerExtensions(); + compilerExtensions = pPlatform->peekCompilerExtensions(); EXPECT_THAT(compilerExtensions, ::testing::HasSubstr(std::string("-cl-ext=-all,+cl"))); if (std::string(pPlatform->getDevice(0)->getDeviceInfo().clVersion).find("OpenCL 2.1") != std::string::npos) { @@ -151,3 +152,51 @@ TEST_F(PlatformFailingTest, givenPlatformInitializationWhenIncorrectHwInfoThenIn EXPECT_FALSE(platform->isInitialized()); delete platform; } + +TEST_F(PlatformTest, getAsCompilerEnabledExtensionsString) { + const HardwareInfo *hwInfo; + hwInfo = platformDevices[0]; + std::string extensionsList = getExtensionsList(*hwInfo); + EXPECT_THAT(extensionsList, ::testing::HasSubstr(std::string("cl_khr_3d_image_writes"))); + + std::string compilerExtensions = convertEnabledExtensionsToCompilerInternalOptions(extensionsList.c_str()); + EXPECT_THAT(compilerExtensions, ::testing::HasSubstr(std::string("-cl-ext=-all,+cl"))); + + if (hwInfo->capabilityTable.clVersionSupport > 20) { + EXPECT_THAT(compilerExtensions, ::testing::HasSubstr(std::string("cl_khr_subgroups"))); + EXPECT_THAT(compilerExtensions, ::testing::HasSubstr(std::string("cl_khr_il_program"))); + } + + if (hwInfo->capabilityTable.ftrSupportsFP64) { + EXPECT_THAT(compilerExtensions, ::testing::HasSubstr(std::string("cl_khr_fp64"))); + } +} + +TEST_F(PlatformTest, getAsCompilerEnabledExtensionsStringTestNotFP64) { + HardwareInfo TesthwInfo = *platformDevices[0]; + TesthwInfo.capabilityTable.ftrSupportsFP64 = false; + TesthwInfo.capabilityTable.clVersionSupport = 10; + + std::string extensionsList = getExtensionsList(TesthwInfo); + EXPECT_THAT(extensionsList, ::testing::HasSubstr(std::string("cl_khr_3d_image_writes"))); + + std::string compilerExtensions = convertEnabledExtensionsToCompilerInternalOptions(extensionsList.c_str()); + EXPECT_THAT(compilerExtensions, ::testing::HasSubstr(std::string("-cl-ext=-all,+cl"))); + + EXPECT_THAT(compilerExtensions, ::testing::Not(::testing::HasSubstr(std::string("cl_khr_fp64")))); + EXPECT_THAT(compilerExtensions, ::testing::Not(::testing::HasSubstr(std::string("cl_khr_subgroups")))); +} + +TEST_F(PlatformTest, testRemoveLastSpace) { + std::string emptyString = ""; + removeLastSpace(emptyString); + EXPECT_EQ(std::string(""), emptyString); + + std::string xString = "x"; + removeLastSpace(xString); + EXPECT_EQ(std::string("x"), xString); + + std::string xSpaceString = "x "; + removeLastSpace(xSpaceString); + EXPECT_EQ(std::string("x"), xSpaceString); +} \ No newline at end of file