Skip to content

Commit 65565ac

Browse files
Refactor Buffer::create
- simplify conditions by separation - reorder some calls - pass allocateMemory to AllocationFlags Change-Id: Iab6fd35f9b9c10bfbc19058e69058346ef87dad8
1 parent 330b9ed commit 65565ac

File tree

5 files changed

+50
-24
lines changed

5 files changed

+50
-24
lines changed

runtime/mem_obj/buffer.cpp

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -146,30 +146,29 @@ Buffer *Buffer::create(Context *context,
146146
zeroCopyAllowed = false;
147147
}
148148

149+
if (allocateMemory && context->isProvidingPerformanceHints()) {
150+
context->providePerformanceHint(CL_CONTEXT_DIAGNOSTICS_LEVEL_GOOD_INTEL, CL_BUFFER_NEEDS_ALLOCATE_MEMORY);
151+
}
152+
149153
if (!memory) {
150-
AllocationFlags allocFlags = MemObjHelper::getAllocationFlags(flags);
154+
AllocationFlags allocFlags = MemObjHelper::getAllocationFlags(flags, allocateMemory);
151155
DevicesBitfield devices = MemObjHelper::getDevicesBitfield(flags);
152-
allocFlags.flags.allocateMemory = allocateMemory;
153156
memory = memoryManager->allocateGraphicsMemoryInPreferredPool(allocFlags, devices, hostPtr, static_cast<size_t>(size), allocationType);
154157
}
155158

