[llvm] [libsycl] Add device image registration & compatibility check (PR #187528)

Alexey Bader via llvm-commits llvm-commits at lists.llvm.org
Sun Apr 5 08:00:02 PDT 2026


================
@@ -0,0 +1,154 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include <sycl/__impl/exception.hpp>
+
+#include <detail/device_impl.hpp>
+#include <detail/offload/offload_utils.hpp>
+#include <detail/program_manager.hpp>
+
+#include <cstring>
+
+_LIBSYCL_BEGIN_NAMESPACE_SYCL
+namespace detail {
+
+static inline bool checkFatBinVersion(const __sycl_tgt_bin_desc &FatbinDesc) {
+  return FatbinDesc.Version == _LIBSYCL_SUPPORTED_OFFLOAD_BINARY_VERSION;
+}
+
+static inline bool
+checkDeviceImageValidness(const __sycl_tgt_device_image &DeviceImage) {
+  return (DeviceImage.Version == _LIBSYCL_SUPPORTED_DEVICE_BINARY_VERSION) &&
+         (DeviceImage.OffloadKind == OFK_SYCL) &&
+         (DeviceImage.ImageFormat == IMG_SPIRV);
+}
+
+void ProgramManager::addImages(__sycl_tgt_bin_desc *FatbinDesc) {
+  assert(FatbinDesc && "Device images descriptor can't be nullptr");
+
+  if (!checkFatBinVersion(*FatbinDesc))
+    throw sycl::exception(sycl::make_error_code(sycl::errc::runtime),
+                          "Incompatible version of device images descriptor.");
+  if (!FatbinDesc->NumDeviceBinaries)
+    return;
+
+  std::lock_guard<std::mutex> Guard(MImageCollectionMutex);
+  for (int I = 0; I < FatbinDesc->NumDeviceBinaries; I++) {
+    const auto &RawDeviceImage = FatbinDesc->DeviceImages[I];
+    if (!checkDeviceImageValidness(RawDeviceImage))
+      throw sycl::exception(sycl::make_error_code(sycl::errc::runtime),
+                            "Incompatible device image.");
+
+    const EntryTy *EntriesB = RawDeviceImage.EntriesBegin;
+    const EntryTy *EntriesE = RawDeviceImage.EntriesEnd;
+    // Ignore "empty" device image.
+    if (EntriesB == EntriesE)
+      continue;
+
+    std::unique_ptr<DeviceImageWrapper> NewImageWrapper =
+        std::make_unique<DeviceImageWrapper>(RawDeviceImage);
+
+    for (auto EntriesIt = EntriesB; EntriesIt != EntriesE; EntriesIt++) {
----------------
bader wrote:

No. I mean [range-based for loop C++ feature](https://en.cppreference.com/w/cpp/language/range-for.html).

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


More information about the llvm-commits mailing list