Skip to content

Commit 2d1ef04

Browse files
Allow aligning allocations VA to 2MBt pu
Currently under a debug flag Related-To: NEO-5750 Signed-off-by: Maciej Dziuban <[email protected]>
1 parent 6bb76c8 commit 2d1ef04

File tree

4 files changed

+136
-2
lines changed

4 files changed

+136
-2
lines changed

opencl/test/unit_test/os_interface/linux/drm_memory_manager_allocate_in_device_pool_tests_dg1.cpp

Lines changed: 127 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,16 @@ class DrmMemoryManagerLocalMemoryTest : public ::testing::Test {
131131
memoryManager = std::make_unique<TestedDrmMemoryManager>(localMemoryEnabled, false, false, *executionEnvironment);
132132
}
133133

134+
bool isAllocationWithinHeap(const GraphicsAllocation &allocation, HeapIndex heap) {
135+
const auto allocationStart = allocation.getGpuAddress();
136+
const auto allocationEnd = allocationStart + allocation.getUnderlyingBufferSize();
137+
const auto heapStart = GmmHelper::canonize(memoryManager->getGfxPartition(rootDeviceIndex)->getHeapBase(heap));
138+
const auto heapEnd = GmmHelper::canonize(memoryManager->getGfxPartition(rootDeviceIndex)->getHeapLimit(heap));
139+
return heapStart <= allocationStart && allocationEnd <= heapEnd;
140+
}
141+
134142
protected:
143+
DebugManagerStateRestore restorer{};
135144
ExecutionEnvironment *executionEnvironment = nullptr;
136145
std::unique_ptr<MockDevice> device;
137146
std::unique_ptr<TestedDrmMemoryManager> memoryManager;
@@ -882,8 +891,13 @@ TEST_F(DrmMemoryManagerLocalMemoryTest, givenSupportedTypeWhenAllocatingInDevice
882891
EXPECT_LT(GmmHelper::canonize(memoryManager->getGfxPartition(0)->getHeapBase(HeapIndex::HEAP_INTERNAL_DEVICE_MEMORY)), gpuAddress);
883892
EXPECT_GT(GmmHelper::canonize(memoryManager->getGfxPartition(0)->getHeapLimit(HeapIndex::HEAP_INTERNAL_DEVICE_MEMORY)), gpuAddress);
884893
} else {
894+
const bool prefer2MBAlignment = allocation->getUnderlyingBufferSize() >= 2 * MemoryConstants::megaByte;
895+
const bool prefer57bitAddressing = memoryManager->getGfxPartition(0)->getHeapLimit(HeapIndex::HEAP_EXTENDED) > 0 && !allocData.flags.resource48Bit;
896+
885897
auto heap = HeapIndex::HEAP_STANDARD64KB;
886-
if (memoryManager->getGfxPartition(0)->getHeapLimit(HeapIndex::HEAP_EXTENDED) && !allocData.flags.resource48Bit) {
898+
if (prefer2MBAlignment) {
899+
heap = HeapIndex::HEAP_STANDARD2MB;
900+
} else if (prefer57bitAddressing) {
887901
heap = HeapIndex::HEAP_EXTENDED;
888902
}
889903

@@ -899,6 +913,118 @@ TEST_F(DrmMemoryManagerLocalMemoryTest, givenSupportedTypeWhenAllocatingInDevice
899913
}
900914
}
901915

916+
TEST_F(DrmMemoryManagerLocalMemoryTest, given2MbAlignmentAllowedWhenAllocatingAllocationLessThen2MbThenUse64kbHeap) {
917+
AllocationData allocationData;
918+
allocationData.allFlags = 0;
919+
allocationData.size = MemoryConstants::pageSize;
920+
allocationData.flags.allocateMemory = true;
921+
allocationData.rootDeviceIndex = rootDeviceIndex;
922+
allocationData.type = GraphicsAllocation::AllocationType::BUFFER;
923+
allocationData.flags.resource48Bit = true;
924+
MemoryManager::AllocationStatus allocationStatus;
925+
926+
{
927+
DebugManager.flags.AlignLocalMemoryVaTo2MB.set(-1);
928+
auto allocation = memoryManager->allocateGraphicsMemoryInDevicePool(allocationData, allocationStatus);
929+
ASSERT_NE(nullptr, allocation);
930+
EXPECT_EQ(MemoryManager::AllocationStatus::Success, allocationStatus);
931+
EXPECT_TRUE(isAllocationWithinHeap(*allocation, HeapIndex::HEAP_STANDARD64KB));
932+
memoryManager->freeGraphicsMemory(allocation);
933+
}
934+
{
935+
DebugManager.flags.AlignLocalMemoryVaTo2MB.set(0);
936+
auto allocation = memoryManager->allocateGraphicsMemoryInDevicePool(allocationData, allocationStatus);
937+
ASSERT_NE(nullptr, allocation);
938+
EXPECT_EQ(MemoryManager::AllocationStatus::Success, allocationStatus);
939+
EXPECT_TRUE(isAllocationWithinHeap(*allocation, HeapIndex::HEAP_STANDARD64KB));
940+
memoryManager->freeGraphicsMemory(allocation);
941+
}
942+
{
943+
DebugManager.flags.AlignLocalMemoryVaTo2MB.set(1);
944+
auto allocation = memoryManager->allocateGraphicsMemoryInDevicePool(allocationData, allocationStatus);
945+
ASSERT_NE(nullptr, allocation);
946+
EXPECT_EQ(MemoryManager::AllocationStatus::Success, allocationStatus);
947+
EXPECT_TRUE(isAllocationWithinHeap(*allocation, HeapIndex::HEAP_STANDARD64KB));
948+
memoryManager->freeGraphicsMemory(allocation);
949+
}
950+
}
951+
952+
TEST_F(DrmMemoryManagerLocalMemoryTest, given2MbAlignmentAllowedWhenAllocatingAllocationBiggerThan2MbThenUse2MbHeap) {
953+
AllocationData allocationData;
954+
allocationData.allFlags = 0;
955+
allocationData.size = 2 * MemoryConstants::megaByte;
956+
allocationData.flags.allocateMemory = true;
957+
allocationData.rootDeviceIndex = rootDeviceIndex;
958+
allocationData.type = GraphicsAllocation::AllocationType::BUFFER;
959+
allocationData.flags.resource48Bit = true;
960+
MemoryManager::AllocationStatus allocationStatus;
961+
962+
{
963+
DebugManager.flags.AlignLocalMemoryVaTo2MB.set(-1);
964+
auto allocation = memoryManager->allocateGraphicsMemoryInDevicePool(allocationData, allocationStatus);
965+
ASSERT_NE(nullptr, allocation);
966+
EXPECT_EQ(MemoryManager::AllocationStatus::Success, allocationStatus);
967+
EXPECT_TRUE(isAllocationWithinHeap(*allocation, HeapIndex::HEAP_STANDARD2MB));
968+
memoryManager->freeGraphicsMemory(allocation);
969+
}
970+
{
971+
DebugManager.flags.AlignLocalMemoryVaTo2MB.set(0);
972+
auto allocation = memoryManager->allocateGraphicsMemoryInDevicePool(allocationData, allocationStatus);
973+
ASSERT_NE(nullptr, allocation);
974+
EXPECT_EQ(MemoryManager::AllocationStatus::Success, allocationStatus);
975+
EXPECT_TRUE(isAllocationWithinHeap(*allocation, HeapIndex::HEAP_STANDARD64KB));
976+
memoryManager->freeGraphicsMemory(allocation);
977+
}
978+
{
979+
DebugManager.flags.AlignLocalMemoryVaTo2MB.set(1);
980+
auto allocation = memoryManager->allocateGraphicsMemoryInDevicePool(allocationData, allocationStatus);
981+
ASSERT_NE(nullptr, allocation);
982+
EXPECT_EQ(MemoryManager::AllocationStatus::Success, allocationStatus);
983+
EXPECT_TRUE(isAllocationWithinHeap(*allocation, HeapIndex::HEAP_STANDARD2MB));
984+
memoryManager->freeGraphicsMemory(allocation);
985+
}
986+
}
987+
988+
TEST_F(DrmMemoryManagerLocalMemoryTest, givenExtendedHeapPreferredAnd2MbAlignmentAllowedWhenAllocatingAllocationBiggerThenPrefer2MbHeap) {
989+
if (memoryManager->getGfxPartition(0)->getHeapLimit(HeapIndex::HEAP_EXTENDED) == 0) {
990+
GTEST_SKIP();
991+
}
992+
993+
AllocationData allocationData;
994+
allocationData.allFlags = 0;
995+
allocationData.size = 2 * MemoryConstants::megaByte;
996+
allocationData.flags.allocateMemory = true;
997+
allocationData.rootDeviceIndex = rootDeviceIndex;
998+
allocationData.type = GraphicsAllocation::AllocationType::BUFFER;
999+
allocationData.flags.resource48Bit = false;
1000+
MemoryManager::AllocationStatus allocationStatus;
1001+
1002+
{
1003+
DebugManager.flags.AlignLocalMemoryVaTo2MB.set(-1);
1004+
auto allocation = memoryManager->allocateGraphicsMemoryInDevicePool(allocationData, allocationStatus);
1005+
ASSERT_NE(nullptr, allocation);
1006+
EXPECT_EQ(MemoryManager::AllocationStatus::Success, allocationStatus);
1007+
EXPECT_TRUE(isAllocationWithinHeap(*allocation, HeapIndex::HEAP_STANDARD2MB));
1008+
memoryManager->freeGraphicsMemory(allocation);
1009+
}
1010+
{
1011+
DebugManager.flags.AlignLocalMemoryVaTo2MB.set(0);
1012+
auto allocation = memoryManager->allocateGraphicsMemoryInDevicePool(allocationData, allocationStatus);
1013+
ASSERT_NE(nullptr, allocation);
1014+
EXPECT_EQ(MemoryManager::AllocationStatus::Success, allocationStatus);
1015+
EXPECT_TRUE(isAllocationWithinHeap(*allocation, HeapIndex::HEAP_EXTENDED));
1016+
memoryManager->freeGraphicsMemory(allocation);
1017+
}
1018+
{
1019+
DebugManager.flags.AlignLocalMemoryVaTo2MB.set(1);
1020+
auto allocation = memoryManager->allocateGraphicsMemoryInDevicePool(allocationData, allocationStatus);
1021+
ASSERT_NE(nullptr, allocation);
1022+
EXPECT_EQ(MemoryManager::AllocationStatus::Success, allocationStatus);
1023+
EXPECT_TRUE(isAllocationWithinHeap(*allocation, HeapIndex::HEAP_STANDARD2MB));
1024+
memoryManager->freeGraphicsMemory(allocation);
1025+
}
1026+
}
1027+
9021028
TEST_F(DrmMemoryManagerLocalMemoryTest, givenUnsupportedTypeWhenAllocatingInDevicePoolThenRetryInNonDevicePoolStatusAndNullptrIsReturned) {
9031029
MemoryManager::AllocationStatus status = MemoryManager::AllocationStatus::Success;
9041030
AllocationData allocData;

opencl/test/unit_test/test_files/igdrcl.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,4 +238,5 @@ DebuggerLogBitmask = 0
238238
GTPinAllocateBufferInSharedMemory = -1
239239
DeferOsContextInitialization = -1
240240
DebuggerOptDisable = -1
241+
AlignLocalMemoryVaTo2MB = -1
241242
EngineInstancedSubDevices = 0

shared/source/debug_settings/debug_variables_base.inl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ DECLARE_DEBUG_VARIABLE(int32_t, OverrideSlmSize, -1, "Force different slm size t
225225
DECLARE_DEBUG_VARIABLE(int32_t, UseCyclesPerSecondTimer, 0, "0: default behavior, 0: disabled: Report L0 timer in nanosecond units, 1: enabled: Report L0 timer in cycles per second")
226226
DECLARE_DEBUG_VARIABLE(int32_t, WaitLoopCount, -1, "-1: use default, >=0: number of iterations in wait loop")
227227
DECLARE_DEBUG_VARIABLE(int32_t, GTPinAllocateBufferInSharedMemory, -1, "Force GTPin to allocate buffer in shared memory")
228+
DECLARE_DEBUG_VARIABLE(int32_t, AlignLocalMemoryVaTo2MB, -1, "VA for local mem allocs greater than 2MB, will be aligned to 2MB on Linux -1: default, 0: disabled, 1: enabled")
228229

229230
/*DRIVER TOGGLES*/
230231
DECLARE_DEBUG_VARIABLE(int32_t, ForceOCLVersion, 0, "Force specific OpenCL API version")

shared/source/os_interface/linux/drm_memory_manager_allocate_in_device_pool_dg1.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,16 @@ uint64_t getGpuAddress(HeapAssigner &heapAssigner, const HardwareInfo &hwInfo, G
144144
sizeAllocated = 0;
145145
break;
146146
default:
147+
const bool prefer2MBAlignment = DebugManager.flags.AlignLocalMemoryVaTo2MB.get() != 0 && sizeAllocated >= 2 * MemoryConstants::megaByte;
148+
const bool prefer57bitAddressing = gfxPartition->getHeapLimit(HeapIndex::HEAP_EXTENDED) > 0 && !resource48Bit;
149+
147150
auto heapIndex = HeapIndex::HEAP_STANDARD64KB;
148-
if ((gfxPartition->getHeapLimit(HeapIndex::HEAP_EXTENDED) > 0) && !resource48Bit) {
151+
if (prefer2MBAlignment) {
152+
heapIndex = HeapIndex::HEAP_STANDARD2MB;
153+
} else if (prefer57bitAddressing) {
149154
heapIndex = HeapIndex::HEAP_EXTENDED;
150155
}
156+
151157
gpuAddress = GmmHelper::canonize(gfxPartition->heapAllocate(heapIndex, sizeAllocated));
152158
break;
153159
}

0 commit comments

Comments
 (0)