Skip to content

Commit f9307bb

Browse files
Fix don't store internal host mem in hostPtrMap
Signed-off-by: Maciej Plewka <[email protected]>
1 parent 0d68c55 commit f9307bb

File tree

4 files changed

+82
-6
lines changed

4 files changed

+82
-6
lines changed

level_zero/core/source/cmdlist/cmdlist.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,10 @@ NEO::GraphicsAllocation *CommandList::getHostPtrAlloc(const void *buffer, uint64
8080
UNRECOVERABLE_IF(alloc == nullptr);
8181
if (this->cmdListType == CommandListType::TYPE_IMMEDIATE && this->isFlushTaskSubmissionEnabled) {
8282
this->csr->getInternalAllocationStorage()->storeAllocation(std::unique_ptr<NEO::GraphicsAllocation>(alloc), NEO::AllocationUsage::TEMPORARY_ALLOCATION);
83-
} else {
83+
} else if (alloc->getAllocationType() == NEO::AllocationType::EXTERNAL_HOST_PTR) {
8484
hostPtrMap.insert(std::make_pair(buffer, alloc));
85+
} else {
86+
commandContainer.getDeallocationContainer().push_back(alloc);
8587
}
8688
return alloc;
8789
}

level_zero/core/source/cmdlist/cmdlist_hw.inl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1757,10 +1757,12 @@ inline AlignedAllocationData CommandListCoreFamily<gfxCoreFamily>::getAlignedAll
17571757
} else {
17581758
alloc = getHostPtrAlloc(buffer, bufferSize, hostCopyAllowed);
17591759
alignedPtr = static_cast<uintptr_t>(alignDown(alloc->getGpuAddress(), NEO::EncodeSurfaceState<GfxFamily>::getSurfaceBaseAddressAlignment()));
1760-
auto hostAllocCpuPtr = reinterpret_cast<uintptr_t>(alloc->getUnderlyingBuffer());
1761-
hostAllocCpuPtr = alignDown(hostAllocCpuPtr, NEO::EncodeSurfaceState<GfxFamily>::getSurfaceBaseAddressAlignment());
1762-
auto allignedPtrOffset = sourcePtr - hostAllocCpuPtr;
1763-
alignedPtr = ptrOffset(alignedPtr, allignedPtrOffset);
1760+
if (alloc->getAllocationType() == NEO::AllocationType::EXTERNAL_HOST_PTR) {
1761+
auto hostAllocCpuPtr = reinterpret_cast<uintptr_t>(alloc->getUnderlyingBuffer());
1762+
hostAllocCpuPtr = alignDown(hostAllocCpuPtr, NEO::EncodeSurfaceState<GfxFamily>::getSurfaceBaseAddressAlignment());
1763+
auto allignedPtrOffset = sourcePtr - hostAllocCpuPtr;
1764+
alignedPtr = ptrOffset(alignedPtr, allignedPtrOffset);
1765+
}
17641766
}
17651767

17661768
hostPointerNeedsFlush = true;

level_zero/core/test/unit_tests/sources/cmdlist/test_cmdlist_3.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,5 +1287,76 @@ HWTEST2_F(CommandListCreate, givenNonEmptyCommandsToPatchWhenClearCommandsToPatc
12871287
EXPECT_TRUE(pCommandList->commandsToPatch.empty());
12881288
}
12891289

