Skip to content

Commit bb6497f

Browse files
authored
[BPI] Reuse the AsmWriter's BB naming scheme in BranchProbabilityPrinterPass (llvm#73593)
When using `BranchProbabilityPrinterPass`, if a BB has no name, we get pretty unusable information like `edge -> has probability...` (i.e. we have no idea what the vertices of that edge are). This patch uses `printAsOperand`, which uses the same naming scheme as `Function::dump`, so for example during debugging sessions, the IR obtained from a function and the names used by `BranchProbabilityPrinterPass` will match. A shortcoming is that `printAsOperand` will result in the numbering algorithm re-running for every edge and every vertex (when `BranchProbabilityPrinterPass` is run on a function). If, for the given scenario, this is a problem, we can revisit this subsequently. Another nuance is that the entry basic block will be numbered, which may be slightly confusing when it's anonymous, but it's easily identifiable - the first edge would have it as source (and the number should be easily recognizable)
1 parent bd38203 commit bb6497f

30 files changed

+615
-583
lines changed

llvm/lib/Analysis/BranchProbabilityInfo.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,8 +1188,11 @@ BranchProbabilityInfo::printEdgeProbability(raw_ostream &OS,
11881188
const BasicBlock *Src,
11891189
const BasicBlock *Dst) const {
11901190
const BranchProbability Prob = getEdgeProbability(Src, Dst);
1191-
OS << "edge " << Src->getName() << " -> " << Dst->getName()
1192-
<< " probability is " << Prob
1191+
OS << "edge ";
1192+
Src->printAsOperand(OS, false, Src->getModule());
1193+
OS << " -> ";
1194+
Dst->printAsOperand(OS, false, Dst->getModule());
1195+
OS << " probability is " << Prob
11931196
<< (isEdgeHot(Src, Dst) ? " [HOT edge]\n" : "\n");
11941197

11951198
return OS;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_analyze_test_checks.py
2+
; RUN: opt < %s -passes='print<branch-prob>' -disable-output 2>&1 | FileCheck %s
3+
4+
define void @fct() {
5+
; CHECK-LABEL: 'fct'
6+
; CHECK-NEXT: ---- Branch Probabilities ----
7+
; CHECK-NEXT: edge %entry -> %0 probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
8+
; CHECK-NEXT: edge %0 -> %1 probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
9+
;
10+
entry:
11+
br label %0
12+
0:
13+
br label %1
14+
1:
15+
ret void
16+
}
17+
18+
define void @fct2() {
19+
; CHECK-LABEL: 'fct2'
20+
; CHECK-NEXT: ---- Branch Probabilities ----
21+
; CHECK-NEXT: edge %0 -> %1 probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
22+
; CHECK-NEXT: edge %1 -> %2 probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
23+
;
24+
br label %1
25+
1:
26+
br label %2
27+
2:
28+
ret void
29+
}

llvm/test/Analysis/BranchProbabilityInfo/basic.ll

Lines changed: 120 additions & 120 deletions
Large diffs are not rendered by default.

llvm/test/Analysis/BranchProbabilityInfo/deopt-intrinsic.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ entry:
88
%cond = icmp eq i32 %a, 42
99
br i1 %cond, label %exit, label %deopt
1010

11-
; CHECK: edge entry -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
12-
; CHECK: edge entry -> deopt probability is 0x00000000 / 0x80000000 = 0.00%
11+
; CHECK: edge %entry -> %exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
12+
; CHECK: edge %entry -> %deopt probability is 0x00000000 / 0x80000000 = 0.00%
1313

1414
deopt:
1515
%rval = call i32(...) @llvm.experimental.deoptimize.i32() [ "deopt"() ]

llvm/test/Analysis/BranchProbabilityInfo/deopt-invoke.ll

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ declare void @cold() cold
99
; Even though the likeliness of 'invoke' to throw an exception is assessed as low
1010
; all other paths are even less likely. Check that hot paths leads to excepion handler.
1111
define void @test1(i32 %0) personality ptr @"personality_function" !prof !1 {
12-
;CHECK: edge entry -> unreached probability is 0x00000001 / 0x80000000 = 0.00%
13-
;CHECK: edge entry -> invoke probability is 0x7fffffff / 0x80000000 = 100.00% [HOT edge]
14-
;CHECK: edge invoke -> invoke.cont.unreached probability is 0x00000000 / 0x80000000 = 0.00%
15-
;CHECK: edge invoke -> land.pad probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
16-
;CHECK: edge land.pad -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
12+
;CHECK: edge %entry -> %unreached probability is 0x00000001 / 0x80000000 = 0.00%
13+
;CHECK: edge %entry -> %invoke probability is 0x7fffffff / 0x80000000 = 100.00% [HOT edge]
14+
;CHECK: edge %invoke -> %invoke.cont.unreached probability is 0x00000000 / 0x80000000 = 0.00%
15+
;CHECK: edge %invoke -> %land.pad probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
16+
;CHECK: edge %land.pad -> %exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
1717

1818
entry:
1919
br i1 undef, label %unreached, label %invoke, !prof !2
@@ -39,11 +39,11 @@ exit:
3939
}
4040

4141
define void @test2(i32 %0) personality ptr @"personality_function" {
42-
;CHECK: edge entry -> unreached probability is 0x00000000 / 0x80000000 = 0.00%
43-
;CHECK: edge entry -> invoke probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
44-
;CHECK: edge invoke -> invoke.cont.cold probability is 0x7fff8000 / 0x80000000 = 100.00% [HOT edge]
45-
;CHECK: edge invoke -> land.pad probability is 0x00008000 / 0x80000000 = 0.00%
46-
;CHECK: edge land.pad -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
42+
;CHECK: edge %entry -> %unreached probability is 0x00000000 / 0x80000000 = 0.00%
43+
;CHECK: edge %entry -> %invoke probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
44+
;CHECK: edge %invoke -> %invoke.cont.cold probability is 0x7fff8000 / 0x80000000 = 100.00% [HOT edge]
45+
;CHECK: edge %invoke -> %land.pad probability is 0x00008000 / 0x80000000 = 0.00%
46+
;CHECK: edge %land.pad -> %exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
4747

4848
entry:
4949
br i1 undef, label %unreached, label %invoke
@@ -69,11 +69,11 @@ exit:
6969
}
7070

7171
define void @test3(i32 %0) personality ptr @"personality_function" {
72-
;CHECK: edge entry -> unreached probability is 0x00000000 / 0x80000000 = 0.00%
73-
;CHECK: edge entry -> invoke probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
74-
;CHECK: edge invoke -> invoke.cont.cold probability is 0x7fff8000 / 0x80000000 = 100.00% [HOT edge]
75-
;CHECK: edge invoke -> land.pad probability is 0x00008000 / 0x80000000 = 0.00%
76-
;CHECK: edge land.pad -> exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
72+
;CHECK: edge %entry -> %unreached probability is 0x00000000 / 0x80000000 = 0.00%
73+
;CHECK: edge %entry -> %invoke probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
74+
;CHECK: edge %invoke -> %invoke.cont.cold probability is 0x7fff8000 / 0x80000000 = 100.00% [HOT edge]
75+
;CHECK: edge %invoke -> %land.pad probability is 0x00008000 / 0x80000000 = 0.00%
76+
;CHECK: edge %land.pad -> %exit probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
7777
entry:
7878
br i1 undef, label %unreached, label %invoke
7979
invoke:

llvm/test/Analysis/BranchProbabilityInfo/fcmp.ll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
; This function tests the floating point unorder comparison. The probability
44
; of NaN should be extremely small.
55
; CHECK: Printing analysis {{.*}} for function 'uno'
6-
; CHECK: edge -> a probability is 0x00000800 / 0x80000000 = 0.00%
7-
; CHECK: edge -> b probability is 0x7ffff800 / 0x80000000 = 100.00% [HOT edge]
6+
; CHECK: edge %0 -> %a probability is 0x00000800 / 0x80000000 = 0.00%
7+
; CHECK: edge %0 -> %b probability is 0x7ffff800 / 0x80000000 = 100.00% [HOT edge]
88

99
define void @uno(float %val1, float %val2) {
1010
%cond = fcmp uno float %val1, %val2
@@ -21,8 +21,8 @@ b:
2121

2222
; This function tests the floating point order comparison.
2323
; CHECK: Printing analysis {{.*}} for function 'ord'
24-
; CHECK: edge -> a probability is 0x7ffff800 / 0x80000000 = 100.00% [HOT edge]
25-
; CHECK: edge -> b probability is 0x00000800 / 0x80000000 = 0.00%
24+
; CHECK: edge %0 -> %a probability is 0x7ffff800 / 0x80000000 = 100.00% [HOT edge]
25+
; CHECK: edge %0 -> %b probability is 0x00000800 / 0x80000000 = 0.00%
2626

2727
define void @ord(float %val1, float %val2) {
2828
%cond = fcmp ord float %val1, %val2

llvm/test/Analysis/BranchProbabilityInfo/hoist.ll

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
; RUN: opt < %s -passes='print<branch-prob>' -disable-output 2>&1 | FileCheck %s
22

33
; CHECK-LABEL: no_hoist
4-
; CHECK: edge entry -> if.end probability is 0x40000000 / 0x80000000 = 50.00%
5-
; CHECK: edge entry -> if.then probability is 0x40000000 / 0x80000000 = 50.00%
6-
; CHECK: edge if.end -> if.end4 probability is 0x40000000 / 0x80000000 = 50.00%
7-
; CHECK: edge if.end -> if.then3 probability is 0x40000000 / 0x80000000 = 50.00%
4+
; CHECK: edge %entry -> %if.end probability is 0x40000000 / 0x80000000 = 50.00%
5+
; CHECK: edge %entry -> %if.then probability is 0x40000000 / 0x80000000 = 50.00%
6+
; CHECK: edge %if.end -> %if.end4 probability is 0x40000000 / 0x80000000 = 50.00%
7+
; CHECK: edge %if.end -> %if.then3 probability is 0x40000000 / 0x80000000 = 50.00%
88
define dso_local void @no_hoist(i64 %arg1, i64 %arg2) local_unnamed_addr #0 {
99
entry:
1010
%and = and i64 %arg1, 1152921504606846976
@@ -29,10 +29,10 @@ if.end4: ; preds = %if.then3, %if.end
2929
}
3030

3131
; CHECK-LABEL: hoist
32-
; CHECK: edge entry -> if.end probability is 0x40000000 / 0x80000000 = 50.00%
33-
; CHECK: edge entry -> if.then probability is 0x40000000 / 0x80000000 = 50.00%
34-
; CHECK: edge if.end -> if.end4 probability is 0x40000000 / 0x80000000 = 50.00%
35-
; CHECK: edge if.end -> if.then3 probability is 0x40000000 / 0x80000000 = 50.00%
32+
; CHECK: edge %entry -> %if.end probability is 0x40000000 / 0x80000000 = 50.00%
33+
; CHECK: edge %entry -> %if.then probability is 0x40000000 / 0x80000000 = 50.00%
34+
; CHECK: edge %if.end -> %if.end4 probability is 0x40000000 / 0x80000000 = 50.00%
35+
; CHECK: edge %if.end -> %if.then3 probability is 0x40000000 / 0x80000000 = 50.00%
3636
define dso_local void @hoist(i64 %arg1, i64 %arg2) local_unnamed_addr #0 {
3737
entry:
3838
%const = bitcast i64 1152921504606846976 to i64

0 commit comments

Comments
 (0)