[llvm] [SYCL] Add sycl::device initial implementation (PR #176972)

Alexey Bader via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 23 09:00:44 PST 2026


================
@@ -0,0 +1,183 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// This file contains the declaration of the SYCL 2020 device class, which
+/// represents a single SYCL device on which kernels can be executed.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef _LIBSYCL___IMPL_DEVICE_HPP
+#define _LIBSYCL___IMPL_DEVICE_HPP
+
+#include <sycl/__impl/aspect.hpp>
+#include <sycl/__impl/backend.hpp>
+#include <sycl/__impl/device_selector.hpp>
+#include <sycl/__impl/info/device.hpp>
+
+#include <sycl/__impl/detail/config.hpp>
+#include <sycl/__impl/detail/obj_utils.hpp>
+
+_LIBSYCL_BEGIN_NAMESPACE_SYCL
+
+class platform;
+
+namespace detail {
+class DeviceImpl;
+} // namespace detail
+
+// SYCL 2020 4.6.4. Device class.
+class _LIBSYCL_EXPORT device {
+public:
+  device(const device &rhs) = default;
+
+  device(device &&rhs) = default;
+
+  device &operator=(const device &rhs) = default;
+
+  device &operator=(device &&rhs) = default;
+
+  friend bool operator==(const device &lhs, const device &rhs) {
+    return lhs.impl == rhs.impl;
+  }
+
+  friend bool operator!=(const device &lhs, const device &rhs) {
+    return !(lhs == rhs);
+  }
+
+  /// Constructs a SYCL device instance using the default device (device chosen
+  /// by default device selector).
+  device();
+
+  /// Constructs a SYCL device instance using the device
+  /// identified by the provided device selector.
+  /// \param DeviceSelector is SYCL 2020 Device Selector, a simple callable that
+  /// takes a device and returns an int.
+  template <
+      typename DeviceSelector,
+      // `DeviceImpl` (used as a parameter in private ctor) is incomplete
+      // so would result in a error trying to instantiate
+      // `EnableIfDeviceSelectorIsInvocable` below. Filter it out
+      // before trying to do that.
+      typename =
+          std::enable_if_t<!std::is_same_v<DeviceSelector, detail::DeviceImpl>>,
+      typename = detail::EnableIfDeviceSelectorIsInvocable<DeviceSelector>>
+  explicit device(const DeviceSelector &deviceSelector)
+      : device(detail::SelectDevice(deviceSelector)) {}
+
+  /// Returns the backend associated with this device.
+  ///
+  /// \return the backend associated with this device.
+  backend get_backend() const noexcept;
+
+  /// Check if device is a CPU device.
+  ///
+  /// \return true if SYCL device is a CPU device.
+  bool is_cpu() const;
+
+  /// Check if device is a GPU device.
+  ///
+  /// \return true if SYCL device is a GPU device.
+  bool is_gpu() const;
+
+  /// Check if device is an accelerator device.
+  ///
+  /// \return true if SYCL device is an accelerator device.
+  bool is_accelerator() const;
+
+  /// Get associated SYCL platform.
+  ///
+  /// \return The associated SYCL platform.
+  platform get_platform() const;
+
+  /// Queries this SYCL device for information requested by the template
+  /// parameter param.
+  ///
+  /// \return device info of type described in 4.6.4.4. Information descriptors.
----------------
bader wrote:

That would make it clearer.
Another option would be to reference the spec version and section number only (i.e. omit section name).

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


More information about the llvm-commits mailing list