Skip to content

Commit b32b578

Browse files
Fix calculations for offseted addresses
Signed-off-by: Zbigniew Zdanowicz <zbigniew.zdanowicz@intel.com>
1 parent 798256e commit b32b578

File tree

14 files changed

+346
-169
lines changed

14 files changed

+346
-169
lines changed

level_zero/core/source/cmdlist/cmdlist.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,13 @@ NEO::GraphicsAllocation *CommandList::getAllocationFromHostPtrMap(const void *bu
5555
return nullptr;
5656
}
5757

58-
NEO::GraphicsAllocation *CommandList::getHostPtrAlloc(const void *buffer, uint64_t bufferSize, size_t *offset) {
58+
NEO::GraphicsAllocation *CommandList::getHostPtrAlloc(const void *buffer, uint64_t bufferSize) {
5959
NEO::GraphicsAllocation *alloc = getAllocationFromHostPtrMap(buffer, bufferSize);
6060
if (alloc) {
61-
*offset += ptrDiff(buffer, alloc->getUnderlyingBuffer());
6261
return alloc;
6362
}
6463
alloc = device->allocateMemoryFromHostPtr(buffer, bufferSize);
64+
UNRECOVERABLE_IF(alloc == nullptr);
6565
hostPtrMap.insert(std::make_pair(buffer, alloc));
6666
return alloc;
6767
}

level_zero/core/source/cmdlist/cmdlist.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ struct CommandList : _ze_command_list_handle_t {
197197
bool indirectAllocationsAllowed = false;
198198
bool internalUsage = false;
199199
NEO::GraphicsAllocation *getAllocationFromHostPtrMap(const void *buffer, uint64_t bufferSize);
200-
NEO::GraphicsAllocation *getHostPtrAlloc(const void *buffer, uint64_t bufferSize, size_t *offset);
200+
NEO::GraphicsAllocation *getHostPtrAlloc(const void *buffer, uint64_t bufferSize);
201201
bool containsStatelessUncachedResource = false;
202202
};
203203

level_zero/core/source/cmdlist/cmdlist_hw.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ struct CommandListCoreFamily : CommandListImp {
205205
void programThreadArbitrationPolicy(Device *device);
206206

207207
uint64_t getInputBufferSize(NEO::ImageType imageType, uint64_t bytesPerPixel, const ze_image_region_t *region);
208-
virtual AlignedAllocationData getAlignedAllocation(Device *device, const void *buffer, uint64_t bufferSize);
208+
MOCKABLE_VIRTUAL AlignedAllocationData getAlignedAllocation(Device *device, const void *buffer, uint64_t bufferSize);
209209
ze_result_t addEventsToCmdList(ze_event_handle_t hEvent, uint32_t numWaitEvents, ze_event_handle_t *phWaitEvents);
210210
};
211211

level_zero/core/source/cmdlist/cmdlist_hw.inl

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,7 +1216,8 @@ ze_result_t CommandListCoreFamily<gfxCoreFamily>::appendBlitFill(void *ptr,
12161216
appendEventForProfiling(hEvent, true);
12171217
NEO::GraphicsAllocation *gpuAllocation = device->getDriverHandle()->getDriverSystemMemoryAllocation(ptr,
12181218
size,
1219-
neoDevice->getRootDeviceIndex());
1219+
neoDevice->getRootDeviceIndex(),
1220+
nullptr);
12201221
if (gpuAllocation == nullptr) {
12211222
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
12221223
}
@@ -1301,17 +1302,15 @@ inline AlignedAllocationData CommandListCoreFamily<gfxCoreFamily>::getAlignedAll
13011302

13021303
if (srcAllocFound == false) {
13031304
alloc = device->getDriverHandle()->findHostPointerAllocation(ptr, static_cast<size_t>(bufferSize), device->getRootDeviceIndex());
1304-
if (alloc != nullptr) {
1305-
offset += ptrDiff(buffer, alloc->getUnderlyingBuffer());
1306-
} else {
1307-
alloc = getHostPtrAlloc(buffer, bufferSize, &offset);
1305+
if (alloc == nullptr) {
1306+
alloc = getHostPtrAlloc(buffer, bufferSize);
13081307
}
13091308
alignedPtr = static_cast<uintptr_t>(alignDown(alloc->getGpuAddress(), NEO::EncodeSurfaceState<GfxFamily>::getSurfaceBaseAddressAlignment()));
13101309

13111310
hostPointerNeedsFlush = true;
13121311
} else {
13131312
alloc = allocData->gpuAllocations.getGraphicsAllocation(device->getRootDeviceIndex());
1314-
alignedPtr = reinterpret_cast<uintptr_t>(ptr) - offset;
1313+
alignedPtr = sourcePtr;
13151314

13161315
if (allocData->memoryType == InternalMemoryType::HOST_UNIFIED_MEMORY ||
13171316
allocData->memoryType == InternalMemoryType::SHARED_UNIFIED_MEMORY) {

level_zero/core/source/context/context_imp.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,11 @@ ze_result_t ContextImp::freeMem(const void *ptr) {
8888
ze_result_t ContextImp::makeMemoryResident(ze_device_handle_t hDevice, void *ptr, size_t size) {
8989
Device *device = L0::Device::fromHandle(hDevice);
9090
NEO::Device *neoDevice = device->getNEODevice();
91-
auto allocation = device->getDriverHandle()->getDriverSystemMemoryAllocation(ptr, size, neoDevice->getRootDeviceIndex());
91+
auto allocation = device->getDriverHandle()->getDriverSystemMemoryAllocation(
92+
ptr,
93+
size,
94+
neoDevice->getRootDeviceIndex(),
95+
nullptr);
9296
if (allocation == nullptr) {
9397
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
9498
}
@@ -112,7 +116,11 @@ ze_result_t ContextImp::makeMemoryResident(ze_device_handle_t hDevice, void *ptr
112116
ze_result_t ContextImp::evictMemory(ze_device_handle_t hDevice, void *ptr, size_t size) {
113117
Device *device = L0::Device::fromHandle(hDevice);
114118
NEO::Device *neoDevice = device->getNEODevice();
115-
auto allocation = device->getDriverHandle()->getDriverSystemMemoryAllocation(ptr, size, neoDevice->getRootDeviceIndex());
119+
auto allocation = device->getDriverHandle()->getDriverSystemMemoryAllocation(
120+
ptr,
121+
size,
122+
neoDevice->getRootDeviceIndex(),
123+
nullptr);
116124
if (allocation == nullptr) {
117125
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
118126
}

level_zero/core/source/driver/driver_handle.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ struct DriverHandle : _ze_driver_handle_t {
7575
virtual ze_result_t getHostPointerBaseAddress(void *ptr, void **baseAddress) = 0;
7676

7777
virtual NEO::GraphicsAllocation *findHostPointerAllocation(void *ptr, size_t size, uint32_t rootDeviceIndex) = 0;
78-
virtual NEO::GraphicsAllocation *getDriverSystemMemoryAllocation(void *ptr, size_t size, uint32_t rootDeviceIndex) = 0;
78+
virtual NEO::GraphicsAllocation *getDriverSystemMemoryAllocation(void *ptr,
79+
size_t size,
80+
uint32_t rootDeviceIndex,
81+
uintptr_t *gpuAddress) = 0;
7982

8083
static DriverHandle *fromHandle(ze_driver_handle_t handle) { return static_cast<DriverHandle *>(handle); }
8184
inline ze_driver_handle_t toHandle() { return this; }

level_zero/core/source/driver/driver_handle_imp.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,13 +453,27 @@ NEO::GraphicsAllocation *DriverHandleImp::findHostPointerAllocation(void *ptr, s
453453
return nullptr;
454454
}
455455

456-
NEO::GraphicsAllocation *DriverHandleImp::getDriverSystemMemoryAllocation(void *ptr, size_t size, uint32_t rootDeviceIndex) {
456+
NEO::GraphicsAllocation *DriverHandleImp::getDriverSystemMemoryAllocation(void *ptr,
457+
size_t size,
458+
uint32_t rootDeviceIndex,
459+
uintptr_t *gpuAddress) {
457460
NEO::SvmAllocationData *allocData = nullptr;
458461
bool allocFound = findAllocationDataForRange(ptr, size, &allocData);
459462
if (allocFound) {
463+
if (gpuAddress != nullptr) {
464+
*gpuAddress = reinterpret_cast<uintptr_t>(ptr);
465+
}
460466
return allocData->gpuAllocations.getGraphicsAllocation(rootDeviceIndex);
461467
}
462-
return findHostPointerAllocation(ptr, size, rootDeviceIndex);
468+
auto allocation = findHostPointerAllocation(ptr, size, rootDeviceIndex);
469+
if (allocation != nullptr) {
470+
if (gpuAddress != nullptr) {
471+
uintptr_t offset = reinterpret_cast<uintptr_t>(ptr) -
472+
reinterpret_cast<uintptr_t>(allocation->getUnderlyingBuffer());
473+
*gpuAddress = static_cast<uintptr_t>(allocation->getGpuAddress()) + offset;
474+
}
475+
}
476+
return allocation;
463477
}
464478

465479
} // namespace L0

level_zero/core/source/driver/driver_handle_imp.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,10 @@ struct DriverHandleImp : public DriverHandle {
7676
ze_result_t getHostPointerBaseAddress(void *ptr, void **baseAddress) override;
7777

7878
virtual NEO::GraphicsAllocation *findHostPointerAllocation(void *ptr, size_t size, uint32_t rootDeviceIndex) override;
79-
virtual NEO::GraphicsAllocation *getDriverSystemMemoryAllocation(void *ptr, size_t size, uint32_t rootDeviceIndex) override;
79+
virtual NEO::GraphicsAllocation *getDriverSystemMemoryAllocation(void *ptr,
80+
size_t size,
81+
uint32_t rootDeviceIndex,
82+
uintptr_t *gpuAddress) override;
8083
uint32_t parseAffinityMask(std::vector<std::unique_ptr<NEO::Device>> &neoDevices);
8184
void createHostPointerManager();
8285

level_zero/core/source/kernel/kernel_imp.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -524,11 +524,11 @@ ze_result_t KernelImp::setArgBuffer(uint32_t argIndex, size_t argSize, const voi
524524
uintptr_t gpuAddress = 0u;
525525
NEO::GraphicsAllocation *alloc = module->getDevice()->getDriverHandle()->getDriverSystemMemoryAllocation(requestedAddress,
526526
1u,
527-
module->getDevice()->getRootDeviceIndex());
527+
module->getDevice()->getRootDeviceIndex(),
528+
&gpuAddress);
528529
if (nullptr == alloc) {
529530
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
530531
}
531-
gpuAddress = static_cast<uintptr_t>(alloc->getGpuAddress());
532532
return setArgBufferWithAlloc(argIndex, gpuAddress, alloc);
533533
}
534534

level_zero/core/test/unit_tests/mocks/mock_driver_handle.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,15 @@ struct Mock<DriverHandle> : public DriverHandleImp {
7373
NEO::GraphicsAllocation *findHostPointerAllocation(void *ptr, size_t size, uint32_t rootDeviceIndex) override {
7474
return nullptr;
7575
}
76-
NEO::GraphicsAllocation *getDriverSystemMemoryAllocation(void *ptr, size_t size, uint32_t rootDeviceIndex) override {
76+
NEO::GraphicsAllocation *getDriverSystemMemoryAllocation(void *ptr,
77+
size_t size,
78+
uint32_t rootDeviceIndex,
79+
uintptr_t *gpuAddress) override {
7780
auto svmData = svmAllocsManager->getSVMAlloc(ptr);
7881
if (svmData != nullptr) {
82+
if (gpuAddress != nullptr) {
83+
*gpuAddress = reinterpret_cast<uintptr_t>(ptr);
84+
}
7985
return svmData->gpuAllocations.getGraphicsAllocation(rootDeviceIndex);
8086
}
8187
return nullptr;

0 commit comments

Comments
 (0)