Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-115999: Add free-threaded specialization for COMPARE_OP #126410

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions Lib/test/test_opcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -1520,6 +1520,39 @@ def binary_subscr_str_int():
self.assert_specialized(binary_subscr_str_int, "BINARY_SUBSCR_STR_INT")
self.assert_no_opcode(binary_subscr_str_int, "BINARY_SUBSCR")

@cpython_only
@requires_specialization_ft
def test_compare_op(self):
def compare_op_int():
for _ in range(100):
a, b = 1, 2
c = a == b
self.assertFalse(c)

compare_op_int()
self.assert_specialized(compare_op_int, "COMPARE_OP_INT")
self.assert_no_opcode(compare_op_int, "COMPARE_OP")

def compare_op_float():
for _ in range(100):
a, b = 1.0, 2.0
c = a == b
self.assertFalse(c)

compare_op_float()
self.assert_specialized(compare_op_float, "COMPARE_OP_FLOAT")
self.assert_no_opcode(compare_op_float, "COMPARE_OP")

def compare_op_str():
for _ in range(100):
a, b = "spam", "ham"
c = a == b
self.assertFalse(c)

compare_op_str()
self.assert_specialized(compare_op_str, "COMPARE_OP_STR")
self.assert_no_opcode(compare_op_str, "COMPARE_OP")


if __name__ == "__main__":
unittest.main()
2 changes: 1 addition & 1 deletion Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -2404,7 +2404,7 @@ dummy_func(
};

specializing op(_SPECIALIZE_COMPARE_OP, (counter/1, left, right -- left, right)) {
#if ENABLE_SPECIALIZATION
#if ENABLE_SPECIALIZATION_FT
if (ADAPTIVE_COUNTER_TRIGGERS(counter)) {
next_instr = this_instr;
_Py_Specialize_CompareOp(left, right, next_instr, oparg);
Expand Down
2 changes: 1 addition & 1 deletion Python/generated_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 7 additions & 9 deletions Python/specialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -2415,23 +2415,24 @@
{
PyObject *lhs = PyStackRef_AsPyObjectBorrow(lhs_st);
PyObject *rhs = PyStackRef_AsPyObjectBorrow(rhs_st);
uint8_t specialized_op;

assert(ENABLE_SPECIALIZATION);
assert(ENABLE_SPECIALIZATION_FT);
assert(_PyOpcode_Caches[COMPARE_OP] == INLINE_CACHE_ENTRIES_COMPARE_OP);
// All of these specializations compute boolean values, so they're all valid
// regardless of the fifth-lowest oparg bit.
_PyCompareOpCache *cache = (_PyCompareOpCache *)(instr + 1);

Check warning on line 2424 in Python/specialize.c

View workflow job for this annotation

GitHub Actions / Ubuntu / build and test (ubuntu-24.04)

unused variable ‘cache’ [-Wunused-variable]

Check warning on line 2424 in Python/specialize.c

View workflow job for this annotation

GitHub Actions / Ubuntu (free-threading) / build and test (ubuntu-24.04)

unused variable ‘cache’ [-Wunused-variable]

Check warning on line 2424 in Python/specialize.c

View workflow job for this annotation

GitHub Actions / Hypothesis tests on Ubuntu

unused variable ‘cache’ [-Wunused-variable]

Check warning on line 2424 in Python/specialize.c

View workflow job for this annotation

GitHub Actions / Address sanitizer (ubuntu-24.04)

unused variable ‘cache’ [-Wunused-variable]
if (Py_TYPE(lhs) != Py_TYPE(rhs)) {
SPECIALIZATION_FAIL(COMPARE_OP, compare_op_fail_kind(lhs, rhs));
goto failure;
}
if (PyFloat_CheckExact(lhs)) {
instr->op.code = COMPARE_OP_FLOAT;
specialized_op = COMPARE_OP_FLOAT;
Yhg1s marked this conversation as resolved.
Show resolved Hide resolved
goto success;
}
if (PyLong_CheckExact(lhs)) {
if (_PyLong_IsCompact((PyLongObject *)lhs) && _PyLong_IsCompact((PyLongObject *)rhs)) {
instr->op.code = COMPARE_OP_INT;
specialized_op = COMPARE_OP_INT;
goto success;
}
else {
Expand All @@ -2446,19 +2447,16 @@
goto failure;
}
else {
instr->op.code = COMPARE_OP_STR;
specialized_op = COMPARE_OP_STR;
goto success;
}
}
SPECIALIZATION_FAIL(COMPARE_OP, compare_op_fail_kind(lhs, rhs));
failure:
STAT_INC(COMPARE_OP, failure);
instr->op.code = COMPARE_OP;
cache->counter = adaptive_counter_backoff(cache->counter);
unspecialize(instr);
return;
success:
STAT_INC(COMPARE_OP, success);
cache->counter = adaptive_counter_cooldown();
specialize(instr, specialized_op);
}

#ifdef Py_STATS
Expand Down
Loading