Skip to content

Commit 15f32b9

Browse files
committed
move out_len to JitOptContext
1 parent 0d07c5b commit 15f32b9

File tree

4 files changed

+64
-64
lines changed

4 files changed

+64
-64
lines changed

Include/internal/pycore_optimizer_types.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ typedef struct _JitOptContext {
129129
JitOptRef *limit;
130130
JitOptRef locals_and_stack[MAX_ABSTRACT_INTERP_SIZE];
131131
_PyUOpInstruction *out_buffer;
132+
int out_len;
132133
} JitOptContext;
133134

134135

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, &out_len, (OP), (ARG), (OPERAND))
167+
#define ADD_OP(OP, ARG, OPERAND) add_op(ctx, this_instr, (OP), (ARG), (OPERAND))
168168

169169
static inline void
170-
add_op(JitOptContext *ctx, _PyUOpInstruction *this_instr, int *out_len,
170+
add_op(JitOptContext *ctx, _PyUOpInstruction *this_instr,
171171
uint16_t opcode, uint16_t oparg, uintptr_t operand0)
172172
{
173-
_PyUOpInstruction *out = &ctx->out_buffer[*out_len];
173+
_PyUOpInstruction *out = &ctx->out_buffer[ctx->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-
(*out_len)++;
180+
ctx->out_len++;
181181
}
182182

183183
/* Shortened forms for convenience, used in optimizer_bytecodes.c */
@@ -242,13 +242,12 @@ static int
242242
optimize_to_bool(
243243
_PyUOpInstruction *this_instr,
244244
JitOptContext *ctx,
245-
int *out_len,
246245
JitOptRef value,
247246
JitOptRef *result_ptr,
248247
bool insert_mode)
249248
{
250249
if (sym_matches_type(value, &PyBool_Type)) {
251-
add_op(ctx, this_instr, out_len, _NOP, 0, 0);
250+
ADD_OP(_NOP, 0, 0);
252251
*result_ptr = value;
253252
return 1;
254253
}
@@ -258,17 +257,17 @@ optimize_to_bool(
258257
int opcode = insert_mode ?
259258
_INSERT_1_LOAD_CONST_INLINE_BORROW :
260259
_POP_TOP_LOAD_CONST_INLINE_BORROW;
261-
add_op(ctx, this_instr, out_len, opcode, 0, (uintptr_t)load);
260+
ADD_OP(opcode, 0, (uintptr_t)load);
262261
*result_ptr = sym_new_const(ctx, load);
263262
return 1;
264263
}
265264
return 0;
266265
}
267266

268267
static void
269-
eliminate_pop_guard(_PyUOpInstruction *this_instr, JitOptContext *ctx, int *out_len, bool exit)
268+
eliminate_pop_guard(_PyUOpInstruction *this_instr, JitOptContext *ctx, bool exit)
270269
{
271-
add_op(ctx, this_instr, out_len, _POP_TOP, 0, 0);
270+
ADD_OP(_POP_TOP, 0, 0);
272271
if (exit) {
273272
REPLACE_OP((this_instr+1), _EXIT_TRACE, 0, 0);
274273
this_instr[1].target = this_instr->target;
@@ -277,15 +276,15 @@ eliminate_pop_guard(_PyUOpInstruction *this_instr, JitOptContext *ctx, int *out_
277276

278277
static JitOptRef
279278
lookup_attr(JitOptContext *ctx, _PyBloomFilter *dependencies, _PyUOpInstruction *this_instr,
280-
int *out_len, PyTypeObject *type, PyObject *name, uint16_t immortal,
279+
PyTypeObject *type, PyObject *name, uint16_t immortal,
281280
uint16_t mortal)
282281
{
283282
// The cached value may be dead, so we need to do the lookup again... :(
284283
if (type && PyType_Check(type)) {
285284
PyObject *lookup = _PyType_Lookup(type, name);
286285
if (lookup) {
287286
int opcode = _Py_IsImmortal(lookup) ? immortal : mortal;
288-
add_op(ctx, this_instr, out_len, opcode, 0, (uintptr_t)lookup);
287+
ADD_OP(opcode, 0, (uintptr_t)lookup);
289288
PyType_Watch(TYPE_WATCHER_ID, (PyObject *)type);
290289
_Py_BloomFilter_Add(dependencies, type);
291290
return sym_new_const(ctx, lookup);
@@ -395,7 +394,9 @@ optimize_uops(
395394
frame->func = func;
396395
ctx->curr_frame_depth++;
397396
ctx->frame = frame;
398-
int out_len = 0;
397+
398+
int *cur_len = &ctx->out_len;
399+
*cur_len = 0;
399400

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

476-
return out_len;
477+
return *cur_len;
477478

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

Python/optimizer_bytecodes.c

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

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

5453
extern PyCodeObject *get_code(_PyUOpInstruction *op);
5554

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

5958
PyCodeObject *co;
6059
int oparg;
61-
int out_len;
6260
JitOptSymbol *flag;
6361
JitOptSymbol *left;
6462
JitOptSymbol *right;
@@ -389,36 +387,36 @@ dummy_func(void) {
389387
}
390388

391389
op(_TO_BOOL, (value -- res)) {
392-
int already_bool = optimize_to_bool(this_instr, ctx, &out_len, value, &res, false);
390+
int already_bool = optimize_to_bool(this_instr, ctx, value, &res, false);
393391
if (!already_bool) {
394392
res = sym_new_truthiness(ctx, value, true);
395393
}
396394
}
397395

398396
op(_TO_BOOL_BOOL, (value -- value)) {
399-
int already_bool = optimize_to_bool(this_instr, ctx, &out_len, value, &value, false);
397+
int already_bool = optimize_to_bool(this_instr, ctx, value, &value, false);
400398
if (!already_bool) {
401399
sym_set_type(value, &PyBool_Type);
402400
}
403401
}
404402

405403
op(_TO_BOOL_INT, (value -- res)) {
406-
int already_bool = optimize_to_bool(this_instr, ctx, &out_len, value, &res, false);
404+
int already_bool = optimize_to_bool(this_instr, ctx, value, &res, false);
407405
if (!already_bool) {
408406
sym_set_type(value, &PyLong_Type);
409407
res = sym_new_truthiness(ctx, value, true);
410408
}
411409
}
412410

413411
op(_TO_BOOL_LIST, (value -- res)) {
414-
int already_bool = optimize_to_bool(this_instr, ctx, &out_len, value, &res, false);
412+
int already_bool = optimize_to_bool(this_instr, ctx, value, &res, false);
415413
if (!already_bool) {
416414
res = sym_new_type(ctx, &PyBool_Type);
417415
}
418416
}
419417

420418
op(_TO_BOOL_NONE, (value -- res)) {
421-
int already_bool = optimize_to_bool(this_instr, ctx, &out_len, value, &res, false);
419+
int already_bool = optimize_to_bool(this_instr, ctx, value, &res, false);
422420
if (!already_bool) {
423421
sym_set_const(value, Py_None);
424422
res = sym_new_const(ctx, Py_False);
@@ -444,7 +442,7 @@ dummy_func(void) {
444442
}
445443

446444
op(_TO_BOOL_STR, (value -- res, v)) {
447-
int already_bool = optimize_to_bool(this_instr, ctx, &out_len, value, &res, true);
445+
int already_bool = optimize_to_bool(this_instr, ctx, value, &res, true);
448446
v = value;
449447
if (!already_bool) {
450448
res = sym_new_truthiness(ctx, value, true);
@@ -707,7 +705,7 @@ dummy_func(void) {
707705
(void)descr;
708706
PyTypeObject *type = (PyTypeObject *)sym_get_const(ctx, owner);
709707
PyObject *name = get_co_name(ctx, oparg >> 1);
710-
attr = lookup_attr(ctx, dependencies, this_instr, &out_len, type, name,
708+
attr = lookup_attr(ctx, dependencies, this_instr, type, name,
711709
_POP_TOP_LOAD_CONST_INLINE_BORROW,
712710
_POP_TOP_LOAD_CONST_INLINE);
713711
}
@@ -716,7 +714,7 @@ dummy_func(void) {
716714
(void)descr;
717715
PyTypeObject *type = sym_get_type(owner);
718716
PyObject *name = get_co_name(ctx, oparg >> 1);
719-
attr = lookup_attr(ctx, dependencies, this_instr, &out_len, type, name,
717+
attr = lookup_attr(ctx, dependencies, this_instr, type, name,
720718
_POP_TOP_LOAD_CONST_INLINE_BORROW,
721719
_POP_TOP_LOAD_CONST_INLINE);
722720
}
@@ -725,7 +723,7 @@ dummy_func(void) {
725723
(void)descr;
726724
PyTypeObject *type = sym_get_type(owner);
727725
PyObject *name = get_co_name(ctx, oparg >> 1);
728-
attr = lookup_attr(ctx, dependencies, this_instr, &out_len, type, name,
726+
attr = lookup_attr(ctx, dependencies, this_instr, type, name,
729727
_POP_TOP_LOAD_CONST_INLINE_BORROW,
730728
_POP_TOP_LOAD_CONST_INLINE);
731729
}
@@ -734,7 +732,7 @@ dummy_func(void) {
734732
(void)descr;
735733
PyTypeObject *type = sym_get_type(owner);
736734
PyObject *name = get_co_name(ctx, oparg >> 1);
737-
attr = lookup_attr(ctx, dependencies, this_instr, &out_len, type, name,
735+
attr = lookup_attr(ctx, dependencies, this_instr, type, name,
738736
_LOAD_CONST_UNDER_INLINE_BORROW,
739737
_LOAD_CONST_UNDER_INLINE);
740738
self = owner;
@@ -744,7 +742,7 @@ dummy_func(void) {
744742
(void)descr;
745743
PyTypeObject *type = sym_get_type(owner);
746744
PyObject *name = get_co_name(ctx, oparg >> 1);
747-
attr = lookup_attr(ctx, dependencies, this_instr, &out_len, type, name,
745+
attr = lookup_attr(ctx, dependencies, this_instr, type, name,
748746
_LOAD_CONST_UNDER_INLINE_BORROW,
749747
_LOAD_CONST_UNDER_INLINE);
750748
self = owner;
@@ -754,7 +752,7 @@ dummy_func(void) {
754752
(void)descr;
755753
PyTypeObject *type = sym_get_type(owner);
756754
PyObject *name = get_co_name(ctx, oparg >> 1);
757-
attr = lookup_attr(ctx, dependencies, this_instr, &out_len, type, name,
755+
attr = lookup_attr(ctx, dependencies, this_instr, type, name,
758756
_LOAD_CONST_UNDER_INLINE_BORROW,
759757
_LOAD_CONST_UNDER_INLINE);
760758
self = owner;
@@ -775,7 +773,7 @@ dummy_func(void) {
775773
if (sym_is_const(ctx, callable) && sym_matches_type(callable, &PyFunction_Type)) {
776774
assert(PyFunction_Check(sym_get_const(ctx, callable)));
777775
ADD_OP(_CHECK_FUNCTION_VERSION_INLINE, 0, func_version);
778-
ctx->out_buffer[out_len - 1].operand1 = (uintptr_t)sym_get_const(ctx, callable);
776+
ctx->out_buffer[ctx->out_len - 1].operand1 = (uintptr_t)sym_get_const(ctx, callable);
779777
}
780778
sym_set_type(callable, &PyFunction_Type);
781779
}
@@ -785,7 +783,7 @@ dummy_func(void) {
785783
PyMethodObject *method = (PyMethodObject *)sym_get_const(ctx, callable);
786784
assert(PyMethod_Check(method));
787785
ADD_OP(_CHECK_FUNCTION_VERSION_INLINE, 0, func_version);
788-
ctx->out_buffer[out_len - 1].operand1 = (uintptr_t)method->im_func;
786+
ctx->out_buffer[ctx->out_len - 1].operand1 = (uintptr_t)method->im_func;
789787
}
790788
sym_set_type(callable, &PyMethod_Type);
791789
}
@@ -1119,7 +1117,7 @@ dummy_func(void) {
11191117
if (sym_is_const(ctx, flag)) {
11201118
PyObject *value = sym_get_const(ctx, flag);
11211119
assert(value != NULL);
1122-
eliminate_pop_guard(this_instr, ctx, &out_len, value != Py_True);
1120+
eliminate_pop_guard(this_instr, ctx, value != Py_True);
11231121
}
11241122
sym_set_const(flag, Py_True);
11251123
}
@@ -1164,7 +1162,7 @@ dummy_func(void) {
11641162
if (sym_is_const(ctx, flag)) {
11651163
PyObject *value = sym_get_const(ctx, flag);
11661164
assert(value != NULL);
1167-
eliminate_pop_guard(this_instr, ctx, &out_len, value != Py_False);
1165+
eliminate_pop_guard(this_instr, ctx, value != Py_False);
11681166
}
11691167
sym_set_const(flag, Py_False);
11701168
}
@@ -1173,11 +1171,11 @@ dummy_func(void) {
11731171
if (sym_is_const(ctx, val)) {
11741172
PyObject *value = sym_get_const(ctx, val);
11751173
assert(value != NULL);
1176-
eliminate_pop_guard(this_instr, ctx, &out_len, !Py_IsNone(value));
1174+
eliminate_pop_guard(this_instr, ctx, !Py_IsNone(value));
11771175
}
11781176
else if (sym_has_type(val)) {
11791177
assert(!sym_matches_type(val, &_PyNone_Type));
1180-
eliminate_pop_guard(this_instr, ctx, &out_len, true);
1178+
eliminate_pop_guard(this_instr, ctx, true);
11811179
}
11821180
sym_set_const(val, Py_None);
11831181
}
@@ -1186,11 +1184,11 @@ dummy_func(void) {
11861184
if (sym_is_const(ctx, val)) {
11871185
PyObject *value = sym_get_const(ctx, val);
11881186
assert(value != NULL);
1189-
eliminate_pop_guard(this_instr, ctx, &out_len, Py_IsNone(value));
1187+
eliminate_pop_guard(this_instr, ctx, Py_IsNone(value));
11901188
}
11911189
else if (sym_has_type(val)) {
11921190
assert(!sym_matches_type(val, &_PyNone_Type));
1193-
eliminate_pop_guard(this_instr, ctx, &out_len, false);
1191+
eliminate_pop_guard(this_instr, ctx, false);
11941192
}
11951193
}
11961194

@@ -1525,7 +1523,7 @@ dummy_func(void) {
15251523
ctx->frame->globals_watched = true;
15261524
}
15271525
if (ctx->frame->globals_checked_version != version && this_instr[-1].opcode == _NOP) {
1528-
REPLACE_OP(&ctx->out_buffer[out_len - 1], _GUARD_GLOBALS_VERSION, 0, version);
1526+
REPLACE_OP(&ctx->out_buffer[ctx->out_len - 1], _GUARD_GLOBALS_VERSION, 0, version);
15291527
ctx->frame->globals_checked_version = version;
15301528
}
15311529
if (ctx->frame->globals_checked_version == version) {

0 commit comments

Comments
 (0)