[llvm] a33accd - [PGO] Early detection regarding whether pgo counter promotion is possible

Andy Kaylor via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 24 09:56:15 PST 2020


Author: Andy Kaylor
Date: 2020-01-24T09:55:41-08:00
New Revision: a33accde954fc0a2056d2538d107c6ac4143a917

URL: https://github.com/llvm/llvm-project/commit/a33accde954fc0a2056d2538d107c6ac4143a917
DIFF: https://github.com/llvm/llvm-project/commit/a33accde954fc0a2056d2538d107c6ac4143a917.diff

LOG: [PGO] Early detection regarding whether pgo counter promotion is possible

Patch by Chris Chrulski

This fixes a problem with the current behavior when assertions are enabled.
A loop that exits to a catchswitch instruction is skipped for the counter
promotion, however this check was being done after the PGOCounterPromoter
tried to collect an insertion point for the exit block. A call to
getFirstInsertionPt() on a block that begins with a catchswitch instruction
triggers an assertion. This change performs a check whether the counter
promotion is possible prior to collecting the ExitBlocks and InsertPts.

Differential Revision: https://reviews.llvm.org/D73222

Added: 
    llvm/test/Transforms/PGOProfile/counter_promo_exit_catchswitch.ll

Modified: 
    llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp b/llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp
