Skip to content

Commit 7457d45

Browse files
committed
Add free-threaded specialization for COMPARE_OP, and tests for COMPARE_OP
specialization in general.
1 parent 2e95c5b commit 7457d45

File tree

4 files changed

+35
-7
lines changed

4 files changed

+35
-7
lines changed

Lib/test/test_dis.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,6 +1335,32 @@ def test_call_specialize(self):
13351335
got = self.get_disassembly(co, adaptive=True)
13361336
self.do_disassembly_compare(got, call_quicken)
13371337

1338+
@cpython_only
1339+
@requires_specialization_ft
1340+
def test_compare_specialize(self):
1341+
compare_op_quicken = """\
1342+
0 RESUME_CHECK 0
1343+
1344+
1 LOAD_NAME 0 (a)
1345+
LOAD_NAME 1 (b)
1346+
%s
1347+
RETURN_VALUE
1348+
"""
1349+
co_int = compile('a == b', "<int>", "eval")
1350+
self.code_quicken(lambda: exec(co_int, {}, {'a': 1, 'b': 2}))
1351+
got = self.get_disassembly(co_int, adaptive=True)
1352+
self.do_disassembly_compare(got, compare_op_quicken % "COMPARE_OP_INT 72 (==)")
1353+
1354+
co_float = compile('a == b', "<int>", "eval")
1355+
self.code_quicken(lambda: exec(co_float, {}, {'a': 1.0, 'b': 2.0}))
1356+
got = self.get_disassembly(co_float, adaptive=True)
1357+
self.do_disassembly_compare(got, compare_op_quicken % "COMPARE_OP_FLOAT 72 (==)")
1358+
1359+
co_unicode = compile('a == b', "<unicode>", "eval")
1360+
self.code_quicken(lambda: exec(co_unicode, {}, {'a': 'a', 'b': 'b'}))
1361+
got = self.get_disassembly(co_unicode, adaptive=True)
1362+
self.do_disassembly_compare(got, compare_op_quicken % "COMPARE_OP_STR 72 (==)")
1363+
13381364
@cpython_only
13391365
@requires_specialization
13401366
def test_loop_quicken(self):

Python/bytecodes.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2381,7 +2381,7 @@ dummy_func(
23812381
};
23822382

23832383
specializing op(_SPECIALIZE_COMPARE_OP, (counter/1, left, right -- left, right)) {
2384-
#if ENABLE_SPECIALIZATION
2384+
#if ENABLE_SPECIALIZATION_FT
23852385
if (ADAPTIVE_COUNTER_TRIGGERS(counter)) {
23862386
next_instr = this_instr;
23872387
_Py_Specialize_CompareOp(left, right, next_instr, oparg);

Python/generated_cases.c.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/specialize.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2382,8 +2382,9 @@ _Py_Specialize_CompareOp(_PyStackRef lhs_st, _PyStackRef rhs_st, _Py_CODEUNIT *i
23822382
{
23832383
PyObject *lhs = PyStackRef_AsPyObjectBorrow(lhs_st);
23842384
PyObject *rhs = PyStackRef_AsPyObjectBorrow(rhs_st);
2385+
uint8_t specialized_op;
23852386

2386-
assert(ENABLE_SPECIALIZATION);
2387+
assert(ENABLE_SPECIALIZATION_FT);
23872388
assert(_PyOpcode_Caches[COMPARE_OP] == INLINE_CACHE_ENTRIES_COMPARE_OP);
23882389
// All of these specializations compute boolean values, so they're all valid
23892390
// regardless of the fifth-lowest oparg bit.
@@ -2393,12 +2394,12 @@ _Py_Specialize_CompareOp(_PyStackRef lhs_st, _PyStackRef rhs_st, _Py_CODEUNIT *i
23932394
goto failure;
23942395
}
23952396
if (PyFloat_CheckExact(lhs)) {
2396-
instr->op.code = COMPARE_OP_FLOAT;
2397+
specialized_op = COMPARE_OP_FLOAT;
23972398
goto success;
23982399
}
23992400
if (PyLong_CheckExact(lhs)) {
24002401
if (_PyLong_IsCompact((PyLongObject *)lhs) && _PyLong_IsCompact((PyLongObject *)rhs)) {
2401-
instr->op.code = COMPARE_OP_INT;
2402+
specialized_op = COMPARE_OP_INT;
24022403
goto success;
24032404
}
24042405
else {
@@ -2413,18 +2414,19 @@ _Py_Specialize_CompareOp(_PyStackRef lhs_st, _PyStackRef rhs_st, _Py_CODEUNIT *i
24132414
goto failure;
24142415
}
24152416
else {
2416-
instr->op.code = COMPARE_OP_STR;
2417+
specialized_op = COMPARE_OP_STR;
24172418
goto success;
24182419
}
24192420
}
24202421
SPECIALIZATION_FAIL(COMPARE_OP, compare_op_fail_kind(lhs, rhs));
24212422
failure:
24222423
STAT_INC(COMPARE_OP, failure);
2423-
instr->op.code = COMPARE_OP;
2424+
SET_OPCODE_OR_RETURN(instr, COMPARE_OP);
24242425
cache->counter = adaptive_counter_backoff(cache->counter);
24252426
return;
24262427
success:
24272428
STAT_INC(COMPARE_OP, success);
2429+
SET_OPCODE_OR_RETURN(instr, specialized_op);
24282430
cache->counter = adaptive_counter_cooldown();
24292431
}
24302432

0 commit comments

Comments
 (0)