Skip to content

Commit 4dd1155

Browse files
Map allocation support for blit operations
Change-Id: Iff1b972b2a46610f9690c3412d00b994691e6e28 Signed-off-by: Dunajski, Bartosz <bartosz.dunajski@intel.com> Related-To: NEO-3020
1 parent 438b27a commit 4dd1155

File tree

7 files changed

+163
-29
lines changed

7 files changed

+163
-29
lines changed

runtime/built_ins/builtins_dispatch_builder.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ struct BuiltinOpParams {
3737
MemObj *dstMemObj = nullptr;
3838
GraphicsAllocation *srcSvmAlloc = nullptr;
3939
GraphicsAllocation *dstSvmAlloc = nullptr;
40+
GraphicsAllocation *mapAllocation = nullptr;
4041
const MemObjsForAuxTranslation *memObjsForAuxTranslation = nullptr;
4142
AuxTranslationDirection auxTranslationDirection = AuxTranslationDirection::None;
4243
bool unifiedMemoryArgsRequireMemSync = true;

runtime/command_queue/enqueue_read_buffer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ cl_int CommandQueueHw<GfxFamily>::enqueueReadBuffer(
9292
dc.srcMemObj = buffer;
9393
dc.srcOffset = {offset, 0, 0};
9494
dc.size = {size, 0, 0};
95+
dc.mapAllocation = mapAllocation;
9596
builder.buildDispatchInfos(dispatchInfo, dc);
9697

9798
if (context->isProvidingPerformanceHints()) {

runtime/command_queue/enqueue_write_buffer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ cl_int CommandQueueHw<GfxFamily>::enqueueWriteBuffer(
8888
dc.dstMemObj = buffer;
8989
dc.dstOffset = {offset, 0, 0};
9090
dc.size = {size, 0, 0};
91+
dc.mapAllocation = mapAllocation;
9192
builder.buildDispatchInfos(dispatchInfo, dc);
9293

9394
enqueueHandler<CL_COMMAND_WRITE_BUFFER>(

runtime/helpers/blit_commands_helper.cpp

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,34 +18,45 @@ namespace NEO {
1818
BlitProperties BlitProperties::constructPropertiesForReadWriteBuffer(BlitterConstants::BlitDirection blitDirection,
1919
CommandStreamReceiver &commandStreamReceiver,
2020
GraphicsAllocation *memObjAllocation, size_t memObjOffset,
21+
GraphicsAllocation *mapAllocation,
2122
void *hostPtr, size_t hostPtrOffset,
2223
bool blocking, size_t copyOffset, uint64_t copySize) {
2324

24-
HostPtrSurface hostPtrSurface(hostPtr, static_cast<size_t>(copySize), true);
25-
bool success = commandStreamReceiver.createAllocationForHostSurface(hostPtrSurface, false);
26-
UNRECOVERABLE_IF(!success);
27-
auto hostPtrAllocation = hostPtrSurface.getAllocation();
25+
GraphicsAllocation *hostAllocation = nullptr;
26+
27+
if (mapAllocation) {
28+
hostAllocation = mapAllocation;
29+
hostPtrOffset += ptrDiff(hostPtr, mapAllocation->getGpuAddress());
30+
} else {
31+
HostPtrSurface hostPtrSurface(hostPtr, static_cast<size_t>(copySize), true);
32+
bool success = commandStreamReceiver.createAllocationForHostSurface(hostPtrSurface, false);
33+
UNRECOVERABLE_IF(!success);
34+
hostAllocation = hostPtrSurface.getAllocation();
35+
}
2836

2937
auto offset = copyOffset + memObjOffset;
3038
if (BlitterConstants::BlitDirection::HostPtrToBuffer == blitDirection) {
31-
return {nullptr, blitDirection, {}, AuxTranslationDirection::None, memObjAllocation, hostPtrAllocation, blocking, offset, hostPtrOffset, copySize};
39+
return {nullptr, blitDirection, {}, AuxTranslationDirection::None, memObjAllocation, hostAllocation, blocking, offset, hostPtrOffset, copySize};
3240
} else {
33-
return {nullptr, blitDirection, {}, AuxTranslationDirection::None, hostPtrAllocation, memObjAllocation, blocking, hostPtrOffset, offset, copySize};
41+
return {nullptr, blitDirection, {}, AuxTranslationDirection::None, hostAllocation, memObjAllocation, blocking, hostPtrOffset, offset, copySize};
3442
}
3543
}
3644

3745
BlitProperties BlitProperties::constructPropertiesForReadWriteBuffer(BlitterConstants::BlitDirection blitDirection,
3846
CommandStreamReceiver &commandStreamReceiver,
3947
const BuiltinOpParams &builtinOpParams,
4048
bool blocking) {
49+
auto mapAllocation = builtinOpParams.mapAllocation;
4150
if (BlitterConstants::BlitDirection::HostPtrToBuffer == blitDirection) {
4251
return constructPropertiesForReadWriteBuffer(blitDirection, commandStreamReceiver, builtinOpParams.dstMemObj->getGraphicsAllocation(),
43-
builtinOpParams.dstMemObj->getOffset(), builtinOpParams.srcPtr, builtinOpParams.srcOffset.x,
44-
blocking, builtinOpParams.dstOffset.x, builtinOpParams.size.x);
52+
builtinOpParams.dstMemObj->getOffset(), mapAllocation, builtinOpParams.srcPtr,
53+
builtinOpParams.srcOffset.x, blocking, builtinOpParams.dstOffset.x,
54+
builtinOpParams.size.x);
4555
} else {
4656
return constructPropertiesForReadWriteBuffer(blitDirection, commandStreamReceiver, builtinOpParams.srcMemObj->getGraphicsAllocation(),
47-
builtinOpParams.srcMemObj->getOffset(), builtinOpParams.dstPtr, builtinOpParams.dstOffset.x,
48-
blocking, builtinOpParams.srcOffset.x, builtinOpParams.size.x);
57+
builtinOpParams.srcMemObj->getOffset(), mapAllocation, builtinOpParams.dstPtr,
58+
builtinOpParams.dstOffset.x, blocking, builtinOpParams.srcOffset.x,
59+
builtinOpParams.size.x);
4960
}
5061
}
5162

runtime/helpers/blit_commands_helper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ struct BlitProperties {
2424
static BlitProperties constructPropertiesForReadWriteBuffer(BlitterConstants::BlitDirection blitDirection,
2525
CommandStreamReceiver &commandStreamReceiver,
2626
GraphicsAllocation *memObjAllocation, size_t memObjOFfset,
27+
GraphicsAllocation *mapAllocation,
2728
void *hostPtr, size_t hostPtrOffset,
2829
bool blocking, size_t copyOffset, uint64_t copySize);
2930

unit_tests/command_stream/command_stream_receiver_hw_tests.cpp

Lines changed: 116 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -384,8 +384,8 @@ HWTEST_F(BcsTests, givenBltSizeWithLeftoverWhenDispatchedThenProgramAllRequiredC
384384
csr.taskCount = newTaskCount - 1;
385385
EXPECT_EQ(0u, csr.recursiveLockCounter.load());
386386
auto blitProperties = BlitProperties::constructPropertiesForReadWriteBuffer(BlitterConstants::BlitDirection::HostPtrToBuffer,
387-
csr, buffer->getGraphicsAllocation(), 0, hostPtr, 0, true,
388-
0, bltSize);
387+
csr, buffer->getGraphicsAllocation(), 0, nullptr, hostPtr,
388+
0, true, 0, bltSize);
389389

390390
csr.blitBuffer(blitProperties);
391391
EXPECT_EQ(newTaskCount, csr.taskCount);
@@ -444,8 +444,8 @@ HWTEST_F(BcsTests, givenCsrDependenciesWhenProgrammingCommandStreamThenAddSemaph
444444
size_t numberNodesPerContainer = 5;
445445

446446
auto blitProperties = BlitProperties::constructPropertiesForReadWriteBuffer(BlitterConstants::BlitDirection::HostPtrToBuffer,
447-
csr, buffer->getGraphicsAllocation(), 0, hostPtr, 0, true,
448-
0, 1);
447+
csr, buffer->getGraphicsAllocation(), 0, nullptr, hostPtr,
448+
0, true, 0, 1);
449449

450450
MockTimestampPacketContainer timestamp0(*csr.getTimestampPacketAllocator(), numberNodesPerContainer);
451451
MockTimestampPacketContainer timestamp1(*csr.getTimestampPacketAllocator(), numberNodesPerContainer);
@@ -493,8 +493,8 @@ HWTEST_F(BcsTests, givenInputAllocationsWhenBlitDispatchedThenMakeAllAllocations
493493
EXPECT_EQ(0u, csr.makeSurfacePackNonResidentCalled);
494494

495495
auto blitProperties = BlitProperties::constructPropertiesForReadWriteBuffer(BlitterConstants::BlitDirection::HostPtrToBuffer,
496-
csr, buffer->getGraphicsAllocation(), 0, hostPtr, 0, true,
497-
0, 1);
496+
csr, buffer->getGraphicsAllocation(), 0, nullptr, hostPtr, 0,
497+
true, 0, 1);
498498

499499
csr.blitBuffer(blitProperties);
500500

@@ -522,8 +522,8 @@ HWTEST_F(BcsTests, givenBufferWhenBlitCalledThenFlushCommandBuffer) {
522522
csr.taskCount = newTaskCount - 1;
523523

524524
auto blitProperties = BlitProperties::constructPropertiesForReadWriteBuffer(BlitterConstants::BlitDirection::HostPtrToBuffer,
525-
csr, buffer->getGraphicsAllocation(), 0, hostPtr, 0, true,
526-
0, 1);
525+
csr, buffer->getGraphicsAllocation(), 0, nullptr, hostPtr,
526+
0, true, 0, 1);
527527

528528
csr.blitBuffer(blitProperties);
529529

@@ -571,8 +571,8 @@ HWTEST_F(BcsTests, whenBlitFromHostPtrCalledThenCallWaitWithKmdFallback) {
571571
void *hostPtr = reinterpret_cast<void *>(0x12340000);
572572

573573
auto blitProperties = BlitProperties::constructPropertiesForReadWriteBuffer(BlitterConstants::BlitDirection::HostPtrToBuffer,
574-
*myMockCsr, buffer->getGraphicsAllocation(), 0, hostPtr, 0,
575-
false, 0, 1);
574+
*myMockCsr, buffer->getGraphicsAllocation(), 0, nullptr,
575+
hostPtr, 0, false, 0, 1);
576576

577577
myMockCsr->blitBuffer(blitProperties);
578578

@@ -602,8 +602,8 @@ HWTEST_F(BcsTests, whenBlitFromHostPtrCalledThenCleanTemporaryAllocations) {
602602
EXPECT_EQ(0u, mockInternalAllocationsStorage->cleanAllocationsCalled);
603603

604604
auto blitProperties = BlitProperties::constructPropertiesForReadWriteBuffer(BlitterConstants::BlitDirection::HostPtrToBuffer,
605-
bcsCsr, buffer->getGraphicsAllocation(), 0, hostPtr, 0,
606-
false, 0, 1);
605+
bcsCsr, buffer->getGraphicsAllocation(), 0, nullptr, hostPtr,
606+
0, false, 0, 1);
607607

608608
bcsCsr.blitBuffer(blitProperties);
609609

@@ -636,7 +636,7 @@ HWTEST_F(BcsTests, givenBufferWhenBlitOperationCalledThenProgramCorrectGpuAddres
636636
HardwareParse hwParser;
637637
auto blitProperties = BlitProperties::constructPropertiesForReadWriteBuffer(BlitterConstants::BlitDirection::HostPtrToBuffer,
638638
csr, subBuffer1->getGraphicsAllocation(), subBuffer1Offset,
639-
hostPtr, hostPtrOffset, true, 0, 1);
639+
nullptr, hostPtr, hostPtrOffset, true, 0, 1);
640640

641641
csr.blitBuffer(blitProperties);
642642

@@ -655,7 +655,7 @@ HWTEST_F(BcsTests, givenBufferWhenBlitOperationCalledThenProgramCorrectGpuAddres
655655
auto offset = csr.commandStream.getUsed();
656656
auto blitProperties = BlitProperties::constructPropertiesForReadWriteBuffer(BlitterConstants::BlitDirection::BufferToHostPtr,
657657
csr, subBuffer1->getGraphicsAllocation(), subBuffer1Offset,
658-
hostPtr, hostPtrOffset, true, 0, 1);
658+
nullptr, hostPtr, hostPtrOffset, true, 0, 1);
659659

660660
csr.blitBuffer(blitProperties);
661661

@@ -687,6 +687,104 @@ HWTEST_F(BcsTests, givenBufferWhenBlitOperationCalledThenProgramCorrectGpuAddres
687687
}
688688
}
689689

690+
HWTEST_F(BcsTests, givenMapAllocationWhenDispatchReadWriteOperationThenSetValidGpuAddress) {
691+
auto &csr = pDevice->getUltCommandStreamReceiver<FamilyType>();
692+
auto memoryManager = csr.getMemoryManager();
693+
694+
AllocationProperties properties{false, 1234, GraphicsAllocation::AllocationType::EXTERNAL_HOST_PTR, false};
695+
GraphicsAllocation *mapAllocation = memoryManager->allocateGraphicsMemoryWithProperties(properties, reinterpret_cast<void *>(0x12340000));
696+
697+
auto mapAllocationOffset = 0x1234;
698+
auto mapPtr = reinterpret_cast<void *>(mapAllocation->getGpuAddress() + mapAllocationOffset);
699+
700+
cl_int retVal = CL_SUCCESS;
701+
auto buffer = clUniquePtr<Buffer>(Buffer::create(context.get(), CL_MEM_READ_WRITE, 100, nullptr, retVal));
702+
703+
const size_t hostPtrOffset = 0x1234;
704+
705+
{
706+
// from hostPtr
707+
HardwareParse hwParser;
708+
auto blitProperties = BlitProperties::constructPropertiesForReadWriteBuffer(BlitterConstants::BlitDirection::HostPtrToBuffer,
709+
csr, buffer->getGraphicsAllocation(), 0,
710+
mapAllocation, mapPtr, hostPtrOffset, true, 0, 1);
711+
712+
csr.blitBuffer(blitProperties);
713+
714+
hwParser.parseCommands<FamilyType>(csr.commandStream);
715+
716+
auto bltCmd = genCmdCast<typename FamilyType::XY_COPY_BLT *>(*hwParser.cmdList.begin());
717+
EXPECT_NE(nullptr, bltCmd);
718+
if (pDevice->isFullRangeSvm()) {
719+
EXPECT_EQ(reinterpret_cast<uint64_t>(ptrOffset(mapPtr, hostPtrOffset)), bltCmd->getSourceBaseAddress());
720+
}
721+
EXPECT_EQ(buffer->getGraphicsAllocation()->getGpuAddress(), bltCmd->getDestinationBaseAddress());
722+
}
723+
724+
{
725+
// to hostPtr
726+
HardwareParse hwParser;
727+
auto offset = csr.commandStream.getUsed();
728+
auto blitProperties = BlitProperties::constructPropertiesForReadWriteBuffer(BlitterConstants::BlitDirection::BufferToHostPtr,
729+
csr, buffer->getGraphicsAllocation(), 0,
730+
mapAllocation, mapPtr, hostPtrOffset, true, 0, 1);
731+
732+
csr.blitBuffer(blitProperties);
733+
734+
hwParser.parseCommands<FamilyType>(csr.commandStream, offset);
735+
736+
auto bltCmd = genCmdCast<typename FamilyType::XY_COPY_BLT *>(*hwParser.cmdList.begin());
737+
EXPECT_NE(nullptr, bltCmd);
738+
if (pDevice->isFullRangeSvm()) {
739+
EXPECT_EQ(reinterpret_cast<uint64_t>(ptrOffset(mapPtr, hostPtrOffset)), bltCmd->getDestinationBaseAddress());
740+
}
741+
EXPECT_EQ(buffer->getGraphicsAllocation()->getGpuAddress(), bltCmd->getSourceBaseAddress());
742+
}
743+
744+
memoryManager->freeGraphicsMemory(mapAllocation);
745+
}
746+
747+
HWTEST_F(BcsTests, givenMapAllocationInBuiltinOpParamsWhenConstructingThenUseItAsSourceOrDstAllocation) {
748+
auto &csr = pDevice->getUltCommandStreamReceiver<FamilyType>();
749+
auto memoryManager = csr.getMemoryManager();
750+
751+
AllocationProperties properties{false, 1234, GraphicsAllocation::AllocationType::EXTERNAL_HOST_PTR, false};
752+
GraphicsAllocation *mapAllocation = memoryManager->allocateGraphicsMemoryWithProperties(properties, reinterpret_cast<void *>(0x12340000));
753+
754+
auto mapAllocationOffset = 0x1234;
755+
auto mapPtr = reinterpret_cast<void *>(mapAllocation->getGpuAddress() + mapAllocationOffset);
756+
757+
cl_int retVal = CL_SUCCESS;
758+
auto buffer = clUniquePtr<Buffer>(Buffer::create(context.get(), CL_MEM_READ_WRITE, 100, nullptr, retVal));
759+
760+
{
761+
// from hostPtr
762+
BuiltinOpParams builtinOpParams = {};
763+
builtinOpParams.dstMemObj = buffer.get();
764+
builtinOpParams.srcPtr = mapPtr;
765+
builtinOpParams.size.x = 1;
766+
builtinOpParams.mapAllocation = mapAllocation;
767+
768+
auto blitProperties = BlitProperties::constructPropertiesForReadWriteBuffer(BlitterConstants::BlitDirection::HostPtrToBuffer,
769+
csr, builtinOpParams, true);
770+
EXPECT_EQ(mapAllocation, blitProperties.srcAllocation);
771+
}
772+
{
773+
// to hostPtr
774+
BuiltinOpParams builtinOpParams = {};
775+
builtinOpParams.srcMemObj = buffer.get();
776+
builtinOpParams.dstPtr = mapPtr;
777+
builtinOpParams.size.x = 1;
778+
builtinOpParams.mapAllocation = mapAllocation;
779+
780+
auto blitProperties = BlitProperties::constructPropertiesForReadWriteBuffer(BlitterConstants::BlitDirection::BufferToHostPtr,
781+
csr, builtinOpParams, true);
782+
EXPECT_EQ(mapAllocation, blitProperties.dstAllocation);
783+
}
784+
785+
memoryManager->freeGraphicsMemory(mapAllocation);
786+
}
787+
690788
HWTEST_F(BcsTests, givenBufferWithOffsetWhenBlitOperationCalledThenProgramCorrectGpuAddresses) {
691789
auto &csr = pDevice->getUltCommandStreamReceiver<FamilyType>();
692790

@@ -703,8 +801,8 @@ HWTEST_F(BcsTests, givenBufferWithOffsetWhenBlitOperationCalledThenProgramCorrec
703801
HardwareParse hwParser;
704802
auto offset = csr.commandStream.getUsed();
705803
auto blitProperties = BlitProperties::constructPropertiesForReadWriteBuffer(BlitterConstants::BlitDirection::HostPtrToBuffer,
706-
csr, buffer1->getGraphicsAllocation(), 0, hostPtr,
707-
0, true, buffer1Offset, 1);
804+
csr, buffer1->getGraphicsAllocation(), 0, nullptr,
805+
hostPtr, 0, true, buffer1Offset, 1);
708806

709807
csr.blitBuffer(blitProperties);
710808

@@ -722,8 +820,8 @@ HWTEST_F(BcsTests, givenBufferWithOffsetWhenBlitOperationCalledThenProgramCorrec
722820
HardwareParse hwParser;
723821
auto offset = csr.commandStream.getUsed();
724822
auto blitProperties = BlitProperties::constructPropertiesForReadWriteBuffer(BlitterConstants::BlitDirection::BufferToHostPtr,
725-
csr, buffer1->getGraphicsAllocation(), 0, hostPtr,
726-
0, true, buffer1Offset, 1);
823+
csr, buffer1->getGraphicsAllocation(), 0, nullptr,
824+
hostPtr, 0, true, buffer1Offset, 1);
727825

728826
csr.blitBuffer(blitProperties);
729827

0 commit comments

Comments
 (0)