@@ -7866,7 +7866,6 @@ static pi_result USMSharedAllocImpl(void **ResultPtr, pi_context Context,
7866
7866
pi_device Device,
7867
7867
pi_usm_mem_properties *Properties,
7868
7868
size_t Size, pi_uint32 Alignment) {
7869
- (void )Properties;
7870
7869
PI_ASSERT (Context, PI_ERROR_INVALID_CONTEXT);
7871
7870
PI_ASSERT (Device, PI_ERROR_INVALID_DEVICE);
7872
7871
@@ -7891,6 +7890,31 @@ static pi_result USMSharedAllocImpl(void **ResultPtr, pi_context Context,
7891
7890
reinterpret_cast <std::uintptr_t >(*ResultPtr) % Alignment == 0 ,
7892
7891
PI_ERROR_INVALID_VALUE);
7893
7892
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
+
7894
7918
return PI_SUCCESS;
7895
7919
}
7896
7920
@@ -7942,7 +7966,9 @@ pi_result USMSharedMemoryAlloc::allocateImpl(void **ResultPtr, size_t Size,
7942
7966
pi_result USMSharedReadOnlyMemoryAlloc::allocateImpl (void **ResultPtr,
7943
7967
size_t Size,
7944
7968
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,
7946
7972
Alignment);
7947
7973
}
7948
7974
0 commit comments