index 4aa774e7d301..c091c32e2469 100644
--- a/llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp
+++ b/llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp
@@ -247,9 +247,14 @@ class PGOCounterPromoter {
       : LoopToCandidates(LoopToCands), ExitBlocks(), InsertPts(), L(CurLoop),
         LI(LI), BFI(BFI) {
 
+    // Skip collection of ExitBlocks and InsertPts for loops that will not be
+    // able to have counters promoted.
     SmallVector<BasicBlock *, 8> LoopExitBlocks;
     SmallPtrSet<BasicBlock *, 8> BlockSet;
+
     L.getExitBlocks(LoopExitBlocks);
+    if (!isPromotionPossible(&L, LoopExitBlocks))
+      return;
 
     for (BasicBlock *ExitBlock : LoopExitBlocks) {
       if (BlockSet.insert(ExitBlock).second) {
@@ -318,21 +323,31 @@ class PGOCounterPromoter {
     return true;
   }
 
-  // Returns the max number of Counter Promotions for LP.
-  unsigned getMaxNumOfPromotionsInLoop(Loop *LP) {
+  // Check whether the loop satisfies the basic conditions needed to perform
+  // Counter Promotions.
+  bool isPromotionPossible(Loop *LP,
+                           const SmallVectorImpl<BasicBlock *> &LoopExitBlocks) {
     // We can't insert into a catchswitch.
-    SmallVector<BasicBlock *, 8> LoopExitBlocks;
-    LP->getExitBlocks(LoopExitBlocks);
     if (llvm::any_of(LoopExitBlocks, [](BasicBlock *Exit) {
           return isa<CatchSwitchInst>(Exit->getTerminator());
         }))
-      return 0;
+      return false;
 
     if (!LP->hasDedicatedExits())
-      return 0;
+      return false;
 
     BasicBlock *PH = LP->getLoopPreheader();
     if (!PH)
+      return false;
+
+    return true;
+  }
+
+  // Returns the max number of Counter Promotions for LP.
+  unsigned getMaxNumOfPromotionsInLoop(Loop *LP) {
+    SmallVector<BasicBlock *, 8> LoopExitBlocks;
+    LP->getExitBlocks(LoopExitBlocks);
+    if (!isPromotionPossible(LP, LoopExitBlocks))
       return 0;
 
     SmallVector<BasicBlock *, 8> ExitingBlocks;

diff  --git a/llvm/test/Transforms/PGOProfile/counter_promo_exit_catchswitch.ll b/llvm/test/Transforms/PGOProfile/counter_promo_exit_catchswitch.ll
new file mode 100644
index 000000000000..c49d92965578
--- /dev/null
+++ b/llvm/test/Transforms/PGOProfile/counter_promo_exit_catchswitch.ll
@@ -0,0 +1,73 @@
+; Test that instrumentation counter promotion for loops does not fail during
+; compilation for loops that exit to a catchswitch block. In this case, counters
+; do not get promoted out of the loop body.
+
+; RUN: opt < %s -pgo-instr-gen -instrprof -do-counter-promotion=true -S | FileCheck %s
+; RUN: opt < %s -passes=pgo-instr-gen,instrprof -do-counter-promotion=true -S | FileCheck %s
+
+; Source used to create test:
+;
+; extern void may_throw(int);
+; char buffer[200];
+; void run(int count) {
+;   try {
+;    for (int i = 0; i < count; ++i) {
+;      if (buffer[i] == 0)
+;        break;
+;      may_throw(i);
+;    }
+;  }
+;  catch (...) {
+;     throw;
+;  }
+;}
+
+%eh.ThrowInfo = type { i32, i32, i32, i32 }
+
+@"?buffer@@3PADA" = dso_local local_unnamed_addr global [200 x i8] zeroinitializer, align 16
+define dso_local void @"?run@@YAXH at Z"(i32 %count) local_unnamed_addr personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) {
+entry:
+  br label %for.cond
+
+for.cond:                                         ; preds = %for.inc, %entry
+  %i.0 = phi i32 [ 0, %entry ], [ %inc, %for.inc ]
+  %cmp = icmp slt i32 %i.0, %count
+  br i1 %cmp, label %for.body, label %cleanup
+
+for.body:                                         ; preds = %for.cond
+; CHECK: for.body:
+; CHECK: %pgocount1 = load i64, i64* getelementptr inbounds ([3 x i64], [3 x i64]* @"__profc_?run@@YAXH at Z", i64 0, i64 0)
+; CHECK: %1 = add i64 %pgocount1, 1
+; CHECK: store i64 %1, i64* getelementptr inbounds ([3 x i64], [3 x i64]* @"__profc_?run@@YAXH at Z", i64 0, i64 0)
+  %idxprom = zext i32 %i.0 to i64
+  %arrayidx = getelementptr inbounds [200 x i8], [200 x i8]* @"?buffer@@3PADA", i64 0, i64 %idxprom
+  %0 = load i8, i8* %arrayidx, align 1
+  %cmp1 = icmp eq i8 %0, 0
+  br i1 %cmp1, label %cleanup, label %if.end
+
+if.end:                                           ; preds = %for.body
+  invoke void @"?may_throw@@YAXH at Z"(i32 %i.0)
+          to label %for.inc unwind label %catch.dispatch
+
+for.inc:                                          ; preds = %if.end
+; CHECK: for.inc:
+; CHECK: %pgocount2 = load i64, i64* getelementptr inbounds ([3 x i64], [3 x i64]* @"__profc_?run@@YAXH at Z", i64 0, i64 1)
+; CHECK: %3 = add i64 %pgocount2, 1
+; CHECK: store i64 %3, i64* getelementptr inbounds ([3 x i64], [3 x i64]* @"__profc_?run@@YAXH at Z", i64 0, i64 1)
+  %inc = add nuw nsw i32 %i.0, 1
+  br label %for.cond
+
+cleanup:                                          ; preds = %for.body, %for.cond
+  ret void
+
+catch.dispatch:                                   ; preds = %if.end
+  %1 = catchswitch within none [label %catch] unwind to caller
+
+catch:                                            ; preds = %catch.dispatch
+  %2 = catchpad within %1 [i8* null, i32 64, i8* null]
+  call void @_CxxThrowException(i8* null, %eh.ThrowInfo* null) #2 [ "funclet"(token %2) ]
+  unreachable
+}
+declare dso_local void @"?may_throw@@YAXH at Z"(i32)
+declare dso_local void @_CxxThrowException(i8*, %eh.ThrowInfo*)
+declare dso_local i32 @__CxxFrameHandler3(...)


        


More information about the llvm-commits mailing list