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

Kseniya Tikhomirova via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 27 07:00:23 PDT 2026


================
@@ -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));
----------------
KseniyaTikhomirova wrote:

we have a helper for this https://github.com/llvm/llvm-project/blob/main/libsycl/include/sycl/__impl/detail/obj_utils.hpp#L64

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


More information about the llvm-commits mailing list