[libc-commits] [libc] 77312b7 - [libc] Add optimized clang_vector memchr implementation
via libc-commits
libc-commits at lists.llvm.org
Fri Nov 20 14:57:46 PST 2026
Author: Aiden Grossman
Date: 2026-11-20T14:57:42-08:00
New Revision: 77312b7603d91fc147b39a6a8a6afa66ab942cb1
URL: https://github.com/llvm/llvm-project/commit/77312b7603d91fc147b39a6a8a6afa66ab942cb1
DIFF: https://github.com/llvm/llvm-project/commit/77312b7603d91fc147b39a6a8a6afa66ab942cb1.diff
LOG: [libc] Add optimized clang_vector memchr implementation
This patch adds a reasonably optimized clang_vector based memchr
implementation. It is comparable in .text size to glibc and about 30%
faster for inputs <64 bytes (90+% of inputs for server applications). We
lose a little bit for larger inputs due to extra instructions in the
main loop that LLVM isn't able to optimize away, but not by too much.
The original implementatin was written by an agent as I prodded it to
keep making the implementation faster. Then I heavily hand-edited mostly
for style.
Assisted-By: LLM
Reviewers: lntue, jhuber6, michaelrj-google
Pull Request: https://github.com/llvm/llvm-project/pull/244469
Added:
Modified:
libc/src/string/memory_utils/generic/inline_strlen.h
Removed:
################################################################################
diff --git a/libc/src/string/memory_utils/generic/inline_strlen.h b/libc/src/string/memory_utils/generic/inline_strlen.h
index cad3275b33a50..0babaf2b5294b 100644
--- a/libc/src/string/memory_utils/generic/inline_strlen.h
+++ b/libc/src/string/memory_utils/generic/inline_strlen.h
@@ -12,6 +12,8 @@
#include "src/__support/CPP/bit.h"
#include "src/__support/CPP/simd.h"
#include "src/__support/common.h"
+#include "src/__support/macros/attributes.h"
+#include "src/__support/macros/optimization.h"
namespace LIBC_NAMESPACE_DECL {
namespace clang_vector {
@@ -55,39 +57,166 @@ LIBC_INLINE static const void *calculate_find_first_character_return(
return src + c_offset;
}
-LIBC_NO_SANITIZE_OOB_ACCESS LIBC_INLINE static const void *
+LIBC_INLINE_VAR constexpr size_t VEC_SIZE = sizeof(cpp::simd<char>);
+LIBC_INLINE_VAR constexpr size_t LOOP_UNROLL_FACTOR = 4;
+LIBC_INLINE_VAR constexpr size_t LOOP_BLOCK_SIZE =
+ LOOP_UNROLL_FACTOR * VEC_SIZE;
+
+// A find_first_character implementation for inputs less than the loop block
+// size. It loads VecN vectors with the end of the last vector aligning
+// with the end of the buffer passed in.
+template <size_t VecN>
+LIBC_NO_SANITIZE_OOB_ACCESS LIBC_ALWAYS_INLINE static const void *
+find_first_character_small(const unsigned char *s, cpp::simd<char> c,
+ size_t n) {
+ static_assert(VecN >= 2 && VecN <= LOOP_UNROLL_FACTOR);
+
+ const unsigned char *ptrs[VecN];
+ cpp::simd_mask<char> masks[VecN];
+ for (size_t i = 0; i < VecN - 1; ++i) {
+ ptrs[i] = s + i * VEC_SIZE;
+ masks[i] = cpp::load<cpp::simd<char>>(ptrs[i], /*aligned=*/false) == c;
+ }
+ ptrs[VecN - 1] = s + n - VEC_SIZE;
+ masks[VecN - 1] =
+ cpp::load<cpp::simd<char>>(ptrs[VecN - 1], /*aligned=*/false) == c;
+
+ cpp::simd_mask<char> combined = masks[0] | masks[1];
+ if constexpr (VecN == 3)
+ combined = combined | masks[2];
+ else if constexpr (VecN == 4)
+ combined = combined | (masks[2] | masks[3]);
+
+ if (cpp::any_of(combined)) {
+ for (size_t i = 0; i < VecN - 1; ++i)
+ if (cpp::any_of(masks[i]))
+ return ptrs[i] + cpp::find_first_set(masks[i]);
+ return ptrs[VecN - 1] + cpp::find_first_set(masks[VecN - 1]);
+ }
+ return nullptr;
+}
+
+// A find_first_character implementation for large inputs. The input is
+// expected to be aligned to LOOP_BLOCK_SIZE. There is then a loop that
+// does four VEC_SIZE loads per iteration.
+LIBC_NO_SANITIZE_OOB_ACCESS LIBC_ALWAYS_INLINE static const void *
+find_first_aligned_loop(const cpp::simd<char> *s, cpp::simd<char> c, size_t n) {
+ LIBC_LOOP_NOUNROLL
+ for (;;) {
+ cpp::simd_mask<char> m0 =
+ cpp::load<cpp::simd<char>>(s, /*aligned=*/true) == c;
+ cpp::simd_mask<char> m1 =
+ cpp::load<cpp::simd<char>>(s + 1, /*aligned=*/true) == c;
+ cpp::simd_mask<char> m2 =
+ cpp::load<cpp::simd<char>>(s + 2, /*aligned=*/true) == c;
+ cpp::simd_mask<char> m3 =
+ cpp::load<cpp::simd<char>>(s + 3, /*aligned=*/true) == c;
+ if (LIBC_UNLIKELY(cpp::any_of((m0 | m1) | (m2 | m3)))) {
+ size_t found_offset;
+ if (cpp::any_of(m0))
+ found_offset = cpp::find_first_set(m0);
+ else if (cpp::any_of(m1))
+ found_offset = VEC_SIZE + cpp::find_first_set(m1);
+ else if (cpp::any_of(m2))
+ found_offset = 2 * VEC_SIZE + cpp::find_first_set(m2);
+ else
+ found_offset = 3 * VEC_SIZE + cpp::find_first_set(m3);
+ if (found_offset >= n)
+ return nullptr;
+ return reinterpret_cast<const char *>(s) + found_offset;
+ }
+ if (n <= LOOP_BLOCK_SIZE)
+ return nullptr;
+ s += LOOP_UNROLL_FACTOR;
+ n -= LOOP_BLOCK_SIZE;
+ }
+}
+
+LIBC_NO_SANITIZE_OOB_ACCESS LIBC_ALWAYS_INLINE static const void *
find_first_character(const unsigned char *s, unsigned char c, size_t n) {
// s doesn't have to be dereferenceable if there is nothing to search.
if (n == 0)
return nullptr;
- using Vector = cpp::simd<char>;
- using Mask = cpp::simd_mask<char>;
- Vector c_byte = c;
-
- size_t alignment = alignof(Vector);
- const Vector *aligned =
- reinterpret_cast<const Vector *>(__builtin_align_down(s, alignment));
-
- Vector chars = cpp::load<Vector>(aligned, /*aligned=*/true);
- Mask cmp_v = chars == c_byte;
- size_t offset = s - reinterpret_cast<const unsigned char *>(aligned);
-
- cmp_v = shift_mask<unsigned char>(cmp_v, offset);
- if (cpp::any_of(cmp_v))
- return calculate_find_first_character_return(
- reinterpret_cast<const char *>(s), cmp_v, n);
+ constexpr size_t min_page_size = 4096;
+ static_assert(cpp::has_single_bit(min_page_size) &&
+ cpp::has_single_bit(LOOP_BLOCK_SIZE) &&
+ min_page_size % LOOP_BLOCK_SIZE == 0,
+ "4x vector block must cleanly divide a page.");
+
+ cpp::simd<char> c_byte = c;
+
+ if (n <= LOOP_BLOCK_SIZE) {
+ const uintptr_t page_offset =
+ reinterpret_cast<uintptr_t>(s) & (min_page_size - 1);
+ if (LIBC_LIKELY(page_offset <= min_page_size - LOOP_BLOCK_SIZE)) {
+ if (n <= VEC_SIZE) {
+ cpp::simd_mask<char> m0 =
+ cpp::load<cpp::simd<char>>(s, /*aligned=*/false) == c_byte;
+ return cpp::any_of(m0) ? calculate_find_first_character_return(
+ reinterpret_cast<const char *>(s), m0, n)
+ : nullptr;
+ }
+ if (n <= 2 * VEC_SIZE)
+ return find_first_character_small<2>(s, c_byte, n);
+ if (n <= 3 * VEC_SIZE)
+ return find_first_character_small<3>(s, c_byte, n);
+ return find_first_character_small<4>(s, c_byte, n);
+ }
+ }
- for (size_t bytes_checked = sizeof(Vector) - offset; bytes_checked < n;
- bytes_checked += sizeof(Vector)) {
- aligned++;
- chars = cpp::load<Vector>(aligned, /*aligned=*/true);
- cmp_v = chars == c_byte;
+ const cpp::simd<char> *loop_aligned;
+ if (LOOP_BLOCK_SIZE <= 64 && n > LOOP_BLOCK_SIZE) {
+ // Align s down to LOOP_BLOCK_SIZE; all 4 vectors lie on the same page.
+ const cpp::simd<char> *block = reinterpret_cast<const cpp::simd<char> *>(
+ __builtin_align_down(s, LOOP_BLOCK_SIZE));
+ size_t offset =
+ static_cast<size_t>(s - reinterpret_cast<const unsigned char *>(block));
+ uint64_t combined = 0;
+ for (size_t i = 0; i < LOOP_UNROLL_FACTOR; ++i) {
+ cpp::simd_mask<char> m =
+ cpp::load<cpp::simd<char>>(block + i, /*aligned=*/true) == c_byte;
+ combined |= static_cast<uint64_t>(cpp::to_bitmask(m)) << (i * VEC_SIZE);
+ }
+ combined >>= offset;
+ if (LIBC_UNLIKELY(combined != 0))
+ return s + cpp::countr_zero(combined);
+ n -= LOOP_BLOCK_SIZE - offset;
+ loop_aligned = block + LOOP_UNROLL_FACTOR;
+ } else {
+ // Fallback for page-crossing small inputs or wide vectors (LOOP_BLOCK_SIZE
+ // > 64): step aligned vectors until done or LOOP_BLOCK_SIZE-aligned.
+ size_t alignment = alignof(cpp::simd<char>);
+ const cpp::simd<char> *aligned = reinterpret_cast<const cpp::simd<char> *>(
+ __builtin_align_down(s, alignment));
+ size_t offset = static_cast<size_t>(
+ s - reinterpret_cast<const unsigned char *>(aligned));
+ cpp::simd_mask<char> cmp_v = shift_mask<unsigned char>(
+ cpp::load<cpp::simd<char>>(aligned, /*aligned=*/true) == c_byte,
+ offset);
if (cpp::any_of(cmp_v))
return calculate_find_first_character_return(
- reinterpret_cast<const char *>(aligned), cmp_v, n - bytes_checked);
+ reinterpret_cast<const char *>(s), cmp_v, n);
+ if (n <= VEC_SIZE - offset)
+ return nullptr;
+ n -= VEC_SIZE - offset;
+ ++aligned;
+
+ while ((reinterpret_cast<uintptr_t>(aligned) & (LOOP_BLOCK_SIZE - 1)) !=
+ 0) {
+ cpp::simd_mask<char> m =
+ cpp::load<cpp::simd<char>>(aligned, /*aligned=*/true) == c_byte;
+ if (cpp::any_of(m))
+ return calculate_find_first_character_return(
+ reinterpret_cast<const char *>(aligned), m, n);
+ if (n <= VEC_SIZE)
+ return nullptr;
+ n -= VEC_SIZE;
+ ++aligned;
+ }
+ loop_aligned = aligned;
}
- return nullptr;
+ return find_first_aligned_loop(loop_aligned, c_byte, n);
}
} // namespace clang_vector
More information about the libc-commits
mailing list