From f3783fb3161d3134d71b15e0644239ac6dc0781e Mon Sep 17 00:00:00 2001 From: Lorenc Bushi Date: Wed, 1 May 2024 11:58:21 -0700 Subject: [PATCH 1/5] Return invalid value on USM allocation functions when alignmnet value is not power of 2 --- source/adapters/level_zero/queue.cpp | 7 +++++-- source/adapters/level_zero/usm.cpp | 26 ++++++++------------------ 2 files changed, 13 insertions(+), 20 deletions(-) diff --git a/source/adapters/level_zero/queue.cpp b/source/adapters/level_zero/queue.cpp index 1030b491cf..51cceccbbc 100644 --- a/source/adapters/level_zero/queue.cpp +++ b/source/adapters/level_zero/queue.cpp @@ -902,8 +902,11 @@ UR_APIEXPORT ur_result_t UR_APICALL urQueueFinish( UR_APIEXPORT ur_result_t UR_APICALL urQueueFlush( ur_queue_handle_t Queue ///< [in] handle of the queue to be flushed. ) { - std::scoped_lock Lock(Queue->Mutex); - return Queue->executeAllOpenCommandLists(); + // Flushing cross-queue dependencies is covered by + // createAndRetainUrZeEventList, so this can be left as a no-op. return + // Queue->executeAllOpenCommandLists(); + std::ignore = Queue; + return UR_RESULT_SUCCESS; } // Configuration of the command-list batching. diff --git a/source/adapters/level_zero/usm.cpp b/source/adapters/level_zero/usm.cpp index 09e340dfe0..19490f83ac 100644 --- a/source/adapters/level_zero/usm.cpp +++ b/source/adapters/level_zero/usm.cpp @@ -308,7 +308,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urUSMHostAlloc( uint32_t Align = USMDesc ? USMDesc->align : 0; // L0 supports alignment up to 64KB and silently ignores higher values. // We flag alignment > 64KB as an invalid value. - if (Align > 65536) + if (Align > 65536 || Align & (Align - 1) != 0) return UR_RESULT_ERROR_INVALID_VALUE; ur_platform_handle_t Plt = Context->getPlatform(); @@ -337,11 +337,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urUSMHostAlloc( // find the allocator depending on context as we do for Shared and Device // allocations. umf_memory_pool_handle_t hPoolInternal = nullptr; - if (!UseUSMAllocator || - // L0 spec says that allocation fails if Alignment != 2^n, in order to - // keep the same behavior for the allocator, just call L0 API directly and - // return the error code. - ((Align & (Align - 1)) != 0)) { + if (!UseUSMAllocator) { hPoolInternal = Context->HostMemProxyPool.get(); } else if (Pool) { hPoolInternal = Pool->HostMemPool.get(); @@ -381,7 +377,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urUSMDeviceAlloc( // L0 supports alignment up to 64KB and silently ignores higher values. // We flag alignment > 64KB as an invalid value. - if (Alignment > 65536) + // L0 spec says that alignment values that are not powers of 2 are invalid. + if (Alignment > 65536 || Alignment & (Alignment - 1) != 0) return UR_RESULT_ERROR_INVALID_VALUE; ur_platform_handle_t Plt = Device->Platform; @@ -408,11 +405,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urUSMDeviceAlloc( } umf_memory_pool_handle_t hPoolInternal = nullptr; - if (!UseUSMAllocator || - // L0 spec says that allocation fails if Alignment != 2^n, in order to - // keep the same behavior for the allocator, just call L0 API directly and - // return the error code. - ((Alignment & (Alignment - 1)) != 0)) { + if (!UseUSMAllocator) { auto It = Context->DeviceMemProxyPools.find(Device->ZeDevice); if (It == Context->DeviceMemProxyPools.end()) return UR_RESULT_ERROR_INVALID_VALUE; @@ -485,7 +478,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urUSMSharedAlloc( // L0 supports alignment up to 64KB and silently ignores higher values. // We flag alignment > 64KB as an invalid value. - if (Alignment > 65536) + // L0 spec says that alignment values that are not powers of 2 are invalid. + if (Alignment > 65536 || Alignment && (Alignment - 1) != 0) return UR_RESULT_ERROR_INVALID_VALUE; ur_platform_handle_t Plt = Device->Platform; @@ -508,11 +502,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urUSMSharedAlloc( } umf_memory_pool_handle_t hPoolInternal = nullptr; - if (!UseUSMAllocator || - // L0 spec says that allocation fails if Alignment != 2^n, in order to - // keep the same behavior for the allocator, just call L0 API directly and - // return the error code. - ((Alignment & (Alignment - 1)) != 0)) { + if (!UseUSMAllocator) { auto &Allocator = (DeviceReadOnly ? Context->SharedReadOnlyMemProxyPools : Context->SharedMemProxyPools); auto It = Allocator.find(Device->ZeDevice); From 500e693fa2960c908656b18e9693f27b3568fb27 Mon Sep 17 00:00:00 2001 From: Lorenc Bushi <113361374+lbushi25@users.noreply.github.com> Date: Wed, 1 May 2024 15:01:23 -0400 Subject: [PATCH 2/5] Update queue.cpp --- source/adapters/level_zero/queue.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/source/adapters/level_zero/queue.cpp b/source/adapters/level_zero/queue.cpp index 51cceccbbc..65ab3892eb 100644 --- a/source/adapters/level_zero/queue.cpp +++ b/source/adapters/level_zero/queue.cpp @@ -903,8 +903,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urQueueFlush( ur_queue_handle_t Queue ///< [in] handle of the queue to be flushed. ) { // Flushing cross-queue dependencies is covered by - // createAndRetainUrZeEventList, so this can be left as a no-op. return - // Queue->executeAllOpenCommandLists(); + // createAndRetainUrZeEventList, so this can be left as a no-op. std::ignore = Queue; return UR_RESULT_SUCCESS; } From 6a94375a3a6cae6782560fabb82b12c3d37f2db8 Mon Sep 17 00:00:00 2001 From: Lorenc Bushi <113361374+lbushi25@users.noreply.github.com> Date: Wed, 1 May 2024 15:03:14 -0400 Subject: [PATCH 3/5] Update usm.cpp --- source/adapters/level_zero/usm.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/source/adapters/level_zero/usm.cpp b/source/adapters/level_zero/usm.cpp index 19490f83ac..1769f7e1ed 100644 --- a/source/adapters/level_zero/usm.cpp +++ b/source/adapters/level_zero/usm.cpp @@ -308,6 +308,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urUSMHostAlloc( uint32_t Align = USMDesc ? USMDesc->align : 0; // L0 supports alignment up to 64KB and silently ignores higher values. // We flag alignment > 64KB as an invalid value. + // L0 spec says that alignment values that are not powers of 2 are invalid. if (Align > 65536 || Align & (Align - 1) != 0) return UR_RESULT_ERROR_INVALID_VALUE; From e256f5499df38659584cf5eece5bd84b93d6b119 Mon Sep 17 00:00:00 2001 From: Lorenc Bushi <113361374+lbushi25@users.noreply.github.com> Date: Wed, 1 May 2024 16:24:48 -0400 Subject: [PATCH 4/5] Update usm.cpp --- source/adapters/level_zero/usm.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/adapters/level_zero/usm.cpp b/source/adapters/level_zero/usm.cpp index 1769f7e1ed..2952884caf 100644 --- a/source/adapters/level_zero/usm.cpp +++ b/source/adapters/level_zero/usm.cpp @@ -480,7 +480,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urUSMSharedAlloc( // L0 supports alignment up to 64KB and silently ignores higher values. // We flag alignment > 64KB as an invalid value. // L0 spec says that alignment values that are not powers of 2 are invalid. - if (Alignment > 65536 || Alignment && (Alignment - 1) != 0) + if (Alignment > 65536 || Alignment & (Alignment - 1) != 0) return UR_RESULT_ERROR_INVALID_VALUE; ur_platform_handle_t Plt = Device->Platform; From 47e59a876b157603c9d0fb3f09c4790e601434ff Mon Sep 17 00:00:00 2001 From: Lorenc Bushi <113361374+lbushi25@users.noreply.github.com> Date: Thu, 2 May 2024 12:24:45 -0400 Subject: [PATCH 5/5] Update usm.cpp --- source/adapters/level_zero/usm.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/source/adapters/level_zero/usm.cpp b/source/adapters/level_zero/usm.cpp index 2952884caf..c4cbfc9d26 100644 --- a/source/adapters/level_zero/usm.cpp +++ b/source/adapters/level_zero/usm.cpp @@ -309,7 +309,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urUSMHostAlloc( // L0 supports alignment up to 64KB and silently ignores higher values. // We flag alignment > 64KB as an invalid value. // L0 spec says that alignment values that are not powers of 2 are invalid. - if (Align > 65536 || Align & (Align - 1) != 0) + if (Align > 65536 || (Align & (Align - 1)) != 0) return UR_RESULT_ERROR_INVALID_VALUE; ur_platform_handle_t Plt = Context->getPlatform(); @@ -379,7 +379,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urUSMDeviceAlloc( // L0 supports alignment up to 64KB and silently ignores higher values. // We flag alignment > 64KB as an invalid value. // L0 spec says that alignment values that are not powers of 2 are invalid. - if (Alignment > 65536 || Alignment & (Alignment - 1) != 0) + if (Alignment > 65536 || (Alignment & (Alignment - 1)) != 0) return UR_RESULT_ERROR_INVALID_VALUE; ur_platform_handle_t Plt = Device->Platform; @@ -480,7 +480,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urUSMSharedAlloc( // L0 supports alignment up to 64KB and silently ignores higher values. // We flag alignment > 64KB as an invalid value. // L0 spec says that alignment values that are not powers of 2 are invalid. - if (Alignment > 65536 || Alignment & (Alignment - 1) != 0) + if (Alignment > 65536 || (Alignment & (Alignment - 1)) != 0) return UR_RESULT_ERROR_INVALID_VALUE; ur_platform_handle_t Plt = Device->Platform;