[libcxx-commits] [libcxx] [libc++] Fix ambiguity in ranges::advance and ranges::next affecting flat_set (PR #177773)
Amgad Emara via libcxx-commits
libcxx-commits at lists.llvm.org
Sat Jan 24 07:16:39 PST 2026
https://github.com/TheAmgadX created https://github.com/llvm/llvm-project/pull/177773
This PR fixes an overload ambiguity in std::ranges::advance when the sentinel
type is the same as the iterator’s difference type.
After constraining the sentinel overload of ranges::advance, the same ambiguity
was exposed in std::ranges::next, which forwards to ranges::advance. This occurs
in std::flat_set initializer-list construction through the following call
sequence:
std::__inplace_merge
-> std::__upper_bound
-> _IterOps::next
The sentinel overload of ranges::next is therefore constrained as well to avoid
participating when the sentinel type matches iter_difference_t<I>.
Fixes #175091.
Tests added:
- flat_set initializer_list.compile.pass.cpp
>From f87259c249a48f7e8c899e6a91de321320c87e58 Mon Sep 17 00:00:00 2001
From: TheAmgadX <theamgadx at gmail.com>
Date: Wed, 21 Jan 2026 20:58:31 +0200
Subject: [PATCH 1/3] [libc++] Implement P2876R3: std::simd constructors and
accessors
This commit adds the missing constructors and accessors to std::simd as specified in P2876R3.
Changes include:
- Implementation of new constructors and functions in <experimental/simd>.
- Updated libcxx/docs/Status/Cxx2cPapers.csv status to mark P2876R3 as complete for LLVM 22.
- added tests in ./libcxx/test/std/experimental/simd/simd.mask.class
Partially addresses #148143
---
libcxx/docs/Status/Cxx2cPapers.csv | 2 +-
.../include/experimental/__simd/simd_mask.h | 40 +++++++++++
.../simd_mask_ctor_bitset.pass.cpp | 48 +++++++++++++
.../simd_mask_ctor_unsigned.pass.cpp | 51 ++++++++++++++
.../simd_mask_named_conversions.pass.cpp | 69 +++++++++++++++++++
5 files changed, 209 insertions(+), 1 deletion(-)
create mode 100644 libcxx/test/std/experimental/simd/simd.mask.class/simd_mask_ctor_bitset.pass.cpp
create mode 100644 libcxx/test/std/experimental/simd/simd.mask.class/simd_mask_ctor_unsigned.pass.cpp
create mode 100644 libcxx/test/std/experimental/simd/simd.mask.class/simd_mask_named_conversions.pass.cpp
diff --git a/libcxx/docs/Status/Cxx2cPapers.csv b/libcxx/docs/Status/Cxx2cPapers.csv
index 29642fc53cac6..76282328d5eeb 100644
--- a/libcxx/docs/Status/Cxx2cPapers.csv
+++ b/libcxx/docs/Status/Cxx2cPapers.csv
@@ -130,7 +130,7 @@
"`P3709R2 <https://wg21.link/P3709R2>`__","Reconsider parallel ``ranges::rotate_copy`` and ``ranges::reverse_copy``","2025-06 (Sofia)","","","`#148138 <https://github.com/llvm/llvm-project/issues/148138>`__",""
"`P3641R0 <https://wg21.link/P3641R0>`__","Rename ``std::observable`` to ``std::observable_checkpoint``, and add a feature-test macro","2025-06 (Sofia)","","","`#148139 <https://github.com/llvm/llvm-project/issues/148139>`__",""
"`P3044R2 <https://wg21.link/P3044R2>`__","sub-``string_view`` from ``string``","2025-06 (Sofia)","|Complete|","22","`#148140 <https://github.com/llvm/llvm-project/issues/148140>`__",""
-"`P2876R3 <https://wg21.link/P2876R3>`__","Proposal to extend ``std::simd`` with more constructors and accessors","2025-06 (Sofia)","","","`#148143 <https://github.com/llvm/llvm-project/issues/148143>`__",""
+"`P2876R3 <https://wg21.link/P2876R3>`__","Proposal to extend ``std::simd`` with more constructors and accessors","2025-06 (Sofia)","|Complete|","22","`#148143 <https://github.com/llvm/llvm-project/issues/148143>`__",""
"`P3480R6 <https://wg21.link/P3480R6>`__","``std::simd`` is a range","2025-06 (Sofia)","","","`#148144 <https://github.com/llvm/llvm-project/issues/148144>`__",""
"`P2664R11 <https://wg21.link/P2664R11>`__","Extend ``std::simd`` with permutation API","2025-06 (Sofia)","","","`#148145 <https://github.com/llvm/llvm-project/issues/148145>`__",""
"`P3691R1 <https://wg21.link/P3691R1>`__","Reconsider naming of the namespace for ``std::simd``","2025-06 (Sofia)","","","`#148148 <https://github.com/llvm/llvm-project/issues/148148>`__",""
diff --git a/libcxx/include/experimental/__simd/simd_mask.h b/libcxx/include/experimental/__simd/simd_mask.h
index a11766545b43d..337a1d92188a7 100644
--- a/libcxx/include/experimental/__simd/simd_mask.h
+++ b/libcxx/include/experimental/__simd/simd_mask.h
@@ -14,6 +14,8 @@
#include <__cstddef/size_t.h>
#include <__type_traits/enable_if.h>
#include <__type_traits/is_same.h>
+#include <bitset>
+#include <concepts>
#include <experimental/__simd/declaration.h>
#include <experimental/__simd/reference.h>
#include <experimental/__simd/traits.h>
@@ -65,6 +67,26 @@ class simd_mask {
_Impl::__load(__s_, _Flags::template __apply<simd_mask>(__mem));
}
+ // [simd.mask.ctor]
+ _LIBCPP_HIDE_FROM_ABI constexpr simd_mask(const std::bitset<size()>& __b) noexcept {
+ for (size_t __i = 0; __i < size(); ++__i) {
+ (*this)[__i] = __b[__i];
+ }
+ }
+
+ template <class _Up>
+ requires std::unsigned_integral<_Up>
+ _LIBCPP_HIDE_FROM_ABI constexpr explicit simd_mask(_Up __val) noexcept {
+ constexpr size_t __bit_limit = sizeof(_Up) * 8;
+ for (size_t __i = 0; __i < size(); ++__i) {
+ if (__i >= __bit_limit) {
+ (*this)[__i] = false;
+ } else {
+ (*this)[__i] = static_cast<bool>((__val >> __i) & 1);
+ }
+ }
+ }
+
// copy functions
template <class _Flags, enable_if_t<is_simd_flag_type_v<_Flags>, int> = 0>
_LIBCPP_HIDE_FROM_ABI void copy_from(const value_type* __mem, _Flags) {
@@ -76,6 +98,24 @@ class simd_mask {
_Impl::__store(__s_, _Flags::template __apply<simd_mask>(__mem));
}
+ // [simd.mask.namedconv]
+ _LIBCPP_HIDE_FROM_ABI constexpr std::bitset<size()> to_bitset() const noexcept {
+ std::bitset<size()> __b;
+ for (size_t __i = 0; __i < size(); ++__i) {
+ __b[__i] = (*this)[__i];
+ }
+ return __b;
+ }
+
+ _LIBCPP_HIDE_FROM_ABI constexpr unsigned long long to_ullong() const noexcept {
+ unsigned long long __val = 0;
+ constexpr size_t __limit = (size() < 64) ? size() : 64;
+ for (size_t __i = 0; __i < __limit; ++__i) {
+ __val |= (static_cast<unsigned long long>((*this)[__i]) << __i);
+ }
+ return __val;
+ }
+
// scalar access [simd.mask.subscr]
_LIBCPP_HIDE_FROM_ABI reference operator[](size_t __i) noexcept { return reference(__s_, __i); }
_LIBCPP_HIDE_FROM_ABI value_type operator[](size_t __i) const noexcept { return __s_.__get(__i); }
diff --git a/libcxx/test/std/experimental/simd/simd.mask.class/simd_mask_ctor_bitset.pass.cpp b/libcxx/test/std/experimental/simd/simd.mask.class/simd_mask_ctor_bitset.pass.cpp
new file mode 100644
index 0000000000000..5ce689436b470
--- /dev/null
+++ b/libcxx/test/std/experimental/simd/simd.mask.class/simd_mask_ctor_bitset.pass.cpp
@@ -0,0 +1,48 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14
+
+// <experimental/simd>
+//
+// [simd.mask.ctor]
+// constexpr simd_mask(const bitset<size()>& b) noexcept;
+
+#include "../test_utils.h"
+#include <bitset>
+
+namespace ex = std::experimental::parallelism_v2;
+
+template <class T, std::size_t>
+struct CheckSimdMaskCtorBitset {
+ template <class SimdAbi>
+ void operator()() {
+ constexpr std::size_t array_size = ex::simd_size_v<T, SimdAbi>;
+
+ std::bitset<array_size> b;
+ bool expected_buffer[array_size];
+
+ for (size_t i = 0; i < array_size; ++i) {
+ bool val = (i % 2 == 0);
+ if (val)
+ b[i] = true;
+ expected_buffer[i] = val;
+ }
+
+ // Construct mask
+ ex::simd_mask<T, SimdAbi> mask(b);
+
+ assert_simd_mask_values_equal(mask, expected_buffer);
+ static_assert(noexcept(ex::simd_mask<T, SimdAbi>(b)));
+ }
+};
+
+int main(int, char**) {
+ test_all_simd_abi<CheckSimdMaskCtorBitset>();
+ return 0;
+}
\ No newline at end of file
diff --git a/libcxx/test/std/experimental/simd/simd.mask.class/simd_mask_ctor_unsigned.pass.cpp b/libcxx/test/std/experimental/simd/simd.mask.class/simd_mask_ctor_unsigned.pass.cpp
new file mode 100644
index 0000000000000..6157e9f1181aa
--- /dev/null
+++ b/libcxx/test/std/experimental/simd/simd.mask.class/simd_mask_ctor_unsigned.pass.cpp
@@ -0,0 +1,51 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14
+
+// <experimental/simd>
+//
+// [simd.mask.ctor]
+// template<class U> constexpr explicit simd_mask(U val) noexcept;
+
+#include "../test_utils.h"
+
+namespace ex = std::experimental::parallelism_v2;
+
+template <class T, std::size_t>
+struct CheckSimdMaskCtorUnsigned {
+ template <class SimdAbi>
+ void operator()() {
+ constexpr std::size_t array_size = ex::simd_size_v<T, SimdAbi>;
+
+ // Pattern: 101010...
+ unsigned long long pattern = 0xAAAAAAAAAAAAAAAA;
+ bool expected_buffer[array_size];
+
+ constexpr size_t limit = (array_size < 64) ? array_size : 64;
+
+ for (size_t i = 0; i < limit; ++i) {
+ expected_buffer[i] = (pattern >> i) & 1;
+ }
+
+
+ for (size_t i = limit; i < array_size; ++i) {
+ expected_buffer[i] = false;
+ }
+
+ ex::simd_mask<T, SimdAbi> mask(pattern);
+
+ assert_simd_mask_values_equal(mask, expected_buffer);
+ static_assert(noexcept(ex::simd_mask<T, SimdAbi>(pattern)));
+ }
+};
+
+int main(int, char**) {
+ test_all_simd_abi<CheckSimdMaskCtorUnsigned>();
+ return 0;
+}
\ No newline at end of file
diff --git a/libcxx/test/std/experimental/simd/simd.mask.class/simd_mask_named_conversions.pass.cpp b/libcxx/test/std/experimental/simd/simd.mask.class/simd_mask_named_conversions.pass.cpp
new file mode 100644
index 0000000000000..321267a0a915a
--- /dev/null
+++ b/libcxx/test/std/experimental/simd/simd.mask.class/simd_mask_named_conversions.pass.cpp
@@ -0,0 +1,69 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14
+
+// <experimental/simd>
+//
+// [simd.mask.namedconv]
+// constexpr bitset<size()> to_bitset() const noexcept;
+// constexpr unsigned long long to_ullong() const;
+
+#include "../test_utils.h"
+#include <bitset>
+
+namespace ex = std::experimental::parallelism_v2;
+
+template <class T, std::size_t>
+struct CheckSimdMaskToBitset {
+ template <class SimdAbi>
+ void operator()() {
+ constexpr std::size_t array_size = ex::simd_size_v<T, SimdAbi>;
+
+ // Case: All True
+ ex::simd_mask<T, SimdAbi> mask(true);
+ std::bitset<array_size> b = mask.to_bitset();
+ assert(b.all());
+
+ // Case: All False
+ ex::simd_mask<T, SimdAbi> mask_false(false);
+ std::bitset<array_size> b_false = mask_false.to_bitset();
+ assert(b_false.none());
+
+ static_assert(noexcept(mask.to_bitset()));
+ }
+};
+
+template <class T, std::size_t>
+struct CheckSimdMaskToUllong {
+ template <class SimdAbi>
+ void operator()() {
+ constexpr std::size_t array_size = ex::simd_size_v<T, SimdAbi>;
+
+ // Case: All True
+ ex::simd_mask<T, SimdAbi> mask(true);
+ unsigned long long val = mask.to_ullong();
+
+ unsigned long long expected = ~0ULL;
+ if (array_size < 64) {
+ expected = (1ULL << array_size) - 1;
+ }
+
+ assert(val == expected);
+
+ // Case: All False
+ ex::simd_mask<T, SimdAbi> mask_false(false);
+ assert(mask_false.to_ullong() == 0ULL);
+ }
+};
+
+int main(int, char**) {
+ test_all_simd_abi<CheckSimdMaskToBitset>();
+ test_all_simd_abi<CheckSimdMaskToUllong>();
+ return 0;
+}
\ No newline at end of file
>From cb7817de6721ec969cba7a4caaa8c56d5b1c30e2 Mon Sep 17 00:00:00 2001
From: TheAmgadX <theamgadx at gmail.com>
Date: Wed, 21 Jan 2026 22:13:52 +0200
Subject: [PATCH 2/3] [libcxx] Optimize simd_mask bit-extraction constructor
Splits the logic into two separate loops: one for bit-extraction and
one for zero-padding. This removes a branch from the hot loop,
improving instruction predictability and helping the compiler
autovectorize the code.
I can provide a manual AVX-specialized implementation if required
---
libcxx/include/experimental/__simd/simd_mask.h | 18 +++++++++++-------
1 file changed, 11 insertions(+), 7 deletions(-)
diff --git a/libcxx/include/experimental/__simd/simd_mask.h b/libcxx/include/experimental/__simd/simd_mask.h
index 337a1d92188a7..f93bc8695262b 100644
--- a/libcxx/include/experimental/__simd/simd_mask.h
+++ b/libcxx/include/experimental/__simd/simd_mask.h
@@ -77,13 +77,17 @@ class simd_mask {
template <class _Up>
requires std::unsigned_integral<_Up>
_LIBCPP_HIDE_FROM_ABI constexpr explicit simd_mask(_Up __val) noexcept {
- constexpr size_t __bit_limit = sizeof(_Up) * 8;
- for (size_t __i = 0; __i < size(); ++__i) {
- if (__i >= __bit_limit) {
- (*this)[__i] = false;
- } else {
- (*this)[__i] = static_cast<bool>((__val >> __i) & 1);
- }
+ constexpr size_t __bits = sizeof(_Up) * 8;
+
+ constexpr size_t __bit_limit = __bits > size() ? size() : __bits;
+ size_t __i = 0;
+
+ for (; __i < __bit_limit; ++__i) {
+ (*this)[__i] = static_cast<bool>((__val >> __i) & 1);
+ }
+
+ for(; __i < size(); ++__i){
+ (*this)[__i] = false;
}
}
>From 1c4026a00e1aaafbf966685c4e0b2ade83ca9b63 Mon Sep 17 00:00:00 2001
From: TheAmgadX <theamgadx at gmail.com>
Date: Sat, 24 Jan 2026 17:07:55 +0200
Subject: [PATCH 3/3] [libc++] Fix ambiguity in ranges::advance and
ranges::next
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Fix an overload ambiguity in std::ranges::advance when the sentinel type is the
same as the iterator’s difference type.
After fixing ranges::advance, the same ambiguity was exposed in
std::ranges::next, which forwards to ranges::advance. The ambiguity is
triggered in std::flat_set initializer-list construction via the following
call chain:
std::__inplace_merge
-> std::__upper_bound
-> _IterOps::next
Both ranges::advance and ranges::next are now constrained to prevent the
sentinel overload from participating when the sentinel matches
iter_difference_t<I>.
Fixes #175091.
Tests:
- flat_set initializer_list.compile.pass.cpp
---
libcxx/include/__iterator/advance.h | 1 +
libcxx/include/__iterator/next.h | 1 +
.../initializer_list.compile.pass.cpp | 26 +++++++++++++++++++
3 files changed, 28 insertions(+)
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/initializer_list.compile.pass.cpp
diff --git a/libcxx/include/__iterator/advance.h b/libcxx/include/__iterator/advance.h
index c7d3c1f0e8f05..e83cde609f5b2 100644
--- a/libcxx/include/__iterator/advance.h
+++ b/libcxx/include/__iterator/advance.h
@@ -120,6 +120,7 @@ struct __advance {
// Preconditions: Either `assignable_from<I&, S> || sized_sentinel_for<S, I>` is modeled, or [i, bound_sentinel)
// denotes a range.
template <input_or_output_iterator _Ip, sentinel_for<_Ip> _Sp>
+ requires (!same_as<_Sp, iter_difference_t<_Ip>>)
_LIBCPP_HIDE_FROM_ABI constexpr void operator()(_Ip& __i, _Sp __bound_sentinel) const {
// If `I` and `S` model `assignable_from<I&, S>`, equivalent to `i = std::move(bound_sentinel)`.
if constexpr (assignable_from<_Ip&, _Sp>) {
diff --git a/libcxx/include/__iterator/next.h b/libcxx/include/__iterator/next.h
index 1143ab31ff7c2..3348f5b6fb8b4 100644
--- a/libcxx/include/__iterator/next.h
+++ b/libcxx/include/__iterator/next.h
@@ -49,6 +49,7 @@ struct __next {
}
template <input_or_output_iterator _Ip, sentinel_for<_Ip> _Sp>
+ requires (!same_as<_Sp, iter_difference_t<_Ip>>)
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Ip operator()(_Ip __x, _Sp __bound_sentinel) const {
ranges::advance(__x, __bound_sentinel);
return __x;
diff --git a/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/initializer_list.compile.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/initializer_list.compile.pass.cpp
new file mode 100644
index 0000000000000..194bd534d267c
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.set/flat.set.cons/initializer_list.compile.pass.cpp
@@ -0,0 +1,26 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17
+
+#include <flat_set>
+
+struct T {
+ T(const auto&);
+ friend bool operator==(T, T);
+};
+
+struct Comp {
+ bool operator()(T, T) const;
+};
+
+int main(int, char**) {
+ std::flat_set<T, Comp> x = {0};
+ (void)x;
+ return 0;
+}
More information about the libcxx-commits
mailing list