156-
if (allocateMemory) {
157-
if (memory) {
158-
memoryManager->addAllocationToHostPtrManager(memory);
159-
}
160-
if (context->isProvidingPerformanceHints()) {
161-
context->providePerformanceHint(CL_CONTEXT_DIAGNOSTICS_LEVEL_GOOD_INTEL, CL_BUFFER_NEEDS_ALLOCATE_MEMORY);
162-
}
163-
} else {
164-
if (!memory && Buffer::isReadOnlyMemoryPermittedByFlags(flags)) {
165-
allocationType = GraphicsAllocation::AllocationType::BUFFER_HOST_MEMORY;
166-
zeroCopyAllowed = false;
167-
copyMemoryFromHostPtr = true;
168-
AllocationFlags allocFlags = MemObjHelper::getAllocationFlags(flags);
169-
DevicesBitfield devices = MemObjHelper::getDevicesBitfield(flags);
170-
allocFlags.flags.allocateMemory = true;
171-
memory = memoryManager->allocateGraphicsMemoryInPreferredPool(allocFlags, devices, nullptr, static_cast<size_t>(size), allocationType);
172-
}
159+
if (allocateMemory && memory && MemoryPool::isSystemMemoryPool(memory->getMemoryPool())) {
160+
memoryManager->addAllocationToHostPtrManager(memory);
161+
}
162+
163+
// if memory pointer should not be allcoated and graphics allocation is nullptr
164+
// and cl_mem flags allow, create non-zerocopy buffer
165+
if (!allocateMemory && !memory && Buffer::isReadOnlyMemoryPermittedByFlags(flags)) {
166+
allocationType = GraphicsAllocation::AllocationType::BUFFER_HOST_MEMORY;
167+
zeroCopyAllowed = false;
168+
copyMemoryFromHostPtr = true;
169+
AllocationFlags allocFlags = MemObjHelper::getAllocationFlags(flags, true);
170+
DevicesBitfield devices = MemObjHelper::getDevicesBitfield(flags);
171+
memory = memoryManager->allocateGraphicsMemoryInPreferredPool(allocFlags, devices, nullptr, static_cast<size_t>(size), allocationType);
173172
}
174173

175174
if (!memory) {

runtime/mem_obj/mem_obj_helper.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ bool MemObjHelper::checkExtraMemFlagsForBuffer(cl_mem_flags flags) {
1313
return false;
1414
}
1515

16-
AllocationFlags MemObjHelper::getAllocationFlags(cl_mem_flags flags) {
17-
return AllocationFlags(); // Initialized by default constructor
16+
AllocationFlags MemObjHelper::getAllocationFlags(cl_mem_flags flags, bool allocateMemory) {
17+
return AllocationFlags(allocateMemory);
1818
}
1919

2020
DevicesBitfield MemObjHelper::getDevicesBitfield(cl_mem_flags flags) {

runtime/mem_obj/mem_obj_helper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class MemObjHelper {
2828

2929
static bool checkExtraMemFlagsForBuffer(cl_mem_flags flags);
3030

31-
static AllocationFlags getAllocationFlags(cl_mem_flags flags);
31+
static AllocationFlags getAllocationFlags(cl_mem_flags flags, bool allocateMemory);
3232

3333
static DevicesBitfield getDevicesBitfield(cl_mem_flags flags);
3434

runtime/mem_obj/pipe.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,8 @@ Pipe *Pipe::create(Context *context,
4848
DEBUG_BREAK_IF(!memoryManager);
4949

5050
while (true) {
51-
AllocationFlags allocFlags = MemObjHelper::getAllocationFlags(flags);
51+
AllocationFlags allocFlags = MemObjHelper::getAllocationFlags(flags, true);
5252
DevicesBitfield devices = MemObjHelper::getDevicesBitfield(flags);
53-
allocFlags.flags.allocateMemory = true;
5453
auto size = static_cast<size_t>(packetSize * (maxPackets + 1) + intelPipeHeaderReservedSpace);
5554
GraphicsAllocation *memory = memoryManager->allocateGraphicsMemoryInPreferredPool(allocFlags, devices, nullptr, size, GraphicsAllocation::AllocationType::PIPE);
5655
if (!memory) {

unit_tests/mem_obj/buffer_tests.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,34 @@ TEST(Buffer, givenNullptrPassedToBufferCreateWhenAllocationIsNotSystemMemoryPool
225225
EXPECT_FALSE(buffer->isMemObjZeroCopy());
226226
}
227227

228+
TEST(Buffer, givenNullptrPassedToBufferCreateWhenAllocationIsNotSystemMemoryPoolThenAllocationIsNotAddedToHostPtrManager) {
229+
std::unique_ptr<MockDevice> device(MockDevice::createWithNewExecutionEnvironment<MockDevice>(nullptr));
230+
::testing::NiceMock<GMockMemoryManagerFailFirstAllocation> *memoryManager = new ::testing::NiceMock<GMockMemoryManagerFailFirstAllocation>;
231+
232+
device->injectMemoryManager(memoryManager);
233+
MockContext ctx(device.get());
234+
235+
auto allocateNonSystemGraphicsAllocation = [memoryManager](AllocationFlags flags, DevicesBitfield devicesBitfield, const void *hostPtr, size_t size, GraphicsAllocation::AllocationType type) -> GraphicsAllocation * {
236+
auto allocation = memoryManager->allocateGraphicsMemory(size, MemoryConstants::pageSize, false, false);
237+
reinterpret_cast<MemoryAllocation *>(allocation)->overrideMemoryPool(MemoryPool::SystemCpuInaccessible);
238+
return allocation;
239+
};
240+
241+
EXPECT_CALL(*memoryManager, allocateGraphicsMemoryInPreferredPool(::testing::_, ::testing::_, ::testing::_, ::testing::_, ::testing::_))
242+
.WillOnce(::testing::Invoke(allocateNonSystemGraphicsAllocation));
243+
244+
cl_int retVal = 0;
245+
cl_mem_flags flags = CL_MEM_READ_WRITE;
246+
247+
auto hostPtrAllocationCountBefore = memoryManager->hostPtrManager.getFragmentCount();
248+
std::unique_ptr<Buffer> buffer(Buffer::create(&ctx, flags, MemoryConstants::pageSize, nullptr, retVal));
249+
250+
ASSERT_NE(nullptr, buffer.get());
251+
auto hostPtrAllocationCountAfter = memoryManager->hostPtrManager.getFragmentCount();
252+
253+
EXPECT_EQ(hostPtrAllocationCountBefore, hostPtrAllocationCountAfter);
254+
}
255+
228256
TEST(Buffer, givenNullptrPassedToBufferCreateWhenNoSharedContextOrRenderCompressedBuffersThenBuffersAllocationTypeIsBufferOrBufferHostMemory) {
229257
std::unique_ptr<MockDevice> device(MockDevice::createWithNewExecutionEnvironment<MockDevice>(nullptr));
230258
MockContext ctx(device.get());

0 commit comments

Comments
 (0)