[PATCH] D76056: [MemFunctions] Add validity check.

Clement Courbet via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 12 04:30:13 PDT 2020


courbet updated this revision to Diff 249889.
courbet added a comment.

clang-format


Repository:
  rT test-suite

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D76056/new/

https://reviews.llvm.org/D76056

Files:
  MicroBenchmarks/MemFunctions/main.cpp


Index: MicroBenchmarks/MemFunctions/main.cpp
===================================================================
--- MicroBenchmarks/MemFunctions/main.cpp
+++ MicroBenchmarks/MemFunctions/main.cpp
@@ -17,11 +17,19 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include <cstdlib>
 #include <cstring>
+#include <iostream>
 #include <vector>
 
 #include "benchmark/benchmark.h"
 
+// This calls the real memcmp to check the reuslts.
+int RealMemCmp(const char *p, const char *q, size_t s)
+    __attribute__((no_builtin("memcmp"))) {
+  return memcmp(p, q, s);
+}
+
 // Benchmarks `memcmp(p, q, size) OP 0` where n is known at compile time and OP
 // is defined by `Pred`. The compiler typically inlines the memcmp + comparion
 // to loads and compares.
@@ -44,6 +52,13 @@
   for (int i = 0; i < kNumElements; ++i)
     Mod().template Change<kSize>(p + i * kSize);
 
+  // First check the validity of the results.
+  if (Pred()(RealMemCmp(p, q, kSize)) != Pred()(memcmp(p, q, kSize))) {
+    std::cerr << "invalid results for Pred=" << Pred::kDisplay
+              << " Mod=" << Mod::kDisplay << " kSize=" << kSize << std::endl;
+    std::exit(1);
+  }
+
   benchmark::DoNotOptimize(p);
   benchmark::DoNotOptimize(q);
 
@@ -52,7 +67,7 @@
     benchmark::ClobberMemory();
 
     for (int i = 0; i < kNumElements; ++i) {
-      int res = Pred()(memcmp(p + i * kSize, q + i * kSize, kSize));
+      bool res = Pred()(memcmp(p + i * kSize, q + i * kSize, kSize));
       benchmark::DoNotOptimize(res);
     }
   }
@@ -62,36 +77,34 @@
 // Predicates.
 struct EqZero {
   bool operator()(int v) const { return v == 0; }
+  inline static constexpr const char kDisplay[] = "EqZero";
 };
 struct LessThanZero {
   bool operator()(int v) const { return v < 0; }
+  inline static constexpr const char kDisplay[] = "LessThanZero";
 };
 struct GreaterThanZero {
   bool operator()(int v) const { return v > 0; }
+  inline static constexpr const char kDisplay[] = "GreaterThanZero";
 };
 
 // Functors to change the first/mid/last or no value.
 struct None {
   template <int kSize>
   void Change(char* const p) const {}
+  inline static constexpr const char kDisplay[] = "None";
 };
 struct First {
-  template <int kSize>
-  void Change(char* const p) const {
-    p[0] = 128;
-  }
+  template <int kSize> void Change(char *const p) const { p[0] = 0xff; }
+  inline static constexpr const char kDisplay[] = "First";
 };
 struct Mid {
-  template <int kSize>
-  void Change(char* const p) const {
-    p[kSize / 2] = 128;
-  }
+  template <int kSize> void Change(char *const p) const { p[kSize / 2] = 0xff; }
+  inline static constexpr const char kDisplay[] = "Mid";
 };
 struct Last {
-  template <int kSize>
-  void Change(char* const p) const {
-    p[kSize - 1] = 128;
-  }
+  template <int kSize> void Change(char *const p) const { p[kSize - 1] = 0xff; }
+  inline static constexpr const char kDisplay[] = "Last";
 };
 
 #define MEMCMP_BENCHMARK_PRED_CHANGE(size, pred, change) \


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D76056.249889.patch
Type: text/x-patch
Size: 3018 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200312/9f86fb74/attachment.bin>


More information about the llvm-commits mailing list