[libcxx-commits] [libcxx] [libc++] Vectorize std::find (PR #156431)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Mon Sep 15 08:46:48 PDT 2025


================
@@ -44,39 +45,98 @@ _LIBCPP_BEGIN_NAMESPACE_STD
 // generic implementation
 template <class _Iter, class _Sent, class _Tp, class _Proj>
 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Iter
-__find(_Iter __first, _Sent __last, const _Tp& __value, _Proj& __proj) {
+__find_loop(_Iter __first, _Sent __last, const _Tp& __value, _Proj& __proj) {
----------------
ldionne wrote:

In an ideal world, the code in `__find_loop` as-is would be using `__builtin_assume_dereferenceable` and getting vectorized: https://godbolt.org/z/M9xo6PdY1. IIUC this doesn't work in @philnik777 's experience because:

1. The function that we want to vectorize can't contain pointer arithmetic, which is always going to happen because `vector.begin()` and `vector.end()` perform such arithmetic.
2. The code is inside a function template, and apparently the optimization doesn't work inside templates?

I'm just summarizing the issues we encountered while trying to make it work with the builtin. CC @fhahn , let's figure out whether we've done something incorrectly here or whether something needs to change in the optimization.

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


More information about the libcxx-commits mailing list