Skip to content

Commit 6666762

Browse files
authored
[SYCL] Small patch on annotated USM allocation (#11801)
This PR is a small change on the implementation of `aligned_alloc_annotated<T>`, which is a variant of the SYCL annotated USM allocation: https://github.com/intel/llvm/blob/sycl/sycl/doc/extensions/proposed/sycl_ext_oneapi_usm_malloc_properties.asciidoc Right now `aligned_alloc_annotated<T>` is implemented by calling `sycl::aligned_alloc` (allocation in bytes) with a specified alignment (which is a combination (i.e. least common multiple) of the compile-time `alignment<K>` property and the runtime argument), and then cast the returned pointer type from `void *` to `T*` This does not cover the case where T has a extended alignment as follows ``` struct alignas(256) MyStruct { int x; }; ``` in this case, both the combined alignment and `alignof(T)` should be considered, and the greater value of the two should be the final alignment passed to the SYCL runtime. Therefore, this PR changes the impl by directly calling the SYCL core usm API: `aligned_alloc<T>`. Some e2e tests are updated with this header change: when `alignof(T)` is large enough, the `aligned_alloc_annotated<T>` is no longer expected to return null when the specified alignment is not a power of 2, because `alignof(T)` (always a power of 2) is the final alignment passed to the SYCL runtime. Therefore, we bump up the specified alignments in these cases to make sure they are always larger the `alignof(T)`
1 parent d65f3aa commit 6666762

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

sycl/include/sycl/ext/oneapi/experimental/annotated_usm/alloc_base.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,10 @@ aligned_alloc_annotated(size_t alignment, size_t count,
108108
throw sycl::exception(sycl::make_error_code(sycl::errc::invalid),
109109
"Unknown USM allocation kind was specified.");
110110

111-
void *rawPtr = sycl::aligned_alloc(
112-
combine_align(alignment, alignFromPropList), count * sizeof(T),
113-
syclDevice, syclContext, kind, usmPropList);
114-
return annotated_ptr<T, propertyListB>(static_cast<T *>(rawPtr));
111+
size_t combinedAlign = combine_align(alignment, alignFromPropList);
112+
T *rawPtr = sycl::aligned_alloc<T>(combinedAlign, count, syclDevice,
113+
syclContext, kind, usmPropList);
114+
return annotated_ptr<T, propertyListB>(rawPtr);
115115
}
116116

117117
template <typename propertyListA = detail::empty_properties_t,

sycl/test-e2e/Annotated_usm/annotated_usm_align.cpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -375,10 +375,10 @@ template <typename T> void testAlign(sycl::queue &q, unsigned align) {
375375
// 2),
376376
// nullptr is returned
377377
,
378-
[&]() { return TDevice(q, properties{alignment<5>}); },
379-
[&]() { return TDevice(dev, Ctx, properties{alignment<10>}); },
380-
[&]() { return THost(q, properties{alignment<25>}); },
381-
[&]() { return THost(Ctx, properties{alignment<50>}); },
378+
[&]() { return TDevice(q, properties{alignment<75>}); },
379+
[&]() { return TDevice(dev, Ctx, properties{alignment<100>}); },
380+
[&]() { return THost(q, properties{alignment<205>}); },
381+
[&]() { return THost(Ctx, properties{alignment<500>}); },
382382
[&]() {
383383
return TAnnotated(q, alloc::device, properties{alignment<127>});
384384
},
@@ -405,18 +405,23 @@ template <typename T> void testAlign(sycl::queue &q, unsigned align) {
405405
// alignment
406406
// argument is not a power of 2, the result is nullptr
407407
,
408-
[&]() { return ATDevice(3, q); },
409-
[&]() { return ATDevice(7, dev, Ctx); },
410-
[&]() { return ATHost(7, q); },
411-
[&]() { return ATHost(9, Ctx); },
412-
[&]() { return ATAnnotated(15, q, alloc::device); },
413-
[&]() { return ATAnnotated(17, dev, Ctx, alloc::host); }});
408+
[&]() { return ATDevice(65, q); },
409+
[&]() { return ATDevice(70, dev, Ctx); },
410+
[&]() { return ATHost(174, q); },
411+
[&]() { return ATHost(299, Ctx); },
412+
[&]() { return ATAnnotated(550, q, alloc::device); },
413+
[&]() { return ATAnnotated(1700, dev, Ctx, alloc::host); }});
414414
}
415415

416+
struct alignas(64) MyStruct {
417+
int x;
418+
};
419+
416420
int main() {
417421
sycl::queue q;
418422
testAlign<char>(q, 4);
419423
testAlign<int>(q, 128);
420424
testAlign<std::complex<double>>(q, 4);
425+
testAlign<MyStruct>(q, 4);
421426
return 0;
422427
}

0 commit comments

Comments
 (0)