8000 gh-115178: Add Counts of UOp Pairs to pystats (GH-115181) · python/cpython@acf69e0 · GitHub
[go: up one dir, main page]

Skip to content

Commit acf69e0

Browse files

authored
gh-115178: Add Counts of UOp Pairs to pystats (GH-115181)
1 parent c053d52 commit acf69e0

File tree

6 files changed

+26
-4
lines changed

6 files changed

+26
-4
lines changed

Include/cpython/pystats.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ typedef struct _gc_stats {
100100
typedef struct _uop_stats {
101101
uint64_t execution_count;
102102
uint64_t miss;
103+
uint64_t pair_count[MAX_UOP_ID + 1];
103104
} UOpStats;
104105

105106
#define _Py_UOP_HIST_SIZE 32

Include/internal/pycore_code.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,13 @@ extern int _PyStaticCode_Init(PyCodeObject *co);
310310
#define GC_STAT_ADD(gen, name, n) do { if (_Py_stats) _Py_stats->gc_stats[(gen)].name += (n); } while (0)
311311
#define OPT_STAT_INC(name) do { if (_Py_stats) _Py_stats->optimization_stats.name++; } while (0)
312312
#define UOP_STAT_INC(opname, name) do { if (_Py_stats) { assert(opname < 512); _Py_stats->optimization_stats.opcode[opname].name++; } } while (0)
313+
#define UOP_PAIR_INC(uopcode, lastuop) \
314+
do { \
315+
if (lastuop && _Py_stats) { \
316+
_Py_stats->optimization_stats.opcode[lastuop].pair_count[uopcode]++; \
317+
} \
318+
lastuop = uopcode; \
319+
} while (0)
313320
#define OPT_UNSUPPORTED_OPCODE(opname) do { if (_Py_stats) _Py_stats->optimization_stats.unsupported_opcode[opname]++; } while (0)
314321
#define OPT_ERROR_IN_OPCODE(opname) do { if (_Py_stats) _Py_stats->optimization_stats.error_in_opcode[opname]++; } while (0)
315322
#define OPT_HIST(length, name) \
@@ -337,6 +344,7 @@ PyAPI_FUNC(PyObject*) _Py_GetSpecializationStats(void);
337344
#define GC_STAT_ADD(gen, name, n) ((void)0)
338345
#define OPT_STAT_INC(name) ((void)0)
339346
#define UOP_STAT_INC(opname, name) ((void)0)
347+
#define UOP_PAIR_INC(uopcode, lastuop) ((void)0)
340348
#define OPT_UNSUPPORTED_OPCODE(opname) ((void)0)
341349
#define OPT_ERROR_IN_OPCODE(opname) ((void)0)
342350
#define OPT_HIST(length, name) ((void)0)

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,7 @@ Neil Girdhar
642642
Matt Giuca
643643
Andrea Giudiceandrea
644644
Franz Glasner
645+
Jeff Glass
645646
Wim Glenn
646647
Michael Goderbauer
647648
Karan Goel

Python/ceval.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -995,6 +995,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
995995
; // dummy statement after a label, before a declaration
996996
uint16_t uopcode;
997997
#ifdef Py_STATS
998+
int lastuop = 0;
998999
uint64_t trace_uop_execution_counter = 0;
9991000
#endif
10001001

@@ -1018,6 +1019,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
10181019
next_uop++;
10191020
OPT_STAT_INC(uops_executed);
10201021
UOP_STAT_INC(uopcode, execution_count);
1022+
UOP_PAIR_INC(uopcode, lastuop);
10211023
#ifdef Py_STATS
10221024
trace_uop_execution_counter++;
10231025
#endif

Python/specialize.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "pycore_object.h"
1212
#include "pycore_opcode_metadata.h" // _PyOpcode_Caches
1313
#include "pycore_uop_metadata.h" // _PyOpcode_uop_name
14+
#include "pycore_uop_ids.h" // MAX_UOP_ID
1415
#include "pycore_opcode_utils.h" // RESUME_AT_FUNC_START
1516
#include "pycore_pylifecycle.h" // _PyOS_URandomNonblock()
1617
#include "pycore_runtime.h" // _Py_ID()
@@ -269,6 +270,14 @@ print_optimization_stats(FILE *out, OptimizationStats *stats)
269270
}
270271
}
271272

273+
for (int i = 1; i <= MAX_UOP_ID; i++){
274+
for (int j = 1; j <= MAX_UOP_ID; j++) {
275+
if (stats->opcode[i].pair_count[j]) {
276+
fprintf(out, "uop[%s].pair_count[%s] : %" PRIu64 "\n",
277+
_PyOpcode_uop_name[i], _PyOpcode_uop_name[j], stats->opcode[i].pair_count[j]);
278+
}
279+
}
280+
}
272281
for (int i = 0; i < MAX_UOP_ID; i++) {
273282
if (stats->error_in_opcode[i]) {
274283
fprintf(

Tools/scripts/summarize_stats.py

Expand all lines: Tools/scripts/summarize_stats.py
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -736,9 +736,9 @@ def execution_count_section() -> Section:
736736
)
737737

738738

739-
def pair_count_section() -> Section:
739+
def pair_count_section(prefix: str, title=None) -> Section:
740740
def calc_pair_count_table(stats: Stats) -> Rows:
741-
opcode_stats = stats.get_opcode_stats("opcode")
741+
opcode_stats = stats.get_opcode_stats(prefix)
742742
pair_counts = opcode_stats.get_pair_counts()
743743
total = opcode_stats.get_total_execution_count()
744744

@@ -760,7 +760,7 @@ def calc_pair_count_table(stats: Stats) -> Rows:
760760

761761
return Section(
762762
"Pair counts",
763-
"Pair counts for top 100 Tier 1 instructions",
763 9E7A +
f"Pair counts for top 100 {title if title else prefix} pairs",
764764
[
765765
Table(
766766
("Pair", "Count:", "Self:", "Cumulative:"),
@@ -1232,6 +1232,7 @@ def iter_optimization_tables(base_stats: Stats, head_stats: Stats | None = None)
12321232
)
12331233
],
12341234
)
1235+
yield pair_count_section(prefix="uop", title="Non-JIT uop")
12351236
yield Section(
12361237
"Unsupported opcodes",
12371238
"",
@@ -1292,7 +1293,7 @@ def calc_rows(stats: Stats) -> Rows:
12921293

12931294
LAYOUT = [
12941295
execution_count_section(),
1295-
pair_count_section(),
1296+
pair_count_section("opcode"),
12961297
pre_succ_pairs_section(),
12971298
specialization_section(),
12981299
specialization_effectiveness_section(),

0 commit comments

Comments
 (0)
0