Skip to content

Commit d7ce6ef

Browse files
Allocate images through preferred pool call
Change-Id: I79c9c1a0a95a8a3e26ed690530b71ef504cc7ff8
1 parent f90cfb9 commit d7ce6ef

22 files changed

+235
-102
lines changed

runtime/mem_obj/image.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,13 @@ Image *Image::create(Context *context,
211211
if (flags & CL_MEM_USE_HOST_PTR) {
212212

213213
if (!context->isSharedContext) {
214-
memory = memoryManager->allocateGraphicsMemoryForImage(imgInfo, hostPtr);
214+
MemoryProperties properties = {};
215+
properties.flags = flags;
216+
AllocationProperties allocProperties = MemObjHelper::getAllocationProperties(&imgInfo, false);
217+
DevicesBitfield devices = MemObjHelper::getDevicesBitfield(properties);
218+
219+
memory = memoryManager->allocateGraphicsMemoryInPreferredPool(allocProperties, devices, hostPtr);
220+
215221
if (memory) {
216222
if (memory->getUnderlyingBuffer() != hostPtr) {
217223
zeroCopy = false;
@@ -230,7 +236,7 @@ Image *Image::create(Context *context,
230236
} else {
231237
MemoryProperties properties = {};
232238
properties.flags = flags;
233-
AllocationProperties allocProperties = MemObjHelper::getAllocationProperties(&imgInfo);
239+
AllocationProperties allocProperties = MemObjHelper::getAllocationProperties(&imgInfo, true);
234240
DevicesBitfield devices = MemObjHelper::getDevicesBitfield(properties);
235241

236242
memory = memoryManager->allocateGraphicsMemoryInPreferredPool(allocProperties, devices, nullptr);

runtime/mem_obj/mem_obj_helper.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ AllocationProperties MemObjHelper::getAllocationProperties(cl_mem_flags_intel fl
3636
return allocationProperties;
3737
}
3838

39-
AllocationProperties MemObjHelper::getAllocationProperties(ImageInfo *imgInfo) {
40-
return AllocationProperties(imgInfo);
39+
AllocationProperties MemObjHelper::getAllocationProperties(ImageInfo *imgInfo, bool allocateMemory) {
40+
return AllocationProperties(imgInfo, allocateMemory);
4141
}
4242

4343
DevicesBitfield MemObjHelper::getDevicesBitfield(const MemoryProperties &properties) {

runtime/mem_obj/mem_obj_helper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class MemObjHelper {
4040
}
4141

4242
static AllocationProperties getAllocationProperties(cl_mem_flags_intel flags, bool allocateMemory, size_t size, GraphicsAllocation::AllocationType type);
43-
static AllocationProperties getAllocationProperties(ImageInfo *imgInfo);
43+
static AllocationProperties getAllocationProperties(ImageInfo *imgInfo, bool allocateMemory);
4444

4545
static DevicesBitfield getDevicesBitfield(const MemoryProperties &properties);
4646

runtime/memory_manager/memory_manager.cpp

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include "runtime/event/hw_timestamps.h"
1212
#include "runtime/event/perf_counter.h"
1313
#include "runtime/gmm_helper/gmm.h"
14+
#include "runtime/gmm_helper/gmm_helper.h"
15+
#include "runtime/gmm_helper/resource_info.h"
1416
#include "runtime/helpers/aligned_memory.h"
1517
#include "runtime/helpers/basic_math.h"
1618
#include "runtime/helpers/kernel_commands.h"
@@ -89,11 +91,11 @@ GraphicsAllocation *MemoryManager::allocateGraphicsMemoryWithHostPtr(const Alloc
8991
return graphicsAllocation;
9092
}
9193

92-
GraphicsAllocation *MemoryManager::allocateGraphicsMemoryForImageFromHostPtr(ImageInfo &imgInfo, const void *hostPtr) {
93-
bool copyRequired = Image::isCopyRequired(imgInfo, hostPtr);
94+
GraphicsAllocation *MemoryManager::allocateGraphicsMemoryForImageFromHostPtr(const AllocationData &allocationData) {
95+
bool copyRequired = Image::isCopyRequired(*allocationData.imgInfo, allocationData.hostPtr);
9496

95-
if (hostPtr && !copyRequired) {
96-
return allocateGraphicsMemory({false, imgInfo.size, GraphicsAllocation::AllocationType::UNDECIDED}, hostPtr);
97+
if (allocationData.hostPtr && !copyRequired) {
98+
return allocateGraphicsMemoryWithHostPtr(allocationData);
9799
}
98100
return nullptr;
99101
}
@@ -291,7 +293,6 @@ bool MemoryManager::getAllocationData(AllocationData &allocationData, const Allo
291293
GraphicsAllocation *MemoryManager::allocateGraphicsMemoryInPreferredPool(AllocationProperties properties, DevicesBitfield devicesBitfield, const void *hostPtr) {
292294
AllocationData allocationData;
293295
getAllocationData(allocationData, properties, devicesBitfield, hostPtr);
294-
UNRECOVERABLE_IF(allocationData.type == GraphicsAllocation::AllocationType::SHARED_RESOURCE);
295296

296297
AllocationStatus status = AllocationStatus::Error;
297298
GraphicsAllocation *allocation = allocateGraphicsMemoryInDevicePool(allocationData, status);
@@ -306,9 +307,9 @@ GraphicsAllocation *MemoryManager::allocateGraphicsMemoryInPreferredPool(Allocat
306307
}
307308

308309
GraphicsAllocation *MemoryManager::allocateGraphicsMemory(const AllocationData &allocationData) {
309-
if (allocationData.type == GraphicsAllocation::AllocationType::IMAGE) {
310+
if (allocationData.type == GraphicsAllocation::AllocationType::IMAGE || allocationData.type == GraphicsAllocation::AllocationType::SHARED_RESOURCE) {
310311
UNRECOVERABLE_IF(allocationData.imgInfo == nullptr);
311-
return allocateGraphicsMemoryForImage(*allocationData.imgInfo, allocationData.hostPtr);
312+
return allocateGraphicsMemoryForImage(allocationData);
312313
}
313314

314315
if (force32bitAllocations && allocationData.flags.allow32Bit && is64bit) {
@@ -323,6 +324,23 @@ GraphicsAllocation *MemoryManager::allocateGraphicsMemory(const AllocationData &
323324
return allocateGraphicsMemoryWithAlignment(allocationData);
324325
}
325326

327+
GraphicsAllocation *MemoryManager::allocateGraphicsMemoryForImage(const AllocationData &allocationData) {
328+
auto gmm = std::make_unique<Gmm>(*allocationData.imgInfo);
329+
330+
// AllocationData needs to be reconfigured for System Memory paths
331+
AllocationData allocationDataWithSize = allocationData;
332+
allocationDataWithSize.size = allocationData.imgInfo->size;
333+
334+
auto hostPtrAllocation = allocateGraphicsMemoryForImageFromHostPtr(allocationDataWithSize);
335+
336+
if (hostPtrAllocation) {
337+
hostPtrAllocation->gmm = gmm.release();
338+
return hostPtrAllocation;
339+
}
340+
341+
return allocateGraphicsMemoryForImageImpl(allocationDataWithSize, std::move(gmm));
342+
}
343+
326344
const CsrContainer &MemoryManager::getCommandStreamReceivers() const {
327345
return executionEnvironment.commandStreamReceivers;
328346
}

runtime/memory_manager/memory_manager.h

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ struct AllocationProperties {
6969
flags.flushL3RequiredForWrite = 1;
7070
flags.allocateMemory = allocateMemory;
7171
}
72-
AllocationProperties(ImageInfo *imgInfo) : AllocationProperties(true, 0, GraphicsAllocation::AllocationType::IMAGE) {
72+
AllocationProperties(ImageInfo *imgInfo, bool allocateMemory) : AllocationProperties(allocateMemory, 0, GraphicsAllocation::AllocationType::IMAGE) {
7373
this->imgInfo = imgInfo;
7474
}
7575
};
@@ -119,8 +119,6 @@ class MemoryManager {
119119

120120
virtual GraphicsAllocation *allocate32BitGraphicsMemory(size_t size, const void *ptr, AllocationOrigin allocationOrigin) = 0;
121121

122-
virtual GraphicsAllocation *allocateGraphicsMemoryForImage(ImageInfo &imgInfo, const void *hostPtr) = 0;
123-
124122
GraphicsAllocation *allocateGraphicsMemoryInPreferredPool(AllocationProperties properties, DevicesBitfield devicesBitfield, const void *hostPtr);
125123

126124
virtual GraphicsAllocation *createGraphicsAllocationFromSharedHandle(osHandle handle, bool requireSpecificBitness) = 0;
@@ -231,19 +229,27 @@ class MemoryManager {
231229
virtual GraphicsAllocation *allocateGraphicsMemory64kb(AllocationData allocationData) = 0;
232230
virtual GraphicsAllocation *allocateGraphicsMemoryInDevicePool(const AllocationData &allocationData, AllocationStatus &status) {
233231
status = AllocationStatus::Error;
234-
if (!allocationData.flags.useSystemMemory && !(allocationData.flags.allow32Bit && this->force32bitAllocations)) {
235-
auto allocation = allocateGraphicsMemory(allocationData);
236-
if (allocation) {
237-
allocation->devicesBitfield = allocationData.devicesBitfield;
238-
allocation->flushL3Required = allocationData.flags.flushL3;
239-
status = AllocationStatus::Success;
232+
switch (allocationData.type) {
233+
case GraphicsAllocation::AllocationType::IMAGE:
234+
case GraphicsAllocation::AllocationType::SHARED_RESOURCE:
235+
break;
236+
default:
237+
if (!allocationData.flags.useSystemMemory && !(allocationData.flags.allow32Bit && this->force32bitAllocations)) {
238+
auto allocation = allocateGraphicsMemory(allocationData);
239+
if (allocation) {
240+
allocation->devicesBitfield = allocationData.devicesBitfield;
241+
allocation->flushL3Required = allocationData.flags.flushL3;
242+
status = AllocationStatus::Success;
243+
}
244+
return allocation;
240245
}
241-
return allocation;
242246
}
243247
status = AllocationStatus::RetryInNonDevicePool;
244248
return nullptr;
245249
}
246-
GraphicsAllocation *allocateGraphicsMemoryForImageFromHostPtr(ImageInfo &imgInfo, const void *hostPtr);
250+
GraphicsAllocation *allocateGraphicsMemoryForImageFromHostPtr(const AllocationData &allocationData);
251+
MOCKABLE_VIRTUAL GraphicsAllocation *allocateGraphicsMemoryForImage(const AllocationData &allocationData);
252+
virtual GraphicsAllocation *allocateGraphicsMemoryForImageImpl(const AllocationData &allocationData, std::unique_ptr<Gmm> gmm) = 0;
247253

248254
virtual void *lockResourceImpl(GraphicsAllocation &graphicsAllocation) = 0;
249255
virtual void unlockResourceImpl(GraphicsAllocation &graphicsAllocation) = 0;

runtime/memory_manager/os_agnostic_memory_manager.cpp

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -217,23 +217,20 @@ void OsAgnosticMemoryManager::cleanOsHandles(OsHandleStorage &handleStorage) {
217217
}
218218
}
219219
}
220-
GraphicsAllocation *OsAgnosticMemoryManager::allocateGraphicsMemoryForImage(ImageInfo &imgInfo, const void *hostPtr) {
220+
GraphicsAllocation *OsAgnosticMemoryManager::allocateGraphicsMemoryForImageImpl(const AllocationData &allocationData, std::unique_ptr<Gmm> gmm) {
221221
GraphicsAllocation *alloc = nullptr;
222-
auto gmm = std::make_unique<Gmm>(imgInfo);
223-
224-
alloc = allocateGraphicsMemoryForImageFromHostPtr(imgInfo, hostPtr);
225-
226-
if (!alloc) {
227-
if ((!GmmHelper::allowTiling(*imgInfo.imgDesc) && imgInfo.mipCount == 0)) {
228-
alloc = allocateGraphicsMemoryWithProperties({imgInfo.size, GraphicsAllocation::AllocationType::UNDECIDED});
229-
} else {
230-
auto ptr = allocateSystemMemory(alignUp(imgInfo.size, MemoryConstants::pageSize), MemoryConstants::pageSize);
231-
if (ptr != nullptr) {
232-
alloc = new MemoryAllocation(ptr, ptr, reinterpret_cast<uint64_t>(ptr), imgInfo.size, counter,
233-
MemoryPool::SystemCpuInaccessible, this->getOsContextCount(), false);
234-
counter++;
235-
}
236-
}
222+
223+
if (!GmmHelper::allowTiling(*allocationData.imgInfo->imgDesc) && allocationData.imgInfo->mipCount == 0) {
224+
alloc = allocateGraphicsMemoryWithAlignment(allocationData);
225+
alloc->gmm = gmm.release();
226+
return alloc;
227+
}
228+
229+
auto ptr = allocateSystemMemory(alignUp(allocationData.imgInfo->size, MemoryConstants::pageSize), MemoryConstants::pageSize);
230+
if (ptr != nullptr) {
231+
alloc = new MemoryAllocation(ptr, ptr, reinterpret_cast<uint64_t>(ptr), allocationData.imgInfo->size, counter,
232+
MemoryPool::SystemCpuInaccessible, this->getOsContextCount(), false);
233+
counter++;
237234
}
238235

239236
if (alloc) {

runtime/memory_manager/os_agnostic_memory_manager.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ class OsAgnosticMemoryManager : public MemoryManager {
4848
GraphicsAllocation *allocate32BitGraphicsMemory(size_t size, const void *ptr, AllocationOrigin allocationOrigin) override;
4949
GraphicsAllocation *createGraphicsAllocationFromSharedHandle(osHandle handle, bool requireSpecificBitness) override;
5050
GraphicsAllocation *createGraphicsAllocationFromNTHandle(void *handle) override { return nullptr; }
51-
GraphicsAllocation *allocateGraphicsMemoryForImage(ImageInfo &imgInfo, const void *hostPtr) override;
5251
GraphicsAllocation *allocateGraphicsMemoryInDevicePool(const AllocationData &allocationData, AllocationStatus &status) override;
5352

5453
void addAllocationToHostPtrManager(GraphicsAllocation *gfxAllocation) override;
@@ -71,6 +70,7 @@ class OsAgnosticMemoryManager : public MemoryManager {
7170
protected:
7271
GraphicsAllocation *allocateGraphicsMemoryWithAlignment(const AllocationData &allocationData) override;
7372
GraphicsAllocation *allocateGraphicsMemory64kb(AllocationData allocationData) override;
73+
GraphicsAllocation *allocateGraphicsMemoryForImageImpl(const AllocationData &allocationData, std::unique_ptr<Gmm> gmm) override;
7474

7575
void *lockResourceImpl(GraphicsAllocation &graphicsAllocation) override { return ptrOffset(graphicsAllocation.getUnderlyingBuffer(), static_cast<size_t>(graphicsAllocation.allocationOffset)); };
7676
void unlockResourceImpl(GraphicsAllocation &graphicsAllocation) override{};

runtime/os_interface/linux/drm_memory_manager.cpp

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -284,30 +284,21 @@ DrmAllocation *DrmMemoryManager::allocateGraphicsMemory64kb(AllocationData alloc
284284
return nullptr;
285285
}
286286

287-
GraphicsAllocation *DrmMemoryManager::allocateGraphicsMemoryForImage(ImageInfo &imgInfo, const void *hostPtr) {
288-
auto gmm = std::make_unique<Gmm>(imgInfo);
289-
290-
auto hostPtrAllocation = allocateGraphicsMemoryForImageFromHostPtr(imgInfo, hostPtr);
291-
292-
if (hostPtrAllocation) {
293-
hostPtrAllocation->gmm = gmm.release();
294-
return hostPtrAllocation;
295-
}
296-
297-
if (!GmmHelper::allowTiling(*imgInfo.imgDesc)) {
298-
auto alloc = MemoryManager::allocateGraphicsMemoryWithProperties({imgInfo.size, GraphicsAllocation::AllocationType::UNDECIDED});
287+
GraphicsAllocation *DrmMemoryManager::allocateGraphicsMemoryForImageImpl(const AllocationData &allocationData, std::unique_ptr<Gmm> gmm) {
288+
if (!GmmHelper::allowTiling(*allocationData.imgInfo->imgDesc)) {
289+
auto alloc = allocateGraphicsMemoryWithAlignment(allocationData);
299290
if (alloc) {
300291
alloc->gmm = gmm.release();
301292
}
302293
return alloc;
303294
}
304295

305296
StorageAllocatorType allocatorType = UNKNOWN_ALLOCATOR;
306-
uint64_t gpuRange = acquireGpuRange(imgInfo.size, allocatorType, false);
297+
uint64_t gpuRange = acquireGpuRange(allocationData.imgInfo->size, allocatorType, false);
307298
DEBUG_BREAK_IF(gpuRange == reinterpret_cast<uint64_t>(MAP_FAILED));
308299

309300
drm_i915_gem_create create = {0, 0, 0};
310-
create.size = imgInfo.size;
301+
create.size = allocationData.imgInfo->size;
311302

312303
auto ret = this->drm->ioctl(DRM_IOCTL_I915_GEM_CREATE, &create);
313304
DEBUG_BREAK_IF(ret != 0);
@@ -317,17 +308,17 @@ GraphicsAllocation *DrmMemoryManager::allocateGraphicsMemoryForImage(ImageInfo &
317308
if (!bo) {
318309
return nullptr;
319310
}
320-
bo->size = imgInfo.size;
311+
bo->size = allocationData.imgInfo->size;
321312
bo->address = reinterpret_cast<void *>(gpuRange);
322313
bo->softPin(gpuRange);
323314

324-
auto ret2 = bo->setTiling(I915_TILING_Y, static_cast<uint32_t>(imgInfo.rowPitch));
315+
auto ret2 = bo->setTiling(I915_TILING_Y, static_cast<uint32_t>(allocationData.imgInfo->rowPitch));
325316
DEBUG_BREAK_IF(ret2 != true);
326317
((void)(ret2));
327318

328-
bo->setUnmapSize(imgInfo.size);
319+
bo->setUnmapSize(allocationData.imgInfo->size);
329320

330-
auto allocation = new DrmAllocation(bo, nullptr, (uint64_t)gpuRange, imgInfo.size, MemoryPool::SystemCpuInaccessible, getOsContextCount(), false);
321+
auto allocation = new DrmAllocation(bo, nullptr, (uint64_t)gpuRange, allocationData.imgInfo->size, MemoryPool::SystemCpuInaccessible, getOsContextCount(), false);
331322
bo->setAllocationType(allocatorType);
332323
allocation->gmm = gmm.release();
333324
return allocation;

runtime/os_interface/linux/drm_memory_manager.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ class DrmMemoryManager : public MemoryManager {
3232
void removeAllocationFromHostPtrManager(GraphicsAllocation *gfxAllocation) override;
3333
void freeGraphicsMemoryImpl(GraphicsAllocation *gfxAllocation) override;
3434
DrmAllocation *allocateGraphicsMemoryForNonSvmHostPtr(size_t size, void *cpuPtr) override;
35-
GraphicsAllocation *allocateGraphicsMemoryForImage(ImageInfo &imgInfo, const void *hostPtr) override;
3635
DrmAllocation *allocate32BitGraphicsMemory(size_t size, const void *ptr, AllocationOrigin allocationOrigin) override;
3736
GraphicsAllocation *createGraphicsAllocationFromSharedHandle(osHandle handle, bool requireSpecificBitness) override;
3837
GraphicsAllocation *createPaddedAllocation(GraphicsAllocation *inputGraphicsAllocation, size_t sizeWithPadding) override;
@@ -69,6 +68,7 @@ class DrmMemoryManager : public MemoryManager {
6968
DrmAllocation *allocateGraphicsMemoryWithAlignment(const AllocationData &allocationData) override;
7069
DrmAllocation *allocateGraphicsMemoryWithHostPtr(const AllocationData &allocationData) override;
7170
DrmAllocation *allocateGraphicsMemory64kb(AllocationData allocationData) override;
71+
GraphicsAllocation *allocateGraphicsMemoryForImageImpl(const AllocationData &allocationData, std::unique_ptr<Gmm> gmm) override;
7272

7373
void *lockResourceImpl(GraphicsAllocation &graphicsAllocation) override;
7474
void unlockResourceImpl(GraphicsAllocation &graphicsAllocation) override;

runtime/os_interface/windows/wddm_memory_manager.cpp

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,12 @@ WddmMemoryManager::WddmMemoryManager(bool enable64kbPages, bool enableLocalMemor
4040
mallocRestrictions.minAddress = wddm->getWddmMinAddress();
4141
}
4242

43-
GraphicsAllocation *WddmMemoryManager::allocateGraphicsMemoryForImage(ImageInfo &imgInfo, const void *hostPtr) {
44-
auto gmm = std::make_unique<Gmm>(imgInfo);
45-
46-
auto hostPtrAllocation = allocateGraphicsMemoryForImageFromHostPtr(imgInfo, hostPtr);
47-
if (hostPtrAllocation) {
48-
hostPtrAllocation->gmm = gmm.release();
49-
return hostPtrAllocation;
50-
}
51-
52-
if (!GmmHelper::allowTiling(*imgInfo.imgDesc) && imgInfo.mipCount == 0) {
53-
return allocateGraphicsMemoryWithProperties({imgInfo.size, GraphicsAllocation::AllocationType::UNDECIDED});
43+
GraphicsAllocation *WddmMemoryManager::allocateGraphicsMemoryForImageImpl(const AllocationData &allocationData, std::unique_ptr<Gmm> gmm) {
44+
if (!GmmHelper::allowTiling(*allocationData.imgInfo->imgDesc) && allocationData.imgInfo->mipCount == 0) {
45+
return allocateGraphicsMemoryWithAlignment(allocationData);
5446
}
5547

56-
auto allocation = std::make_unique<WddmAllocation>(nullptr, imgInfo.size, nullptr, MemoryPool::SystemCpuInaccessible, getOsContextCount(), false);
48+
auto allocation = std::make_unique<WddmAllocation>(nullptr, allocationData.imgInfo->size, nullptr, MemoryPool::SystemCpuInaccessible, getOsContextCount(), false);
5749
allocation->gmm = gmm.get();
5850

5951
if (!WddmMemoryManager::createWddmAllocation(allocation.get(), AllocationOrigin::EXTERNAL_ALLOCATION)) {

0 commit comments

Comments
 (0)