r205606 - CodeGen: Don't create branch weight metadata from empty profiles

Justin Bogner mail at justinbogner.com
Thu Apr 3 19:48:51 PDT 2014


Author: bogner
Date: Thu Apr  3 21:48:51 2014
New Revision: 205606

URL: http://llvm.org/viewvc/llvm-project?rev=205606&view=rev
Log:
CodeGen: Don't create branch weight metadata from empty profiles

If all of our weights are zero when calculating branch weights, it
means we haven't profiled the code in question. Avoid creating a
metadata node that says all branches are equally likely in this case.

The test also checks constructs that hit the other createBranchWeights
overload. These were already working.

Added:
    cfe/trunk/test/Profile/Inputs/c-unprofiled-blocks.profdata
    cfe/trunk/test/Profile/c-unprofiled-blocks.c
Modified:
    cfe/trunk/lib/CodeGen/CodeGenPGO.cpp

Modified: cfe/trunk/lib/CodeGen/CodeGenPGO.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenPGO.cpp?rev=205606&r1=205605&r2=205606&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenPGO.cpp (original)
+++ cfe/trunk/lib/CodeGen/CodeGenPGO.cpp Thu Apr  3 21:48:51 2014
@@ -1004,9 +1004,13 @@ llvm::MDNode *CodeGenPGO::createBranchWe
   if (Weights.size() < 2)
     return nullptr;
 
+  // Check for empty weights.
+  uint64_t MaxWeight = *std::max_element(Weights.begin(), Weights.end());
+  if (MaxWeight == 0)
+    return nullptr;
+
   // Calculate how to scale down to 32-bits.
-  uint64_t Scale = calculateWeightScale(*std::max_element(Weights.begin(),
-                                                          Weights.end()));
+  uint64_t Scale = calculateWeightScale(MaxWeight);
 
   SmallVector<uint32_t, 16> ScaledWeights;
   ScaledWeights.reserve(Weights.size());

Added: cfe/trunk/test/Profile/Inputs/c-unprofiled-blocks.profdata
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Profile/Inputs/c-unprofiled-blocks.profdata?rev=205606&view=auto
==============================================================================
--- cfe/trunk/test/Profile/Inputs/c-unprofiled-blocks.profdata (added)
+++ cfe/trunk/test/Profile/Inputs/c-unprofiled-blocks.profdata Thu Apr  3 21:48:51 2014
@@ -0,0 +1,32 @@
+never_called
+9
+9
+0
+0
+0
+0
+0
+0
+0
+0
+0
+
+main
+1
+1
+1
+
+dead_code
+10
+10
+1
+0
+0
+0
+0
+0
+0
+0
+0
+0
+

Added: cfe/trunk/test/Profile/c-unprofiled-blocks.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Profile/c-unprofiled-blocks.c?rev=205606&view=auto
==============================================================================
--- cfe/trunk/test/Profile/c-unprofiled-blocks.c (added)
+++ cfe/trunk/test/Profile/c-unprofiled-blocks.c Thu Apr  3 21:48:51 2014
@@ -0,0 +1,68 @@
+// Blocks that we have no profile data for (ie, it was never reached in training
+// runs) shouldn't have any branch weight metadata added.
+
+// RUN: %clang_cc1 -triple x86_64-apple-macosx10.9 -main-file-name c-unprofiled-blocks.c %s -o - -emit-llvm -fprofile-instr-use=%S/Inputs/c-unprofiled-blocks.profdata | FileCheck -check-prefix=PGOUSE %s
+
+// PGOUSE-LABEL: @never_called(i32 %i)
+int never_called(int i) {
+  // PGOUSE: br i1 %{{[^,]*}}, label %{{[^,]*}}, label %{{[^,]*}}{{$}}
+  if (i) {}
+
+  // PGOUSE: br i1 %{{[^,]*}}, label %{{[^,]*}}, label %{{[^,]*}}{{$}}
+  for (i = 0; i < 100; ++i) {
+  }
+
+  // PGOUSE: br i1 %{{[^,]*}}, label %{{[^,]*}}, label %{{[^,]*}}{{$}}
+  while (--i) {}
+
+  // PGOUSE: br i1 %{{[^,]*}}, label %{{[^,]*}}, label %{{[^,]*}}{{$}}
+  do {} while (i++ < 75);
+
+  // PGOUSE: switch {{.*}} [
+  // PGOUSE-NEXT: i32 12
+  // PGOUSE-NEXT: i32 82
+  // PGOUSE-NEXT: ]{{$}}
+  switch (i) {
+  case 12: return 3;
+  case 82: return 0;
+  default: return 89;
+  }
+}
+
+// PGOUSE-LABEL: @dead_code(i32 %i)
+int dead_code(int i) {
+  // PGOUSE: br {{.*}}, !prof !{{[0-9]+}}
+  if (i) {
+    // This branch is never reached.
+
+    // PGOUSE: br i1 %{{[^,]*}}, label %{{[^,]*}}, label %{{[^,]*}}{{$}}
+    if (!i) {}
+
+    // PGOUSE: br i1 %{{[^,]*}}, label %{{[^,]*}}, label %{{[^,]*}}{{$}}
+    for (i = 0; i < 100; ++i) {
+    }
+
+    // PGOUSE: br i1 %{{[^,]*}}, label %{{[^,]*}}, label %{{[^,]*}}{{$}}
+    while (--i) {}
+
+    // PGOUSE: br i1 %{{[^,]*}}, label %{{[^,]*}}, label %{{[^,]*}}{{$}}
+    do {} while (i++ < 75);
+
+    // PGOUSE: switch {{.*}} [
+    // PGOUSE-NEXT: i32 12
+    // PGOUSE-NEXT: i32 82
+    // PGOUSE-NEXT: ]{{$}}
+    switch (i) {
+    case 12: return 3;
+    case 82: return 0;
+    default: return 89;
+    }
+  }
+  return 2;
+}
+
+// PGOUSE-LABEL: @main(i32 %argc, i8** %argv)
+int main(int argc, const char *argv[]) {
+  dead_code(0);
+  return 0;
+}





More information about the cfe-commits mailing list