Skip to content

Commit d7a93cb

Browse files
authored
summarize_stats.py: add pairs by opcode (GH-31957)
1 parent 48d9262 commit d7a93cb

File tree

1 file changed

+44
-9
lines changed

1 file changed

+44
-9
lines changed

Tools/scripts/summarize_stats.py

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525
pass
2626
opname.append(name)
2727

28+
# opcode_name --> opcode
29+
# Sort alphabetically.
30+
opmap = {name: i for i, name in enumerate(opname)}
31+
opmap = dict(sorted(opmap.items()))
32+
2833
TOTAL = "specialization.deferred", "specialization.hit", "specialization.miss", "execution_count"
2934

3035
def print_specialization_stats(name, family_stats, defines):
@@ -281,16 +286,16 @@ def get_total(opcode_stats):
281286
return total
282287

283288
def emit_pair_counts(opcode_stats, total):
289+
pair_counts = []
290+
for i, opcode_stat in enumerate(opcode_stats):
291+
if i == 0:
292+
continue
293+
for key, value in opcode_stat.items():
294+
if key.startswith("pair_count"):
295+
x, _, _ = key[11:].partition("]")
296+
if value:
297+
pair_counts.append((value, (i, int(x))))
284298
with Section("Pair counts", summary="Pair counts for top 100 pairs"):
285-
pair_counts = []
286-
for i, opcode_stat in enumerate(opcode_stats):
287-
if i == 0:
288-
continue
289-
for key, value in opcode_stat.items():
290-
if key.startswith("pair_count"):
291-
x, _, _ = key[11:].partition("]")
292-
if value:
293-
pair_counts.append((value, (i, int(x))))
294299
pair_counts.sort(reverse=True)
295300
cumulative = 0
296301
rows = []
@@ -302,6 +307,36 @@ def emit_pair_counts(opcode_stats, total):
302307
emit_table(("Pair", "Count:", "Self:", "Cumulative:"),
303308
rows
304309
)
310+
with Section("Predecessor/Successor Pairs", summary="Top 3 predecessors and successors of each opcode"):
311+
predecessors = collections.defaultdict(collections.Counter)
312+
successors = collections.defaultdict(collections.Counter)
313+
total_predecessors = collections.Counter()
314+
total_successors = collections.Counter()
315+
for count, (first, second) in pair_counts:
316+
if count:
317+
predecessors[second][first] = count
318+
successors[first][second] = count
319+
total_predecessors[second] += count
320+
total_successors[first] += count
321+
for name, i in opmap.items():
322+
total1 = total_predecessors[i]
323+
total2 = total_successors[i]
324+
if total1 == 0 and total2 == 0:
325+
continue
326+
pred_rows = succ_rows = ()
327+
if total1:
328+
pred_rows = [(opname[pred], count, f"{count/total1:.1%}")
329+
for (pred, count) in predecessors[i].most_common(3)]
330+
if total2:
331+
succ_rows = [(opname[succ], count, f"{count/total2:.1%}")
332+
for (succ, count) in successors[i].most_common(3)]
333+
with Section(name, 3, f"Successors and predecessors for {name}"):
334+
emit_table(("Predecessors", "Count:", "Percentage:"),
335+
pred_rows
336+
)
337+
emit_table(("Successors", "Count:", "Percentage:"),
338+
succ_rows
339+
)
305340

306341
def main():
307342
stats = gather_stats()

0 commit comments

Comments
 (0)