Skip to content

Commit bf0f97e

Browse files
[SYCL][Level Zero] Use MemAdvise for read-only shared allocs (#7496)
Those don't affect stability: > Memory advice is a performance hint only and is not required for > functional correctness.
1 parent 7e0f76b commit bf0f97e

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

sycl/plugins/level_zero/pi_level_zero.cpp

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7866,7 +7866,6 @@ static pi_result USMSharedAllocImpl(void **ResultPtr, pi_context Context,
78667866
pi_device Device,
78677867
pi_usm_mem_properties *Properties,
78687868
size_t Size, pi_uint32 Alignment) {
7869-
(void)Properties;
78707869
PI_ASSERT(Context, PI_ERROR_INVALID_CONTEXT);
78717870
PI_ASSERT(Device, PI_ERROR_INVALID_DEVICE);
78727871

@@ -7891,6 +7890,31 @@ static pi_result USMSharedAllocImpl(void **ResultPtr, pi_context Context,
78917890
reinterpret_cast<std::uintptr_t>(*ResultPtr) % Alignment == 0,
78927891
PI_ERROR_INVALID_VALUE);
78937892

7893+
// See if the memory is going to be read-only on the device.
7894+
bool DeviceReadOnly = false;
7895+
// Check that incorrect bits are not set in the properties.
7896+
if (Properties && *Properties != 0) {
7897+
PI_ASSERT(*(Properties) == PI_MEM_ALLOC_FLAGS && *(Properties + 2) == 0,
7898+
PI_ERROR_INVALID_VALUE);
7899+
DeviceReadOnly = *(Properties + 1) & PI_MEM_ALLOC_DEVICE_READ_ONLY;
7900+
}
7901+
if (!DeviceReadOnly)
7902+
return PI_SUCCESS;
7903+
7904+
// For read-only memory, let L0 know about that by using advises.
7905+
7906+
// zeCommandListAppendMemAdvise must not be called from simultaneous threads
7907+
// with the same command list handle.
7908+
std::scoped_lock<pi_mutex> Lock(Context->ImmediateCommandListMutex);
7909+
7910+
ZE_CALL(zeCommandListAppendMemAdvise,
7911+
(Context->ZeCommandListInit, Device->ZeDevice, *ResultPtr, Size,
7912+
ZE_MEMORY_ADVICE_SET_READ_MOSTLY));
7913+
7914+
ZE_CALL(zeCommandListAppendMemAdvise,
7915+
(Context->ZeCommandListInit, Device->ZeDevice, *ResultPtr, Size,
7916+
ZE_MEMORY_ADVICE_SET_PREFERRED_LOCATION));
7917+
78947918
return PI_SUCCESS;
78957919
}
78967920

@@ -7942,7 +7966,9 @@ pi_result USMSharedMemoryAlloc::allocateImpl(void **ResultPtr, size_t Size,
79427966
pi_result USMSharedReadOnlyMemoryAlloc::allocateImpl(void **ResultPtr,
79437967
size_t Size,
79447968
pi_uint32 Alignment) {
7945-
return USMSharedAllocImpl(ResultPtr, Context, Device, nullptr, Size,
7969+
pi_usm_mem_properties Props[] = {PI_MEM_ALLOC_FLAGS,
7970+
PI_MEM_ALLOC_DEVICE_READ_ONLY, 0};
7971+
return USMSharedAllocImpl(ResultPtr, Context, Device, Props, Size,
79467972
Alignment);
79477973
}
79487974

0 commit comments

Comments
 (0)