Skip to content

Commit 45f8ce0

Browse files
committed
remove out_len
1 parent 031d6e3 commit 45f8ce0

File tree

5 files changed

+66
-66
lines changed

5 files changed

+66
-66
lines changed

Include/internal/pycore_optimizer_types.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ extern "C" {
1010

1111
#include "pycore_uop.h" // UOP_MAX_TRACE_LENGTH
1212

13-
typedef struct _PyJitTracerState _PyJitTracerState;
14-
1513
// Holds locals, stack, locals, stack ... (in that order)
1614
#define MAX_ABSTRACT_INTERP_SIZE 512
1715

@@ -130,7 +128,7 @@ typedef struct _JitOptContext {
130128
JitOptRef *n_consumed;
131129
JitOptRef *limit;
132130
JitOptRef locals_and_stack[MAX_ABSTRACT_INTERP_SIZE];
133-
_PyJitTracerState *tracer;
131+
_PyUOpInstruction *out_buffer;
134132
} JitOptContext;
135133

136134

Include/internal/pycore_tstate.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ typedef struct _PyJitTracerState {
6060
JitOptContext opt_context;
6161
_PyUOpInstruction code_buffer[UOP_MAX_TRACE_LENGTH];
6262
_PyUOpInstruction out_buffer[UOP_MAX_TRACE_LENGTH];
63-
int out_len;
6463
} _PyJitTracerState;
6564

6665
#endif

Python/optimizer_analysis.c

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -164,20 +164,20 @@ is_terminator_uop(const _PyUOpInstruction *uop)
164164
(INST)->oparg = ARG; \
165165
(INST)->operand0 = OPERAND;
166166

167-
#define ADD_OP(OP, ARG, OPERAND) add_op(ctx, this_instr, (OP), (ARG), (OPERAND))
167+
#define ADD_OP(OP, ARG, OPERAND) add_op(ctx, this_instr, &out_len, (OP), (ARG), (OPERAND))
168168

169169
static inline void
170-
add_op(JitOptContext *ctx, _PyUOpInstruction *this_instr,
170+
add_op(JitOptContext *ctx, _PyUOpInstruction *this_instr, int *out_len,
171171
uint16_t opcode, uint16_t oparg, uintptr_t operand0)
172172
{
173-
_PyUOpInstruction *out = &ctx->tracer->out_buffer[ctx->tracer->out_len];
173+
_PyUOpInstruction *out = &ctx->out_buffer[*out_len];
174174
out->opcode = (opcode);
175175
out->format = this_instr->format;
176176
out->oparg = (oparg);
177177
out->target = this_instr->target;
178178
out->operand0 = (operand0);
179179
out->operand1 = this_instr->operand1;
180-
ctx->tracer->out_len++;
180+
(*out_len)++;
181181
}
182182

183183
/* Shortened forms for convenience, used in optimizer_bytecodes.c */
@@ -242,12 +242,13 @@ static int
242242
optimize_to_bool(
243243
_PyUOpInstruction *this_instr,
244244
JitOptContext *ctx,
245+
int *out_len,
245246
JitOptRef value,
246247
JitOptRef *result_ptr,
247248
bool insert_mode)
248249
{
249250
if (sym_matches_type(value, &PyBool_Type)) {
250-
ADD_OP(_NOP, 0, 0);
251+
add_op(ctx, this_instr, out_len, _NOP, 0, 0);
251252
*result_ptr = value;
252253
return 1;
253254
}
@@ -257,17 +258,17 @@ optimize_to_bool(
257258
int opcode = insert_mode ?
258259
_INSERT_1_LOAD_CONST_INLINE_BORROW :
259260
_POP_TOP_LOAD_CONST_INLINE_BORROW;
260-
ADD_OP(opcode, 0, (uintptr_t)load);
261+
add_op(ctx, this_instr, out_len, opcode, 0, (uintptr_t)load);
261262
*result_ptr = sym_new_const(ctx, load);
262263
return 1;
263264
}
264265
return 0;
265266
}
266267

267268
static void
268-
eliminate_pop_guard(_PyUOpInstruction *this_instr, JitOptContext *ctx, bool exit)
269+
eliminate_pop_guard(_PyUOpInstruction *this_instr, JitOptContext *ctx, int *out_len, bool exit)
269270
{
270-
ADD_OP(_POP_TOP, 0, 0);
271+
add_op(ctx, this_instr, out_len, _POP_TOP, 0, 0);
271272
if (exit) {
272273
REPLACE_OP((this_instr+1), _EXIT_TRACE, 0, 0);
273274
this_instr[1].target = this_instr->target;
@@ -276,15 +277,15 @@ eliminate_pop_guard(_PyUOpInstruction *this_instr, JitOptContext *ctx, bool exit
276277

277278
static JitOptRef
278279
lookup_attr(JitOptContext *ctx, _PyBloomFilter *dependencies, _PyUOpInstruction *this_instr,
279-
PyTypeObject *type, PyObject *name, uint16_t immortal,
280+
int *out_len, PyTypeObject *type, PyObject *name, uint16_t immortal,
280281
uint16_t mortal)
281282
{
282283
// The cached value may be dead, so we need to do the lookup again... :(
283284
if (type && PyType_Check(type)) {
284285
PyObject *lookup = _PyType_Lookup(type, name);
285286
if (lookup) {
286287
int opcode = _Py_IsImmortal(lookup) ? immortal : mortal;
287-
ADD_OP(opcode, 0, (uintptr_t)lookup);
288+
add_op(ctx, this_instr, out_len, opcode, 0, (uintptr_t)lookup);
288289
PyType_Watch(TYPE_WATCHER_ID, (PyObject *)type);
289290
_Py_BloomFilter_Add(dependencies, type);
290291
return sym_new_const(ctx, lookup);
@@ -377,7 +378,7 @@ optimize_uops(
377378
JitOptContext *ctx = &tstate->jit_tracer_state->opt_context;
378379
uint32_t opcode = UINT16_MAX;
379380

380-
ctx->tracer = tstate->jit_tracer_state;
381+
ctx->out_buffer = tstate->jit_tracer_state->out_buffer;
381382

382383
// Make sure that watchers are set up
383384
PyInterpreterState *interp = _PyInterpreterState_GET();
@@ -394,7 +395,7 @@ optimize_uops(
394395
frame->func = func;
395396
ctx->curr_frame_depth++;
396397
ctx->frame = frame;
397-
ctx->tracer->out_len = 0;
398+
int out_len = 0;
398399

399400
_PyUOpInstruction *this_instr = NULL;
400401
JitOptRef *stack_pointer = ctx->frame->stack_pointer;
@@ -427,8 +428,8 @@ optimize_uops(
427428
Py_UNREACHABLE();
428429
}
429430
// If no ADD_OP was called during this iteration, copy the original instruction
430-
if (ctx->tracer->out_len == i) {
431-
ctx->tracer->out_buffer[ctx->tracer->out_len++] = *this_instr;
431+
if (out_len == i) {
432+
ctx->out_buffer[out_len++] = *this_instr;
432433
}
433434
assert(ctx->frame != NULL);
434435
if (!CURRENT_FRAME_IS_INIT_SHIM()) {
@@ -459,20 +460,20 @@ optimize_uops(
459460
* would be no benefit in retrying later */
460461
_Py_uop_abstractcontext_fini(ctx);
461462
// Check that the trace ends with a proper terminator
462-
if (ctx->tracer->out_len > 0) {
463-
_PyUOpInstruction *last_uop = &ctx->tracer->out_buffer[ctx->tracer->out_len - 1];
463+
if (out_len > 0) {
464+
_PyUOpInstruction *last_uop = &ctx->out_buffer[out_len - 1];
464465
if (!is_terminator_uop(last_uop)) {
465466
// Copy remaining uops from original trace until we find a terminator
466-
for (int i = ctx->tracer->out_len; i < trace_len; i++) {
467-
ctx->tracer->out_buffer[ctx->tracer->out_len++] = trace[i];
467+
for (int i = out_len; i < trace_len; i++) {
468+
ctx->out_buffer[out_len++] = trace[i];
468469
if (is_terminator_uop(&trace[i])) {
469470
break;
470471
}
471472
}
472473
}
473474
}
474475

475-
return ctx->tracer->out_len;
476+
return out_len;
476477

477478
error:
478479
DPRINTF(3, "\n");

Python/optimizer_bytecodes.c

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,13 @@ extern int
4343
optimize_to_bool(
4444
_PyUOpInstruction *this_instr,
4545
JitOptContext *ctx,
46+
int *out_len,
4647
JitOptSymbol *value,
4748
JitOptSymbol **result_ptr,
4849
bool insert_mode);
4950

5051
extern void
51-
eliminate_pop_guard(_PyUOpInstruction *this_instr, JitOptContext *ctx, bool exit);
52+
eliminate_pop_guard(_PyUOpInstruction *this_instr, JitOptContext *ctx, int *out_len, bool exit);
5253

5354
extern PyCodeObject *get_code(_PyUOpInstruction *op);
5455

@@ -57,6 +58,7 @@ dummy_func(void) {
5758

5859
PyCodeObject *co;
5960
int oparg;
61+
int out_len;
6062
JitOptSymbol *flag;
6163
JitOptSymbol *left;
6264
JitOptSymbol *right;
@@ -381,36 +383,36 @@ dummy_func(void) {
381383
}
382384

383385
op(_TO_BOOL, (value -- res)) {
384-
int already_bool = optimize_to_bool(this_instr, ctx, value, &res, false);
386+
int already_bool = optimize_to_bool(this_instr, ctx, &out_len, value, &res, false);
385387
if (!already_bool) {
386388
res = sym_new_truthiness(ctx, value, true);
387389
}
388390
}
389391

390392
op(_TO_BOOL_BOOL, (value -- value)) {
391-
int already_bool = optimize_to_bool(this_instr, ctx, value, &value, false);
393+
int already_bool = optimize_to_bool(this_instr, ctx, &out_len, value, &value, false);
392394
if (!already_bool) {
393395
sym_set_type(value, &PyBool_Type);
394396
}
395397
}
396398

397399
op(_TO_BOOL_INT, (value -- res)) {
398-
int already_bool = optimize_to_bool(this_instr, ctx, value, &res, false);
400+
int already_bool = optimize_to_bool(this_instr, ctx, &out_len, value, &res, false);
399401
if (!already_bool) {
400402
sym_set_type(value, &PyLong_Type);
401403
res = sym_new_truthiness(ctx, value, true);
402404
}
403405
}
404406

405407
op(_TO_BOOL_LIST, (value -- res)) {
406-
int already_bool = optimize_to_bool(this_instr, ctx, value, &res, false);
408+
int already_bool = optimize_to_bool(this_instr, ctx, &out_len, value, &res, false);
407409
if (!already_bool) {
408410
res = sym_new_type(ctx, &PyBool_Type);
409411
}
410412
}
411413

412414
op(_TO_BOOL_NONE, (value -- res)) {
413-
int already_bool = optimize_to_bool(this_instr, ctx, value, &res, false);
415+
int already_bool = optimize_to_bool(this_instr, ctx, &out_len, value, &res, false);
414416
if (!already_bool) {
415417
sym_set_const(value, Py_None);
416418
res = sym_new_const(ctx, Py_False);
@@ -436,7 +438,7 @@ dummy_func(void) {
436438
}
437439

438440
op(_TO_BOOL_STR, (value -- res, v)) {
439-
int already_bool = optimize_to_bool(this_instr, ctx, value, &res, true);
441+
int already_bool = optimize_to_bool(this_instr, ctx, &out_len, value, &res, true);
440442
v = value;
441443
if (!already_bool) {
442444
res = sym_new_truthiness(ctx, value, true);
@@ -693,7 +695,7 @@ dummy_func(void) {
693695
(void)descr;
694696
PyTypeObject *type = (PyTypeObject *)sym_get_const(ctx, owner);
695697
PyObject *name = get_co_name(ctx, oparg >> 1);
696-
attr = lookup_attr(ctx, dependencies, this_instr, type, name,
698+
attr = lookup_attr(ctx, dependencies, this_instr, &out_len, type, name,
697699
_POP_TOP_LOAD_CONST_INLINE_BORROW,
698700
_POP_TOP_LOAD_CONST_INLINE);
699701
}
@@ -702,7 +704,7 @@ dummy_func(void) {
702704
(void)descr;
703705
PyTypeObject *type = sym_get_type(owner);
704706
PyObject *name = get_co_name(ctx, oparg >> 1);
705-
attr = lookup_attr(ctx, dependencies, this_instr, type, name,
707+
attr = lookup_attr(ctx, dependencies, this_instr, &out_len, type, name,
706708
_POP_TOP_LOAD_CONST_INLINE_BORROW,
707709
_POP_TOP_LOAD_CONST_INLINE);
708710
}
@@ -711,7 +713,7 @@ dummy_func(void) {
711713
(void)descr;
712714
PyTypeObject *type = sym_get_type(owner);
713715
PyObject *name = get_co_name(ctx, oparg >> 1);
714-
attr = lookup_attr(ctx, dependencies, this_instr, type, name,
716+
attr = lookup_attr(ctx, dependencies, this_instr, &out_len, type, name,
715717
_POP_TOP_LOAD_CONST_INLINE_BORROW,
716718
_POP_TOP_LOAD_CONST_INLINE);
717719
}
@@ -720,7 +722,7 @@ dummy_func(void) {
720722
(void)descr;
721723
PyTypeObject *type = sym_get_type(owner);
722724
PyObject *name = get_co_name(ctx, oparg >> 1);
723-
attr = lookup_attr(ctx, dependencies, this_instr, type, name,
725+
attr = lookup_attr(ctx, dependencies, this_instr, &out_len, type, name,
724726
_LOAD_CONST_UNDER_INLINE_BORROW,
725727
_LOAD_CONST_UNDER_INLINE);
726728
self = owner;
@@ -730,7 +732,7 @@ dummy_func(void) {
730732
(void)descr;
731733
PyTypeObject *type = sym_get_type(owner);
732734
PyObject *name = get_co_name(ctx, oparg >> 1);
733-
attr = lookup_attr(ctx, dependencies, this_instr, type, name,
735+
attr = lookup_attr(ctx, dependencies, this_instr, &out_len, type, name,
734736
_LOAD_CONST_UNDER_INLINE_BORROW,
735737
_LOAD_CONST_UNDER_INLINE);
736738
self = owner;
@@ -740,7 +742,7 @@ dummy_func(void) {
740742
(void)descr;
741743
PyTypeObject *type = sym_get_type(owner);
742744
PyObject *name = get_co_name(ctx, oparg >> 1);
743-
attr = lookup_attr(ctx, dependencies, this_instr, type, name,
745+
attr = lookup_attr(ctx, dependencies, this_instr, &out_len, type, name,
744746
_LOAD_CONST_UNDER_INLINE_BORROW,
745747
_LOAD_CONST_UNDER_INLINE);
746748
self = owner;
@@ -761,7 +763,7 @@ dummy_func(void) {
761763
if (sym_is_const(ctx, callable) && sym_matches_type(callable, &PyFunction_Type)) {
762764
assert(PyFunction_Check(sym_get_const(ctx, callable)));
763765
ADD_OP(_CHECK_FUNCTION_VERSION_INLINE, 0, func_version);
764-
ctx->tracer->out_buffer[ctx->tracer->out_len - 1].operand1 = (uintptr_t)sym_get_const(ctx, callable);
766+
ctx->out_buffer[out_len - 1].operand1 = (uintptr_t)sym_get_const(ctx, callable);
765767
}
766768
sym_set_type(callable, &PyFunction_Type);
767769
}
@@ -771,7 +773,7 @@ dummy_func(void) {
771773
PyMethodObject *method = (PyMethodObject *)sym_get_const(ctx, callable);
772774
assert(PyMethod_Check(method));
773775
ADD_OP(_CHECK_FUNCTION_VERSION_INLINE, 0, func_version);
774-
ctx->tracer->out_buffer[ctx->tracer->out_len - 1].operand1 = (uintptr_t)method->im_func;
776+
ctx->out_buffer[out_len - 1].operand1 = (uintptr_t)method->im_func;
775777
}
776778
sym_set_type(callable, &PyMethod_Type);
777779
}
@@ -1105,7 +1107,7 @@ dummy_func(void) {
11051107
if (sym_is_const(ctx, flag)) {
11061108
PyObject *value = sym_get_const(ctx, flag);
11071109
assert(value != NULL);
1108-
eliminate_pop_guard(this_instr, ctx, value != Py_True);
1110+
eliminate_pop_guard(this_instr, ctx, &out_len, value != Py_True);
11091111
}
11101112
sym_set_const(flag, Py_True);
11111113
}
@@ -1150,7 +1152,7 @@ dummy_func(void) {
11501152
if (sym_is_const(ctx, flag)) {
11511153
PyObject *value = sym_get_const(ctx, flag);
11521154
assert(value != NULL);
1153-
eliminate_pop_guard(this_instr, ctx, value != Py_False);
1155+
eliminate_pop_guard(this_instr, ctx, &out_len, value != Py_False);
11541156
}
11551157
sym_set_const(flag, Py_False);
11561158
}
@@ -1159,11 +1161,11 @@ dummy_func(void) {
11591161
if (sym_is_const(ctx, val)) {
11601162
PyObject *value = sym_get_const(ctx, val);
11611163
assert(value != NULL);
1162-
eliminate_pop_guard(this_instr, ctx, !Py_IsNone(value));
1164+
eliminate_pop_guard(this_instr, ctx, &out_len, !Py_IsNone(value));
11631165
}
11641166
else if (sym_has_type(val)) {
11651167
assert(!sym_matches_type(val, &_PyNone_Type));
1166-
eliminate_pop_guard(this_instr, ctx, true);
1168+
eliminate_pop_guard(this_instr, ctx, &out_len, true);
11671169
}
11681170
sym_set_const(val, Py_None);
11691171
}
@@ -1172,11 +1174,11 @@ dummy_func(void) {
11721174
if (sym_is_const(ctx, val)) {
11731175
PyObject *value = sym_get_const(ctx, val);
11741176
assert(value != NULL);
1175-
eliminate_pop_guard(this_instr, ctx, Py_IsNone(value));
1177+
eliminate_pop_guard(this_instr, ctx, &out_len, Py_IsNone(value));
11761178
}
11771179
else if (sym_has_type(val)) {
11781180
assert(!sym_matches_type(val, &_PyNone_Type));
1179-
eliminate_pop_guard(this_instr, ctx, false);
1181+
eliminate_pop_guard(this_instr, ctx, &out_len, false);
11801182
}
11811183
}
11821184

@@ -1511,7 +1513,7 @@ dummy_func(void) {
15111513
ctx->frame->globals_watched = true;
15121514
}
15131515
if (ctx->frame->globals_checked_version != version && this_instr[-1].opcode == _NOP) {
1514-
REPLACE_OP(&ctx->tracer->out_buffer[ctx->tracer->out_len - 1], _GUARD_GLOBALS_VERSION, 0, version);
1516+
REPLACE_OP(&ctx->out_buffer[out_len - 1], _GUARD_GLOBALS_VERSION, 0, version);
15151517
ctx->frame->globals_checked_version = version;
15161518
}
15171519
if (ctx->frame->globals_checked_version == version) {

0 commit comments

Comments
 (0)