diff --git a/level_zero/core/test/unit_tests/sources/memory/test_memory.cpp b/level_zero/core/test/unit_tests/sources/memory/test_memory.cpp index 009b82702c..f253a4e738 100644 --- a/level_zero/core/test/unit_tests/sources/memory/test_memory.cpp +++ b/level_zero/core/test/unit_tests/sources/memory/test_memory.cpp @@ -1695,7 +1695,7 @@ TEST_F(MemoryTest, whenAllocatingDeviceMemoryThenAlignmentIsPassedCorrectlyAndMe do { alignment >>= 1; memoryManager->validateAllocateProperties = [alignment](const AllocationProperties &properties) { - EXPECT_EQ(properties.alignment, alignUp(alignment, MemoryConstants::pageSize64k)); + EXPECT_EQ(properties.alignment, alignUpNonZero(alignment, MemoryConstants::pageSize64k)); }; void *ptr = nullptr; ze_result_t result = context->allocDeviceMem(device->toHandle(), &deviceDesc, size, alignment, &ptr); @@ -1722,7 +1722,7 @@ TEST_F(MemoryTest, whenAllocatingHostMemoryThenAlignmentIsPassedCorrectlyAndMemo do { alignment >>= 1; memoryManager->validateAllocateProperties = [alignment](const AllocationProperties &properties) { - EXPECT_EQ(properties.alignment, alignUp(alignment, MemoryConstants::pageSize)); + EXPECT_EQ(properties.alignment, alignUpNonZero(alignment, MemoryConstants::pageSize)); }; void *ptr = nullptr; ze_result_t result = context->allocHostMem(&hostDesc, size, alignment, &ptr); @@ -1753,7 +1753,7 @@ TEST_F(MemoryTest, whenAllocatingSharedMemoryThenAlignmentIsPassedCorrectlyAndMe do { alignment >>= 1; memoryManager->validateAllocateProperties = [alignment](const AllocationProperties &properties) { - EXPECT_EQ(properties.alignment, alignUp(alignment, MemoryConstants::pageSize64k)); + EXPECT_EQ(properties.alignment, alignUpNonZero(alignment, MemoryConstants::pageSize64k)); }; void *ptr = nullptr; ze_result_t result = context->allocSharedMem(device->toHandle(), &deviceDesc, &hostDesc, size, alignment, &ptr); diff --git a/shared/source/helpers/aligned_memory.h b/shared/source/helpers/aligned_memory.h index 2c58123539..3d713800d0 100644 --- a/shared/source/helpers/aligned_memory.h +++ b/shared/source/helpers/aligned_memory.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 Intel Corporation + * Copyright (C) 2018-2023 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -25,6 +25,15 @@ constexpr inline TNoRef alignUp(T before, size_t alignment) { return (before + mask) & ~mask; } +template ::type> +constexpr inline TNoRef alignUpNonZero(T before, size_t alignment) { + if (before == 0) { + return alignment; + } else { + return alignUp(before, alignment); + } +} + template constexpr inline T *alignUp(T *ptrBefore, size_t alignment) { return reinterpret_cast(alignUp(reinterpret_cast(ptrBefore), alignment)); diff --git a/shared/source/memory_manager/unified_memory_manager.cpp b/shared/source/memory_manager/unified_memory_manager.cpp index d82fa587b2..9d2efb5df2 100644 --- a/shared/source/memory_manager/unified_memory_manager.cpp +++ b/shared/source/memory_manager/unified_memory_manager.cpp @@ -196,7 +196,7 @@ void *SVMAllocsManager::createSVMAlloc(size_t size, const SvmAllocationPropertie void *SVMAllocsManager::createHostUnifiedMemoryAllocation(size_t size, const UnifiedMemoryProperties &memoryProperties) { - size_t pageSizeForAlignment = alignUp(memoryProperties.alignment, MemoryConstants::pageSize); + size_t pageSizeForAlignment = alignUpNonZero(memoryProperties.alignment, MemoryConstants::pageSize); size_t alignedSize = alignUp(size, MemoryConstants::pageSize); bool compressionEnabled = false; @@ -248,7 +248,7 @@ void *SVMAllocsManager::createUnifiedMemoryAllocation(size_t size, const UnifiedMemoryProperties &memoryProperties) { auto rootDeviceIndex = memoryProperties.getRootDeviceIndex(); DeviceBitfield deviceBitfield = memoryProperties.subdeviceBitfields.at(rootDeviceIndex); - size_t pageSizeForAlignment = alignUp(memoryProperties.alignment, MemoryConstants::pageSize64k); + size_t pageSizeForAlignment = alignUpNonZero(memoryProperties.alignment, MemoryConstants::pageSize64k); size_t alignedSize = alignUp(size, MemoryConstants::pageSize64k); auto externalPtr = reinterpret_cast(memoryProperties.allocationFlags.hostptr); @@ -377,7 +377,7 @@ void *SVMAllocsManager::createUnifiedKmdMigratedAllocation(size_t size, const Sv auto rootDeviceIndex = unifiedMemoryProperties.getRootDeviceIndex(); auto &deviceBitfield = unifiedMemoryProperties.subdeviceBitfields.at(rootDeviceIndex); - size_t pageSizeForAlignment = std::max(alignUp(unifiedMemoryProperties.alignment, MemoryConstants::pageSize2M), MemoryConstants::pageSize2M); + size_t pageSizeForAlignment = std::max(alignUpNonZero(unifiedMemoryProperties.alignment, MemoryConstants::pageSize2M), MemoryConstants::pageSize2M); size_t alignedSize = alignUp(size, MemoryConstants::pageSize2M); AllocationProperties gpuProperties{rootDeviceIndex, true, @@ -570,7 +570,7 @@ void *SVMAllocsManager::createUnifiedAllocationWithDeviceStorage(size_t size, co auto rootDeviceIndex = unifiedMemoryProperties.getRootDeviceIndex(); auto externalPtr = reinterpret_cast(unifiedMemoryProperties.allocationFlags.hostptr); bool useExternalHostPtrForCpu = externalPtr != nullptr; - const auto pageSizeForAlignment = alignUp(unifiedMemoryProperties.alignment, MemoryConstants::pageSize64k); + const auto pageSizeForAlignment = alignUpNonZero(unifiedMemoryProperties.alignment, MemoryConstants::pageSize64k); size_t alignedSize = alignUp(size, MemoryConstants::pageSize64k); DeviceBitfield subDevices = unifiedMemoryProperties.subdeviceBitfields.at(rootDeviceIndex); auto cpuAlignment = std::max(pageSizeForAlignment, memoryManager->peekExecutionEnvironment().rootDeviceEnvironments[rootDeviceIndex]->getProductHelper().getSvmCpuAlignment()); diff --git a/shared/test/unit_test/helpers/aligned_memory_tests.cpp b/shared/test/unit_test/helpers/aligned_memory_tests.cpp index d39c052b54..3b749d96ae 100644 --- a/shared/test/unit_test/helpers/aligned_memory_tests.cpp +++ b/shared/test/unit_test/helpers/aligned_memory_tests.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2022 Intel Corporation + * Copyright (C) 2018-2023 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -119,6 +119,24 @@ INSTANTIATE_TEST_CASE_P( 256, 4096)); +struct AlignUpNonZero : public ::testing::TestWithParam> { +}; + +TEST_P(AlignUpNonZero, GivenSizeAndAlignmentThenAlignedResultIsNonZero) { + size_t size = std::get<0>(GetParam()); + size_t alignment = std::get<1>(GetParam()); + const size_t result = alignUpNonZero(size, alignment); + EXPECT_TRUE(0 < result); + EXPECT_TRUE(0 == result % alignment); +} + +INSTANTIATE_TEST_CASE_P( + AlignUpNonZeroParameterized, + AlignUpNonZero, + testing::Combine( + testing::Values(0, 1), + testing::Values(1, 4, 8, 32, 64, 256, 4096))); + TEST(AlignWholeSize, GivenSizeLessThanPageSizeWhenAligningWholeSizeToPageThenAlignedSizeIsPageSize) { int size = 1; auto retSize = alignSizeWholePage(ptrAlignedToPage, size); diff --git a/shared/test/unit_test/memory_manager/unified_memory_manager_tests.cpp b/shared/test/unit_test/memory_manager/unified_memory_manager_tests.cpp index 1f722176e1..ec9559d14d 100644 --- a/shared/test/unit_test/memory_manager/unified_memory_manager_tests.cpp +++ b/shared/test/unit_test/memory_manager/unified_memory_manager_tests.cpp @@ -337,7 +337,7 @@ TEST_F(SVMLocalMemoryAllocatorTest, givenAlignmentThenUnifiedMemoryAllocationsAr do { alignment >>= 1; memoryManager->validateAllocateProperties = [alignment](const AllocationProperties &properties) { - EXPECT_EQ(properties.alignment, alignUp(alignment, MemoryConstants::pageSize64k)); + EXPECT_EQ(properties.alignment, alignUpNonZero(alignment, MemoryConstants::pageSize64k)); }; SVMAllocsManager::UnifiedMemoryProperties unifiedMemoryProperties(InternalMemoryType::DEVICE_UNIFIED_MEMORY, alignment, rootDeviceIndices, deviceBitfields); unifiedMemoryProperties.device = device; @@ -362,7 +362,7 @@ TEST_F(SVMLocalMemoryAllocatorTest, givenAlignmentThenHostUnifiedMemoryAllocatio do { alignment >>= 1; memoryManager->validateAllocateProperties = [alignment](const AllocationProperties &properties) { - EXPECT_EQ(properties.alignment, alignUp(alignment, MemoryConstants::pageSize)); + EXPECT_EQ(properties.alignment, alignUpNonZero(alignment, MemoryConstants::pageSize)); }; SVMAllocsManager::UnifiedMemoryProperties unifiedMemoryProperties(InternalMemoryType::HOST_UNIFIED_MEMORY, alignment, rootDeviceIndices, deviceBitfields); unifiedMemoryProperties.device = device; @@ -391,7 +391,7 @@ TEST_F(SVMLocalMemoryAllocatorTest, givenAlignmentThenSharedUnifiedMemoryAllocat do { alignment >>= 1; memoryManager->validateAllocateProperties = [alignment](const AllocationProperties &properties) { - EXPECT_EQ(properties.alignment, alignUp(alignment, MemoryConstants::pageSize64k)); + EXPECT_EQ(properties.alignment, alignUpNonZero(alignment, MemoryConstants::pageSize64k)); }; SVMAllocsManager::UnifiedMemoryProperties unifiedMemoryProperties(InternalMemoryType::SHARED_UNIFIED_MEMORY, alignment, rootDeviceIndices, deviceBitfields); unifiedMemoryProperties.device = device;