[llvm] [libsycl] Add constructors for context (PR #214538)

via llvm-commits llvm-commits at lists.llvm.org
Sun Aug 16 05:18:24 PDT 2026


https://github.com/Robertkq updated https://github.com/llvm/llvm-project/pull/214538

>From c558c873d717ad33c158b9bb48f16988bfd4980d Mon Sep 17 00:00:00 2001
From: Robertkq <robertvuia06 at gmail.com>
Date: Thu, 6 Aug 2026 20:17:40 +0300
Subject: [PATCH 1/2] Add public ctors for context

---
 libsycl/docs/index.md                   |  2 +-
 libsycl/include/sycl/__impl/context.hpp | 76 +++++++++++++++++++++++++
 libsycl/src/context.cpp                 | 55 ++++++++++++++++++
 libsycl/src/detail/context_impl.cpp     |  4 +-
 libsycl/src/detail/context_impl.hpp     |  1 +
 5 files changed, 134 insertions(+), 4 deletions(-)

diff --git a/libsycl/docs/index.md b/libsycl/docs/index.md
index de14261322039..6bfac45f0e886 100644
--- a/libsycl/docs/index.md
+++ b/libsycl/docs/index.md
@@ -102,7 +102,7 @@ which doesn't currently support Windows.
 
 - device selection: to add compatibility with old SYCL 1.2.1 device selectors, still part of SYCL 2020 specification
 
-- `context`: to implement get_info, properties & public constructors once context support is added to liboffload
+- `context`: to implement get_info, properties once context support is added to liboffload
 
 - `queue`:
 
diff --git a/libsycl/include/sycl/__impl/context.hpp b/libsycl/include/sycl/__impl/context.hpp
index 3acc91fad9647..9c7c72b85a06a 100644
--- a/libsycl/include/sycl/__impl/context.hpp
+++ b/libsycl/include/sycl/__impl/context.hpp
@@ -16,8 +16,10 @@
 #ifndef _LIBSYCL___IMPL_CONTEXT_HPP
 #define _LIBSYCL___IMPL_CONTEXT_HPP
 
+#include <sycl/__impl/async_handler.hpp>
 #include <sycl/__impl/backend.hpp>
 #include <sycl/__impl/info/desc_base.hpp>
+#include <sycl/__impl/property_list.hpp>
 
 #include <sycl/__impl/detail/config.hpp>
 #include <sycl/__impl/detail/obj_utils.hpp>
@@ -40,6 +42,80 @@ using is_context_info_desc_t = typename is_info_desc<T, context>::return_type;
 // SYCL 2020 4.6.3. Context class
 class _LIBSYCL_EXPORT context {
 public:
+  /// @brief Constructs a SYCL context instance using an instance of
+  /// default_selector.
+  ///
+  /// @param propList SYCL properties to be associated with the context.
+  explicit context(const property_list &propList = {});
+
+  /// @brief Constructs a SYCL context instance using an instance of
+  /// device_selector.
+  ///
+  /// @param asyncHandler Async handler to be used for asynchronous error
+  /// reporting.
+  /// @param propList SYCL properties to be associated with the context.
+  explicit context(async_handler asyncHandler,
+                   const property_list &propList = {});
+
+  /// @brief Constructs a SYCL context instance using the provided device.
+  /// The context will be associated with the platform of the provided device.
+  ///
+  /// @param dev is an instance of SYCL device
+  /// @param propList SYCL properties to be associated with the context.
+  explicit context(const device &dev, const property_list &propList = {});
+
+  /// @brief Constructs a SYCL context instance using the provided device.
+  /// The context will be associated with the platform of the provided device.
+  ///
+  /// @param dev is an instance of SYCL device
+  /// @param asyncHandler Async handler to be used for asynchronous error
+  /// reporting.
+  /// @param propList SYCL properties to be associated with the context.
+  explicit context(const device &dev, async_handler asyncHandler,
+                   const property_list &propList = {});
+
+  /// @brief Constructs a SYCL context instance using the provided platform.
+  /// The context will be associated with the provided platform and with each
+  /// SYCL device that is associated with the Platform.
+  ///
+  /// @param plt is an instance of SYCL platform
+  /// @param propList SYCL properties to be associated with the context.
+  explicit context(const platform &plt, const property_list &propList = {});
+
+  /// @brief Constructs a SYCL context instance using the provided platform.
+  /// The context will be associated with the provided platform and with each
+  /// SYCL device that is associated with the Platform.
+  ///
+  /// @param plt is an instance of SYCL platform
+  /// @param asyncHandler Async handler to be used for asynchronous error
+  /// reporting.
+  /// @param propList SYCL properties to be associated with the context.
+  explicit context(const platform &plt, async_handler asyncHandler,
+                   const property_list &propList = {});
+
+  /// @brief Constructs a SYCL context instance using the provided list of
+  /// devices. The context will be associated with each SYCL device in the
+  /// deviceList. This requires that all devices in the deviceList are
+  /// associated with the same platform.
+  ///
+  /// @param deviceList is a vector of SYCL devices
+  /// @param propList SYCL properties to be associated with the context.
+  explicit context(const std::vector<device> &deviceList,
+                   const property_list &propList = {});
+
+  /// @brief Constructs a SYCL context instance using the provided list of
+  /// devices. The context will be associated with each SYCL device in the
+  /// deviceList. This requires that all devices in the deviceList are
+  /// associated with the same platform.
+  ///
+  /// @param deviceList is a vector of SYCL devices
+  /// @param asyncHandler Async handler to be used for asynchronous error
+  /// reporting.
+  /// @param propList SYCL properties to be associated with the context.
+  explicit context(const std::vector<device> &deviceList,
+                   async_handler asyncHandler,
+                   const property_list &propList = {});
+
   context(const context &rhs) = default;
 
   context(context &&rhs) = default;
diff --git a/libsycl/src/context.cpp b/libsycl/src/context.cpp
index 3241d6fd47b67..b9c6d8a7b0da5 100644
--- a/libsycl/src/context.cpp
+++ b/libsycl/src/context.cpp
@@ -6,15 +6,70 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include <sycl/__impl/context.hpp>
 #include <sycl/__impl/device.hpp>
+#include <sycl/__impl/exception.hpp>
 
 #include <detail/context_impl.hpp>
 #include <detail/platform_impl.hpp>
 
+#include <algorithm>
 #include <cassert>
+#include <vector>
 
 _LIBSYCL_BEGIN_NAMESPACE_SYCL
 
+context::context(const property_list &propList) : context(device{}, propList) {}
+
+context::context(async_handler asyncHandler, const property_list &propList)
+    : context(device{}, asyncHandler, propList) {}
+
+context::context(const device &dev, const property_list &propList)
+    : context(std::vector<device>{dev}, propList) {}
+
+context::context(const device &dev, async_handler asyncHandler,
+                 const property_list &propList)
+    : context(std::vector<device>{dev}, asyncHandler, propList) {}
+
+context::context(const platform &plt, const property_list &propList)
+    : context(plt.get_devices(), propList) {}
+
+context::context(const platform &plt, async_handler asyncHandler,
+                 const property_list &propList)
+    : context(plt.get_devices(), asyncHandler, propList) {}
+
+context::context(const std::vector<device> &deviceList,
+                 const property_list &propList)
+    : context(deviceList, detail::defaultAsyncHandler, propList) {}
+
+context::context(const std::vector<device> &deviceList,
+                 async_handler asyncHandler, const property_list &propList)
+    : impl() {
+  if (deviceList.empty()) {
+    throw exception(make_error_code(errc::invalid),
+                    "Device list must not be empty");
+  }
+
+  const auto &platform = deviceList[0].get_platform();
+  if (std::any_of(deviceList.begin(), deviceList.end(),
+                  [&platform](const device &dev) {
+                    return dev.get_platform() != platform;
+                  })) {
+    throw exception(make_error_code(errc::invalid),
+                    "All devices must be associated with the same platform");
+  }
+
+  std::vector<detail::DeviceImpl *> deviceImpls;
+  deviceImpls.reserve(deviceList.size());
+  for (const device &dev : deviceList) {
+    deviceImpls.push_back(detail::ImplUtils::getSyclObjImpl(dev));
+  }
+
+  impl = detail::ContextImpl::create(std::move(deviceImpls), asyncHandler,
+                                     propList);
+  assert(impl && "ContextImpl creation failed");
+}
+
 backend context::get_backend() const noexcept { return impl->getBackend(); }
 
 platform context::get_platform() const {
diff --git a/libsycl/src/detail/context_impl.cpp b/libsycl/src/detail/context_impl.cpp
index 66a63131b49bb..9f05250cc283c 100644
--- a/libsycl/src/detail/context_impl.cpp
+++ b/libsycl/src/detail/context_impl.cpp
@@ -16,9 +16,7 @@ namespace detail {
 ContextImpl::ContextImpl(std::vector<DeviceImpl *> &&DeviceList,
                          const async_handler &AsyncHandler,
                          const property_list &PropList, Private)
-    : MAsyncHandler(AsyncHandler), MDevices(DeviceList) {
-  (void)PropList;
-
+    : MAsyncHandler(AsyncHandler), MDevices(DeviceList), MPropList(PropList) {
   assert(!MDevices.empty() && "Device list must not be empty");
 
   std::vector<ol_device_handle_t> DeviceIds;
diff --git a/libsycl/src/detail/context_impl.hpp b/libsycl/src/detail/context_impl.hpp
index ec9c8d10aca7e..99caed18e42d7 100644
--- a/libsycl/src/detail/context_impl.hpp
+++ b/libsycl/src/detail/context_impl.hpp
@@ -86,6 +86,7 @@ class ContextImpl : public std::enable_shared_from_this<ContextImpl> {
   const async_handler MAsyncHandler;
   const std::vector<DeviceImpl *> MDevices;
   ol_context_handle_t MOffloadContext{};
+  property_list MPropList;
 };
 
 } // namespace detail

>From 7ca1db61de2d8d3d5b958bd7975f8c3be4ada643 Mon Sep 17 00:00:00 2001
From: Robertkq <robertvuia06 at gmail.com>
Date: Sun, 16 Aug 2026 15:17:43 +0300
Subject: [PATCH 2/2] Add E2E & unit tests for context ctors

---
 libsycl/src/detail/context_impl.cpp         |  3 +-
 libsycl/test/basic/context.cpp              | 62 ++++++++++++++++++
 libsycl/unittests/CMakeLists.txt            |  1 +
 libsycl/unittests/context/CMakeLists.txt    |  3 +
 libsycl/unittests/context/context_ctors.cpp | 71 +++++++++++++++++++++
 5 files changed, 139 insertions(+), 1 deletion(-)
 create mode 100644 libsycl/test/basic/context.cpp
 create mode 100644 libsycl/unittests/context/CMakeLists.txt
 create mode 100644 libsycl/unittests/context/context_ctors.cpp

diff --git a/libsycl/src/detail/context_impl.cpp b/libsycl/src/detail/context_impl.cpp
index 9f05250cc283c..b0618d2a5ab0e 100644
--- a/libsycl/src/detail/context_impl.cpp
+++ b/libsycl/src/detail/context_impl.cpp
@@ -16,7 +16,8 @@ namespace detail {
 ContextImpl::ContextImpl(std::vector<DeviceImpl *> &&DeviceList,
                          const async_handler &AsyncHandler,
                          const property_list &PropList, Private)
-    : MAsyncHandler(AsyncHandler), MDevices(DeviceList), MPropList(PropList) {
+    : MAsyncHandler(AsyncHandler), MDevices(std::move(DeviceList)),
+      MPropList(PropList) {
   assert(!MDevices.empty() && "Device list must not be empty");
 
   std::vector<ol_device_handle_t> DeviceIds;
diff --git a/libsycl/test/basic/context.cpp b/libsycl/test/basic/context.cpp
new file mode 100644
index 0000000000000..cfb3ac471542e
--- /dev/null
+++ b/libsycl/test/basic/context.cpp
@@ -0,0 +1,62 @@
+// REQUIRES: any-device
+// RUN: %clangxx -fsycl %s -o %t.out
+// RUN: %t.out
+
+#include <iostream>
+
+#include <sycl/sycl.hpp>
+
+using namespace sycl;
+
+void return_fail() {
+  std::cout << "Failed" << std::endl;
+  exit(1);
+}
+
+void dummyAsyncHandler(sycl::exception_list) {}
+
+void check(const context &ctx) {
+  auto devices = ctx.get_devices();
+  if (devices.empty()) {
+    std::cout << "Context has no devices" << std::endl;
+    return_fail();
+  }
+
+  auto plt = ctx.get_platform();
+  for (const auto &dev : devices) {
+    if (dev.get_platform() != plt) {
+      std::cout << "Device platform does not match context platform"
+                << std::endl;
+      return_fail();
+    }
+  }
+  auto backend = ctx.get_backend();
+  for (const auto &dev : devices) {
+    if (dev.get_backend() != backend) {
+      std::cout << "Device backend does not match context backend" << std::endl;
+      return_fail();
+    }
+  }
+}
+
+int main() {
+  context ctx;
+  check(ctx);
+
+  device dev;
+  context ctx2(dev);
+  check(ctx2);
+
+  device dev2;
+
+  platform plt = dev.get_platform();
+  context ctx3(plt);
+  check(ctx3);
+
+  context ctx4({dev, dev2}, dummyAsyncHandler,
+               {/* explicit properties list */});
+  check(ctx4);
+
+  std::cout << "Passed" << std::endl;
+  return 0;
+}
diff --git a/libsycl/unittests/CMakeLists.txt b/libsycl/unittests/CMakeLists.txt
index 597651abd0bfc..ef6007359eb01 100644
--- a/libsycl/unittests/CMakeLists.txt
+++ b/libsycl/unittests/CMakeLists.txt
@@ -10,6 +10,7 @@ add_subdirectory(event)
 add_subdirectory(platform)
 add_subdirectory(program_manager)
 add_subdirectory(queue)
+add_subdirectory(context)
 
 # The unit test executables are GoogleTest binaries that are discovered and run
 # by lit, mirroring how liboffload runs its unit tests.
diff --git a/libsycl/unittests/context/CMakeLists.txt b/libsycl/unittests/context/CMakeLists.txt
new file mode 100644
index 0000000000000..9e599e3bcd589
--- /dev/null
+++ b/libsycl/unittests/context/CMakeLists.txt
@@ -0,0 +1,3 @@
+add_sycl_unittest(ContextTests
+    context_ctors.cpp
+)
diff --git a/libsycl/unittests/context/context_ctors.cpp b/libsycl/unittests/context/context_ctors.cpp
new file mode 100644
index 0000000000000..8cb97efcb9124
--- /dev/null
+++ b/libsycl/unittests/context/context_ctors.cpp
@@ -0,0 +1,71 @@
+#include <mock/helpers.hpp>
+
+#include <sycl/__impl/context.hpp>
+#include <sycl/__impl/device.hpp>
+#include <sycl/__impl/exception.hpp>
+#include <sycl/__impl/platform.hpp>
+
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+#include <utility>
+
+using namespace sycl;
+using namespace ::testing;
+
+void dummyAsyncHandler(exception_list) {}
+
+TEST(Context, DefaultConstructor) {
+  mock::MockWrapper Mock;
+
+  std::ignore = device{};
+
+  EXPECT_CALL(Mock.get(), olCreateContext(_, _, _)).Times(1);
+  EXPECT_CALL(Mock.get(), olDestroyContext(_)).Times(1);
+
+  context Ctx;
+}
+
+TEST(Context, DeviceConstructor) {
+  mock::MockWrapper Mock;
+  device Dev;
+
+  EXPECT_CALL(Mock.get(), olCreateContext(_, _, _)).Times(1);
+  EXPECT_CALL(Mock.get(), olDestroyContext(_)).Times(1);
+
+  context Ctx(Dev);
+}
+
+TEST(Context, DeviceConstructorWithAsyncHandler) {
+  mock::MockWrapper Mock;
+  device Dev;
+  async_handler AsyncHandler = dummyAsyncHandler;
+
+  EXPECT_CALL(Mock.get(), olCreateContext(_, _, _)).Times(1);
+  EXPECT_CALL(Mock.get(), olDestroyContext(_)).Times(1);
+
+  context Ctx(Dev, AsyncHandler);
+}
+
+TEST(Context, PlatformConstructor) {
+  mock::MockWrapper Mock;
+  device Dev;
+  platform Plt = Dev.get_platform();
+
+  EXPECT_CALL(Mock.get(), olCreateContext(_, _, _)).Times(1);
+  EXPECT_CALL(Mock.get(), olDestroyContext(_)).Times(1);
+
+  context Ctx(Plt);
+}
+
+TEST(Context, DeviceListConstructor) {
+  mock::MockWrapper Mock;
+  device Dev1;
+  device Dev2;
+  async_handler AsyncHandler = dummyAsyncHandler;
+
+  EXPECT_CALL(Mock.get(), olCreateContext(_, _, _)).Times(1);
+  EXPECT_CALL(Mock.get(), olDestroyContext(_)).Times(1);
+
+  context Ctx({Dev1, Dev2}, AsyncHandler);
+}
\ No newline at end of file



More information about the llvm-commits mailing list