3 # Script to analyze results of our branch prediction heuristics
5 # This file is part of GCC.
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
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
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/>. */
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.
28 # These values are useful to determine the quality of given heuristics.
29 # Hitrate may be directly used in predict.def.
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:
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%
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
76 return 1.0 * sum(values
) / len(values
)
78 def average_cutoff(values
, cut
):
80 skip
= floor(l
* cut
/ 2)
83 values
= values
[skip
:-skip
]
84 return average(values
)
88 return values
[int(len(values
) / 2)]
91 def __init__(self
, name
):
98 def get_hitrate(self
):
99 return self
.hits
/ self
.count
101 def count_formatted(self
):
103 for unit
in ['','K','M','G','T','P','E','Z']:
105 return "%3.2f%s" % (v
, unit
)
107 return "%.1f%s" % (v
, 'Y')
110 def __init__(self
, filename
):
111 self
.filename
= filename
113 self
.niter_vector
= []
115 def add(self
, name
, prediction
, count
, hits
):
116 if not name
in self
.heuristics
:
117 self
.heuristics
[name
] = Summary(name
)
119 s
= self
.heuristics
[name
]
125 s
.fits
+= max(hits
, count
- hits
)
127 def add_loop_niter(self
, niter
):
129 self
.niter_vector
.append(niter
)
131 def branches_max(self
):
132 return max([v
.branches
for k
, v
in self
.heuristics
.items()])
135 return max([v
.count
for k
, v
in self
.heuristics
.items()])
137 def dump(self
, sorting
):
138 sorter
= lambda x
: x
[1].branches
139 if sorting
== 'hitrate':
140 sorter
= lambda x
: x
[1].get_hitrate()
141 elif sorting
== 'coverage':
142 sorter
= lambda x
: x
[1].count
144 print('%-40s %8s %6s %-16s %14s %8s %6s' % ('HEURISTICS', 'BRANCHES', '(REL)',
145 'HITRATE', 'COVERAGE', 'COVERAGE', '(REL)'))
146 for (k
, v
) in sorted(self
.heuristics
.items(), key
= sorter
):
147 print('%-40s %8i %5.1f%% %6.2f%% / %6.2f%% %14i %8s %5.1f%%' %
148 (k
, v
.branches
, percentage(v
.branches
, self
.branches_max ()),
149 percentage(v
.hits
, v
.count
), percentage(v
.fits
, v
.count
),
150 v
.count
, v
.count_formatted(), percentage(v
.count
, self
.count_max()) ))
152 if len(self
.niter_vector
) > 0:
153 print ('\nLoop count: %d' % len(self
.niter_vector
)),
154 print(' avg. # of iter: %.2f' % average(self
.niter_vector
))
155 print(' median # of iter: %.2f' % median(self
.niter_vector
))
156 for v
in [1, 5, 10, 20, 30]:
158 print(' avg. (%d%% cutoff) # of iter: %.2f' % (v
, average_cutoff(self
.niter_vector
, cut
)))
160 parser
= argparse
.ArgumentParser()
161 parser
.add_argument('dump_file', metavar
= 'dump_file', help = 'IPA profile dump file')
162 parser
.add_argument('-s', '--sorting', dest
= 'sorting', choices
= ['branches', 'hitrate', 'coverage'], default
= 'branches')
164 args
= parser
.parse_args()
166 profile
= Profile(sys
.argv
[1])
167 r
= re
.compile(' (.*) heuristics( of edge [0-9]*->[0-9]*)?( \\(.*\\))?: (.*)%.*exec ([0-9]*) hit ([0-9]*)')
168 loop_niter_str
= ';; profile-based iteration count: '
169 for l
in open(args
.dump_file
).readlines():
171 if m
!= None and m
.group(3) == None:
173 prediction
= float(m
.group(4))
174 count
= int(m
.group(5))
175 hits
= int(m
.group(6))
177 profile
.add(name
, prediction
, count
, hits
)
178 elif l
.startswith(loop_niter_str
):
179 v
= int(l
[len(loop_niter_str
):])
180 profile
.add_loop_niter(v
)
182 profile
.dump(args
.sorting
)