1 /*
2  * Copyright (C) 2018-2021 Intel Corporation
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  */
7 
8 #include "shared/source/compiler_interface/oclc_extensions.h"
9 #include "shared/source/device/device.h"
10 #include "shared/source/helpers/hw_info.h"
11 #include "shared/source/helpers/string.h"
12 #include "shared/source/os_interface/device_factory.h"
13 #include "shared/test/common/helpers/debug_manager_state_restore.h"
14 #include "shared/test/common/helpers/ult_hw_config.h"
15 #include "shared/test/common/helpers/variable_backup.h"
16 #include "shared/test/common/mocks/mock_builtins.h"
17 #include "shared/test/common/mocks/mock_csr.h"
18 #include "shared/test/common/mocks/mock_device.h"
19 #include "shared/test/common/mocks/mock_execution_environment.h"
20 #include "shared/test/common/mocks/mock_sip.h"
21 #include "shared/test/common/mocks/mock_source_level_debugger.h"
22 #include "shared/test/unit_test/fixtures/mock_aub_center_fixture.h"
23 
24 #include "opencl/source/cl_device/cl_device.h"
25 #include "opencl/source/sharings/sharing_factory.h"
26 #include "opencl/test/unit_test/fixtures/platform_fixture.h"
27 #include "opencl/test/unit_test/mocks/mock_cl_device.h"
28 #include "opencl/test/unit_test/mocks/mock_platform.h"
29 #include "opencl/test/unit_test/mocks/ult_cl_device_factory.h"
30 #include "opencl/test/unit_test/test_macros/test_checks_ocl.h"
31 
32 #include "gmock/gmock.h"
33 #include "gtest/gtest.h"
34 
35 using namespace NEO;
36 
37 struct PlatformTest : public ::testing::Test {
SetUpPlatformTest38     void SetUp() override {
39         MockSipData::clearUseFlags();
40         backupSipInitType = std::make_unique<VariableBackup<bool>>(&MockSipData::useMockSip, true);
41 
42         pPlatform.reset(new MockPlatform());
43     }
TearDownPlatformTest44     void TearDown() override {
45         MockSipData::clearUseFlags();
46     }
47     std::unique_ptr<MockPlatform> pPlatform;
48     std::unique_ptr<VariableBackup<bool>> backupSipInitType;
49 
50     cl_int retVal = CL_SUCCESS;
51 };
52 
53 struct MockPlatformWithMockExecutionEnvironment : public MockPlatform {
54 
MockPlatformWithMockExecutionEnvironmentMockPlatformWithMockExecutionEnvironment55     MockPlatformWithMockExecutionEnvironment() : MockPlatform(*(new MockExecutionEnvironment(nullptr, false, 1))) {
56         MockAubCenterFixture::setMockAubCenter(*executionEnvironment.rootDeviceEnvironments[0]);
57     }
58 };
59 
TEST_F(PlatformTest,GivenUninitializedPlatformWhenInitializeIsCalledThenPlatformIsInitialized)60 TEST_F(PlatformTest, GivenUninitializedPlatformWhenInitializeIsCalledThenPlatformIsInitialized) {
61     EXPECT_FALSE(pPlatform->isInitialized());
62 
63     pPlatform->initializeWithNewDevices();
64 
65     EXPECT_TRUE(pPlatform->isInitialized());
66 }
67 
TEST_F(PlatformTest,WhenGetNumDevicesIsCalledThenExpectedValuesAreReturned)68 TEST_F(PlatformTest, WhenGetNumDevicesIsCalledThenExpectedValuesAreReturned) {
69     EXPECT_EQ(0u, pPlatform->getNumDevices());
70 
71     pPlatform->initializeWithNewDevices();
72 
73     EXPECT_GT(pPlatform->getNumDevices(), 0u);
74 }
75 
TEST_F(PlatformTest,WhenGetDeviceIsCalledThenExpectedValuesAreReturned)76 TEST_F(PlatformTest, WhenGetDeviceIsCalledThenExpectedValuesAreReturned) {
77     EXPECT_EQ(nullptr, pPlatform->getClDevice(0));
78 
79     pPlatform->initializeWithNewDevices();
80 
81     EXPECT_NE(nullptr, pPlatform->getClDevice(0));
82 
83     auto numDevices = pPlatform->getNumDevices();
84     EXPECT_EQ(nullptr, pPlatform->getClDevice(numDevices));
85 }
86 
TEST_F(PlatformTest,WhenGetClDevicesIsCalledThenExpectedValuesAreReturned)87 TEST_F(PlatformTest, WhenGetClDevicesIsCalledThenExpectedValuesAreReturned) {
88     EXPECT_EQ(nullptr, pPlatform->getClDevices());
89 
90     pPlatform->initializeWithNewDevices();
91 
92     EXPECT_NE(nullptr, pPlatform->getClDevices());
93 }
94 
TEST_F(PlatformTest,givenSupportingCl21WhenGettingExtensionsStringThenSubgroupsIsEnabled)95 TEST_F(PlatformTest, givenSupportingCl21WhenGettingExtensionsStringThenSubgroupsIsEnabled) {
96     REQUIRE_OCL_21_OR_SKIP(defaultHwInfo);
97 
98     pPlatform->initializeWithNewDevices();
99     auto compilerExtensions = pPlatform->getClDevice(0)->peekCompilerExtensions();
100 
101     auto isIndependentForwardProgressSupported = pPlatform->getClDevice(0)->getDeviceInfo().independentForwardProgress;
102 
103     EXPECT_THAT(compilerExtensions, ::testing::HasSubstr(std::string(" -cl-ext=-all,+cl")));
104     if (isIndependentForwardProgressSupported) {
105         EXPECT_THAT(compilerExtensions, ::testing::HasSubstr(std::string("cl_khr_subgroups")));
106     }
107 }
108 
TEST_F(PlatformTest,givenMidThreadPreemptionWhenInitializingPlatformThenCallGetSipKernel)109 TEST_F(PlatformTest, givenMidThreadPreemptionWhenInitializingPlatformThenCallGetSipKernel) {
110     DebugManagerStateRestore dbgRestorer;
111     DebugManager.flags.ForcePreemptionMode.set(static_cast<int32_t>(PreemptionMode::MidThread));
112 
113     auto builtIns = new MockBuiltins();
114     auto executionEnvironment = pPlatform->peekExecutionEnvironment();
115     executionEnvironment->prepareRootDeviceEnvironments(1);
116     executionEnvironment->rootDeviceEnvironments[0]->builtins.reset(builtIns);
117 
118     EXPECT_EQ(SipKernelType::COUNT, MockSipData::calledType);
119     EXPECT_FALSE(MockSipData::called);
120     pPlatform->initializeWithNewDevices();
121     EXPECT_EQ(SipKernelType::Csr, MockSipData::calledType);
122     EXPECT_TRUE(MockSipData::called);
123 }
124 
TEST_F(PlatformTest,givenDisabledPreemptionAndNoSourceLevelDebuggerWhenInitializingPlatformThenDoNotCallGetSipKernel)125 TEST_F(PlatformTest, givenDisabledPreemptionAndNoSourceLevelDebuggerWhenInitializingPlatformThenDoNotCallGetSipKernel) {
126     DebugManagerStateRestore dbgRestorer;
127     DebugManager.flags.ForcePreemptionMode.set(static_cast<int32_t>(PreemptionMode::Disabled));
128 
129     auto builtIns = new MockBuiltins();
130     auto executionEnvironment = pPlatform->peekExecutionEnvironment();
131     executionEnvironment->prepareRootDeviceEnvironments(1);
132     executionEnvironment->rootDeviceEnvironments[0]->builtins.reset(builtIns);
133 
134     EXPECT_EQ(SipKernelType::COUNT, MockSipData::calledType);
135     EXPECT_FALSE(MockSipData::called);
136     pPlatform->initializeWithNewDevices();
137     EXPECT_EQ(SipKernelType::COUNT, MockSipData::calledType);
138     EXPECT_FALSE(MockSipData::called);
139 }
140 
TEST_F(PlatformTest,givenDisabledPreemptionInactiveSourceLevelDebuggerWhenInitializingPlatformThenDoNotCallGetSipKernel)141 TEST_F(PlatformTest, givenDisabledPreemptionInactiveSourceLevelDebuggerWhenInitializingPlatformThenDoNotCallGetSipKernel) {
142     DebugManagerStateRestore dbgRestorer;
143     DebugManager.flags.ForcePreemptionMode.set(static_cast<int32_t>(PreemptionMode::Disabled));
144 
145     auto builtIns = new MockBuiltins();
146     auto executionEnvironment = pPlatform->peekExecutionEnvironment();
147     executionEnvironment->prepareRootDeviceEnvironments(1);
148     executionEnvironment->rootDeviceEnvironments[0]->builtins.reset(builtIns);
149     auto sourceLevelDebugger = new MockSourceLevelDebugger();
150     sourceLevelDebugger->setActive(false);
151     executionEnvironment->rootDeviceEnvironments[0]->debugger.reset(sourceLevelDebugger);
152 
153     EXPECT_EQ(SipKernelType::COUNT, MockSipData::calledType);
154     EXPECT_FALSE(MockSipData::called);
155     pPlatform->initializeWithNewDevices();
156     EXPECT_EQ(SipKernelType::COUNT, MockSipData::calledType);
157     EXPECT_FALSE(MockSipData::called);
158 }
159 
TEST_F(PlatformTest,givenDisabledPreemptionActiveSourceLevelDebuggerWhenInitializingPlatformThenCallGetSipKernel)160 TEST_F(PlatformTest, givenDisabledPreemptionActiveSourceLevelDebuggerWhenInitializingPlatformThenCallGetSipKernel) {
161     DebugManagerStateRestore dbgRestorer;
162     DebugManager.flags.ForcePreemptionMode.set(static_cast<int32_t>(PreemptionMode::Disabled));
163 
164     auto builtIns = new MockBuiltins();
165     auto executionEnvironment = pPlatform->peekExecutionEnvironment();
166     executionEnvironment->prepareRootDeviceEnvironments(1);
167     executionEnvironment->rootDeviceEnvironments[0]->builtins.reset(builtIns);
168     executionEnvironment->rootDeviceEnvironments[0]->debugger.reset(new MockActiveSourceLevelDebugger());
169 
170     EXPECT_EQ(SipKernelType::COUNT, MockSipData::calledType);
171     EXPECT_FALSE(MockSipData::called);
172     pPlatform->initializeWithNewDevices();
173     EXPECT_TRUE(MockSipData::called);
174     EXPECT_LE(SipKernelType::DbgCsr, MockSipData::calledType);
175     EXPECT_GE(SipKernelType::DbgCsrLocal, MockSipData::calledType);
176 }
177 
TEST(PlatformTestSimple,givenCsrHwTypeWhenPlatformIsInitializedThenInitAubCenterIsNotCalled)178 TEST(PlatformTestSimple, givenCsrHwTypeWhenPlatformIsInitializedThenInitAubCenterIsNotCalled) {
179     DebugManagerStateRestore stateRestore;
180     DebugManager.flags.SetCommandStreamReceiver.set(0);
181     MockPlatformWithMockExecutionEnvironment platform;
182 
183     bool ret = platform.initializeWithNewDevices();
184     EXPECT_TRUE(ret);
185     auto rootDeviceEnvironment = static_cast<MockRootDeviceEnvironment *>(platform.peekExecutionEnvironment()->rootDeviceEnvironments[0].get());
186     EXPECT_FALSE(rootDeviceEnvironment->initAubCenterCalled);
187 }
188 
TEST(PlatformTestSimple,givenNotCsrHwTypeWhenPlatformIsInitializedThenInitAubCenterIsCalled)189 TEST(PlatformTestSimple, givenNotCsrHwTypeWhenPlatformIsInitializedThenInitAubCenterIsCalled) {
190     DebugManagerStateRestore stateRestore;
191     DebugManager.flags.SetCommandStreamReceiver.set(1);
192     VariableBackup<UltHwConfig> backup(&ultHwConfig);
193     ultHwConfig.useHwCsr = true;
194     MockPlatformWithMockExecutionEnvironment platform;
195     bool ret = platform.initializeWithNewDevices();
196     EXPECT_TRUE(ret);
197     auto rootDeviceEnvironment = static_cast<MockRootDeviceEnvironment *>(platform.peekExecutionEnvironment()->rootDeviceEnvironments[0].get());
198     EXPECT_TRUE(rootDeviceEnvironment->initAubCenterCalled);
199 }
200 
TEST(PlatformTestSimple,WhenConvertingCustomOclCFeaturesToCompilerInternalOptionsThenResultIsCorrect)201 TEST(PlatformTestSimple, WhenConvertingCustomOclCFeaturesToCompilerInternalOptionsThenResultIsCorrect) {
202     OpenClCFeaturesContainer customOpenclCFeatures;
203 
204     cl_name_version feature;
205     strcpy_s(feature.name, CL_NAME_VERSION_MAX_NAME_SIZE, "custom_feature");
206     customOpenclCFeatures.push_back(feature);
207     auto compilerOption = convertEnabledExtensionsToCompilerInternalOptions("", customOpenclCFeatures);
208     EXPECT_STREQ(" -cl-ext=-all,+custom_feature ", compilerOption.c_str());
209 
210     strcpy_s(feature.name, CL_NAME_VERSION_MAX_NAME_SIZE, "other_extra_feature");
211     customOpenclCFeatures.push_back(feature);
212     compilerOption = convertEnabledExtensionsToCompilerInternalOptions("", customOpenclCFeatures);
213     EXPECT_STREQ(" -cl-ext=-all,+custom_feature,+other_extra_feature ", compilerOption.c_str());
214 }
215 
TEST(PlatformTestSimple,WhenConvertingOclCFeaturesToCompilerInternalOptionsThenResultIsCorrect)216 TEST(PlatformTestSimple, WhenConvertingOclCFeaturesToCompilerInternalOptionsThenResultIsCorrect) {
217     UltClDeviceFactory deviceFactory{1, 0};
218     auto pClDevice = deviceFactory.rootDevices[0];
219 
220     std::string expectedCompilerOption = " -cl-ext=-all,";
221     for (auto &openclCFeature : pClDevice->deviceInfo.openclCFeatures) {
222         expectedCompilerOption += "+";
223         expectedCompilerOption += openclCFeature.name;
224         expectedCompilerOption += ",";
225     }
226     expectedCompilerOption.erase(expectedCompilerOption.size() - 1, 1);
227     expectedCompilerOption += " ";
228 
229     auto compilerOption = convertEnabledExtensionsToCompilerInternalOptions("", pClDevice->deviceInfo.openclCFeatures);
230     EXPECT_STREQ(expectedCompilerOption.c_str(), compilerOption.c_str());
231 }
232 
233 namespace NEO {
234 extern CommandStreamReceiverCreateFunc commandStreamReceiverFactory[2 * IGFX_MAX_CORE];
235 }
236 
createMockCommandStreamReceiver(bool withAubDump,ExecutionEnvironment & executionEnvironment,uint32_t rootDeviceIndex,const DeviceBitfield deviceBitfield)237 CommandStreamReceiver *createMockCommandStreamReceiver(bool withAubDump,
238                                                        ExecutionEnvironment &executionEnvironment,
239                                                        uint32_t rootDeviceIndex,
240                                                        const DeviceBitfield deviceBitfield) {
241     return nullptr;
242 };
243 
244 class PlatformFailingTest : public PlatformTest {
245   public:
PlatformFailingTest()246     PlatformFailingTest() {
247         ultHwConfig.useHwCsr = true;
248     }
SetUp()249     void SetUp() override {
250         PlatformTest::SetUp();
251         hwInfo = defaultHwInfo.get();
252         commandStreamReceiverCreateFunc = commandStreamReceiverFactory[hwInfo->platform.eRenderCoreFamily];
253         commandStreamReceiverFactory[hwInfo->platform.eRenderCoreFamily] = createMockCommandStreamReceiver;
254     }
255 
TearDown()256     void TearDown() override {
257         commandStreamReceiverFactory[hwInfo->platform.eRenderCoreFamily] = commandStreamReceiverCreateFunc;
258         PlatformTest::TearDown();
259     }
260 
261     VariableBackup<UltHwConfig> backup{&ultHwConfig};
262     CommandStreamReceiverCreateFunc commandStreamReceiverCreateFunc;
263     const HardwareInfo *hwInfo;
264 };
265 
TEST_F(PlatformFailingTest,givenPlatformInitializationWhenIncorrectHwInfoThenInitializationFails)266 TEST_F(PlatformFailingTest, givenPlatformInitializationWhenIncorrectHwInfoThenInitializationFails) {
267     auto platform = new MockPlatform();
268     bool ret = platform->initializeWithNewDevices();
269     EXPECT_FALSE(ret);
270     EXPECT_FALSE(platform->isInitialized());
271     delete platform;
272 }
273 
TEST_F(PlatformTest,givenSupportingCl21WhenPlatformSupportsFp64ThenFillMatchingSubstringsAndMandatoryTrailingSpace)274 TEST_F(PlatformTest, givenSupportingCl21WhenPlatformSupportsFp64ThenFillMatchingSubstringsAndMandatoryTrailingSpace) {
275     const HardwareInfo *hwInfo;
276     hwInfo = defaultHwInfo.get();
277     std::string extensionsList = getExtensionsList(*hwInfo);
278     OpenClCFeaturesContainer features;
279     getOpenclCFeaturesList(*hwInfo, features);
280 
281     std::string compilerExtensions = convertEnabledExtensionsToCompilerInternalOptions(extensionsList.c_str(), features);
282     EXPECT_THAT(compilerExtensions, ::testing::HasSubstr(std::string(" -cl-ext=-all,+cl")));
283 
284     if (hwInfo->capabilityTable.supportsOcl21Features) {
285         EXPECT_THAT(compilerExtensions, ::testing::HasSubstr(std::string("cl_khr_subgroups")));
286         if (hwInfo->capabilityTable.supportsVme) {
287             EXPECT_THAT(compilerExtensions, ::testing::HasSubstr(std::string("cl_intel_spirv_device_side_avc_motion_estimation")));
288         } else {
289             EXPECT_THAT(compilerExtensions, testing::Not(::testing::HasSubstr(std::string("cl_intel_spirv_device_side_avc_motion_estimation"))));
290         }
291         if (hwInfo->capabilityTable.supportsMediaBlock) {
292             EXPECT_THAT(compilerExtensions, ::testing::HasSubstr(std::string("cl_intel_spirv_media_block_io")));
293         } else {
294             EXPECT_THAT(compilerExtensions, testing::Not(::testing::HasSubstr(std::string("cl_intel_spirv_media_block_io"))));
295         }
296 
297         EXPECT_THAT(compilerExtensions, ::testing::HasSubstr(std::string("cl_intel_spirv_subgroups")));
298         EXPECT_THAT(compilerExtensions, ::testing::HasSubstr(std::string("cl_khr_spirv_no_integer_wrap_decoration")));
299     }
300 
301     if (hwInfo->capabilityTable.ftrSupportsFP64) {
302         EXPECT_THAT(compilerExtensions, ::testing::HasSubstr(std::string("cl_khr_fp64")));
303     }
304 
305     if (hwInfo->capabilityTable.supportsImages) {
306         EXPECT_THAT(extensionsList, ::testing::HasSubstr(std::string("cl_khr_3d_image_writes")));
307     }
308     EXPECT_THAT(compilerExtensions, ::testing::EndsWith(std::string(" ")));
309 }
310 
TEST_F(PlatformTest,givenNotSupportingCl21WhenPlatformNotSupportFp64ThenNotFillMatchingSubstringAndFillMandatoryTrailingSpace)311 TEST_F(PlatformTest, givenNotSupportingCl21WhenPlatformNotSupportFp64ThenNotFillMatchingSubstringAndFillMandatoryTrailingSpace) {
312     HardwareInfo TesthwInfo = *defaultHwInfo;
313     TesthwInfo.capabilityTable.ftrSupportsFP64 = false;
314     TesthwInfo.capabilityTable.clVersionSupport = 10;
315     TesthwInfo.capabilityTable.supportsOcl21Features = false;
316 
317     std::string extensionsList = getExtensionsList(TesthwInfo);
318     OpenClCFeaturesContainer features;
319     getOpenclCFeaturesList(*defaultHwInfo, features);
320     if (TesthwInfo.capabilityTable.supportsImages) {
321         EXPECT_THAT(extensionsList, ::testing::HasSubstr(std::string("cl_khr_3d_image_writes")));
322     }
323 
324     std::string compilerExtensions = convertEnabledExtensionsToCompilerInternalOptions(extensionsList.c_str(), features);
325     EXPECT_THAT(compilerExtensions, ::testing::HasSubstr(std::string("-cl-ext=-all,+cl")));
326 
327     EXPECT_THAT(compilerExtensions, ::testing::Not(::testing::HasSubstr(std::string("cl_khr_fp64"))));
328     EXPECT_THAT(compilerExtensions, ::testing::Not(::testing::HasSubstr(std::string("cl_khr_subgroups"))));
329 
330     EXPECT_THAT(compilerExtensions, ::testing::EndsWith(std::string(" ")));
331 }
332 
TEST_F(PlatformTest,givenFtrSupportAtomicsWhenCreateExtentionsListThenGetMatchingSubstrings)333 TEST_F(PlatformTest, givenFtrSupportAtomicsWhenCreateExtentionsListThenGetMatchingSubstrings) {
334     const HardwareInfo *hwInfo;
335     hwInfo = defaultHwInfo.get();
336     std::string extensionsList = getExtensionsList(*hwInfo);
337     OpenClCFeaturesContainer features;
338     getOpenclCFeaturesList(*hwInfo, features);
339     std::string compilerExtensions = convertEnabledExtensionsToCompilerInternalOptions(extensionsList.c_str(), features);
340 
341     if (hwInfo->capabilityTable.ftrSupportsInteger64BitAtomics) {
342         EXPECT_THAT(compilerExtensions, ::testing::HasSubstr(std::string("cl_khr_int64_base_atomics")));
343         EXPECT_THAT(compilerExtensions, ::testing::HasSubstr(std::string("cl_khr_int64_extended_atomics")));
344     } else {
345         EXPECT_THAT(compilerExtensions, ::testing::Not(::testing::HasSubstr(std::string("cl_khr_int64_base_atomics"))));
346         EXPECT_THAT(compilerExtensions, ::testing::Not(::testing::HasSubstr(std::string("cl_khr_int64_extended_atomics"))));
347     }
348 }
349 
TEST_F(PlatformTest,givenSupportedMediaBlockAndClVersion21WhenCreateExtentionsListThenDeviceReportsSpritvMediaBlockIoExtension)350 TEST_F(PlatformTest, givenSupportedMediaBlockAndClVersion21WhenCreateExtentionsListThenDeviceReportsSpritvMediaBlockIoExtension) {
351     HardwareInfo hwInfo = *defaultHwInfo;
352     hwInfo.capabilityTable.supportsMediaBlock = true;
353     hwInfo.capabilityTable.clVersionSupport = 21;
354     hwInfo.capabilityTable.supportsOcl21Features = true;
355     std::string extensionsList = getExtensionsList(hwInfo);
356     OpenClCFeaturesContainer features;
357     getOpenclCFeaturesList(*defaultHwInfo, features);
358     std::string compilerExtensions = convertEnabledExtensionsToCompilerInternalOptions(extensionsList.c_str(), features);
359 
360     EXPECT_THAT(compilerExtensions, testing::HasSubstr(std::string("cl_intel_spirv_media_block_io")));
361 }
362 
TEST_F(PlatformTest,givenNotSupportedMediaBlockAndClVersion21WhenCreateExtentionsListThenDeviceNotReportsSpritvMediaBlockIoExtension)363 TEST_F(PlatformTest, givenNotSupportedMediaBlockAndClVersion21WhenCreateExtentionsListThenDeviceNotReportsSpritvMediaBlockIoExtension) {
364     HardwareInfo hwInfo = *defaultHwInfo;
365     hwInfo.capabilityTable.supportsMediaBlock = false;
366     hwInfo.capabilityTable.clVersionSupport = 21;
367     std::string extensionsList = getExtensionsList(hwInfo);
368     OpenClCFeaturesContainer features;
369     getOpenclCFeaturesList(*defaultHwInfo, features);
370     std::string compilerExtensions = convertEnabledExtensionsToCompilerInternalOptions(extensionsList.c_str(), features);
371 
372     EXPECT_THAT(compilerExtensions, testing::Not(testing::HasSubstr(std::string("cl_intel_spirv_media_block_io"))));
373 }
374 
TEST_F(PlatformTest,givenSupportedImagesWhenCreateExtentionsListThenDeviceNotReportsKhr3DImageWritesExtension)375 TEST_F(PlatformTest, givenSupportedImagesWhenCreateExtentionsListThenDeviceNotReportsKhr3DImageWritesExtension) {
376     HardwareInfo hwInfo = *defaultHwInfo;
377     hwInfo.capabilityTable.supportsImages = true;
378     std::string extensionsList = getExtensionsList(hwInfo);
379     OpenClCFeaturesContainer features;
380     getOpenclCFeaturesList(*defaultHwInfo, features);
381     std::string compilerExtensions = convertEnabledExtensionsToCompilerInternalOptions(extensionsList.c_str(), features);
382 
383     EXPECT_THAT(compilerExtensions, testing::HasSubstr(std::string("cl_khr_3d_image_writes")));
384 }
385 
TEST_F(PlatformTest,givenNotSupportedImagesWhenCreateExtentionsListThenDeviceNotReportsKhr3DImageWritesExtension)386 TEST_F(PlatformTest, givenNotSupportedImagesWhenCreateExtentionsListThenDeviceNotReportsKhr3DImageWritesExtension) {
387     HardwareInfo hwInfo = *defaultHwInfo;
388     hwInfo.capabilityTable.supportsImages = false;
389     std::string extensionsList = getExtensionsList(hwInfo);
390     OpenClCFeaturesContainer features;
391     getOpenclCFeaturesList(*defaultHwInfo, features);
392     std::string compilerExtensions = convertEnabledExtensionsToCompilerInternalOptions(extensionsList.c_str(), features);
393 
394     EXPECT_THAT(compilerExtensions, testing::Not(testing::HasSubstr(std::string("cl_khr_3d_image_writes"))));
395 }
396 
TEST(PlatformConstructionTest,givenPlatformConstructorWhenItIsCalledTwiceThenTheSamePlatformIsReturned)397 TEST(PlatformConstructionTest, givenPlatformConstructorWhenItIsCalledTwiceThenTheSamePlatformIsReturned) {
398     platformsImpl->clear();
399     auto platform1 = constructPlatform();
400     EXPECT_EQ(platform1, platform());
401     auto platform2 = constructPlatform();
402     EXPECT_EQ(platform2, platform1);
403     EXPECT_NE(platform1, nullptr);
404 }
405 
TEST(PlatformConstructionTest,givenPlatformConstructorWhenItIsCalledAfterResetThenNewPlatformIsConstructed)406 TEST(PlatformConstructionTest, givenPlatformConstructorWhenItIsCalledAfterResetThenNewPlatformIsConstructed) {
407     platformsImpl->clear();
408     auto platform = constructPlatform();
409     std::unique_ptr<Platform> temporaryOwnership(std::move((*platformsImpl)[0]));
410     platformsImpl->clear();
411     auto platform2 = constructPlatform();
412     EXPECT_NE(platform2, platform);
413     EXPECT_NE(platform, nullptr);
414     EXPECT_NE(platform2, nullptr);
415     platformsImpl->clear();
416 }
417 
TEST(PlatformInitTest,givenNullptrDeviceInPassedDeviceVectorWhenInitializePlatformThenExceptionIsThrown)418 TEST(PlatformInitTest, givenNullptrDeviceInPassedDeviceVectorWhenInitializePlatformThenExceptionIsThrown) {
419     std::vector<std::unique_ptr<Device>> devices;
420     devices.push_back(nullptr);
421     EXPECT_THROW(platform()->initialize(std::move(devices)), std::exception);
422 }
423 
TEST(PlatformInitTest,givenInitializedPlatformWhenInitializeIsCalledOneMoreTimeWithNullptrDeviceThenSuccessIsEarlyReturned)424 TEST(PlatformInitTest, givenInitializedPlatformWhenInitializeIsCalledOneMoreTimeWithNullptrDeviceThenSuccessIsEarlyReturned) {
425     initPlatform();
426     EXPECT_TRUE(platform()->isInitialized());
427     std::vector<std::unique_ptr<Device>> devices;
428     devices.push_back(nullptr);
429     EXPECT_TRUE(platform()->initialize(std::move(devices)));
430 }
431 
TEST(PlatformInitTest,givenSingleDeviceWithNonZeroRootDeviceIndexInPassedDeviceVectorWhenInitializePlatformThenCreateOnlyOneClDevice)432 TEST(PlatformInitTest, givenSingleDeviceWithNonZeroRootDeviceIndexInPassedDeviceVectorWhenInitializePlatformThenCreateOnlyOneClDevice) {
433     std::vector<std::unique_ptr<Device>> devices;
434     auto executionEnvironment = new MockExecutionEnvironment(defaultHwInfo.get(), false, 3);
435     devices.push_back(std::make_unique<MockDevice>(executionEnvironment, 2));
436     auto status = platform()->initialize(std::move(devices));
437     EXPECT_TRUE(status);
438     size_t expectedNumDevices = 1u;
439     EXPECT_EQ(expectedNumDevices, platform()->getNumDevices());
440     EXPECT_EQ(2u, platform()->getClDevice(0)->getRootDeviceIndex());
441 }
442 
TEST(PlatformGroupDevicesTest,whenMultipleDevicesAreCreatedThenGroupDevicesCreatesVectorPerEachProductFamily)443 TEST(PlatformGroupDevicesTest, whenMultipleDevicesAreCreatedThenGroupDevicesCreatesVectorPerEachProductFamily) {
444     DebugManagerStateRestore restorer;
445     const size_t numRootDevices = 5u;
446 
447     DebugManager.flags.CreateMultipleRootDevices.set(numRootDevices);
448     auto executionEnvironment = new ExecutionEnvironment();
449 
450     for (auto i = 0u; i < numRootDevices; i++) {
451         executionEnvironment->rootDeviceEnvironments.push_back(std::make_unique<MockRootDeviceEnvironment>(*executionEnvironment));
452     }
453     auto inputDevices = DeviceFactory::createDevices(*executionEnvironment);
454     EXPECT_EQ(numRootDevices, inputDevices.size());
455 
456     auto skl0Device = inputDevices[0].get();
457     auto kbl0Device = inputDevices[1].get();
458     auto skl1Device = inputDevices[2].get();
459     auto skl2Device = inputDevices[3].get();
460     auto cfl0Device = inputDevices[4].get();
461 
462     executionEnvironment->rootDeviceEnvironments[0]->getMutableHardwareInfo()->platform.eProductFamily = IGFX_SKYLAKE;
463     executionEnvironment->rootDeviceEnvironments[1]->getMutableHardwareInfo()->platform.eProductFamily = IGFX_KABYLAKE;
464     executionEnvironment->rootDeviceEnvironments[2]->getMutableHardwareInfo()->platform.eProductFamily = IGFX_SKYLAKE;
465     executionEnvironment->rootDeviceEnvironments[3]->getMutableHardwareInfo()->platform.eProductFamily = IGFX_SKYLAKE;
466     executionEnvironment->rootDeviceEnvironments[4]->getMutableHardwareInfo()->platform.eProductFamily = IGFX_COFFEELAKE;
467 
468     auto groupedDevices = Platform::groupDevices(std::move(inputDevices));
469 
470     EXPECT_EQ(3u, groupedDevices.size());
471     EXPECT_EQ(1u, groupedDevices[0].size());
472     EXPECT_EQ(1u, groupedDevices[1].size());
473     EXPECT_EQ(3u, groupedDevices[2].size());
474 
475     EXPECT_EQ(skl0Device, groupedDevices[2][0].get());
476     EXPECT_EQ(skl1Device, groupedDevices[2][1].get());
477     EXPECT_EQ(skl2Device, groupedDevices[2][2].get());
478     EXPECT_EQ(kbl0Device, groupedDevices[1][0].get());
479     EXPECT_EQ(cfl0Device, groupedDevices[0][0].get());
480 }
481