TSAN caught us racing in ScalarBench.cpp
authorcommit-bot@chromium.org <commit-bot@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>
Fri, 30 May 2014 14:56:58 +0000 (14:56 +0000)
committercommit-bot@chromium.org <commit-bot@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>
Fri, 30 May 2014 14:56:58 +0000 (14:56 +0000)
http://108.170.220.102:10117/builders/Test-Ubuntu13-ShuttleA-HD2000-x86_64-Debug-TSAN/builds/914/steps/RunDM/logs/stdio

BUG=skia:
R=reed@google.com, mtklein@google.com

Author: mtklein@chromium.org

Review URL: https://codereview.chromium.org/305033002

git-svn-id: http://skia.googlecode.com/svn/trunk@14992 2bbb7eff-a529-9590-31e7-b0007b416f81

bench/ScalarBench.cpp

index 54ee0fcd195967a73bd532b2bf9b08872fc9b76d..a023704fcd132d7bbbdd042ab9d0db43dbb218f7 100644 (file)
@@ -41,18 +41,6 @@ private:
     typedef SkBenchmark INHERITED;
 };
 
-// we want to stop the compiler from eliminating code that it thinks is a no-op
-// so we have a non-static global we increment, hoping that will convince the
-// compiler to execute everything
-int gScalarBench_NonStaticGlobal;
-
-#define always_do(pred)                     \
-    do {                                    \
-        if (pred) {                         \
-            ++gScalarBench_NonStaticGlobal; \
-        }                                   \
-    } while (0)
-
 // having unknown values in our arrays can throw off the timing a lot, perhaps
 // handling NaN values is a lot slower. Anyway, this guy is just meant to put
 // reasonable values in our arrays.
@@ -71,8 +59,10 @@ public:
 protected:
     virtual int mulLoopCount() const { return 4; }
     virtual void performTest() {
-        always_do(fArray[6] != 0.0f || fArray[7] != 0.0f || fArray[8] != 1.0f);
-        always_do(fArray[2] != 0.0f || fArray[5] != 0.0f);
+        // xoring into a volatile prevents the compiler from optimizing these checks away.
+        volatile bool junk = false;
+        junk ^= (fArray[6] != 0.0f || fArray[7] != 0.0f || fArray[8] != 1.0f);
+        junk ^= (fArray[2] != 0.0f || fArray[5] != 0.0f);
     }
 private:
     float fArray[9];
@@ -88,11 +78,13 @@ public:
 protected:
     virtual int mulLoopCount() const { return 4; }
     virtual void performTest() {
-        always_do(SkScalarAs2sCompliment(fArray[6]) |
-                  SkScalarAs2sCompliment(fArray[7]) |
-                  (SkScalarAs2sCompliment(fArray[8]) - kPersp1Int));
-        always_do(SkScalarAs2sCompliment(fArray[2]) |
-                  SkScalarAs2sCompliment(fArray[5]));
+        // xoring into a volatile prevents the compiler from optimizing these checks away.
+        volatile bool junk = false;
+        junk ^= (SkScalarAs2sCompliment(fArray[6]) |
+                 SkScalarAs2sCompliment(fArray[7]) |
+                (SkScalarAs2sCompliment(fArray[8]) - kPersp1Int));
+        junk ^= (SkScalarAs2sCompliment(fArray[2]) |
+                 SkScalarAs2sCompliment(fArray[5]));
     }
 private:
     static const int32_t kPersp1Int = 0x3f800000;