[llvm] [SYCL] Add platform enumeration and info query using liboffload (PR #166927)

Alexey Sachkov via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 12 06:29:20 PST 2025


================
@@ -0,0 +1,81 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 helper functions for tranformation between implementation
+/// and SYCL's interface objects.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef _LIBSYCL___IMPL_DETAIL_OBJ_UTILS_HPP
+#define _LIBSYCL___IMPL_DETAIL_OBJ_UTILS_HPP
+
+#include <sycl/__impl/detail/config.hpp>
+
+#include <cassert>
+#include <memory>
+#include <optional>
+#include <type_traits>
+#include <utility>
+
+_LIBSYCL_BEGIN_NAMESPACE_SYCL
+
+namespace detail {
+
+// Note! This class relies on the fact that all SYCL interface
+// classes contain "impl" field that points to implementation object. "impl"
+// field should be accessible from this class.
+struct ImplUtils {
+  // Helper function for extracting implementation from SYCL's interface
+  // objects.
+  template <class Obj>
+  static const decltype(Obj::impl) &getSyclObjImpl(const Obj &SyclObj) {
+    assert(SyclObj.impl && "every constructor should create an impl");
+    return SyclObj.impl;
+  }
+
+  // Helper function for creation SYCL interface objects from implementations.
+  template <typename SyclObject, typename From>
+  static SyclObject createSyclObjFromImpl(From &&from) {
+    if constexpr (std::is_same_v<decltype(SyclObject::impl),
+                                 std::shared_ptr<std::decay_t<From>>>)
+      return SyclObject{from.shared_from_this()};
+    else
+      return SyclObject{std::forward<From>(from)};
+  }
+};
+
+template <class Obj>
+auto getSyclObjImpl(const Obj &SyclObj)
+    -> decltype(ImplUtils::getSyclObjImpl(SyclObj)) {
+  return ImplUtils::getSyclObjImpl(SyclObj);
+}
+
+template <typename SyclObject, typename From>
+SyclObject createSyclObjFromImpl(From &&from) {
+  return ImplUtils::createSyclObjFromImpl<SyclObject>(std::forward<From>(from));
+}
+
+// std::hash support (4.5.2. Common reference semantics)
+template <typename T> struct HashBase {
+  size_t operator()(const T &Obj) const {
+#ifdef __SYCL_DEVICE_ONLY__
+    (void)Obj;
+    return 0;
----------------
AlexeySachkov wrote:

> can we at least have it trap at run-time instead of returning a valid object that doesn't satisfy the function postconditions? E.g., `__builtin_unreachable()`?

This automatically assumes, that the underlying device compiler (and any formats for representing device code) support such semantics. That is not the case for SPIR-V, as far as I know, for example.

What is the reason for having the macro in the first place here? I can't imagine hash APIs being used from device code without violating some other restrictions or simply being a UB.

As such, triggering a compilation/link failure through an unresolved symbol wouldn't be a bad idea. We can just leave this function as a declaration-only for device code as well.



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


More information about the llvm-commits mailing list