Skip to content

Commit 6c83c72

Browse files
committed
fix review idea
Signed-off-by: Manjusaka <me@manjusaka.me>
1 parent dfbd0c2 commit 6c83c72

File tree

9 files changed

+1050
-986
lines changed

9 files changed

+1050
-986
lines changed

Include/internal/pycore_uop_ids.h

Lines changed: 922 additions & 918 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_uop_metadata.h

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Lib/test/test_capi/test_opt.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2949,6 +2949,7 @@ def f(n):
29492949
self.assertIsNotNone(ex)
29502950
uops = get_opnames(ex)
29512951
self.assertIn("_TO_BOOL_STR", uops)
2952+
self.assertLessEqual(count_ops(ex, "_POP_TOP"), 3)
29522953
self.assertIn("_POP_TOP_NOP", uops)
29532954

29542955
def test_attr_promotion_failure(self):

Python/bytecodes.c

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -550,18 +550,9 @@ dummy_func(
550550
op(_TO_BOOL_STR, (value -- res, v)) {
551551
STAT_INC(TO_BOOL, hit);
552552
PyObject *value_o = PyStackRef_AsPyObjectBorrow(value);
553-
if (value_o == &_Py_STR(empty)) {
554-
assert(_Py_IsImmortal(value_o));
555-
v = value;
556-
DEAD(value);
557-
res = PyStackRef_False;
558-
}
559-
else {
560-
assert(Py_SIZE(value_o));
561-
v = value;
562-
DEAD(value);
563-
res = PyStackRef_True;
564-
}
553+
res = value_o == &_Py_STR(empty) ? PyStackRef_False : PyStackRef_True;
554+
v = value;
555+
DEAD(value);
565556
}
566557

567558
macro(TO_BOOL_STR) =
@@ -5311,6 +5302,12 @@ dummy_func(
53115302
value = PyStackRef_FromPyObjectBorrow(ptr);
53125303
}
53135304

5305+
tier2 op(_INSERT_1_LOAD_CONST_INLINE_BORROW, (ptr/4, left -- res, l)) {
5306+
res = PyStackRef_FromPyObjectBorrow(ptr);
5307+
l = left;
5308+
INPUTS_DEAD();
5309+
}
5310+
53145311
tier2 op(_SHUFFLE_2_LOAD_CONST_INLINE_BORROW, (ptr/4, callable, null, arg -- res, a)) {
53155312
res = PyStackRef_FromPyObjectBorrow(ptr);
53165313
a = arg;

Python/executor_cases.c.h

Lines changed: 63 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/generated_cases.c.h

Lines changed: 2 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/optimizer_analysis.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,8 @@ optimize_to_bool(
214214
_PyUOpInstruction *this_instr,
215215
JitOptContext *ctx,
216216
JitOptRef value,
217-
JitOptRef *result_ptr)
217+
JitOptRef *result_ptr,
218+
bool insert_mode)
218219
{
219220
if (sym_matches_type(value, &PyBool_Type)) {
220221
REPLACE_OP(this_instr, _NOP, 0, 0);
@@ -224,7 +225,10 @@ optimize_to_bool(
224225
int truthiness = sym_truthiness(ctx, value);
225226
if (truthiness >= 0) {
226227
PyObject *load = truthiness ? Py_True : Py_False;
227-
REPLACE_OP(this_instr, _POP_TOP_LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)load);
228+
int opcode = insert_mode ?
229+
_INSERT_1_LOAD_CONST_INLINE_BORROW :
230+
_POP_TOP_LOAD_CONST_INLINE_BORROW;
231+
REPLACE_OP(this_instr, opcode, 0, (uintptr_t)load);
228232
*result_ptr = sym_new_const(ctx, load);
229233
return 1;
230234
}

Python/optimizer_bytecodes.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ optimize_to_bool(
4444
_PyUOpInstruction *this_instr,
4545
JitOptContext *ctx,
4646
JitOptSymbol *value,
47-
JitOptSymbol **result_ptr);
47+
JitOptSymbol **result_ptr,
48+
bool insert_mode);
4849

4950
extern void
5051
eliminate_pop_guard(_PyUOpInstruction *this_instr, bool exit);
@@ -90,7 +91,7 @@ dummy_func(void) {
9091
}
9192

9293
op(_LOAD_FAST_BORROW, (-- value)) {
93-
value = PyJitRef_Borrow(GETLOCAL(oparg));
94+
value = PyJitRef_Borrow(GETLOCAL(oparg));
9495
}
9596

9697
op(_LOAD_FAST_AND_CLEAR, (-- value)) {
@@ -377,36 +378,36 @@ dummy_func(void) {
377378
}
378379

379380
op(_TO_BOOL, (value -- res)) {
380-
int already_bool = optimize_to_bool(this_instr, ctx, value, &res);
381+
int already_bool = optimize_to_bool(this_instr, ctx, value, &res, false);
381382
if (!already_bool) {
382383
res = sym_new_truthiness(ctx, value, true);
383384
}
384385
}
385386

386387
op(_TO_BOOL_BOOL, (value -- value)) {
387-
int already_bool = optimize_to_bool(this_instr, ctx, value, &value);
388+
int already_bool = optimize_to_bool(this_instr, ctx, value, &value, false);
388389
if (!already_bool) {
389390
sym_set_type(value, &PyBool_Type);
390391
}
391392
}
392393

393394
op(_TO_BOOL_INT, (value -- res)) {
394-
int already_bool = optimize_to_bool(this_instr, ctx, value, &res);
395+
int already_bool = optimize_to_bool(this_instr, ctx, value, &res, false);
395396
if (!already_bool) {
396397
sym_set_type(value, &PyLong_Type);
397398
res = sym_new_truthiness(ctx, value, true);
398399
}
399400
}
400401

401402
op(_TO_BOOL_LIST, (value -- res)) {
402-
int already_bool = optimize_to_bool(this_instr, ctx, value, &res);
403+
int already_bool = optimize_to_bool(this_instr, ctx, value, &res, false);
403404
if (!already_bool) {
404405
res = sym_new_type(ctx, &PyBool_Type);
405406
}
406407
}
407408

408409
op(_TO_BOOL_NONE, (value -- res)) {
409-
int already_bool = optimize_to_bool(this_instr, ctx, value, &res);
410+
int already_bool = optimize_to_bool(this_instr, ctx, value, &res, false);
410411
if (!already_bool) {
411412
sym_set_const(value, Py_None);
412413
res = sym_new_const(ctx, Py_False);
@@ -432,7 +433,7 @@ dummy_func(void) {
432433
}
433434

434435
op(_TO_BOOL_STR, (value -- res, v)) {
435-
int already_bool = optimize_to_bool(this_instr, ctx, value, &res);
436+
int already_bool = optimize_to_bool(this_instr, ctx, value, &res, true);
436437
v = value;
437438
if (!already_bool) {
438439
res = sym_new_truthiness(ctx, value, true);

0 commit comments

Comments
 (0)