1 /*
2  * Copyright (C) 2020-2021 Intel Corporation
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  */
7 
8 #include "shared/source/helpers/memory_properties_helpers.h"
9 #include "shared/test/common/mocks/mock_device.h"
10 #include "shared/test/common/mocks/mock_graphics_allocation.h"
11 #include "shared/test/common/mocks/ult_device_factory.h"
12 
13 #include "opencl/source/helpers/cl_memory_properties_helpers.h"
14 #include "opencl/source/mem_obj/mem_obj_helper.h"
15 #include "opencl/test/unit_test/mocks/mock_cl_device.h"
16 #include "opencl/test/unit_test/mocks/mock_context.h"
17 
18 #include "CL/cl_ext_intel.h"
19 #include "gtest/gtest.h"
20 #include "memory_properties_flags.h"
21 
22 using namespace NEO;
23 
TEST(MemoryProperties,givenResource48BitMemoryPropertySetWhenGetAllocationPropertiesCalledThenSetAllocationPropertyToo)24 TEST(MemoryProperties, givenResource48BitMemoryPropertySetWhenGetAllocationPropertiesCalledThenSetAllocationPropertyToo) {
25     UltDeviceFactory deviceFactory{1, 0};
26     MemoryProperties memoryProperties{};
27     memoryProperties.pDevice = deviceFactory.rootDevices[0];
28     memoryProperties.flags.resource48Bit = true;
29 
30     DeviceBitfield deviceBitfield{0xf};
31 
32     HardwareInfo hwInfo(*defaultHwInfo);
33 
34     auto allocationProperties = MemoryPropertiesHelper::getAllocationProperties(0, memoryProperties, true, 0, GraphicsAllocation::AllocationType::BUFFER,
35                                                                                 false, hwInfo, deviceBitfield, false);
36 
37     EXPECT_EQ(1u, allocationProperties.flags.resource48Bit);
38 }
39 
TEST(MemoryProperties,givenValidPropertiesWhenCreateMemoryPropertiesThenTrueIsReturned)40 TEST(MemoryProperties, givenValidPropertiesWhenCreateMemoryPropertiesThenTrueIsReturned) {
41     UltDeviceFactory deviceFactory{1, 0};
42     auto pDevice = deviceFactory.rootDevices[0];
43     MemoryProperties properties;
44 
45     properties = ClMemoryPropertiesHelper::createMemoryProperties(CL_MEM_READ_WRITE, 0, 0, pDevice);
46     EXPECT_TRUE(properties.flags.readWrite);
47 
48     properties = ClMemoryPropertiesHelper::createMemoryProperties(CL_MEM_WRITE_ONLY, 0, 0, pDevice);
49     EXPECT_TRUE(properties.flags.writeOnly);
50 
51     properties = ClMemoryPropertiesHelper::createMemoryProperties(CL_MEM_READ_ONLY, 0, 0, pDevice);
52     EXPECT_TRUE(properties.flags.readOnly);
53 
54     properties = ClMemoryPropertiesHelper::createMemoryProperties(CL_MEM_USE_HOST_PTR, 0, 0, pDevice);
55     EXPECT_TRUE(properties.flags.useHostPtr);
56 
57     properties = ClMemoryPropertiesHelper::createMemoryProperties(CL_MEM_ALLOC_HOST_PTR, 0, 0, pDevice);
58     EXPECT_TRUE(properties.flags.allocHostPtr);
59 
60     properties = ClMemoryPropertiesHelper::createMemoryProperties(CL_MEM_COPY_HOST_PTR, 0, 0, pDevice);
61     EXPECT_TRUE(properties.flags.copyHostPtr);
62 
63     properties = ClMemoryPropertiesHelper::createMemoryProperties(CL_MEM_HOST_WRITE_ONLY, 0, 0, pDevice);
64     EXPECT_TRUE(properties.flags.hostWriteOnly);
65 
66     properties = ClMemoryPropertiesHelper::createMemoryProperties(CL_MEM_HOST_READ_ONLY, 0, 0, pDevice);
67     EXPECT_TRUE(properties.flags.hostReadOnly);
68 
69     properties = ClMemoryPropertiesHelper::createMemoryProperties(CL_MEM_HOST_NO_ACCESS, 0, 0, pDevice);
70     EXPECT_TRUE(properties.flags.hostNoAccess);
71 
72     properties = ClMemoryPropertiesHelper::createMemoryProperties(CL_MEM_KERNEL_READ_AND_WRITE, 0, 0, pDevice);
73     EXPECT_TRUE(properties.flags.kernelReadAndWrite);
74 
75     properties = ClMemoryPropertiesHelper::createMemoryProperties(CL_MEM_ACCESS_FLAGS_UNRESTRICTED_INTEL, 0, 0, pDevice);
76     EXPECT_TRUE(properties.flags.accessFlagsUnrestricted);
77 
78     properties = ClMemoryPropertiesHelper::createMemoryProperties(CL_MEM_NO_ACCESS_INTEL, 0, 0, pDevice);
79     EXPECT_TRUE(properties.flags.noAccess);
80 
81     properties = ClMemoryPropertiesHelper::createMemoryProperties(0, CL_MEM_LOCALLY_UNCACHED_RESOURCE, 0, pDevice);
82     EXPECT_TRUE(properties.flags.locallyUncachedResource);
83 
84     properties = ClMemoryPropertiesHelper::createMemoryProperties(0, CL_MEM_LOCALLY_UNCACHED_SURFACE_STATE_RESOURCE, 0, pDevice);
85     EXPECT_TRUE(properties.flags.locallyUncachedInSurfaceState);
86 
87     properties = ClMemoryPropertiesHelper::createMemoryProperties(CL_MEM_FORCE_HOST_MEMORY_INTEL, 0, 0, pDevice);
88     EXPECT_TRUE(properties.flags.forceHostMemory);
89 
90     properties = ClMemoryPropertiesHelper::createMemoryProperties(0, 0, CL_MEM_ALLOC_WRITE_COMBINED_INTEL, pDevice);
91     EXPECT_TRUE(properties.allocFlags.allocWriteCombined);
92 
93     properties = ClMemoryPropertiesHelper::createMemoryProperties(0, 0, CL_MEM_ALLOC_INITIAL_PLACEMENT_DEVICE_INTEL, pDevice);
94     EXPECT_TRUE(properties.allocFlags.usmInitialPlacementGpu);
95 
96     properties = ClMemoryPropertiesHelper::createMemoryProperties(0, 0, CL_MEM_ALLOC_INITIAL_PLACEMENT_HOST_INTEL, pDevice);
97     EXPECT_TRUE(properties.allocFlags.usmInitialPlacementCpu);
98 
99     properties = ClMemoryPropertiesHelper::createMemoryProperties(0, CL_MEM_48BIT_RESOURCE_INTEL, 0, pDevice);
100     EXPECT_TRUE(properties.flags.resource48Bit);
101 }
102 
TEST(MemoryProperties,givenClMemForceLinearStorageFlagWhenCreateMemoryPropertiesThenReturnProperValue)103 TEST(MemoryProperties, givenClMemForceLinearStorageFlagWhenCreateMemoryPropertiesThenReturnProperValue) {
104     UltDeviceFactory deviceFactory{1, 0};
105     auto pDevice = deviceFactory.rootDevices[0];
106     MemoryProperties memoryProperties;
107     cl_mem_flags flags = 0;
108     cl_mem_flags_intel flagsIntel = 0;
109 
110     flags |= CL_MEM_FORCE_LINEAR_STORAGE_INTEL;
111     flagsIntel = 0;
112     memoryProperties = ClMemoryPropertiesHelper::createMemoryProperties(flags, flagsIntel, 0, pDevice);
113     EXPECT_TRUE(memoryProperties.flags.forceLinearStorage);
114 
115     flags = 0;
116     flagsIntel |= CL_MEM_FORCE_LINEAR_STORAGE_INTEL;
117     memoryProperties = ClMemoryPropertiesHelper::createMemoryProperties(flags, flagsIntel, 0, pDevice);
118     EXPECT_TRUE(memoryProperties.flags.forceLinearStorage);
119 
120     flags |= CL_MEM_FORCE_LINEAR_STORAGE_INTEL;
121     flagsIntel |= CL_MEM_FORCE_LINEAR_STORAGE_INTEL;
122     memoryProperties = ClMemoryPropertiesHelper::createMemoryProperties(flags, flagsIntel, 0, pDevice);
123     EXPECT_TRUE(memoryProperties.flags.forceLinearStorage);
124 
125     flags = 0;
126     flagsIntel = 0;
127     memoryProperties = ClMemoryPropertiesHelper::createMemoryProperties(flags, flagsIntel, 0, pDevice);
128     EXPECT_FALSE(memoryProperties.flags.forceLinearStorage);
129 }
130 
TEST(MemoryProperties,givenClAllowUnrestrictedSizeFlagWhenCreateMemoryPropertiesThenReturnProperValue)131 TEST(MemoryProperties, givenClAllowUnrestrictedSizeFlagWhenCreateMemoryPropertiesThenReturnProperValue) {
132     UltDeviceFactory deviceFactory{1, 0};
133     auto pDevice = deviceFactory.rootDevices[0];
134     MemoryProperties memoryProperties;
135     cl_mem_flags flags = 0;
136     cl_mem_flags_intel flagsIntel = 0;
137 
138     flags |= CL_MEM_ALLOW_UNRESTRICTED_SIZE_INTEL;
139     flagsIntel = 0;
140     memoryProperties = ClMemoryPropertiesHelper::createMemoryProperties(flags, flagsIntel, 0, pDevice);
141     EXPECT_TRUE(memoryProperties.flags.allowUnrestrictedSize);
142 
143     flags = 0;
144     flagsIntel |= CL_MEM_ALLOW_UNRESTRICTED_SIZE_INTEL;
145     memoryProperties = ClMemoryPropertiesHelper::createMemoryProperties(flags, flagsIntel, 0, pDevice);
146     EXPECT_TRUE(memoryProperties.flags.allowUnrestrictedSize);
147 
148     flags |= CL_MEM_ALLOW_UNRESTRICTED_SIZE_INTEL;
149     flagsIntel |= CL_MEM_ALLOW_UNRESTRICTED_SIZE_INTEL;
150     memoryProperties = ClMemoryPropertiesHelper::createMemoryProperties(flags, flagsIntel, 0, pDevice);
151     EXPECT_TRUE(memoryProperties.flags.allowUnrestrictedSize);
152 
153     flags = 0;
154     flagsIntel = 0;
155     memoryProperties = ClMemoryPropertiesHelper::createMemoryProperties(flags, flagsIntel, 0, pDevice);
156     EXPECT_FALSE(memoryProperties.flags.allowUnrestrictedSize);
157 }
158 
TEST(MemoryProperties,givenClCompressedHintFlagWhenCreateMemoryPropertiesThenReturnProperValue)159 TEST(MemoryProperties, givenClCompressedHintFlagWhenCreateMemoryPropertiesThenReturnProperValue) {
160     MemoryProperties memoryProperties;
161     UltDeviceFactory deviceFactory{1, 0};
162     auto pDevice = deviceFactory.rootDevices[0];
163 
164     cl_mem_flags flags = CL_MEM_COMPRESSED_HINT_INTEL;
165     cl_mem_flags_intel flagsIntel = 0;
166     memoryProperties = ClMemoryPropertiesHelper::createMemoryProperties(flags, flagsIntel, 0, pDevice);
167     EXPECT_TRUE(memoryProperties.flags.compressedHint);
168 
169     flags = 0;
170     flagsIntel |= CL_MEM_COMPRESSED_HINT_INTEL;
171     memoryProperties = ClMemoryPropertiesHelper::createMemoryProperties(flags, flagsIntel, 0, pDevice);
172     EXPECT_TRUE(memoryProperties.flags.compressedHint);
173 
174     flags |= CL_MEM_COMPRESSED_HINT_INTEL;
175     flagsIntel |= CL_MEM_COMPRESSED_HINT_INTEL;
176     memoryProperties = ClMemoryPropertiesHelper::createMemoryProperties(flags, flagsIntel, 0, pDevice);
177     EXPECT_TRUE(memoryProperties.flags.compressedHint);
178 
179     flags = 0;
180     flagsIntel = 0;
181     memoryProperties = ClMemoryPropertiesHelper::createMemoryProperties(flags, flagsIntel, 0, pDevice);
182     EXPECT_FALSE(memoryProperties.flags.compressedHint);
183 }
184 
TEST(MemoryProperties,givenClUncompressedHintFlagWhenCreateMemoryPropertiesThenReturnProperValue)185 TEST(MemoryProperties, givenClUncompressedHintFlagWhenCreateMemoryPropertiesThenReturnProperValue) {
186     MemoryProperties memoryProperties;
187     UltDeviceFactory deviceFactory{1, 0};
188     auto pDevice = deviceFactory.rootDevices[0];
189 
190     cl_mem_flags flags = CL_MEM_UNCOMPRESSED_HINT_INTEL;
191     cl_mem_flags_intel flagsIntel = 0;
192     memoryProperties = ClMemoryPropertiesHelper::createMemoryProperties(flags, flagsIntel, 0, pDevice);
193     EXPECT_TRUE(memoryProperties.flags.uncompressedHint);
194 
195     flags = 0;
196     flagsIntel |= CL_MEM_UNCOMPRESSED_HINT_INTEL;
197     memoryProperties = ClMemoryPropertiesHelper::createMemoryProperties(flags, flagsIntel, 0, pDevice);
198     EXPECT_TRUE(memoryProperties.flags.uncompressedHint);
199 
200     flags |= CL_MEM_UNCOMPRESSED_HINT_INTEL;
201     flagsIntel |= CL_MEM_UNCOMPRESSED_HINT_INTEL;
202     memoryProperties = ClMemoryPropertiesHelper::createMemoryProperties(flags, flagsIntel, 0, pDevice);
203     EXPECT_TRUE(memoryProperties.flags.uncompressedHint);
204 
205     flags = 0;
206     flagsIntel = 0;
207     memoryProperties = ClMemoryPropertiesHelper::createMemoryProperties(flags, flagsIntel, 0, pDevice);
208     EXPECT_FALSE(memoryProperties.flags.uncompressedHint);
209 }
210 
211 struct MemoryPropertiesHelperTests : ::testing::Test {
212     MockContext context;
213     MemoryProperties memoryProperties;
214     cl_mem_flags flags = 0;
215     cl_mem_flags_intel flagsIntel = 0;
216     cl_mem_alloc_flags_intel allocflags = 0;
217 };
218 
TEST_F(MemoryPropertiesHelperTests,givenNullPropertiesWhenParsingMemoryPropertiesThenTrueIsReturned)219 TEST_F(MemoryPropertiesHelperTests, givenNullPropertiesWhenParsingMemoryPropertiesThenTrueIsReturned) {
220     EXPECT_TRUE(ClMemoryPropertiesHelper::parseMemoryProperties(nullptr, memoryProperties, flags, flagsIntel, allocflags,
221                                                                 MemoryPropertiesHelper::ObjType::UNKNOWN, context));
222 }
223 
TEST_F(MemoryPropertiesHelperTests,givenEmptyPropertiesWhenParsingMemoryPropertiesThenTrueIsReturned)224 TEST_F(MemoryPropertiesHelperTests, givenEmptyPropertiesWhenParsingMemoryPropertiesThenTrueIsReturned) {
225     cl_mem_properties_intel properties[] = {0};
226 
227     EXPECT_TRUE(ClMemoryPropertiesHelper::parseMemoryProperties(properties, memoryProperties, flags, flagsIntel, allocflags,
228                                                                 MemoryPropertiesHelper::ObjType::UNKNOWN, context));
229     EXPECT_TRUE(ClMemoryPropertiesHelper::parseMemoryProperties(properties, memoryProperties, flags, flagsIntel, allocflags,
230                                                                 MemoryPropertiesHelper::ObjType::BUFFER, context));
231     EXPECT_TRUE(ClMemoryPropertiesHelper::parseMemoryProperties(properties, memoryProperties, flags, flagsIntel, allocflags,
232                                                                 MemoryPropertiesHelper::ObjType::IMAGE, context));
233 }
234 
TEST_F(MemoryPropertiesHelperTests,givenValidPropertiesWhenParsingMemoryPropertiesThenTrueIsReturned)235 TEST_F(MemoryPropertiesHelperTests, givenValidPropertiesWhenParsingMemoryPropertiesThenTrueIsReturned) {
236     cl_mem_properties_intel properties[] = {
237         CL_MEM_FLAGS,
238         CL_MEM_READ_WRITE | CL_MEM_WRITE_ONLY | CL_MEM_READ_ONLY | CL_MEM_ALLOC_HOST_PTR | CL_MEM_COPY_HOST_PTR |
239             CL_MEM_USE_HOST_PTR | CL_MEM_HOST_WRITE_ONLY | CL_MEM_HOST_READ_ONLY | CL_MEM_HOST_NO_ACCESS | CL_MEM_COMPRESSED_HINT_INTEL |
240             CL_MEM_UNCOMPRESSED_HINT_INTEL,
241         CL_MEM_FLAGS_INTEL,
242         CL_MEM_LOCALLY_UNCACHED_RESOURCE | CL_MEM_LOCALLY_UNCACHED_SURFACE_STATE_RESOURCE | CL_MEM_COMPRESSED_HINT_INTEL |
243             CL_MEM_UNCOMPRESSED_HINT_INTEL,
244         CL_MEM_ALLOC_FLAGS_INTEL,
245         CL_MEM_ALLOC_WRITE_COMBINED_INTEL, CL_MEM_ALLOC_DEFAULT_INTEL,
246         0};
247 
248     EXPECT_TRUE(ClMemoryPropertiesHelper::parseMemoryProperties(properties, memoryProperties, flags, flagsIntel, allocflags,
249                                                                 MemoryPropertiesHelper::ObjType::UNKNOWN, context));
250 }
251 
TEST_F(MemoryPropertiesHelperTests,givenValidPropertiesWhenParsingMemoryPropertiesForBufferThenTrueIsReturned)252 TEST_F(MemoryPropertiesHelperTests, givenValidPropertiesWhenParsingMemoryPropertiesForBufferThenTrueIsReturned) {
253     cl_mem_properties_intel properties[] = {
254         CL_MEM_FLAGS,
255         MemObjHelper::validFlagsForBuffer,
256         CL_MEM_FLAGS_INTEL,
257         MemObjHelper::validFlagsForBufferIntel,
258         0};
259 
260     EXPECT_TRUE(ClMemoryPropertiesHelper::parseMemoryProperties(properties, memoryProperties, flags, flagsIntel, allocflags,
261                                                                 MemoryPropertiesHelper::ObjType::BUFFER, context));
262 }
263 
TEST_F(MemoryPropertiesHelperTests,givenValidPropertiesWhenParsingMemoryPropertiesForImageThenTrueIsReturned)264 TEST_F(MemoryPropertiesHelperTests, givenValidPropertiesWhenParsingMemoryPropertiesForImageThenTrueIsReturned) {
265     cl_mem_properties_intel properties[] = {
266         CL_MEM_FLAGS,
267         MemObjHelper::validFlagsForImage,
268         CL_MEM_FLAGS_INTEL,
269         MemObjHelper::validFlagsForImageIntel,
270         0};
271 
272     EXPECT_TRUE(ClMemoryPropertiesHelper::parseMemoryProperties(properties, memoryProperties, flags, flagsIntel, allocflags,
273                                                                 MemoryPropertiesHelper::ObjType::IMAGE, context));
274 }
275 
TEST_F(MemoryPropertiesHelperTests,givenInvalidPropertiesWhenParsingMemoryPropertiesThenFalseIsReturned)276 TEST_F(MemoryPropertiesHelperTests, givenInvalidPropertiesWhenParsingMemoryPropertiesThenFalseIsReturned) {
277     cl_mem_properties_intel properties[] = {
278         (1 << 30), CL_MEM_ALLOC_HOST_PTR | CL_MEM_COPY_HOST_PTR | CL_MEM_USE_HOST_PTR,
279         0};
280 
281     EXPECT_FALSE(ClMemoryPropertiesHelper::parseMemoryProperties(properties, memoryProperties, flags, flagsIntel, allocflags,
282                                                                  MemoryPropertiesHelper::ObjType::UNKNOWN, context));
283     EXPECT_FALSE(ClMemoryPropertiesHelper::parseMemoryProperties(properties, memoryProperties, flags, flagsIntel, allocflags,
284                                                                  MemoryPropertiesHelper::ObjType::BUFFER, context));
285     EXPECT_FALSE(ClMemoryPropertiesHelper::parseMemoryProperties(properties, memoryProperties, flags, flagsIntel, allocflags,
286                                                                  MemoryPropertiesHelper::ObjType::IMAGE, context));
287 }
288 
TEST_F(MemoryPropertiesHelperTests,givenInvalidPropertiesWhenParsingMemoryPropertiesForImageThenFalseIsReturned)289 TEST_F(MemoryPropertiesHelperTests, givenInvalidPropertiesWhenParsingMemoryPropertiesForImageThenFalseIsReturned) {
290     cl_mem_properties_intel properties[] = {
291         CL_MEM_FLAGS,
292         MemObjHelper::validFlagsForBuffer,
293         CL_MEM_FLAGS_INTEL,
294         MemObjHelper::validFlagsForBufferIntel,
295         0};
296 
297     EXPECT_FALSE(ClMemoryPropertiesHelper::parseMemoryProperties(properties, memoryProperties, flags, flagsIntel, allocflags,
298                                                                  MemoryPropertiesHelper::ObjType::IMAGE, context));
299 }
300 
TEST_F(MemoryPropertiesHelperTests,givenInvalidFlagsWhenParsingMemoryPropertiesForImageThenFalseIsReturned)301 TEST_F(MemoryPropertiesHelperTests, givenInvalidFlagsWhenParsingMemoryPropertiesForImageThenFalseIsReturned) {
302     cl_mem_properties_intel properties[] = {
303         CL_MEM_FLAGS,
304         (1 << 30),
305         CL_MEM_FLAGS_INTEL,
306         MemObjHelper::validFlagsForImageIntel,
307         0};
308 
309     EXPECT_FALSE(ClMemoryPropertiesHelper::parseMemoryProperties(properties, memoryProperties, flags, flagsIntel, allocflags,
310                                                                  MemoryPropertiesHelper::ObjType::IMAGE, context));
311 }
312 
TEST_F(MemoryPropertiesHelperTests,givenInvalidFlagsIntelWhenParsingMemoryPropertiesForImageThenFalseIsReturned)313 TEST_F(MemoryPropertiesHelperTests, givenInvalidFlagsIntelWhenParsingMemoryPropertiesForImageThenFalseIsReturned) {
314     cl_mem_properties_intel properties[] = {
315         CL_MEM_FLAGS,
316         MemObjHelper::validFlagsForImage,
317         CL_MEM_FLAGS_INTEL,
318         (1 << 30),
319         0};
320 
321     EXPECT_FALSE(ClMemoryPropertiesHelper::parseMemoryProperties(properties, memoryProperties, flags, flagsIntel, allocflags,
322                                                                  MemoryPropertiesHelper::ObjType::IMAGE, context));
323 }
324 
TEST_F(MemoryPropertiesHelperTests,givenInvalidPropertiesWhenParsingMemoryPropertiesForBufferThenFalseIsReturned)325 TEST_F(MemoryPropertiesHelperTests, givenInvalidPropertiesWhenParsingMemoryPropertiesForBufferThenFalseIsReturned) {
326     cl_mem_properties_intel properties[] = {
327         CL_MEM_FLAGS,
328         MemObjHelper::validFlagsForImage,
329         CL_MEM_FLAGS_INTEL,
330         MemObjHelper::validFlagsForImageIntel,
331         0};
332 
333     EXPECT_FALSE(ClMemoryPropertiesHelper::parseMemoryProperties(properties, memoryProperties, flags, flagsIntel, allocflags,
334                                                                  MemoryPropertiesHelper::ObjType::BUFFER, context));
335 }
336 
TEST_F(MemoryPropertiesHelperTests,givenInvalidFlagsWhenParsingMemoryPropertiesForBufferThenFalseIsReturned)337 TEST_F(MemoryPropertiesHelperTests, givenInvalidFlagsWhenParsingMemoryPropertiesForBufferThenFalseIsReturned) {
338     cl_mem_properties_intel properties[] = {
339         CL_MEM_FLAGS,
340         (1 << 30),
341         CL_MEM_FLAGS_INTEL,
342         MemObjHelper::validFlagsForBufferIntel,
343         0};
344 
345     EXPECT_FALSE(ClMemoryPropertiesHelper::parseMemoryProperties(properties, memoryProperties, flags, flagsIntel, allocflags,
346                                                                  MemoryPropertiesHelper::ObjType::BUFFER, context));
347 }
348 
TEST_F(MemoryPropertiesHelperTests,givenInvalidFlagsIntelWhenParsingMemoryPropertiesForBufferThenFalseIsReturned)349 TEST_F(MemoryPropertiesHelperTests, givenInvalidFlagsIntelWhenParsingMemoryPropertiesForBufferThenFalseIsReturned) {
350     cl_mem_properties_intel properties[] = {
351         CL_MEM_FLAGS,
352         MemObjHelper::validFlagsForBuffer,
353         CL_MEM_FLAGS_INTEL,
354         (1 << 30),
355         0};
356 
357     EXPECT_FALSE(ClMemoryPropertiesHelper::parseMemoryProperties(properties, memoryProperties, flags, flagsIntel, allocflags,
358                                                                  MemoryPropertiesHelper::ObjType::BUFFER, context));
359 }
360 
TEST_F(MemoryPropertiesHelperTests,givenDifferentParametersWhenCallingFillCachePolicyInPropertiesThenFlushL3FlagsAreCorrectlySet)361 TEST_F(MemoryPropertiesHelperTests, givenDifferentParametersWhenCallingFillCachePolicyInPropertiesThenFlushL3FlagsAreCorrectlySet) {
362     AllocationProperties allocationProperties{mockRootDeviceIndex, 0, GraphicsAllocation::AllocationType::BUFFER, mockDeviceBitfield};
363 
364     for (auto uncached : ::testing::Bool()) {
365         for (auto readOnly : ::testing::Bool()) {
366             for (auto deviceOnlyVisibilty : ::testing::Bool()) {
367                 if (uncached || readOnly || deviceOnlyVisibilty) {
368                     allocationProperties.flags.flushL3RequiredForRead = true;
369                     allocationProperties.flags.flushL3RequiredForWrite = true;
370                     MemoryPropertiesHelper::fillCachePolicyInProperties(allocationProperties, uncached, readOnly, deviceOnlyVisibilty, 0);
371                     EXPECT_FALSE(allocationProperties.flags.flushL3RequiredForRead);
372                     EXPECT_FALSE(allocationProperties.flags.flushL3RequiredForWrite);
373                 } else {
374                     allocationProperties.flags.flushL3RequiredForRead = false;
375                     allocationProperties.flags.flushL3RequiredForWrite = false;
376                     MemoryPropertiesHelper::fillCachePolicyInProperties(allocationProperties, uncached, readOnly, deviceOnlyVisibilty, 0);
377                     EXPECT_TRUE(allocationProperties.flags.flushL3RequiredForRead);
378                     EXPECT_TRUE(allocationProperties.flags.flushL3RequiredForWrite);
379                 }
380             }
381         }
382     }
383 }
384 
TEST_F(MemoryPropertiesHelperTests,givenMemFlagsWithFlagsAndPropertiesWhenParsingMemoryPropertiesThenTheyAreCorrectlyParsed)385 TEST_F(MemoryPropertiesHelperTests, givenMemFlagsWithFlagsAndPropertiesWhenParsingMemoryPropertiesThenTheyAreCorrectlyParsed) {
386     struct TestInput {
387         cl_mem_flags flagsParameter;
388         cl_mem_properties_intel flagsProperties;
389         cl_mem_flags expectedResult;
390     };
391 
392     TestInput testInputs[] = {
393         {0b0, 0b0, 0b0},
394         {0b0, 0b1010, 0b1010},
395         {0b1010, 0b0, 0b1010},
396         {0b1010, 0b101, 0b1111},
397         {0b1010, 0b1010, 0b1010},
398         {0b1111, 0b1111, 0b1111}};
399 
400     for (auto &testInput : testInputs) {
401         flags = testInput.flagsParameter;
402         cl_mem_properties_intel properties[] = {
403             CL_MEM_FLAGS, testInput.flagsProperties,
404             0};
405         EXPECT_TRUE(ClMemoryPropertiesHelper::parseMemoryProperties(properties, memoryProperties, flags, flagsIntel, allocflags,
406                                                                     MemoryPropertiesHelper::ObjType::UNKNOWN, context));
407         EXPECT_EQ(testInput.expectedResult, flags);
408     }
409 }
410 
TEST_F(MemoryPropertiesHelperTests,WhenAdjustingDeviceBitfieldThenCorrectBitfieldIsReturned)411 TEST_F(MemoryPropertiesHelperTests, WhenAdjustingDeviceBitfieldThenCorrectBitfieldIsReturned) {
412     UltClDeviceFactory deviceFactory{2, 4};
413     auto memoryPropertiesRootDevice0 = ClMemoryPropertiesHelper::createMemoryProperties(0, 0, 0, &deviceFactory.rootDevices[0]->getDevice());
414     auto memoryPropertiesRootDevice0Tile0 = ClMemoryPropertiesHelper::createMemoryProperties(0, 0, 0, &deviceFactory.subDevices[0]->getDevice());
415     auto memoryPropertiesRootDevice0Tile1 = ClMemoryPropertiesHelper::createMemoryProperties(0, 0, 0, &deviceFactory.subDevices[1]->getDevice());
416     auto memoryPropertiesRootDevice1 = ClMemoryPropertiesHelper::createMemoryProperties(0, 0, 0, &deviceFactory.rootDevices[1]->getDevice());
417     auto memoryPropertiesRootDevice1Tile0 = ClMemoryPropertiesHelper::createMemoryProperties(0, 0, 0, &deviceFactory.subDevices[4]->getDevice());
418     auto memoryPropertiesRootDevice1Tile1 = ClMemoryPropertiesHelper::createMemoryProperties(0, 0, 0, &deviceFactory.subDevices[5]->getDevice());
419 
420     DeviceBitfield devicesInContextBitfield0001{0b1};
421     DeviceBitfield devicesInContextBitfield0101{0b101};
422     DeviceBitfield devicesInContextBitfield1010{0b1010};
423     DeviceBitfield devicesInContextBitfield1111{0b1111};
424 
425     MemoryProperties memoryPropertiesToProcess[] = {
426         memoryPropertiesRootDevice0, memoryPropertiesRootDevice0Tile0, memoryPropertiesRootDevice0Tile1,
427         memoryPropertiesRootDevice1, memoryPropertiesRootDevice1Tile0, memoryPropertiesRootDevice1Tile1};
428 
429     DeviceBitfield devicesInContextBitfields[] = {devicesInContextBitfield0001, devicesInContextBitfield0101,
430                                                   devicesInContextBitfield1010, devicesInContextBitfield1111};
431     uint32_t rootDevicesToProcess[] = {0, 1, 2};
432 
433     EXPECT_EQ(0b1u, MemoryPropertiesHelper::adjustDeviceBitfield(0, memoryPropertiesRootDevice0Tile0, devicesInContextBitfield1111).to_ulong());
434     EXPECT_EQ(0b10u, MemoryPropertiesHelper::adjustDeviceBitfield(0, memoryPropertiesRootDevice0Tile1, devicesInContextBitfield1111).to_ulong());
435     EXPECT_EQ(0b1111u, MemoryPropertiesHelper::adjustDeviceBitfield(1, memoryPropertiesRootDevice0Tile0, devicesInContextBitfield1111).to_ulong());
436     EXPECT_EQ(0b1111u, MemoryPropertiesHelper::adjustDeviceBitfield(1, memoryPropertiesRootDevice0Tile1, devicesInContextBitfield1111).to_ulong());
437 
438     EXPECT_EQ(0b101u, MemoryPropertiesHelper::adjustDeviceBitfield(0, memoryPropertiesRootDevice0, devicesInContextBitfield0101).to_ulong());
439     EXPECT_EQ(0b1010u, MemoryPropertiesHelper::adjustDeviceBitfield(0, memoryPropertiesRootDevice0, devicesInContextBitfield1010).to_ulong());
440     EXPECT_EQ(0b1111u, MemoryPropertiesHelper::adjustDeviceBitfield(0, memoryPropertiesRootDevice0, devicesInContextBitfield1111).to_ulong());
441 
442     for (auto processedRootDevice : rootDevicesToProcess) {
443         for (auto devicesInContextBitfield : devicesInContextBitfields) {
444             for (auto &memoryProperties : memoryPropertiesToProcess) {
445                 auto expectedDeviceBitfield = devicesInContextBitfield;
446                 if (processedRootDevice == memoryProperties.pDevice->getRootDeviceIndex()) {
447                     expectedDeviceBitfield &= memoryProperties.pDevice->getDeviceBitfield();
448                 }
449                 auto adjustedDeviceBitfield = MemoryPropertiesHelper::adjustDeviceBitfield(
450                     processedRootDevice, memoryProperties, devicesInContextBitfield);
451                 EXPECT_EQ(expectedDeviceBitfield, adjustedDeviceBitfield);
452             }
453         }
454     }
455 }