Skip to content

Commit 330b9ed

Browse files
Upload data to cpu inaccessible memory with enqueueWriteBuffer
Change-Id: Ibb33c4248fd0cb4338c82a9deb3994147c0acba5
1 parent e06aa17 commit 330b9ed

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

runtime/mem_obj/buffer.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ Buffer *Buffer::create(Context *context,
207207
pBuffer->setHostPtrMinSize(size);
208208

209209
if (copyMemoryFromHostPtr) {
210-
if (memory->gmm && memory->gmm->isRenderCompressed) {
210+
if ((memory->gmm && memory->gmm->isRenderCompressed) || !MemoryPool::isSystemMemoryPool(memory->getMemoryPool())) {
211211
auto cmdQ = context->getSpecialQueue();
212212
if (CL_SUCCESS != cmdQ->enqueueWriteBuffer(pBuffer, CL_TRUE, 0, size, hostPtr, 0, nullptr, nullptr)) {
213213
errcodeRet = CL_OUT_OF_RESOURCES;
@@ -390,7 +390,8 @@ bool Buffer::isReadWriteOnCpuAllowed(cl_bool blocking, cl_uint numEventsInWaitLi
390390
return (blocking == CL_TRUE && numEventsInWaitList == 0 && !forceDisallowCPUCopy) && graphicsAllocation->peekSharedHandle() == 0 &&
391391
(isMemObjZeroCopy() || (reinterpret_cast<uintptr_t>(ptr) & (MemoryConstants::cacheLineSize - 1)) != 0) &&
392392
(!context->getDevice(0)->getDeviceInfo().platformLP || (size <= maxBufferSizeForReadWriteOnCpu)) &&
393-
!(graphicsAllocation->gmm && graphicsAllocation->gmm->isRenderCompressed);
393+
!(graphicsAllocation->gmm && graphicsAllocation->gmm->isRenderCompressed) &&
394+
MemoryPool::isSystemMemoryPool(graphicsAllocation->getMemoryPool());
394395
}
395396

396397
Buffer *Buffer::createBufferHw(Context *context,

unit_tests/command_queue/read_write_buffer_cpu_copy.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,3 +292,21 @@ HWTEST_F(ReadWriteBufferCpuCopyTest, cpuCopyCriteriaNotMet) {
292292
alignedFree(alignedHostPtr);
293293
alignedFree(alignedBufferPtr);
294294
}
295+
296+
TEST(ReadWriteBufferOnCpu, givenNoHostPtrAndAlignedSizeWhenMemoryAllocationIsInNonSystemMemoryPoolThenIsReadWriteOnCpuAllowedReturnsFalse) {
297+
std::unique_ptr<MockDevice> device(MockDevice::createWithNewExecutionEnvironment<MockDevice>(nullptr));
298+
auto memoryManager = new MockMemoryManager;
299+
300+
device->injectMemoryManager(memoryManager);
301+
MockContext ctx(device.get());
302+
303+
cl_int retVal = 0;
304+
cl_mem_flags flags = CL_MEM_READ_WRITE;
305+
306+
std::unique_ptr<Buffer> buffer(Buffer::create(&ctx, flags, MemoryConstants::pageSize, nullptr, retVal));
307+
ASSERT_NE(nullptr, buffer.get());
308+
309+
EXPECT_TRUE(buffer->isReadWriteOnCpuAllowed(CL_TRUE, 0, reinterpret_cast<void *>(0x1000), MemoryConstants::pageSize));
310+
reinterpret_cast<MemoryAllocation *>(buffer->getGraphicsAllocation())->overrideMemoryPool(MemoryPool::SystemCpuInaccessible);
311+
EXPECT_FALSE(buffer->isReadWriteOnCpuAllowed(CL_TRUE, 0, reinterpret_cast<void *>(0x1000), MemoryConstants::pageSize));
312+
}

unit_tests/mem_obj/buffer_tests.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,32 @@ TEST(Buffer, givenZeroFlagsNoSharedContextAndRenderCompressedBuffersDisabledWhen
320320
EXPECT_EQ(GraphicsAllocation::AllocationType::BUFFER, type);
321321
}
322322

323+
TEST(Buffer, givenClMemCopyHostPointerPassedToBufferCreateWhenAllocationIsNotInSystemMemoryPoolThenAllocationIsWrittenByEnqueueWriteBuffer) {
324+
std::unique_ptr<MockDevice> device(MockDevice::createWithNewExecutionEnvironment<MockDevice>(nullptr));
325+
::testing::NiceMock<GMockMemoryManagerFailFirstAllocation> *memoryManager = new ::testing::NiceMock<GMockMemoryManagerFailFirstAllocation>;
326+
327+
device->injectMemoryManager(memoryManager);
328+
MockContext ctx(device.get());
329+
330+
auto allocateNonSystemGraphicsAllocation = [memoryManager](AllocationFlags flags, DevicesBitfield devicesBitfield, const void *hostPtr, size_t size, GraphicsAllocation::AllocationType type) -> GraphicsAllocation * {
331+
auto allocation = memoryManager->allocateGraphicsMemory(size, MemoryConstants::pageSize, false, false);
332+
reinterpret_cast<MemoryAllocation *>(allocation)->overrideMemoryPool(MemoryPool::SystemCpuInaccessible);
333+
return allocation;
334+
};
335+
336+
EXPECT_CALL(*memoryManager, allocateGraphicsMemoryInPreferredPool(::testing::_, ::testing::_, ::testing::_, ::testing::_, ::testing::_))
337+
.WillOnce(::testing::Invoke(allocateNonSystemGraphicsAllocation));
338+
339+
cl_int retVal = 0;
340+
cl_mem_flags flags = CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR;
341+
char memory[] = {1, 2, 3, 4, 5, 6, 7, 8};
342+
auto taskCount = device->getCommandStreamReceiver().peekLatestFlushedTaskCount();
343+
344+
std::unique_ptr<Buffer> buffer(Buffer::create(&ctx, flags, sizeof(memory), memory, retVal));
345+
ASSERT_NE(nullptr, buffer.get());
346+
auto taskCountSent = device->getCommandStreamReceiver().peekLatestFlushedTaskCount();
347+
EXPECT_LT(taskCount, taskCountSent);
348+
}
323349
struct RenderCompressedBuffersTests : public ::testing::Test {
324350
void SetUp() override {
325351
localHwInfo = *platformDevices[0];

0 commit comments

Comments
 (0)