]> git.ipfire.org Git - thirdparty/gcc.git/blame - contrib/analyze_brprob.py
Make pointer_query cache a private member.
[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 69
199b1891
ML
70from math import *
71
ca3b6071
ML
72counter_aggregates = set(['combined', 'first match', 'DS theory',
73 'no prediction'])
d1b9a572 74hot_threshold = 10
ca3b6071 75
4877829b
ML
76def percentage(a, b):
77 return 100.0 * a / b
78
199b1891
ML
79def average(values):
80 return 1.0 * sum(values) / len(values)
81
82def average_cutoff(values, cut):
83 l = len(values)
84 skip = floor(l * cut / 2)
85 if skip > 0:
86 values.sort()
87 values = values[skip:-skip]
88 return average(values)
89
90def median(values):
91 values.sort()
92 return values[int(len(values) / 2)]
93
59075bc8
ML
94class PredictDefFile:
95 def __init__(self, path):
96 self.path = path
97 self.predictors = {}
98
99 def parse_and_modify(self, heuristics, write_def_file):
100 lines = [x.rstrip() for x in open(self.path).readlines()]
101
102 p = None
103 modified_lines = []
31ab99f7 104 for i, l in enumerate(lines):
59075bc8 105 if l.startswith('DEF_PREDICTOR'):
31ab99f7
ML
106 next_line = lines[i + 1]
107 if l.endswith(','):
108 l += next_line
59075bc8
ML
109 m = re.match('.*"(.*)".*', l)
110 p = m.group(1)
111 elif l == '':
112 p = None
113
114 if p != None:
115 heuristic = [x for x in heuristics if x.name == p]
116 heuristic = heuristic[0] if len(heuristic) == 1 else None
117
118 m = re.match('.*HITRATE \(([^)]*)\).*', l)
119 if (m != None):
120 self.predictors[p] = int(m.group(1))
121
122 # modify the line
123 if heuristic != None:
124 new_line = (l[:m.start(1)]
125 + str(round(heuristic.get_hitrate()))
126 + l[m.end(1):])
127 l = new_line
128 p = None
129 elif 'PROB_VERY_LIKELY' in l:
130 self.predictors[p] = 100
131 modified_lines.append(l)
132
133 # save the file
134 if write_def_file:
135 with open(self.path, 'w+') as f:
136 for l in modified_lines:
137 f.write(l + '\n')
d1b9a572
ML
138class Heuristics:
139 def __init__(self, count, hits, fits):
140 self.count = count
141 self.hits = hits
142 self.fits = fits
59075bc8 143
4877829b
ML
144class Summary:
145 def __init__(self, name):
146 self.name = name
d1b9a572
ML
147 self.edges= []
148
149 def branches(self):
150 return len(self.edges)
151
152 def hits(self):
153 return sum([x.hits for x in self.edges])
154
155 def fits(self):
156 return sum([x.fits for x in self.edges])
157
158 def count(self):
159 return sum([x.count for x in self.edges])
160
161 def successfull_branches(self):
162 return len([x for x in self.edges if 2 * x.hits >= x.count])
4877829b 163
0d73e480 164 def get_hitrate(self):
d1b9a572 165 return 100.0 * self.hits() / self.count()
ca3b6071
ML
166
167 def get_branch_hitrate(self):
d1b9a572 168 return 100.0 * self.successfull_branches() / self.branches()
0d73e480 169
4877829b 170 def count_formatted(self):
d1b9a572 171 v = self.count()
caba2b36 172 for unit in ['', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y']:
4877829b
ML
173 if v < 1000:
174 return "%3.2f%s" % (v, unit)
175 v /= 1000.0
176 return "%.1f%s" % (v, 'Y')
177
d1b9a572
ML
178 def count(self):
179 return sum([x.count for x in self.edges])
180
59075bc8 181 def print(self, branches_max, count_max, predict_def):
d1b9a572
ML
182 # filter out most hot edges (if requested)
183 self.edges = sorted(self.edges, reverse = True, key = lambda x: x.count)
184 if args.coverage_threshold != None:
185 threshold = args.coverage_threshold * self.count() / 100
186 edges = [x for x in self.edges if x.count < threshold]
187 if len(edges) != 0:
188 self.edges = edges
189
59075bc8
ML
190 predicted_as = None
191 if predict_def != None and self.name in predict_def.predictors:
192 predicted_as = predict_def.predictors[self.name]
193
ca3b6071 194 print('%-40s %8i %5.1f%% %11.2f%% %7.2f%% / %6.2f%% %14i %8s %5.1f%%' %
d1b9a572
ML
195 (self.name, self.branches(),
196 percentage(self.branches(), branches_max),
ca3b6071
ML
197 self.get_branch_hitrate(),
198 self.get_hitrate(),
d1b9a572
ML
199 percentage(self.fits(), self.count()),
200 self.count(), self.count_formatted(),
201 percentage(self.count(), count_max)), end = '')
59075bc8
ML
202
203 if predicted_as != None:
204 print('%12i%% %5.1f%%' % (predicted_as,
205 self.get_hitrate() - predicted_as), end = '')
d1b9a572
ML
206 else:
207 print(' ' * 20, end = '')
208
209 # print details about the most important edges
210 if args.coverage_threshold == None:
211 edges = [x for x in self.edges[:100] if x.count * hot_threshold > self.count()]
212 if args.verbose:
213 for c in edges:
214 r = 100.0 * c.count / self.count()
215 print(' %.0f%%:%d' % (r, c.count), end = '')
216 elif len(edges) > 0:
217 print(' %0.0f%%:%d' % (100.0 * sum([x.count for x in edges]) / self.count(), len(edges)), end = '')
218
59075bc8 219 print()
ca3b6071 220
4877829b
ML
221class Profile:
222 def __init__(self, filename):
223 self.filename = filename
224 self.heuristics = {}
199b1891 225 self.niter_vector = []
4877829b
ML
226
227 def add(self, name, prediction, count, hits):
228 if not name in self.heuristics:
229 self.heuristics[name] = Summary(name)
230
231 s = self.heuristics[name]
ca3b6071 232
4877829b
ML
233 if prediction < 50:
234 hits = count - hits
ca3b6071 235 remaining = count - hits
d1b9a572 236 fits = max(hits, remaining)
ca3b6071 237
d1b9a572 238 s.edges.append(Heuristics(count, hits, fits))
4877829b 239
199b1891
ML
240 def add_loop_niter(self, niter):
241 if niter > 0:
242 self.niter_vector.append(niter)
243
4877829b 244 def branches_max(self):
d1b9a572 245 return max([v.branches() for k, v in self.heuristics.items()])
4877829b
ML
246
247 def count_max(self):
d1b9a572 248 return max([v.count() for k, v in self.heuristics.items()])
4877829b 249
59075bc8 250 def print_group(self, sorting, group_name, heuristics, predict_def):
ca3b6071
ML
251 count_max = self.count_max()
252 branches_max = self.branches_max()
253
d1b9a572 254 sorter = lambda x: x.branches()
ca3b6071
ML
255 if sorting == 'branch-hitrate':
256 sorter = lambda x: x.get_branch_hitrate()
257 elif sorting == 'hitrate':
258 sorter = lambda x: x.get_hitrate()
0d73e480 259 elif sorting == 'coverage':
ca3b6071
ML
260 sorter = lambda x: x.count
261 elif sorting == 'name':
262 sorter = lambda x: x.name.lower()
263
d1b9a572 264 print('%-40s %8s %6s %12s %18s %14s %8s %6s %12s %6s %s' %
ca3b6071 265 ('HEURISTICS', 'BRANCHES', '(REL)',
59075bc8 266 'BR. HITRATE', 'HITRATE', 'COVERAGE', 'COVERAGE', '(REL)',
d1b9a572 267 'predict.def', '(REL)', 'HOT branches (>%d%%)' % hot_threshold))
ca3b6071 268 for h in sorted(heuristics, key = sorter):
59075bc8 269 h.print(branches_max, count_max, predict_def)
ca3b6071
ML
270
271 def dump(self, sorting):
272 heuristics = self.heuristics.values()
273 if len(heuristics) == 0:
274 print('No heuristics available')
275 return
276
59075bc8
ML
277 predict_def = None
278 if args.def_file != None:
279 predict_def = PredictDefFile(args.def_file)
280 predict_def.parse_and_modify(heuristics, args.write_def_file)
281
ca3b6071
ML
282 special = list(filter(lambda x: x.name in counter_aggregates,
283 heuristics))
284 normal = list(filter(lambda x: x.name not in counter_aggregates,
285 heuristics))
0d73e480 286
59075bc8 287 self.print_group(sorting, 'HEURISTICS', normal, predict_def)
ca3b6071 288 print()
59075bc8 289 self.print_group(sorting, 'HEURISTIC AGGREGATES', special, predict_def)
4877829b 290
88617fe4
ML
291 if len(self.niter_vector) > 0:
292 print ('\nLoop count: %d' % len(self.niter_vector)),
293 print(' avg. # of iter: %.2f' % average(self.niter_vector))
294 print(' median # of iter: %.2f' % median(self.niter_vector))
295 for v in [1, 5, 10, 20, 30]:
296 cut = 0.01 * v
ca3b6071
ML
297 print(' avg. (%d%% cutoff) # of iter: %.2f'
298 % (v, average_cutoff(self.niter_vector, cut)))
199b1891 299
0d73e480 300parser = argparse.ArgumentParser()
ca3b6071
ML
301parser.add_argument('dump_file', metavar = 'dump_file',
302 help = 'IPA profile dump file')
303parser.add_argument('-s', '--sorting', dest = 'sorting',
304 choices = ['branches', 'branch-hitrate', 'hitrate', 'coverage', 'name'],
305 default = 'branches')
59075bc8
ML
306parser.add_argument('-d', '--def-file', help = 'path to predict.def')
307parser.add_argument('-w', '--write-def-file', action = 'store_true',
308 help = 'Modify predict.def file in order to set new numbers')
d1b9a572
ML
309parser.add_argument('-c', '--coverage-threshold', type = int,
310 help = 'Ignore edges that have percentage coverage >= coverage-threshold')
311parser.add_argument('-v', '--verbose', action = 'store_true', help = 'Print verbose informations')
0d73e480
ML
312
313args = parser.parse_args()
4877829b 314
59075bc8 315profile = Profile(args.dump_file)
199b1891 316loop_niter_str = ';; profile-based iteration count: '
d1b9a572 317
59075bc8 318for l in open(args.dump_file):
d1b9a572
ML
319 if l.startswith(';;heuristics;'):
320 parts = l.strip().split(';')
321 assert len(parts) == 8
322 name = parts[3]
323 prediction = float(parts[6])
324 count = int(parts[4])
325 hits = int(parts[5])
4877829b
ML
326
327 profile.add(name, prediction, count, hits)
199b1891
ML
328 elif l.startswith(loop_niter_str):
329 v = int(l[len(loop_niter_str):])
330 profile.add_loop_niter(v)
4877829b 331
0d73e480 332profile.dump(args.sorting)