[llvm] [SYCL] Add platform enumeration and info query using liboffload (PR #166927)
Kseniya Tikhomirova via llvm-commits
llvm-commits at lists.llvm.org
Wed Jan 14 10:19:01 PST 2026
================
@@ -0,0 +1,119 @@
+//===----------------------------------------------------------------------===//
+//
+// 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_OFFLOAD_TOPOLOGY
+#define _LIBSYCL_OFFLOAD_TOPOLOGY
+
+#include <sycl/__impl/detail/config.hpp>
+
+#include <OffloadAPI.h>
+
+#include <cassert>
+#include <unordered_map>
+#include <vector>
+
+_LIBSYCL_BEGIN_NAMESPACE_SYCL
+
+namespace detail {
+
+// Minimal span-like view.
+template <class T> struct range_view {
+ T *ptr{};
+ size_t len{};
+ T *begin() const { return ptr; }
+ T *end() const { return ptr + len; }
+ T &operator[](size_t i) const { return ptr[i]; }
+ size_t size() const { return len; }
+};
+
+using PlatformWithDevStorageType =
+ std::unordered_map<ol_platform_handle_t, std::vector<ol_device_handle_t>>;
+
+/// Contiguous global storage of platform handlers and device handles (grouped
+/// by platform) for a backend.
+struct OffloadTopology {
+ OffloadTopology() : MBackend(OL_PLATFORM_BACKEND_UNKNOWN) {}
+ OffloadTopology(ol_platform_backend_t OlBackend) : MBackend(OlBackend) {}
+
+ /// Updates backend for this topology.
+ ///
+ /// \param B new backend value.
+ void set_backend(ol_platform_backend_t B) { MBackend = B; }
+
+ /// Returns all platforms associated with this topology.
+ ///
+ /// \returns minimal span-like view to platforms associated with this
+ /// topology.
+ range_view<const ol_platform_handle_t> platforms() const {
+ return {MPlatforms.data(), MPlatforms.size()};
+ }
+
+ /// Returns all devices associated with specific platform.
+ ///
+ /// \param PlatformId platform_id is index into Platforms.
+ ///
+ /// \returns minimal span-like view to devices associated with specified
+ /// platform.
+ range_view<const ol_device_handle_t>
+ devicesForPlatform(size_t PlatformId) const {
+ if (PlatformId >= MDevRangePerPlatformId.size())
+ return {nullptr, 0};
----------------
KseniyaTikhomirova wrote:
same for me. this function can provide a correct error handling for this "incorrect" usage and it does it. with your description of wide contract I think it fits very well.
https://github.com/llvm/llvm-project/pull/166927
More information about the llvm-commits
mailing list