[libcxx-commits] [libcxx] [libc++] Don't assume alignment of empty ranges (PR #213752)
Louis Dionne via libcxx-commits
libcxx-commits at lists.llvm.org
Fri Aug 7 06:34:48 PDT 2026
================
@@ -60,10 +62,12 @@ __assume_valid_range([[__maybe_unused__]] _Iter&& __first, [[__maybe_unused__]]
_LIBCPP_ASSERT_INTERNAL(std::__is_valid_range(std::__to_address(__first), std::__to_address(__last)),
"Valid range assumption does not hold");
if (!__libcpp_is_constant_evaluated()) {
- using __value_type = typename iterator_traits<__remove_cvref_t<_Iter>>::value_type;
- __builtin_assume_dereferenceable(std::__to_address(__first), (__last - __first) * sizeof(__value_type));
- (void)std::__assume_aligned<_LIBCPP_ALIGNOF(__value_type)>(std::__to_address(__first));
- (void)std::__assume_aligned<_LIBCPP_ALIGNOF(__value_type)>(std::__to_address(__last));
+ if (__first != __last) {
----------------
ldionne wrote:
I will try re-writing `find_if` like this and removing the `if` inside the assumption function:
```
diff --git a/libcxx/include/__algorithm/find_if.h b/libcxx/include/__algorithm/find_if.h
index 2a96e13ce354..9aa1cdeeaf3d 100644
--- a/libcxx/include/__algorithm/find_if.h
+++ b/libcxx/include/__algorithm/find_if.h
@@ -24,11 +24,16 @@ _LIBCPP_BEGIN_NAMESPACE_STD
template <class _Iter, class _Sent, class _Pred, class _Proj>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 _Iter
__find_if(_Iter __first, _Sent __last, _Pred&& __pred, _Proj&& __proj) {
+ if (__first == __last)
+ return __first;
+
std::__assume_valid_range(__first, __last);
- for (; __first != __last; ++__first)
+ do {
if (std::__invoke(__pred, std::__invoke(__proj, *__first)))
break;
+ ++__first
+ } while (__first != __last);
return __first;
}
```
But before I do that, I'll remove the alignment assumption from `find_if` to see if we would even catch any regression of that kind in our existing benchmarks.
https://github.com/llvm/llvm-project/pull/213752
More information about the libcxx-commits
mailing list