[llvm] [libsycl] USM Aligned allocation functions (PR #213468)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Aug 24 07:17:51 PDT 2026
================
@@ -33,18 +47,40 @@ void *malloc_device(std::size_t numBytes, const queue &syclQueue,
// SYCL 2020 4.8.3.3. Host allocation functions.
-void *malloc_host(std::size_t numBytes, const context &syclContext,
- const property_list &propList) {
+static device getHostAllocDevice(const context &syclContext) {
auto ContextDevices = syclContext.get_devices();
- assert(!ContextDevices.empty() && "Context can't be created without device");
- if (std::none_of(
- ContextDevices.begin(), ContextDevices.end(),
- [](device Dev) { return Dev.has(aspect::usm_host_allocations); }))
+
+ auto It = std::find_if(
+ ContextDevices.begin(), ContextDevices.end(),
+ [](const device &Dev) { return Dev.has(aspect::usm_host_allocations); });
+
+ if (It == ContextDevices.end()) {
throw sycl::exception(
sycl::errc::feature_not_supported,
- "All devices of context do not support host USM allocations.");
- return malloc(numBytes, ContextDevices[0], syclContext, usm::alloc::host,
- propList);
+ "None of the context's devices support host USM allocations.");
+ }
+ return *It;
+}
+
+void *aligned_alloc_host(size_t alignment, size_t numBytes,
+ const context &syclContext,
+ const property_list &propList) {
+ auto device = getHostAllocDevice(syclContext);
+ return aligned_alloc(alignment, numBytes, device, syclContext,
+ usm::alloc::host, propList);
+}
+
+void *aligned_alloc_host(size_t alignment, size_t numBytes,
+ const queue &syclQueue,
+ const property_list &propList) {
+ return aligned_alloc_host(alignment, numBytes, syclQueue.get_context(),
+ propList);
+}
+
+void *malloc_host(std::size_t numBytes, const context &syclContext,
+ const property_list &propList) {
+ return aligned_alloc_host(alignof(std::max_align_t), numBytes, syclContext,
----------------
Robertkq wrote:
alignment guarantee from [sycl specs](https://registry.khronos.org/SYCL/specs/sycl-2020/html/sycl-2020.html#_usm_allocations). See row 1 of Table n. 70
https://github.com/llvm/llvm-project/pull/213468
More information about the llvm-commits
mailing list