[libc-commits] [libc] [libc] Add --rhs-offset flag for misaligned strcmp benchmarking (PR #245898)

via libc-commits libc-commits at lists.llvm.org
Wed Nov 25 00:13:31 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: Aiden Grossman (boomanaiden154)

<details>
<summary>Changes</summary>

Makes it easy to test offset cases for comparison to ensure that
alignment doesn't enter any extra slow paths.

Assisted-By: LLM


---
Full diff: https://github.com/llvm/llvm-project/pull/245898.diff


3 Files Affected:

- (modified) libc/benchmarks/LibcMemoryBenchmark.cpp (+29-5) 
- (modified) libc/benchmarks/LibcMemoryBenchmark.h (+6-2) 
- (modified) libc/benchmarks/LibcMemoryBenchmarkMain.cpp (+7-1) 


``````````diff
diff --git a/libc/benchmarks/LibcMemoryBenchmark.cpp b/libc/benchmarks/LibcMemoryBenchmark.cpp
index 73304077783861..a7626fd576143f 100644
--- a/libc/benchmarks/LibcMemoryBenchmark.cpp
+++ b/libc/benchmarks/LibcMemoryBenchmark.cpp
@@ -143,15 +143,39 @@ CopySetup::CopySetup()
 MoveSetup::MoveSetup()
     : ParameterBatch(3), buffer(ParameterBatch::buffer_size * 3) {}
 
-ComparisonSetup::ComparisonSetup()
+ComparisonSetup::ComparisonSetup(size_t rhs_offset)
     : ParameterBatch(2), lhs_buffer(ParameterBatch::buffer_size),
-      rhs_buffer(ParameterBatch::buffer_size) {
+      rhs_buffer(ParameterBatch::buffer_size), rhs_offset(rhs_offset) {
+  if (rhs_offset >= buffer_size)
+    reportFatalUsageError("rhs_offset must be less than buffer_size");
   // The memcmp/strcmp buffers always compare equal, with a null terminator at
-  // the end for strcmp.
+  // the end for strcmp (shifted by rhs_offset in rhs_buffer to test misaligned
+  // string pairs).
   memset(lhs_buffer.begin(), 0xF, buffer_size - 1);
-  memset(rhs_buffer.begin(), 0xF, buffer_size - 1);
+  memset(rhs_buffer.begin(), 0xF, buffer_size - 1 - rhs_offset);
   lhs_buffer[buffer_size - 1] = '\0';
-  rhs_buffer[buffer_size - 1] = '\0';
+  rhs_buffer[buffer_size - 1 - rhs_offset] = '\0';
+}
+
+void ComparisonSetup::check_valid(const ParameterType &p) const {
+  ParameterBatch::check_valid(p);
+  if (p.size_bytes + rhs_offset >= buffer_size) {
+#ifdef LIBC_BENCHMARKS_HAS_LLVM_SUPPORT
+    reportFatalUsageError(
+        llvm::Twine("Call would result in buffer underflow: RhsOffset=")
+            .concat(llvm::Twine(rhs_offset))
+            .concat(", Size=")
+            .concat(llvm::Twine(p.size_bytes))
+            .concat(", BufferSize=")
+            .concat(llvm::Twine(buffer_size)));
+#else
+    std::string message = "Call would result in buffer underflow: RhsOffset=" +
+                          std::to_string(rhs_offset) +
+                          ", Size=" + std::to_string(p.size_bytes) +
+                          ", BufferSize=" + std::to_string(buffer_size);
+    reportFatalUsageError(message.c_str());
+#endif
+  }
 }
 
 SetSetup::SetSetup()
diff --git a/libc/benchmarks/LibcMemoryBenchmark.h b/libc/benchmarks/LibcMemoryBenchmark.h
index 86515cd9d5fbf4..45d029ed7753f7 100644
--- a/libc/benchmarks/LibcMemoryBenchmark.h
+++ b/libc/benchmarks/LibcMemoryBenchmark.h
@@ -262,12 +262,14 @@ struct SetSetup : public ParameterBatch {
 /// Provides left and right buffers for the Comparison operation as well as the
 /// associated size distributions.
 struct ComparisonSetup : public ParameterBatch {
-  ComparisonSetup();
+  explicit ComparisonSetup(size_t rhs_offset = 0);
 
   inline static const ArrayRef<MemorySizeDistribution> get_distributions() {
     return getMemcmpSizeDistributions();
   }
 
+  void check_valid(const ParameterType &p) const;
+
   inline int call(ParameterType parameter,
                   MemcmpOrBcmpFunction memcmp_or_bcmp_func) {
     return memcmp_or_bcmp_func(lhs_buffer + parameter.offset_bytes,
@@ -277,12 +279,14 @@ struct ComparisonSetup : public ParameterBatch {
 
   inline int call(ParameterType parameter, StrcmpFunction strcmp_func) {
     return strcmp_func(lhs_buffer + buffer_size - 1 - parameter.size_bytes,
-                       rhs_buffer + buffer_size - 1 - parameter.size_bytes);
+                       rhs_buffer + buffer_size - 1 - rhs_offset -
+                           parameter.size_bytes);
   }
 
 private:
   AlignedBuffer lhs_buffer;
   AlignedBuffer rhs_buffer;
+  size_t rhs_offset;
 };
 
 /// Provides source buffer for the Search operation (memchr, strlen) as well as
diff --git a/libc/benchmarks/LibcMemoryBenchmarkMain.cpp b/libc/benchmarks/LibcMemoryBenchmarkMain.cpp
index dbacf71aee8ca0..6a1ed7d92aa5aa 100644
--- a/libc/benchmarks/LibcMemoryBenchmarkMain.cpp
+++ b/libc/benchmarks/LibcMemoryBenchmarkMain.cpp
@@ -102,7 +102,13 @@ using BenchmarkSetup = SearchSetup;
 using BenchmarkSetup = SearchSetup;
 #elif defined(LIBC_BENCHMARK_FUNCTION_STRCMP)
 #define LIBC_BENCHMARK_FUNCTION LIBC_BENCHMARK_FUNCTION_STRCMP
-using BenchmarkSetup = ComparisonSetup;
+static cl::opt<uint32_t>
+    RhsOffset("rhs-offset", cl::desc("Relative byte offset of rhs vs lhs"),
+              cl::init(0));
+struct StrcmpSetup : public ComparisonSetup {
+  StrcmpSetup() : ComparisonSetup(RhsOffset) {}
+};
+using BenchmarkSetup = StrcmpSetup;
 #else
 #error "Missing LIBC_BENCHMARK_FUNCTION_XXX definition"
 #endif

``````````

</details>


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


More information about the libc-commits mailing list