Repository navigation
[libc++] Implement any_of in terms of find_if - #207274
Conversation
This way, any optimizations in find_if will be picked up by any_of. Closes llvm#129310
|
@llvm/pr-subscribers-libcxx Author: Louis Dionne (ldionne) ChangesThis way, any optimizations in find_if will be picked up by any_of. Closes #129310 1 Files Affected:
diff --git a/libcxx/include/__algorithm/any_of.h b/libcxx/include/__algorithm/any_of.h
index 4b6eb94517286..94a0917899d63 100644
--- a/libcxx/include/__algorithm/any_of.h
+++ b/libcxx/include/__algorithm/any_of.h
@@ -10,9 +10,10 @@
#ifndef _LIBCPP___ALGORITHM_ANY_OF_H
#define _LIBCPP___ALGORITHM_ANY_OF_H
+#include <__algorithm/find_if.h>
#include <__config>
#include <__functional/identity.h>
-#include <__type_traits/invoke.h>
+#include <__utility/forward.h>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
@@ -23,18 +24,15 @@ _LIBCPP_BEGIN_NAMESPACE_STD
template <class _Iter, class _Sent, class _Proj, class _Pred>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
__any_of(_Iter __first, _Sent __last, _Pred& __pred, _Proj& __proj) {
- for (; __first != __last; ++__first) {
- if (std::__invoke(__pred, std::__invoke(__proj, *__first)))
- return true;
- }
- return false;
+ auto __found = std::__find_if(std::move(__first), __last, std::forward<_Pred>(__pred), std::forward<_Proj>(__proj));
+ return __found != __last;
}
template <class _InputIterator, class _Predicate>
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool
any_of(_InputIterator __first, _InputIterator __last, _Predicate __pred) {
__identity __proj;
- return std::__any_of(__first, __last, __pred, __proj);
+ return std::__any_of(std::move(__first), std::move(__last), __pred, __proj);
}
_LIBCPP_END_NAMESPACE_STD
|
|
/libcxx-bot benchmark libcxx/test/benchmarks/algorithms/nonmodifying/any_all_none_of.bench.cpp
Benchmark results for Linux x86_64:Benchmark results for macOS 26.5 arm64: |
|
/libcxx-bot benchmark libcxx/test/benchmarks/algorithms/nonmodifying/any_all_none_of.bench.cpp
Benchmark results for Linux x86_64:Benchmark results for macOS 26.5 arm64: |
|
/libcxx-bot benchmark libcxx/test/benchmarks/algorithms/nonmodifying/any_all_none_of.bench.cpp
Benchmark results for macOS 26.5 arm64:Benchmark results for Linux x86_64: |
|
/libcxx-bot benchmark libcxx/test/benchmarks/algorithms/nonmodifying/any_all_none_of.bench.cpp
Benchmark results for macOS 26.5 arm64:Benchmark results for Linux x86_64: |
|
/libcxx-bot benchmark libcxx/test/benchmarks/algorithms/nonmodifying/any_all_none_of.bench.cpp
Benchmark results for Linux x86_64:Benchmark results for macOS 26.5 arm64: |
|
/libcxx-bot benchmark libcxx/test/benchmarks/algorithms/nonmodifying/any_all_none_of.bench.cpp
Benchmark results for Linux x86_64:Benchmark results for macOS 26.5 arm64: |
philnik777
left a comment
There was a problem hiding this comment.
Looks like the compiler is finally able to vectorize most of our loops. Yay!
|
This patch triggers https://lab.llvm.org/buildbot/#/builders/55/builds/30810/steps/11/logs/stdio This is not specific to a particular sanitizer, or platform. Probably just FYI. |
…into single type" (#211708) Reverts llvm/llvm-project#211701 Relands llvm/llvm-project#209737 The buildbot failure was misattributed to this change. Real cause is llvm/llvm-project#207274
…into single type" (#211708) Reverts llvm/llvm-project#211701 Relands llvm/llvm-project#209737 The buildbot failure was misattributed to this change. Real cause is llvm/llvm-project#207274
After #207274, libc++ fails __assume_aligned assertion in __assume_valid_range. https://lab.llvm.org/buildbot/#/builders/25/builds/18994 `DenseArrayAttrImpl<T>::operator ArrayRef<T>()` is just a cast of `raw.data()`. `raw` is aligned copy of range from from BytecodeReader done by Builtin_DenseArrayRawDataParameter allocator. However, if range is empty, aligned copying was omitted, leaving unaligned `ArrayRef<char>`. The fix is to replace unaligned `ArrayRef<>` with aligned default constructed.
|
FYI @ldionne this was failed check on unaligned empty range. |
This way, any optimizations in find_if will be picked up by any_of. Closes llvm#129310
… type" (llvm#211708) Reverts llvm#211701 Relands llvm#209737 The buildbot failure was misattributed to this change. Real cause is llvm#207274
After llvm#207274, libc++ fails __assume_aligned assertion in __assume_valid_range. https://lab.llvm.org/buildbot/#/builders/25/builds/18994 `DenseArrayAttrImpl<T>::operator ArrayRef<T>()` is just a cast of `raw.data()`. `raw` is aligned copy of range from from BytecodeReader done by Builtin_DenseArrayRawDataParameter allocator. However, if range is empty, aligned copying was omitted, leaving unaligned `ArrayRef<char>`. The fix is to replace unaligned `ArrayRef<>` with aligned default constructed.
This way, any optimizations in find_if will be picked up by any_of. Closes llvm#129310
… type" (llvm#211708) Reverts llvm#211701 Relands llvm#209737 The buildbot failure was misattributed to this change. Real cause is llvm#207274
After llvm#207274, libc++ fails __assume_aligned assertion in __assume_valid_range. https://lab.llvm.org/buildbot/#/builders/25/builds/18994 `DenseArrayAttrImpl<T>::operator ArrayRef<T>()` is just a cast of `raw.data()`. `raw` is aligned copy of range from from BytecodeReader done by Builtin_DenseArrayRawDataParameter allocator. However, if range is empty, aligned copying was omitted, leaving unaligned `ArrayRef<char>`. The fix is to replace unaligned `ArrayRef<>` with aligned default constructed.
… type" (#211708) Reverts llvm/llvm-project#211701 Relands llvm/llvm-project#209737 The buildbot failure was misattributed to this change. Real cause is llvm/llvm-project#207274 Signed-off-by: Hafidz Muzakky <[email protected]>
|
@ldionne I'm getting some failures downstream due to this in libcxx hardening assertions due to check on unaligned empty range. Same question as @vitalybuka above ^ |
I don't think this is something we've considered before. I think it would be reasonable not to check alignment for empty ranges, but I am somewhat worried by the impact this could have on the optimizer, since the assumptions would now be gated by a runtime check. Let's follow up in the PR. Edit: PR for the alignment issue reported above: #213752 |
This way, any optimizations in find_if will be picked up by any_of.
Closes #129310