Skip to content

Commit 182779d

Browse files
[SYCL] Fixes for usm_allocator properties (#19890)
* Fix `verifyUSMAllocatorProperties` to allow supported properties * No need to specialize `is_property<...>` for USM properties - `std::base_of_v<>`-based implementation covers that.
1 parent c8aecdf commit 182779d

File tree

4 files changed

+52
-13
lines changed

4 files changed

+52
-13
lines changed

sycl/include/sycl/ext/intel/experimental/usm_properties.hpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,10 @@ class buffer_location
3939
uint64_t MLocation;
4040
};
4141

42+
// If new properties are added here, update `verifyUSMAllocatorProperties` to
43+
// include them!
44+
4245
} // namespace intel::experimental::property::usm
4346
} // namespace ext
44-
45-
template <>
46-
struct is_property<ext::oneapi::property::usm::device_read_only>
47-
: std::true_type {};
48-
49-
template <>
50-
struct is_property<ext::intel::experimental::property::usm::buffer_location>
51-
: std::true_type {};
52-
5347
} // namespace _V1
5448
} // namespace sycl

sycl/source/detail/usm/usm_impl.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -659,9 +659,22 @@ void release_from_device_copy(const void *Ptr, const queue &Queue) {
659659
} // namespace ext::oneapi::experimental
660660

661661
__SYCL_EXPORT void verifyUSMAllocatorProperties(const property_list &PropList) {
662-
auto NoAllowedPropertiesCheck = [](int) { return false; };
663-
detail::PropertyValidator::checkPropsAndThrow(
664-
PropList, NoAllowedPropertiesCheck, NoAllowedPropertiesCheck);
662+
auto DataLessCheck = [](int Kind) {
663+
switch (Kind) {
664+
case detail::DeviceReadOnly:
665+
return true;
666+
}
667+
return false;
668+
};
669+
auto WithDataCheck = [](int Kind) {
670+
switch (Kind) {
671+
case detail::PropWithDataKind::AccPropBufferLocation:
672+
return true;
673+
}
674+
return false;
675+
};
676+
detail::PropertyValidator::checkPropsAndThrow(PropList, DataLessCheck,
677+
WithDataCheck);
665678
}
666679

667680
} // namespace _V1

sycl/test-e2e/Adapters/level_zero/usm_device_read_only.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
#include <sycl/ext/oneapi/experimental/annotated_usm/alloc_shared.hpp>
1010

11+
#include <sycl/usm/usm_allocator.hpp>
12+
1113
using namespace std;
1214
using namespace sycl;
1315

@@ -24,7 +26,22 @@ int main(int argc, char *argv[]) {
2426
// CHECK: ---> urUSMSharedAlloc
2527
// CHECK-NOT: zeMemAllocShared
2628

27-
free(ptr1, Q);
29+
sycl::usm_allocator<int, sycl::usm::alloc::shared> allocator_no_prop{Q};
30+
31+
auto ptr3 = allocator_no_prop.allocate(1);
32+
// CHECK: ---> urUSMSharedAlloc
33+
// CHECK: zeMemAllocShared
34+
35+
sycl::usm_allocator<int, sycl::usm::alloc::shared> allocator_prop{
36+
Q, {sycl::ext::oneapi::property::usm::device_read_only{}}};
37+
38+
auto ptr4 = allocator_prop.allocate(1);
39+
// CHECK: ---> urUSMSharedAlloc
40+
// CHECK-NOT: zeMemAllocShared
41+
42+
allocator_prop.deallocate(ptr4, 1);
43+
allocator_no_prop.deallocate(ptr3, 1);
2844
free(ptr2, Q);
45+
free(ptr1, Q);
2946
return 0;
3047
}

sycl/test-e2e/USM/properties.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// RUN: %{build} -o %t.out
2+
// RUN: %{run} %t.out
3+
4+
#include <sycl/ext/intel/experimental/usm_properties.hpp>
5+
#include <sycl/usm/usm_allocator.hpp>
6+
7+
int main() {
8+
sycl::queue q;
9+
10+
// Ensure properties are supported when constructing the allocator:
11+
sycl::usm_allocator<int, sycl::usm::alloc::shared> allocator{
12+
q,
13+
{sycl::ext::oneapi::property::usm::device_read_only{},
14+
sycl::ext::intel::experimental::property::usm::buffer_location{1}}};
15+
}

0 commit comments

Comments
 (0)