]> git.ipfire.org Git - thirdparty/gcc.git/blame - contrib/analyze_brprob.py
Add sorting support to analyze_brprob script
[thirdparty/gcc.git] / contrib / analyze_brprob.py
CommitLineData
4877829b
ML
1#!/usr/bin/env python3
2#
3# Script to analyze results of our branch prediction heuristics
4#
5# This file is part of GCC.
6#
7# GCC is free software; you can redistribute it and/or modify it under
8# the terms of the GNU General Public License as published by the Free
9# Software Foundation; either version 3, or (at your option) any later
10# version.
11#
12# GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13# WARRANTY; without even the implied warranty of MERCHANTABILITY or
14# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15# for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with GCC; see the file COPYING3. If not see
19# <http://www.gnu.org/licenses/>. */
20#
21#
22#
23# This script is used to calculate two basic properties of the branch prediction
24# heuristics - coverage and hitrate. Coverage is number of executions
25# of a given branch matched by the heuristics and hitrate is probability
26# that once branch is predicted as taken it is really taken.
27#
28# These values are useful to determine the quality of given heuristics.
29# Hitrate may be directly used in predict.def.
30#
31# Usage:
32# Step 1: Compile and profile your program. You need to use -fprofile-generate
33# flag to get the profiles.
34# Step 2: Make a reference run of the intrumented application.
35# Step 3: Compile the program with collected profile and dump IPA profiles
36# (-fprofile-use -fdump-ipa-profile-details)
37# Step 4: Collect all generated dump files:
38# find . -name '*.profile' | xargs cat > dump_file
39# Step 5: Run the script:
40# ./analyze_brprob.py dump_file
41# and read results. Basically the following table is printed:
42#
43# HEURISTICS BRANCHES (REL) HITRATE COVERAGE (REL)
44# early return (on trees) 3 0.2% 35.83% / 93.64% 66360 0.0%
45# guess loop iv compare 8 0.6% 53.35% / 53.73% 11183344 0.0%
46# call 18 1.4% 31.95% / 69.95% 51880179 0.2%
47# loop guard 23 1.8% 84.13% / 84.85% 13749065956 42.2%
48# opcode values positive (on trees) 42 3.3% 15.71% / 84.81% 6771097902 20.8%
49# opcode values nonequal (on trees) 226 17.6% 72.48% / 72.84% 844753864 2.6%
50# loop exit 231 18.0% 86.97% / 86.98% 8952666897 27.5%
51# loop iterations 239 18.6% 91.10% / 91.10% 3062707264 9.4%
52# DS theory 281 21.9% 82.08% / 83.39% 7787264075 23.9%
53# no prediction 293 22.9% 46.92% / 70.70% 2293267840 7.0%
54# guessed loop iterations 313 24.4% 76.41% / 76.41% 10782750177 33.1%
55# first match 708 55.2% 82.30% / 82.31% 22489588691 69.0%
56# combined 1282 100.0% 79.76% / 81.75% 32570120606 100.0%
57#
58#
59# The heuristics called "first match" is a heuristics used by GCC branch
60# prediction pass and it predicts 55.2% branches correctly. As you can,
61# the heuristics has very good covertage (69.05%). On the other hand,
62# "opcode values nonequal (on trees)" heuristics has good hirate, but poor
63# coverage.
64
65import sys
66import os
67import re
0d73e480 68import argparse
4877829b
ML
69
70def percentage(a, b):
71 return 100.0 * a / b
72
73class Summary:
74 def __init__(self, name):
75 self.name = name
76 self.branches = 0
77 self.count = 0
78 self.hits = 0
79 self.fits = 0
80
0d73e480
ML
81 def get_hitrate(self):
82 return self.hits / self.count
83
4877829b
ML
84 def count_formatted(self):
85 v = self.count
86 for unit in ['','K','M','G','T','P','E','Z']:
87 if v < 1000:
88 return "%3.2f%s" % (v, unit)
89 v /= 1000.0
90 return "%.1f%s" % (v, 'Y')
91
92class Profile:
93 def __init__(self, filename):
94 self.filename = filename
95 self.heuristics = {}
96
97 def add(self, name, prediction, count, hits):
98 if not name in self.heuristics:
99 self.heuristics[name] = Summary(name)
100
101 s = self.heuristics[name]
102 s.branches += 1
103 s.count += count
104 if prediction < 50:
105 hits = count - hits
106 s.hits += hits
107 s.fits += max(hits, count - hits)
108
109 def branches_max(self):
110 return max([v.branches for k, v in self.heuristics.items()])
111
112 def count_max(self):
113 return max([v.count for k, v in self.heuristics.items()])
114
0d73e480
ML
115 def dump(self, sorting):
116 sorter = lambda x: x[1].branches
117 if sorting == 'hitrate':
118 sorter = lambda x: x[1].get_hitrate()
119 elif sorting == 'coverage':
120 sorter = lambda x: x[1].count
121
4877829b
ML
122 print('%-36s %8s %6s %-16s %14s %8s %6s' % ('HEURISTICS', 'BRANCHES', '(REL)',
123 'HITRATE', 'COVERAGE', 'COVERAGE', '(REL)'))
0d73e480 124 for (k, v) in sorted(self.heuristics.items(), key = sorter):
4877829b
ML
125 print('%-36s %8i %5.1f%% %6.2f%% / %6.2f%% %14i %8s %5.1f%%' %
126 (k, v.branches, percentage(v.branches, self.branches_max ()),
127 percentage(v.hits, v.count), percentage(v.fits, v.count),
128 v.count, v.count_formatted(), percentage(v.count, self.count_max()) ))
129
0d73e480
ML
130parser = argparse.ArgumentParser()
131parser.add_argument('dump_file', metavar = 'dump_file', help = 'IPA profile dump file')
132parser.add_argument('-s', '--sorting', dest = 'sorting', choices = ['branches', 'hitrate', 'coverage'], default = 'branches')
133
134args = parser.parse_args()
4877829b
ML
135
136profile = Profile(sys.argv[1])
e49efc14 137r = re.compile(' (.*) heuristics( of edge [0-9]*->[0-9]*)?( \\(.*\\))?: (.*)%.*exec ([0-9]*) hit ([0-9]*)')
0d73e480 138for l in open(args.dump_file).readlines():
4877829b 139 m = r.match(l)
e49efc14 140 if m != None and m.group(3) == None:
4877829b 141 name = m.group(1)
e49efc14
ML
142 prediction = float(m.group(4))
143 count = int(m.group(5))
144 hits = int(m.group(6))
4877829b
ML
145
146 profile.add(name, prediction, count, hits)
147
0d73e480 148profile.dump(args.sorting)