Skip to content

Commit 63d087c

Browse files
committed
Factor out T2 optimizer symbols for easier testing
1 parent 915d7dd commit 63d087c

10 files changed

+95
-326
lines changed

Include/internal/pycore_optimizer.h

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ extern "C" {
99
#endif
1010

1111
#include "pycore_uop_ids.h"
12+
#include <stdbool.h>
1213

1314
// This is the length of the trace we project initially.
1415
#define UOP_MAX_TRACE_LENGTH 512
@@ -25,6 +26,89 @@ extern PyTypeObject _PyDefaultOptimizer_Type;
2526
extern PyTypeObject _PyUOpExecutor_Type;
2627
extern PyTypeObject _PyUOpOptimizer_Type;
2728

29+
/* Symbols */
30+
31+
struct _Py_UOpsSymType {
32+
int flags;
33+
PyTypeObject *typ;
34+
// constant propagated value (might be NULL)
35+
PyObject *const_val;
36+
};
37+
38+
// Holds locals, stack, locals, stack ... co_consts (in that order)
39+
#define MAX_ABSTRACT_INTERP_SIZE 4096
40+
41+
#define OVERALLOCATE_FACTOR 5
42+
43+
#define TY_ARENA_SIZE (UOP_MAX_TRACE_LENGTH * OVERALLOCATE_FACTOR)
44+
45+
// Need extras for root frame and for overflow frame (see TRACE_STACK_PUSH())
46+
#define MAX_ABSTRACT_FRAME_DEPTH (TRACE_STACK_SIZE + 2)
47+
48+
typedef struct _Py_UOpsSymType _Py_UOpsSymType;
49+
50+
struct _Py_UOpsAbstractFrame {
51+
// Max stacklen
52+
int stack_len;
53+
int locals_len;
54+
55+
_Py_UOpsSymType **stack_pointer;
56+
_Py_UOpsSymType **stack;
57+
_Py_UOpsSymType **locals;
58+
};
59+
60+
typedef struct _Py_UOpsAbstractFrame _Py_UOpsAbstractFrame;
61+
62+
typedef struct ty_arena {
63+
int ty_curr_number;
64+
int ty_max_number;
65+
_Py_UOpsSymType arena[TY_ARENA_SIZE];
66+
} ty_arena;
67+
68+
struct _Py_UOpsAbstractInterpContext {
69+
PyObject_HEAD
70+
// The current "executing" frame.
71+
_Py_UOpsAbstractFrame *frame;
72+
_Py_UOpsAbstractFrame frames[MAX_ABSTRACT_FRAME_DEPTH];
73+
int curr_frame_depth;
74+
75+
// Arena for the symbolic types.
76+
ty_arena t_arena;
77+
78+
_Py_UOpsSymType **n_consumed;
79+
_Py_UOpsSymType **limit;
80+
_Py_UOpsSymType *locals_and_stack[MAX_ABSTRACT_INTERP_SIZE];
81+
};
82+
83+
typedef struct _Py_UOpsAbstractInterpContext _Py_UOpsAbstractInterpContext;
84+
85+
extern bool sym_is_null(_Py_UOpsSymType *sym);
86+
extern bool sym_is_not_null(_Py_UOpsSymType *sym);
87+
extern bool sym_is_const(_Py_UOpsSymType *sym);
88+
extern PyObject *sym_get_const(_Py_UOpsSymType *sym);
89+
extern _Py_UOpsSymType *sym_new_unknown(_Py_UOpsAbstractInterpContext *ctx);
90+
extern _Py_UOpsSymType *sym_new_known_notnull(_Py_UOpsAbstractInterpContext *ctx);
91+
extern _Py_UOpsSymType *sym_new_known_type(
92+
_Py_UOpsAbstractInterpContext *ctx, PyTypeObject *typ);
93+
extern _Py_UOpsSymType *sym_new_const(_Py_UOpsAbstractInterpContext *ctx, PyObject *const_val);
94+
extern _Py_UOpsSymType *sym_new_null(_Py_UOpsAbstractInterpContext *ctx);
95+
extern bool sym_matches_type(_Py_UOpsSymType *sym, PyTypeObject *typ);
96+
extern void sym_set_null(_Py_UOpsSymType *sym);
97+
extern void sym_set_type(_Py_UOpsSymType *sym, PyTypeObject *tp);
98+
99+
extern int abstractcontext_init(
100+
_Py_UOpsAbstractInterpContext *ctx, PyCodeObject *co,
101+
int curr_stacklen, int ir_entries);
102+
extern void abstractcontext_fini(_Py_UOpsAbstractInterpContext *ctx);
103+
104+
extern _Py_UOpsAbstractFrame *ctx_frame_new(
105+
_Py_UOpsAbstractInterpContext *ctx,
106+
PyCodeObject *co,
107+
_Py_UOpsSymType **localsplus_start,
108+
int n_locals_already_filled,
109+
int curr_stackentries);
110+
extern int ctx_frame_pop(_Py_UOpsAbstractInterpContext *ctx);
111+
28112
#ifdef __cplusplus
29113
}
30114
#endif

Makefile.pre.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,7 @@ PYTHON_OBJS= \
446446
Python/object_stack.o \
447447
Python/optimizer.o \
448448
Python/optimizer_analysis.o \
449+
Python/optimizer_symbols.o \
449450
Python/parking_lot.o \
450451
Python/pathconfig.o \
451452
Python/preconfig.o \

PCbuild/_freeze_module.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@
235235
<ClCompile Include="..\Python\object_stack.c" />
236236
<ClCompile Include="..\Python\optimizer.c" />
237237
<ClCompile Include="..\Python\optimizer_analysis.c" />
238+
<ClCompile Include="..\Python\optimizer_symbols.c" />
238239
<ClCompile Include="..\Python\parking_lot.c" />
239240
<ClCompile Include="..\Python\pathconfig.c" />
240241
<ClCompile Include="..\Python\perf_trampoline.c" />

PCbuild/_freeze_module.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,9 @@
310310
<ClCompile Include="..\Python\optimizer_analysis.c">
311311
<Filter>Source Files</Filter>
312312
</ClCompile>
313+
<ClCompile Include="..\Python\optimizer_symbols.c">
314+
<Filter>Python</Filter>
315+
</ClCompile>
313316
<ClCompile Include="..\Parser\parser.c">
314317
<Filter>Source Files</Filter>
315318
</ClCompile>

PCbuild/pythoncore.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,7 @@
601601
<ClCompile Include="..\Python\object_stack.c" />
602602
<ClCompile Include="..\Python\optimizer.c" />
603603
<ClCompile Include="..\Python\optimizer_analysis.c" />
604+
<ClCompile Include="..\Python\optimizer_symbols.c" />
604605
<ClCompile Include="..\Python\parking_lot.c" />
605606
<ClCompile Include="..\Python\pathconfig.c" />
606607
<ClCompile Include="..\Python\perf_trampoline.c" />

PCbuild/pythoncore.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1382,6 +1382,9 @@
13821382
<ClCompile Include="..\Python\optimizer_analysis.c">
13831383
<Filter>Python</Filter>
13841384
</ClCompile>
1385+
<ClCompile Include="..\Python\optimizer_symbols.c">
1386+
<Filter>Python</Filter>
1387+
</ClCompile>
13851388
<ClCompile Include="..\Python\parking_lot.c">
13861389
<Filter>Python</Filter>
13871390
</ClCompile>

0 commit comments

Comments
 (0)