[llvm] [libsycl] Fix and test sycl::item conversion (PR #227692)
Kseniya Tikhomirova via llvm-commits
llvm-commits at lists.llvm.org
Wed Sep 30 05:56:13 PDT 2026
https://github.com/KseniyaTikhomirova created https://github.com/llvm/llvm-project/pull/227692
Assisted-by: Claude Code.
>From 4d309787822adacfe786e1a63f7d7d7c4254077a Mon Sep 17 00:00:00 2001
From: "Tikhomirova, Kseniya" <kseniya.tikhomirova at intel.com>
Date: Wed, 30 Sep 2026 05:53:45 -0700
Subject: [PATCH] [libsycl] Fix and test sycl::item conversion
Signed-off-by: Tikhomirova, Kseniya <kseniya.tikhomirova at intel.com>
---
.../sycl/__impl/index_space_classes.hpp | 10 +++--
libsycl/test/basic/index_space_classes.cpp | 37 +++++++++++++++++++
2 files changed, 44 insertions(+), 3 deletions(-)
diff --git a/libsycl/include/sycl/__impl/index_space_classes.hpp b/libsycl/include/sycl/__impl/index_space_classes.hpp
index 8f99d03d004ee1..6cd0a6930f88e7 100644
--- a/libsycl/include/sycl/__impl/index_space_classes.hpp
+++ b/libsycl/include/sycl/__impl/index_space_classes.hpp
@@ -499,9 +499,9 @@ template <int Dimensions /* = 1*/, bool WithOffset /* = true*/> class item {
/// WithOffset == false.
/// \return an item representing the same information as the object holds but
/// also includes the offset set to 0.
- template <bool HasOffset = WithOffset,
- std::enable_if_t<HasOffset == false, bool> = true>
- operator item<Dimensions, true>() const noexcept {
+ template <bool HasOffset = WithOffset>
+ operator std::enable_if_t<HasOffset == false, item<Dimensions, true>>()
+ const noexcept {
return item<Dimensions, true>(MRange, MId, id<Dimensions>{});
}
@@ -551,6 +551,10 @@ template <int Dimensions /* = 1*/, bool WithOffset /* = true*/> class item {
std::conditional_t<WithOffset, id<Dimensions>, std::monostate> MOffset;
friend class detail::Builder;
+
+ // The conversion to an item with an offset builds an item of another
+ // specialization from its protected constructor.
+ template <int, bool> friend class item;
};
_LIBSYCL_END_NAMESPACE_SYCL
diff --git a/libsycl/test/basic/index_space_classes.cpp b/libsycl/test/basic/index_space_classes.cpp
index 1afb70e634303c..008bf87cf302c8 100644
--- a/libsycl/test/basic/index_space_classes.cpp
+++ b/libsycl/test/basic/index_space_classes.cpp
@@ -277,8 +277,45 @@ void testId() {
assert(static_cast<short>(9) != ConstId);
}
+// Tests the deprecated conversion from an item without an offset to an item
+// with an offset, which must produce an all-zeros offset and keep the range,
+// the id and the linear id.
+template <int Dim> void testItemOffsetConversionForDim() {
+ const sycl::range<Dim> Extent = makeValue<sycl::range, Dim>(4, 8, 16);
+ const sycl::id<Dim> Index = makeValue<sycl::id, Dim>(2, 3, 5);
+
+ const sycl::item<Dim, false> NoOffset =
+ Builder::createItem<Dim, false>(Extent, Index);
+ const sycl::item<Dim, true> WithOffset = NoOffset;
+
+ assert(WithOffset.get_range() == Extent);
+ assert(WithOffset.get_id() == Index);
+ assert(WithOffset.get_offset() == sycl::id<Dim>{});
+ assert(WithOffset.get_linear_id() == NoOffset.get_linear_id());
+ assert((WithOffset ==
+ Builder::createItem<Dim, true>(Extent, Index, sycl::id<Dim>{})));
+}
+
+void testItem() {
+ testItemOffsetConversionForDim<1>();
+ testItemOffsetConversionForDim<2>();
+ testItemOffsetConversionForDim<3>();
+
+ // The conversion is only available on items without an offset.
+ static_assert(
+ (std::is_convertible_v<sycl::item<1, false>, sycl::item<1, true>>));
+ static_assert(
+ (!std::is_convertible_v<sycl::item<1, true>, sycl::item<1, false>>));
+
+ // An item with an offset reports the linear id relative to the offset.
+ const sycl::item<2, true> Offsetted =
+ Builder::createItem<2, true>({4, 8}, {3, 5}, {1, 1});
+ assert(Offsetted.get_linear_id() == 2 * 8 + 4);
+}
+
int main() {
testRange();
testId();
+ testItem();
return 0;
}
More information about the llvm-commits
mailing list