[PATCH] D37074: [PGO] Set edge weights for indirectbr instruction with profile counts

Rong Xu via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 23 12:30:17 PDT 2017


xur updated this revision to Diff 112420.
xur edited the summary of this revision.

https://reviews.llvm.org/D37074

Files:
  lib/Analysis/BranchProbabilityInfo.cpp
  lib/Transforms/Instrumentation/PGOInstrumentation.cpp
  test/Transforms/PGOProfile/Inputs/indirectbr.proftext
  test/Transforms/PGOProfile/indirectbr.ll


Index: test/Transforms/PGOProfile/indirectbr.ll
===================================================================
--- test/Transforms/PGOProfile/indirectbr.ll
+++ test/Transforms/PGOProfile/indirectbr.ll
@@ -0,0 +1,36 @@
+; RUN: llvm-profdata merge %S/Inputs/indirectbr.proftext -o %t.profdata
+; RUN: opt < %s -pgo-instr-use -pgo-test-profile-file=%t.profdata -S | FileCheck %s --check-prefix=USE
+; New PM
+; RUN: opt < %s -passes=pgo-instr-use -pgo-test-profile-file=%t.profdata -S | FileCheck %s --check-prefix=USE
+
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+ at foo.table = internal unnamed_addr constant [3 x i8*] [i8* blockaddress(@foo, %return), i8* blockaddress(@foo, %label2), i8* blockaddress(@foo, %label3)], align 16
+
+define i32 @foo(i32 %i) {
+entry:
+  %cmp = icmp ult i32 %i, 3
+  br i1 %cmp, label %if.then, label %return
+
+if.then:
+  %idxprom = zext i32 %i to i64
+  %arrayidx = getelementptr inbounds [3 x i8*], [3 x i8*]* @foo.table, i64 0, i64 %idxprom
+  %0 = load i8*, i8** %arrayidx, align 8
+  indirectbr i8* %0, [label %return, label %label2, label %label3]
+; USE:  indirectbr i8* %0, [label %return, label %label2, label %label3]
+; USE-SAME: !prof ![[BW_INDBR:[0-9]+]]
+; USE: ![[BW_INDBR]] = !{!"branch_weights", i32 63, i32 20, i32 5}
+
+label2:
+  br label %return
+
+label3:
+  br label %return
+
+return:
+  %retval.0 = phi i32 [ 3, %label3 ], [ 2, %label2 ], [ 0, %entry ], [ 1, %if.then ]
+  ret i32 %retval.0
+}
+
+
Index: test/Transforms/PGOProfile/Inputs/indirectbr.proftext
===================================================================
--- test/Transforms/PGOProfile/Inputs/indirectbr.proftext
+++ test/Transforms/PGOProfile/Inputs/indirectbr.proftext
@@ -0,0 +1,13 @@
+# IR level Instrumentation Flag
+:ir
+foo
+# Func Hash:
+40197883220
+# Num Counters:
+4
+# Counter Values:
+202
+88
+20
+5
+
Index: lib/Transforms/Instrumentation/PGOInstrumentation.cpp
===================================================================
--- lib/Transforms/Instrumentation/PGOInstrumentation.cpp
+++ lib/Transforms/Instrumentation/PGOInstrumentation.cpp
@@ -1094,7 +1094,8 @@
     TerminatorInst *TI = BB.getTerminator();
     if (TI->getNumSuccessors() < 2)
       continue;
-    if (!isa<BranchInst>(TI) && !isa<SwitchInst>(TI))
+    if (!(isa<BranchInst>(TI) || isa<SwitchInst>(TI) ||
+          isa<IndirectBrInst>(TI)))
       continue;
     if (getBBInfo(&BB).CountValue == 0)
       continue;
Index: lib/Analysis/BranchProbabilityInfo.cpp
===================================================================
--- lib/Analysis/BranchProbabilityInfo.cpp
+++ lib/Analysis/BranchProbabilityInfo.cpp
@@ -237,7 +237,7 @@
 bool BranchProbabilityInfo::calcMetadataWeights(const BasicBlock *BB) {
   const TerminatorInst *TI = BB->getTerminator();
   assert(TI->getNumSuccessors() > 1 && "expected more than one successor!");
-  if (!isa<BranchInst>(TI) && !isa<SwitchInst>(TI))
+  if (!(isa<BranchInst>(TI) || isa<SwitchInst>(TI) || isa<IndirectBrInst>(TI)))
     return false;
 
   MDNode *WeightsNode = TI->getMetadata(LLVMContext::MD_prof);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D37074.112420.patch
Type: text/x-patch
Size: 3143 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170823/b584961b/attachment.bin>


More information about the llvm-commits mailing list