xref: /netbsd-src/external/gpl3/gcc/dist/contrib/analyze_brprob.py (revision f8cf1a9151c7af1cb0bd8b09c13c66bca599c027)
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
68import argparse
69
70from math import *
71
72counter_aggregates = set(['combined', 'first match', 'DS theory',
73    'no prediction'])
74hot_threshold = 10
75
76def percentage(a, b):
77    return 100.0 * a / b
78
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
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 = []
104        for i, l in enumerate(lines):
105            if l.startswith('DEF_PREDICTOR'):
106                next_line = lines[i + 1]
107                if l.endswith(','):
108                    l += next_line
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')
138class Heuristics:
139    def __init__(self, count, hits, fits):
140        self.count = count
141        self.hits = hits
142        self.fits = fits
143
144class Summary:
145    def __init__(self, name):
146        self.name = name
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])
163
164    def get_hitrate(self):
165        return 100.0 * self.hits() / self.count()
166
167    def get_branch_hitrate(self):
168        return 100.0 * self.successfull_branches() / self.branches()
169
170    def count_formatted(self):
171        v = self.count()
172        for unit in ['', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y']:
173            if v < 1000:
174                return "%3.2f%s" % (v, unit)
175            v /= 1000.0
176        return "%.1f%s" % (v, 'Y')
177
178    def count(self):
179        return sum([x.count for x in self.edges])
180
181    def print(self, branches_max, count_max, predict_def):
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
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
194        print('%-40s %8i %5.1f%% %11.2f%% %7.2f%% / %6.2f%% %14i %8s %5.1f%%' %
195            (self.name, self.branches(),
196                percentage(self.branches(), branches_max),
197                self.get_branch_hitrate(),
198                self.get_hitrate(),
199                percentage(self.fits(), self.count()),
200                self.count(), self.count_formatted(),
201                percentage(self.count(), count_max)), end = '')
202
203        if predicted_as != None:
204            print('%12i%% %5.1f%%' % (predicted_as,
205                self.get_hitrate() - predicted_as), end = '')
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
219        print()
220
221class Profile:
222    def __init__(self, filename):
223        self.filename = filename
224        self.heuristics = {}
225        self.niter_vector = []
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]
232
233        if prediction < 50:
234            hits = count - hits
235        remaining = count - hits
236        fits = max(hits, remaining)
237
238        s.edges.append(Heuristics(count, hits, fits))
239
240    def add_loop_niter(self, niter):
241        if niter > 0:
242            self.niter_vector.append(niter)
243
244    def branches_max(self):
245        return max([v.branches() for k, v in self.heuristics.items()])
246
247    def count_max(self):
248        return max([v.count() for k, v in self.heuristics.items()])
249
250    def print_group(self, sorting, group_name, heuristics, predict_def):
251        count_max = self.count_max()
252        branches_max = self.branches_max()
253
254        sorter = lambda x: x.branches()
255        if sorting == 'branch-hitrate':
256            sorter = lambda x: x.get_branch_hitrate()
257        elif sorting == 'hitrate':
258            sorter = lambda x: x.get_hitrate()
259        elif sorting == 'coverage':
260            sorter = lambda x: x.count
261        elif sorting == 'name':
262            sorter = lambda x: x.name.lower()
263
264        print('%-40s %8s %6s %12s %18s %14s %8s %6s %12s %6s %s' %
265            ('HEURISTICS', 'BRANCHES', '(REL)',
266            'BR. HITRATE', 'HITRATE', 'COVERAGE', 'COVERAGE', '(REL)',
267            'predict.def', '(REL)', 'HOT branches (>%d%%)' % hot_threshold))
268        for h in sorted(heuristics, key = sorter):
269            h.print(branches_max, count_max, predict_def)
270
271    def dump(self, sorting):
272        heuristics = self.heuristics.values()
273        if len(heuristics) == 0:
274            print('No heuristics available')
275            return
276
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
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))
286
287        self.print_group(sorting, 'HEURISTICS', normal, predict_def)
288        print()
289        self.print_group(sorting, 'HEURISTIC AGGREGATES', special, predict_def)
290
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
297                print('  avg. (%d%% cutoff) # of iter: %.2f'
298                    % (v, average_cutoff(self.niter_vector, cut)))
299
300parser = argparse.ArgumentParser()
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')
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')
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')
312
313args = parser.parse_args()
314
315profile = Profile(args.dump_file)
316loop_niter_str = ';;  profile-based iteration count: '
317
318for l in open(args.dump_file):
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])
326
327        profile.add(name, prediction, count, hits)
328    elif l.startswith(loop_niter_str):
329        v = int(l[len(loop_niter_str):])
330        profile.add_loop_niter(v)
331
332profile.dump(args.sorting)
333