diff --git a/runtime/api/api.cpp b/runtime/api/api.cpp index 30cd40a5ee..f68f24787b 100644 --- a/runtime/api/api.cpp +++ b/runtime/api/api.cpp @@ -3450,11 +3450,6 @@ void *clHostMemAllocINTEL( return nullptr; } - if (size > neoContext->getDevice(0u)->getDeviceInfo().maxMemAllocSize) { - err.set(CL_INVALID_BUFFER_SIZE); - return nullptr; - } - SVMAllocsManager::UnifiedMemoryProperties unifiedMemoryProperties(InternalMemoryType::HOST_UNIFIED_MEMORY); cl_mem_flags flags = 0; cl_mem_flags_intel flagsIntel = 0; @@ -3464,6 +3459,11 @@ void *clHostMemAllocINTEL( return nullptr; } + if (size > neoContext->getDevice(0u)->getDeviceInfo().maxMemAllocSize && !unifiedMemoryProperties.allocationFlags.flags.allowUnrestrictedSize) { + err.set(CL_INVALID_BUFFER_SIZE); + return nullptr; + } + if (isValueSet(unifiedMemoryProperties.allocationFlags.allAllocFlags, CL_MEM_ALLOC_WRITE_COMBINED_INTEL)) { err.set(CL_INVALID_VALUE); return nullptr; @@ -3491,11 +3491,6 @@ void *clDeviceMemAllocINTEL( return nullptr; } - if (size > neoDevice->getDeviceInfo().maxMemAllocSize) { - err.set(CL_INVALID_BUFFER_SIZE); - return nullptr; - } - SVMAllocsManager::UnifiedMemoryProperties unifiedMemoryProperties(InternalMemoryType::DEVICE_UNIFIED_MEMORY); cl_mem_flags flags = 0; cl_mem_flags_intel flagsIntel = 0; @@ -3504,6 +3499,12 @@ void *clDeviceMemAllocINTEL( err.set(CL_INVALID_VALUE); return nullptr; } + + if (size > neoContext->getDevice(0u)->getDeviceInfo().maxMemAllocSize && !unifiedMemoryProperties.allocationFlags.flags.allowUnrestrictedSize) { + err.set(CL_INVALID_BUFFER_SIZE); + return nullptr; + } + unifiedMemoryProperties.device = device; unifiedMemoryProperties.subdeviceBitfield = neoDevice->getDefaultEngine().osContext->getDeviceBitfield(); @@ -3529,11 +3530,6 @@ void *clSharedMemAllocINTEL( return nullptr; } - if (size > neoDevice->getDeviceInfo().maxMemAllocSize) { - err.set(CL_INVALID_BUFFER_SIZE); - return nullptr; - } - SVMAllocsManager::UnifiedMemoryProperties unifiedMemoryProperties(InternalMemoryType::SHARED_UNIFIED_MEMORY); cl_mem_flags flags = 0; cl_mem_flags_intel flagsIntel = 0; @@ -3542,14 +3538,20 @@ void *clSharedMemAllocINTEL( err.set(CL_INVALID_VALUE); return nullptr; } - unifiedMemoryProperties.device = device; - unifiedMemoryProperties.subdeviceBitfield = neoDevice->getDefaultEngine().osContext->getDeviceBitfield(); + + if (size > neoContext->getDevice(0u)->getDeviceInfo().maxMemAllocSize && !unifiedMemoryProperties.allocationFlags.flags.allowUnrestrictedSize) { + err.set(CL_INVALID_BUFFER_SIZE); + return nullptr; + } if (isValueSet(unifiedMemoryProperties.allocationFlags.allAllocFlags, CL_MEM_ALLOC_WRITE_COMBINED_INTEL)) { err.set(CL_INVALID_VALUE); return nullptr; } + unifiedMemoryProperties.device = device; + unifiedMemoryProperties.subdeviceBitfield = neoDevice->getDefaultEngine().osContext->getDeviceBitfield(); + return neoContext->getSVMAllocsManager()->createSharedUnifiedMemoryAllocation(neoContext->getDevice(0)->getRootDeviceIndex(), size, unifiedMemoryProperties, neoContext->getSpecialQueue()); } diff --git a/unit_tests/api/cl_unified_shared_memory_tests.inl b/unit_tests/api/cl_unified_shared_memory_tests.inl index 0fc5e4c867..c31880c839 100644 --- a/unit_tests/api/cl_unified_shared_memory_tests.inl +++ b/unit_tests/api/cl_unified_shared_memory_tests.inl @@ -835,3 +835,80 @@ TEST(clUnifiedSharedMemoryTests, givenInvalidMemPropertiesWhenClSharedMemAllocIn EXPECT_EQ(CL_INVALID_VALUE, retVal); EXPECT_EQ(nullptr, unfiedMemorySharedAllocation); } + +TEST(clUnifiedSharedMemoryTests, givenUnifiedMemoryAllocationSizeGreaterThanMaxMemAllocSizeAndClMemAllowUnrestrictedSizeFlagWhenCreateAllocationThenSuccesIsReturned) { + MockContext mockContext; + cl_int retVal = CL_SUCCESS; + cl_mem_properties_intel properties[] = {CL_MEM_FLAGS, CL_MEM_ALLOW_UNRESTRICTED_SIZE_INTEL, 0}; + auto bigSize = MemoryConstants::gigaByte * 10; + auto allocationSize = static_cast(bigSize); + auto memoryManager = static_cast(mockContext.getDevice(0u)->getMemoryManager()); + memoryManager->turnOnFakingBigAllocations(); + if (memoryManager->peekForce32BitAllocations() || is32bit) { + GTEST_SKIP(); + } + + { + auto unfiedMemoryAllocation = clDeviceMemAllocINTEL(&mockContext, mockContext.getDevice(0u), properties, allocationSize, 0, &retVal); + + EXPECT_EQ(CL_SUCCESS, retVal); + ASSERT_NE(nullptr, unfiedMemoryAllocation); + + retVal = clMemFreeINTEL(&mockContext, unfiedMemoryAllocation); + EXPECT_EQ(CL_SUCCESS, retVal); + } + + { + auto unfiedMemoryAllocation = clSharedMemAllocINTEL(&mockContext, mockContext.getDevice(0u), properties, allocationSize, 0, &retVal); + + EXPECT_EQ(CL_SUCCESS, retVal); + ASSERT_NE(nullptr, unfiedMemoryAllocation); + + retVal = clMemFreeINTEL(&mockContext, unfiedMemoryAllocation); + EXPECT_EQ(CL_SUCCESS, retVal); + } + + { + auto unfiedMemoryAllocation = clHostMemAllocINTEL(&mockContext, properties, allocationSize, 0, &retVal); + + EXPECT_EQ(CL_SUCCESS, retVal); + ASSERT_NE(nullptr, unfiedMemoryAllocation); + + retVal = clMemFreeINTEL(&mockContext, unfiedMemoryAllocation); + EXPECT_EQ(CL_SUCCESS, retVal); + } +} + +TEST(clUnifiedSharedMemoryTests, givenUnifiedMemoryAllocationSizeGreaterThanMaxMemAllocSizeWhenCreateAllocationThenErrorIsReturned) { + MockContext mockContext; + cl_int retVal = CL_SUCCESS; + cl_mem_properties_intel properties[] = {0}; + auto bigSize = MemoryConstants::gigaByte * 10; + auto allocationSize = static_cast(bigSize); + auto memoryManager = static_cast(mockContext.getDevice(0u)->getMemoryManager()); + memoryManager->turnOnFakingBigAllocations(); + if (memoryManager->peekForce32BitAllocations() || is32bit) { + GTEST_SKIP(); + } + + { + auto unfiedMemoryAllocation = clDeviceMemAllocINTEL(&mockContext, mockContext.getDevice(0u), properties, allocationSize, 0, &retVal); + + EXPECT_NE(CL_SUCCESS, retVal); + EXPECT_EQ(nullptr, unfiedMemoryAllocation); + } + + { + auto unfiedMemoryAllocation = clSharedMemAllocINTEL(&mockContext, mockContext.getDevice(0u), properties, allocationSize, 0, &retVal); + + EXPECT_NE(CL_SUCCESS, retVal); + EXPECT_EQ(nullptr, unfiedMemoryAllocation); + } + + { + auto unfiedMemoryAllocation = clHostMemAllocINTEL(&mockContext, properties, allocationSize, 0, &retVal); + + EXPECT_NE(CL_SUCCESS, retVal); + EXPECT_EQ(nullptr, unfiedMemoryAllocation); + } +}