[llvm-branch-commits] [libc] Add memchr benchmark (PR #243751)
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Tue Nov 17 22:57:01 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libc
Author: Aiden Grossman (boomanaiden154)
<details>
<summary>Changes</summary>
There's a bit of a gap between LLVM libc's memchr implementation and
glibc's memchr implementation, even when both have equivalent vector
instructions available. Add some benchmarks to aid in the optimization
effort.
Assisted-By: LLM
---
Full diff: https://github.com/llvm/llvm-project/pull/243751.diff
7 Files Affected:
- (modified) libc/benchmarks/CMakeLists.txt (+18-10)
- (modified) libc/benchmarks/LibcDefaultImplementations.cpp (+7)
- (modified) libc/benchmarks/LibcFunctionPrototypes.h (+6)
- (modified) libc/benchmarks/LibcMemoryBenchmark.cpp (+6)
- (modified) libc/benchmarks/LibcMemoryBenchmark.h (+18)
- (modified) libc/benchmarks/LibcMemoryBenchmarkMain.cpp (+4)
- (modified) libc/benchmarks/LibcMemoryGoogleBenchmarkMain.cpp (+6)
``````````diff
diff --git a/libc/benchmarks/CMakeLists.txt b/libc/benchmarks/CMakeLists.txt
index 2c91e9bd901b6..9d1d310c12afe 100644
--- a/libc/benchmarks/CMakeLists.txt
+++ b/libc/benchmarks/CMakeLists.txt
@@ -204,6 +204,19 @@ if(LIBC_BENCHMARKS_HAS_LLVM_SUPPORT)
# Benchmarking tool
#==============================================================================
+ function(add_libc_benchmark name fq_config_name)
+ set(benchmark_name ${fq_config_name}_benchmark)
+ add_executable(${benchmark_name}
+ EXCLUDE_FROM_ALL
+ LibcMemoryBenchmarkMain.cpp
+ )
+ get_target_property(entrypoint_object_file ${fq_config_name} "OBJECT_FILE_RAW")
+ target_link_libraries(${benchmark_name} PUBLIC json ${entrypoint_object_file})
+ string(TOUPPER ${name} name_upper)
+ target_compile_definitions(${benchmark_name} PRIVATE "-DLIBC_BENCHMARK_FUNCTION_${name_upper}=LIBC_NAMESPACE::${name}" "-DLIBC_BENCHMARK_FUNCTION_NAME=\"${fq_config_name}\"")
+ llvm_update_compile_flags(${benchmark_name})
+ endfunction()
+
# Benchmark all implementations that can run on the target CPU.
function(add_libc_multi_impl_benchmark name)
get_property(fq_implementations GLOBAL PROPERTY ${name}_implementations)
@@ -211,16 +224,7 @@ if(LIBC_BENCHMARKS_HAS_LLVM_SUPPORT)
get_target_property(required_cpu_features ${fq_config_name} REQUIRE_CPU_FEATURES)
cpu_supports(can_run "${required_cpu_features}")
if(can_run)
- set(benchmark_name ${fq_config_name}_benchmark)
- add_executable(${benchmark_name}
- EXCLUDE_FROM_ALL
- LibcMemoryBenchmarkMain.cpp
- )
- get_target_property(entrypoint_object_file ${fq_config_name} "OBJECT_FILE_RAW")
- target_link_libraries(${benchmark_name} PUBLIC json ${entrypoint_object_file})
- string(TOUPPER ${name} name_upper)
- target_compile_definitions(${benchmark_name} PRIVATE "-DLIBC_BENCHMARK_FUNCTION_${name_upper}=LIBC_NAMESPACE::${name}" "-DLIBC_BENCHMARK_FUNCTION_NAME=\"${fq_config_name}\"")
- llvm_update_compile_flags(${benchmark_name})
+ add_libc_benchmark(${name} ${fq_config_name})
else()
message(STATUS "Skipping benchmark for '${fq_config_name}' insufficient host cpu features '${required_cpu_features}'")
endif()
@@ -233,6 +237,9 @@ if(LIBC_BENCHMARKS_HAS_LLVM_SUPPORT)
add_libc_multi_impl_benchmark(memcpy)
add_libc_multi_impl_benchmark(memmove)
add_libc_multi_impl_benchmark(memset)
+ if(TARGET libc.src.string.memchr)
+ add_libc_benchmark(memchr libc.src.string.memchr)
+ endif()
endif()
#==============================================================================
@@ -256,6 +263,7 @@ target_link_libraries(libc.benchmarks.memory_functions.opt_host
libc.src.string.memset_opt_host.__internal__
libc.src.strings.bcmp_opt_host.__internal__
libc.src.strings.bzero_opt_host.__internal__
+ libc.src.string.memchr.__internal__
benchmark_main
)
llvm_update_compile_flags(libc.benchmarks.memory_functions.opt_host)
diff --git a/libc/benchmarks/LibcDefaultImplementations.cpp b/libc/benchmarks/LibcDefaultImplementations.cpp
index 1ff940afcc44f..57d44d9e28ed6 100644
--- a/libc/benchmarks/LibcDefaultImplementations.cpp
+++ b/libc/benchmarks/LibcDefaultImplementations.cpp
@@ -11,12 +11,14 @@ extern void *memset(void *, int, size_t);
extern void bzero(void *, size_t);
extern int memcmp(const void *, const void *, size_t);
extern int bcmp(const void *, const void *, size_t);
+extern void *memchr(const void *, int, size_t);
} // namespace LIBC_NAMESPACE_DECL
// List of implementations to test.
using llvm::libc_benchmarks::BzeroConfiguration;
+using llvm::libc_benchmarks::MemchrConfiguration;
using llvm::libc_benchmarks::MemcmpOrBcmpConfiguration;
using llvm::libc_benchmarks::MemcpyConfiguration;
using llvm::libc_benchmarks::MemmoveConfiguration;
@@ -52,3 +54,8 @@ llvm::ArrayRef<BzeroConfiguration> getBzeroConfigurations() {
{LIBC_NAMESPACE::bzero, "LIBC_NAMESPACE::bzero"}};
return llvm::ArrayRef(kBzeroConfigurations);
}
+llvm::ArrayRef<MemchrConfiguration> getMemchrConfigurations() {
+ static constexpr MemchrConfiguration kMemchrConfigurations[] = {
+ {LIBC_NAMESPACE::memchr, "LIBC_NAMESPACE::memchr"}};
+ return llvm::ArrayRef(kMemchrConfigurations);
+}
diff --git a/libc/benchmarks/LibcFunctionPrototypes.h b/libc/benchmarks/LibcFunctionPrototypes.h
index 550e2e064d2d7..06bafa34c3fb9 100644
--- a/libc/benchmarks/LibcFunctionPrototypes.h
+++ b/libc/benchmarks/LibcFunctionPrototypes.h
@@ -38,6 +38,12 @@ struct MemcmpOrBcmpConfiguration {
llvm::StringRef name;
};
+using MemchrFunction = void *(*)(const void *, int, size_t);
+struct MemchrConfiguration {
+ MemchrFunction function;
+ llvm::StringRef name;
+};
+
} // namespace libc_benchmarks
} // namespace llvm
diff --git a/libc/benchmarks/LibcMemoryBenchmark.cpp b/libc/benchmarks/LibcMemoryBenchmark.cpp
index fe878651eb47c..35b5a90ddd79e 100644
--- a/libc/benchmarks/LibcMemoryBenchmark.cpp
+++ b/libc/benchmarks/LibcMemoryBenchmark.cpp
@@ -154,5 +154,11 @@ ComparisonSetup::ComparisonSetup()
SetSetup::SetSetup()
: ParameterBatch(1), dst_buffer(ParameterBatch::buffer_size) {}
+SearchSetup::SearchSetup()
+ : ParameterBatch(1), src_buffer(ParameterBatch::buffer_size) {
+ // Fill with a non-matching byte so memchr scans all size_bytes.
+ memset(src_buffer.begin(), 0xF, buffer_size);
+}
+
} // namespace libc_benchmarks
} // namespace llvm
diff --git a/libc/benchmarks/LibcMemoryBenchmark.h b/libc/benchmarks/LibcMemoryBenchmark.h
index 1b0e6d4e761c8..0840864eea8ec 100644
--- a/libc/benchmarks/LibcMemoryBenchmark.h
+++ b/libc/benchmarks/LibcMemoryBenchmark.h
@@ -280,6 +280,24 @@ struct ComparisonSetup : public ParameterBatch {
AlignedBuffer rhs_buffer;
};
+/// Provides source buffer for the Search operation (memchr) as well as the
+/// associated size distributions.
+struct SearchSetup : public ParameterBatch {
+ SearchSetup();
+
+ inline static const ArrayRef<MemorySizeDistribution> get_distributions() {
+ return getMemcmpSizeDistributions();
+ }
+
+ inline void *call(ParameterType parameter, MemchrFunction memchr_func) {
+ return memchr_func(src_buffer + parameter.offset_bytes, 0,
+ parameter.size_bytes);
+ }
+
+private:
+ AlignedBuffer src_buffer;
+};
+
} // namespace libc_benchmarks
} // namespace llvm
diff --git a/libc/benchmarks/LibcMemoryBenchmarkMain.cpp b/libc/benchmarks/LibcMemoryBenchmarkMain.cpp
index ec76a049caa21..683f3c2e16347 100644
--- a/libc/benchmarks/LibcMemoryBenchmarkMain.cpp
+++ b/libc/benchmarks/LibcMemoryBenchmarkMain.cpp
@@ -30,6 +30,7 @@ extern void *memset(void *, int, size_t);
extern void bzero(void *, size_t);
extern int memcmp(const void *, const void *, size_t);
extern int bcmp(const void *, const void *, size_t);
+extern void *memchr(const void *, int, size_t);
} // namespace LIBC_NAMESPACE_DECL
@@ -91,6 +92,9 @@ using BenchmarkSetup = ComparisonSetup;
#elif defined(LIBC_BENCHMARK_FUNCTION_BCMP)
#define LIBC_BENCHMARK_FUNCTION LIBC_BENCHMARK_FUNCTION_BCMP
using BenchmarkSetup = ComparisonSetup;
+#elif defined(LIBC_BENCHMARK_FUNCTION_MEMCHR)
+#define LIBC_BENCHMARK_FUNCTION LIBC_BENCHMARK_FUNCTION_MEMCHR
+using BenchmarkSetup = SearchSetup;
#else
#error "Missing LIBC_BENCHMARK_FUNCTION_XXX definition"
#endif
diff --git a/libc/benchmarks/LibcMemoryGoogleBenchmarkMain.cpp b/libc/benchmarks/LibcMemoryGoogleBenchmarkMain.cpp
index d5beb8aa55a37..8feb0e1909d4c 100644
--- a/libc/benchmarks/LibcMemoryGoogleBenchmarkMain.cpp
+++ b/libc/benchmarks/LibcMemoryGoogleBenchmarkMain.cpp
@@ -19,6 +19,7 @@ using llvm::Twine;
using llvm::libc_benchmarks::BzeroConfiguration;
using llvm::libc_benchmarks::ComparisonSetup;
using llvm::libc_benchmarks::CopySetup;
+using llvm::libc_benchmarks::MemchrConfiguration;
using llvm::libc_benchmarks::MemcmpOrBcmpConfiguration;
using llvm::libc_benchmarks::MemcpyConfiguration;
using llvm::libc_benchmarks::MemmoveConfiguration;
@@ -26,6 +27,7 @@ using llvm::libc_benchmarks::MemorySizeDistribution;
using llvm::libc_benchmarks::MemsetConfiguration;
using llvm::libc_benchmarks::MoveSetup;
using llvm::libc_benchmarks::OffsetDistribution;
+using llvm::libc_benchmarks::SearchSetup;
using llvm::libc_benchmarks::SetSetup;
// Alignment to use for when accessing the buffers.
@@ -123,3 +125,7 @@ BENCHMARK_MEMORY_FUNCTION(BM_Memset, SetSetup, MemsetConfiguration,
extern llvm::ArrayRef<BzeroConfiguration> getBzeroConfigurations();
BENCHMARK_MEMORY_FUNCTION(BM_Bzero, SetSetup, BzeroConfiguration,
getBzeroConfigurations());
+
+extern llvm::ArrayRef<MemchrConfiguration> getMemchrConfigurations();
+BENCHMARK_MEMORY_FUNCTION(BM_Memchr, SearchSetup, MemchrConfiguration,
+ getMemchrConfigurations());
``````````
</details>
https://github.com/llvm/llvm-project/pull/243751
More information about the llvm-branch-commits
mailing list