Skip to content

Commit 46f6db4

Browse files
Fix: infer_BINARY_OP now supports smallint input (python#46)
Co-authored-by: Ken Jin <kenjin4096@gmail.com>
1 parent 0af2c7e commit 46f6db4

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

Python/tier2.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1616,12 +1616,13 @@ infer_BINARY_OP(
16161616
PyTypeObject *righttype = (PyTypeObject *)_Py_TYPENODE_CLEAR_TAG(rightroot);
16171617

16181618
if (_Py_TYPENODE_IS_POSITIVE_NULL(leftroot)
1619-
&& (righttype == &PyLong_Type
1619+
&& (righttype == &PyLong_Type || righttype == &PySmallInt_Type
16201620
|| righttype == &PyFloat_Type || righttype == &PyRawFloat_Type)) {
16211621
// Check if same type as right
16221622
*needs_guard = true;
16231623
write_curr = emit_type_guard(write_curr,
1624-
righttype == &PyLong_Type ? CHECK_INT : CHECK_FLOAT, 1, bb_id);
1624+
(righttype == &PyLong_Type || righttype == &PySmallInt_Type)
1625+
? CHECK_INT : CHECK_FLOAT, 1, bb_id);
16251626
return write_curr;
16261627
}
16271628
if (_Py_TYPENODE_GET_TAG(leftroot) == TYPE_ROOT_NEGATIVE) {
@@ -1661,7 +1662,8 @@ infer_BINARY_OP(
16611662
type_propagate(opcode, 0, type_context, NULL);
16621663
return write_curr;
16631664
}
1664-
if (righttype == &PyLong_Type && lefttype == &PyLong_Type) {
1665+
if ((righttype == &PyLong_Type || righttype == &PySmallInt_Type)
1666+
&& (lefttype == &PyLong_Type || lefttype == &PySmallInt_Type)) {
16651667
int opcode = oparg == NB_ADD
16661668
? BINARY_OP_ADD_INT_REST
16671669
: oparg == NB_SUBTRACT

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ We provide pre-compiled binaries for 64-bit Windows 10/11 via GitHub releases.
138138
* Mark interpreter frames as tier 2 or not in https://github.com/pylbbv/pylbbv/pull/34
139139
* Make instruction offset calculation frame aware in https://github.com/pylbbv/pylbbv/pull/37
140140
* Support multiple entry points in a Basic Block in https://github.com/pylbbv/pylbbv/pull/39 and https://github.com/pylbbv/pylbbv/pull/40
141+
* `BINARY_OP` specialisations now apply to `PySmallInt_Type` in https://github.com/pylbbv/pylbbv/pull/46
141142
* Improved workflow:
142143
* workflow: enable CI GH actions tests in https://github.com/pylbbv/pylbbv/pull/35
143144
* Improved type propagator:

tier2_test.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,36 @@ def f(x, items):
615615

616616
# As long as it doesn't crash, everything's good
617617

618+
with TestInfo("infer_BINARY_OP to be compatible with smallint"):
619+
# See https://github.com/pylbbv/pylbbv/issues/45 for more information.
620+
621+
# Testing left smallint
622+
def f(a, b):
623+
z = a + b
624+
return z + 1
625+
626+
trigger_tier2(f, (1,1))
627+
insts = dis.get_instructions(test_typeprop1, tier2=True)
628+
assert [x.opname for x in insts].count("BINARY_OP_ADD_INT_REST") == 2
629+
630+
# Testing right smallint
631+
def f(a, b):
632+
z = a + b
633+
return 1 + z
634+
635+
trigger_tier2(f, (1,1))
636+
insts = dis.get_instructions(test_typeprop1, tier2=True)
637+
assert [x.opname for x in insts].count("BINARY_OP_ADD_INT_REST") == 2
638+
639+
# Testing both sides smallint
640+
def f(a, b):
641+
z = a + b
642+
w = 1
643+
return 1 + w
644+
trigger_tier2(f, (1,1))
645+
insts = dis.get_instructions(test_typeprop1, tier2=True)
646+
assert [x.opname for x in insts].count("BINARY_OP_ADD_INT_REST") == 2
647+
618648
print("Regression tests...Done!")
619649

620650
print("Tests completed ^-^")

0 commit comments

Comments
 (0)