Skip to content

Commit 5e96e4f

Browse files
authored
gh-131798: JIT: Propagate the result in _BINARY_OP_SUBSCR_TUPLE_INT (GH-133003)
1 parent 56c88e4 commit 5e96e4f

File tree

4 files changed

+61
-1
lines changed

4 files changed

+61
-1
lines changed

Lib/test/test_capi/test_opt.py

+17
Original file line numberDiff line numberDiff line change
@@ -1923,6 +1923,23 @@ def testfunc(n):
19231923
self.assertNotIn("_GUARD_TOS_INT", uops)
19241924
self.assertIn("_CALL_LEN", uops)
19251925

1926+
def test_binary_op_subscr_tuple_int(self):
1927+
def testfunc(n):
1928+
x = 0
1929+
for _ in range(n):
1930+
y = (1, 2)
1931+
if y[0] == 1: # _COMPARE_OP_INT + _GUARD_IS_TRUE_POP are removed
1932+
x += 1
1933+
return x
1934+
1935+
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
1936+
self.assertEqual(res, TIER2_THRESHOLD)
1937+
self.assertIsNotNone(ex)
1938+
uops = get_opnames(ex)
1939+
self.assertIn("_BINARY_OP_SUBSCR_TUPLE_INT", uops)
1940+
self.assertNotIn("_COMPARE_OP_INT", uops)
1941+
self.assertNotIn("_GUARD_IS_TRUE_POP", uops)
1942+
19261943

19271944
def global_identity(x):
19281945
return x
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Propagate the return type of ``_BINARY_OP_SUBSCR_TUPLE_INT`` in JIT. Patch
2+
by Tomas Roun

Python/optimizer_bytecodes.c

+21
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,27 @@ dummy_func(void) {
370370
res = sym_new_type(ctx, &PyUnicode_Type);
371371
}
372372

373+
op(_BINARY_OP_SUBSCR_TUPLE_INT, (tuple_st, sub_st -- res)) {
374+
assert(sym_matches_type(tuple_st, &PyTuple_Type));
375+
if (sym_is_const(ctx, sub_st)) {
376+
assert(PyLong_CheckExact(sym_get_const(ctx, sub_st)));
377+
long index = PyLong_AsLong(sym_get_const(ctx, sub_st));
378+
assert(index >= 0);
379+
int tuple_length = sym_tuple_length(tuple_st);
380+
if (tuple_length == -1) {
381+
// Unknown length
382+
res = sym_new_not_null(ctx);
383+
}
384+
else {
385+
assert(index < tuple_length);
386+
res = sym_tuple_getitem(ctx, tuple_st, index);
387+
}
388+
}
389+
else {
390+
res = sym_new_not_null(ctx);
391+
}
392+
}
393+
373394
op(_TO_BOOL, (value -- res)) {
374395
int already_bool = optimize_to_bool(this_instr, ctx, value, &res);
375396
if (!already_bool) {

Python/optimizer_cases.c.h

+21-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)