1290+
template <NEO::AllocationType AllocType>
1291+
class MyDeviceMock : public Mock<Device> {
1292+
public:
1293+
NEO::GraphicsAllocation *allocateMemoryFromHostPtr(const void *buffer, size_t size, bool hostCopyAllowed) override {
1294+
auto alloc = std::make_unique<NEO::MockGraphicsAllocation>(const_cast<void *>(buffer), reinterpret_cast<uintptr_t>(buffer), size);
1295+
alloc->allocationType = AllocType;
1296+
return alloc.release();
1297+
}
1298+
const NEO::HardwareInfo &getHwInfo() const override {
1299+
return neoDevice->getHardwareInfo();
1300+
}
1301+
};
1302+
1303+
HWTEST2_F(CommandListCreate, givenHostPtrAllocAllocWhenInternalMemCreatedThenNewAllocAddedToDealocationContainer, IsAtLeastSkl) {
1304+
auto myDevice = std::make_unique<MyDeviceMock<NEO::AllocationType::INTERNAL_HOST_MEMORY>>();
1305+
myDevice->neoDevice = device->getNEODevice();
1306+
auto commandList = std::make_unique<WhiteBox<::L0::CommandListCoreFamily<gfxCoreFamily>>>();
1307+
commandList->initialize(myDevice.get(), NEO::EngineGroupType::Copy, 0u);
1308+
auto buffer = std::make_unique<uint8_t>(0x100);
1309+
1310+
auto deallocationSize = commandList->commandContainer.getDeallocationContainer().size();
1311+
auto alloc = commandList->getHostPtrAlloc(buffer.get(), 0x80, true);
1312+
EXPECT_EQ(deallocationSize + 1, commandList->commandContainer.getDeallocationContainer().size());
1313+
EXPECT_NE(alloc, nullptr);
1314+
driverHandle.get()->getMemoryManager()->freeGraphicsMemory(alloc);
1315+
commandList->commandContainer.getDeallocationContainer().clear();
1316+
}
1317+
1318+
HWTEST2_F(CommandListCreate, givenHostPtrAllocAllocWhenExternalMemCreatedThenNewAllocAddedToHostPtrMap, IsAtLeastSkl) {
1319+
auto myDevice = std::make_unique<MyDeviceMock<NEO::AllocationType::EXTERNAL_HOST_PTR>>();
1320+
myDevice->neoDevice = device->getNEODevice();
1321+
auto commandList = std::make_unique<WhiteBox<::L0::CommandListCoreFamily<gfxCoreFamily>>>();
1322+
commandList->initialize(myDevice.get(), NEO::EngineGroupType::Copy, 0u);
1323+
auto buffer = std::make_unique<uint8_t>(0x100);
1324+
1325+
auto hostPtrMapSize = commandList->getHostPtrMap().size();
1326+
auto alloc = commandList->getHostPtrAlloc(buffer.get(), 0x100, true);
1327+
EXPECT_EQ(hostPtrMapSize + 1, commandList->getHostPtrMap().size());
1328+
EXPECT_NE(alloc, nullptr);
1329+
driverHandle.get()->getMemoryManager()->freeGraphicsMemory(alloc);
1330+
commandList->hostPtrMap.clear();
1331+
}
1332+
1333+
HWTEST2_F(CommandListCreate, givenGetAlignedAllocationWhenInternalMemWithinDifferentAllocThenReturnNewAlloc, IsAtLeastSkl) {
1334+
auto myDevice = std::make_unique<MyDeviceMock<NEO::AllocationType::INTERNAL_HOST_MEMORY>>();
1335+
myDevice->neoDevice = device->getNEODevice();
1336+
auto commandList = std::make_unique<WhiteBox<::L0::CommandListCoreFamily<gfxCoreFamily>>>();
1337+
commandList->initialize(myDevice.get(), NEO::EngineGroupType::Copy, 0u);
1338+
auto buffer = std::make_unique<uint8_t>(0x100);
1339+
1340+
auto outData1 = commandList->getAlignedAllocation(device, buffer.get(), 0x100, true);
1341+
auto outData2 = commandList->getAlignedAllocation(device, &buffer.get()[5], 0x1, true);
1342+
EXPECT_NE(outData1.alloc, outData2.alloc);
1343+
driverHandle.get()->getMemoryManager()->freeGraphicsMemory(outData1.alloc);
1344+
driverHandle.get()->getMemoryManager()->freeGraphicsMemory(outData2.alloc);
1345+
commandList->commandContainer.getDeallocationContainer().clear();
1346+
}
1347+
HWTEST2_F(CommandListCreate, givenGetAlignedAllocationWhenExternalMemWithinDifferentAllocThenReturnPreviouslyAllocatedMem, IsAtLeastSkl) {
1348+
auto myDevice = std::make_unique<MyDeviceMock<NEO::AllocationType::EXTERNAL_HOST_PTR>>();
1349+
myDevice->neoDevice = device->getNEODevice();
1350+
auto commandList = std::make_unique<WhiteBox<::L0::CommandListCoreFamily<gfxCoreFamily>>>();
1351+
commandList->initialize(myDevice.get(), NEO::EngineGroupType::Copy, 0u);
1352+
auto buffer = std::make_unique<uint8_t>(0x100);
1353+
1354+
auto outData1 = commandList->getAlignedAllocation(device, buffer.get(), 0x100, true);
1355+
auto outData2 = commandList->getAlignedAllocation(device, &buffer.get()[5], 0x1, true);
1356+
EXPECT_EQ(outData1.alloc, outData2.alloc);
1357+
driverHandle.get()->getMemoryManager()->freeGraphicsMemory(outData1.alloc);
1358+
commandList->hostPtrMap.clear();
1359+
}
1360+
12901361
} // namespace ult
12911362
} // namespace L0

shared/test/common/mocks/mock_graphics_allocation.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2018-2021 Intel Corporation
2+
* Copyright (C) 2018-2022 Intel Corporation
33
*
44
* SPDX-License-Identifier: MIT
55
*
@@ -18,6 +18,7 @@ constexpr DeviceBitfield mockDeviceBitfield(0b1);
1818
class MockGraphicsAllocation : public MemoryAllocation {
1919
public:
2020
using MemoryAllocation::allocationOffset;
21+
using MemoryAllocation::allocationType;
2122
using MemoryAllocation::aubInfo;
2223
using MemoryAllocation::gpuAddress;
2324
using MemoryAllocation::MemoryAllocation;

0 commit comments

Comments
 (0)