[llvm] [libsycl] add USM alloc/free functions (PR #184111)
Kseniya Tikhomirova via llvm-commits
llvm-commits at lists.llvm.org
Thu Mar 12 07:10:44 PDT 2026
================
@@ -0,0 +1,300 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef _LIBSYCL___IMPL_USM_FUNCTIONS_HPP
+#define _LIBSYCL___IMPL_USM_FUNCTIONS_HPP
+
+#include <sycl/__impl/context.hpp>
+#include <sycl/__impl/queue.hpp>
+#include <sycl/__impl/usm_alloc_type.hpp>
+
+#include <sycl/__impl/detail/config.hpp>
+
+_LIBSYCL_BEGIN_NAMESPACE_SYCL
+
+// SYCL 2020 4.8.3.2. Device allocation functions.
+
+/// Allocates device USM.
+///
+/// \param numBytes allocation size that is specified in bytes.
+/// \param syclDevice device that is used for allocation.
+/// \param syclContext context that contains syclDevice or its parent device if
+/// syclDevice is a subdevice.
+/// \param propList properties for the memory allocation.
+/// \return a pointer to the newly allocated memory, which is allocated on
+/// syclDevice and which must eventually be deallocated with sycl::free in order
+/// to avoid a memory leak.
+void *_LIBSYCL_EXPORT malloc_device(std::size_t numBytes,
+ const device &syclDevice,
+ const context &syclContext,
+ const property_list &propList = {});
+
+/// Allocates device USM.
+///
+/// \param count allocation size that is specified in number of elements of
+/// type T.
+/// \param syclDevice device that is used for allocation.
+/// \param syclContext context that contains syclDevice or its parent device if
+/// syclDevice is a subdevice.
+/// \param propList properties for the memory allocation.
+/// \return a pointer to the newly allocated memory, which is allocated on
+/// syclDevice and which must eventually be deallocated with sycl::free in order
+/// to avoid a memory leak.
+template <typename T>
+T *malloc_device(std::size_t count, const device &syclDevice,
+ const context &syclContext,
+ const property_list &propList = {}) {
+ // TODO: to rewrite with aligned_malloc_device once it's supported in
+ // liboffload.
+ return static_cast<T *>(
+ malloc_device(count * sizeof(T), syclDevice, syclContext, propList));
+}
+
+/// Allocates device USM.
+///
+/// \param numBytes allocation size that is specified in bytes.
+/// \param syclQueue queue that provides the device and context.
+/// \param propList properties for the memory allocation.
+/// \return a pointer to the newly allocated memory, which is allocated on
+/// syclDevice and which must eventually be deallocated with sycl::free in order
+/// to avoid a memory leak.
+void *_LIBSYCL_EXPORT malloc_device(std::size_t numBytes,
+ const queue &syclQueue,
+ const property_list &propList = {});
+
+/// Allocates device USM.
+///
+/// \param count allocation size that is specified in number of elements of
+/// type T.
+/// \param syclQueue queue that provides the device and context.
+/// \param propList properties for the memory allocation.
+/// \return a pointer to the newly allocated memory, which is allocated on
+/// syclDevice and which must eventually be deallocated with sycl::free in order
+/// to avoid a memory leak.
+template <typename T>
+T *malloc_device(std::size_t count, const queue &syclQueue,
+ const property_list &propList = {}) {
+ return malloc_device<T>(count, syclQueue.get_device(),
+ syclQueue.get_context(), propList);
+}
+
+// SYCL 2020 4.8.3.3. Host allocation functions.
+
+/// Allocates host USM.
+///
+/// \param numBytes allocation size that is specified in bytes.
+/// \param syclContext context that should have access to the allocated memory.
+/// \param propList properties for the memory allocation.
+/// \return a pointer to the newly allocated memory, which must eventually be
+/// deallocated with sycl::free in order to avoid a memory leak.
+void *_LIBSYCL_EXPORT malloc_host(std::size_t numBytes,
+ const context &syclContext,
+ const property_list &propList = {});
+
+/// Allocates host USM.
+///
+/// \param count allocation size that is specified in number of elements of
+/// type T.
+/// \param syclContext context that should have access to the allocated memory.
+/// \param propList properties for the memory allocation.
+/// \return a pointer to the newly allocated memory, which must eventually be
+/// deallocated with sycl::free in order to avoid a memory leak.
+template <typename T>
+T *malloc_host(std::size_t count, const context &syclContext,
+ const property_list &propList = {}) {
+ // TODO: to rewrite with aligned_malloc_host once it's supported in
+ // liboffload.
+ return static_cast<T *>(
+ malloc_host(count * sizeof(T), syclContext, propList));
+}
+
+/// Allocates host USM.
+///
+/// \param numBytes allocation size that is specified in bytes.
----------------
KseniyaTikhomirova wrote:
https://github.com/llvm/llvm-project/pull/184111/changes/d07e37b3d1401fcc4c41dd25c359fd5521375724
https://github.com/llvm/llvm-project/pull/184111
More information about the llvm-commits
mailing list