Skip to content

Commit

Permalink
pythonGH-115480: Reduce guard strength for binary ops when type of on…
Browse files Browse the repository at this point in the history
…e operand is known already (pythonGH-118050)
  • Loading branch information
markshannon authored Apr 22, 2024
1 parent ceb6038 commit a6647d1
Show file tree
Hide file tree
Showing 10 changed files with 316 additions and 103 deletions.
1 change: 1 addition & 0 deletions Include/internal/pycore_optimizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ extern bool _Py_uop_sym_set_type(_Py_UopsSymbol *sym, PyTypeObject *typ);
extern bool _Py_uop_sym_set_const(_Py_UopsSymbol *sym, PyObject *const_val);
extern bool _Py_uop_sym_is_bottom(_Py_UopsSymbol *sym);
extern int _Py_uop_sym_truthiness(_Py_UopsSymbol *sym);
extern PyTypeObject *_Py_uop_sym_get_type(_Py_UopsSymbol *sym);


extern int _Py_uop_abstractcontext_init(_Py_UOpsContext *ctx);
Expand Down
168 changes: 86 additions & 82 deletions Include/internal/pycore_uop_ids.h

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

16 changes: 16 additions & 0 deletions Include/internal/pycore_uop_metadata.h

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

44 changes: 42 additions & 2 deletions Lib/test/test_capi/test_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -903,10 +903,50 @@ def testfunc(n):
self.assertTrue(res)
self.assertIsNotNone(ex)
uops = get_opnames(ex)
guard_both_float_count = [opname for opname in iter_opnames(ex) if opname == "_GUARD_BOTH_INT"]
self.assertLessEqual(len(guard_both_float_count), 1)
guard_both_int_count = [opname for opname in iter_opnames(ex) if opname == "_GUARD_BOTH_INT"]
self.assertLessEqual(len(guard_both_int_count), 1)
self.assertIn("_COMPARE_OP_INT", uops)

def test_compare_op_type_propagation_int_partial(self):
def testfunc(n):
a = 1
for _ in range(n):
if a > 2:
x = 0
if a < 2:
x = 1
return x

res, ex = self._run_with_optimizer(testfunc, 32)
self.assertEqual(res, 1)
self.assertIsNotNone(ex)
uops = get_opnames(ex)
guard_left_int_count = [opname for opname in iter_opnames(ex) if opname == "_GUARD_NOS_INT"]
guard_both_int_count = [opname for opname in iter_opnames(ex) if opname == "_GUARD_BOTH_INT"]
self.assertLessEqual(len(guard_left_int_count), 1)
self.assertEqual(len(guard_both_int_count), 0)
self.assertIn("_COMPARE_OP_INT", uops)

def test_compare_op_type_propagation_float_partial(self):
def testfunc(n):
a = 1.0
for _ in range(n):
if a > 2.0:
x = 0
if a < 2.0:
x = 1
return x

res, ex = self._run_with_optimizer(testfunc, 32)
self.assertEqual(res, 1)
self.assertIsNotNone(ex)
uops = get_opnames(ex)
guard_left_float_count = [opname for opname in iter_opnames(ex) if opname == "_GUARD_NOS_FLOAT"]
guard_both_float_count = [opname for opname in iter_opnames(ex) if opname == "_GUARD_BOTH_FLOAT"]
self.assertLessEqual(len(guard_left_float_count), 1)
self.assertEqual(len(guard_both_float_count), 0)
self.assertIn("_COMPARE_OP_FLOAT", uops)

def test_compare_op_type_propagation_unicode(self):
def testfunc(n):
a = ""
Expand Down
16 changes: 16 additions & 0 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,14 @@ dummy_func(
EXIT_IF(!PyLong_CheckExact(right));
}

op(_GUARD_NOS_INT, (left, unused -- left, unused)) {
EXIT_IF(!PyLong_CheckExact(left));
}

op(_GUARD_TOS_INT, (value -- value)) {
EXIT_IF(!PyLong_CheckExact(value));
}

pure op(_BINARY_OP_MULTIPLY_INT, (left, right -- res)) {
STAT_INC(BINARY_OP, hit);
res = _PyLong_Multiply((PyLongObject *)left, (PyLongObject *)right);
Expand Down Expand Up @@ -462,6 +470,14 @@ dummy_func(
EXIT_IF(!PyFloat_CheckExact(right));
}

op(_GUARD_NOS_FLOAT, (left, unused -- left, unused)) {
EXIT_IF(!PyFloat_CheckExact(left));
}

op(_GUARD_TOS_FLOAT, (value -- value)) {
EXIT_IF(!PyFloat_CheckExact(value));
}

pure op(_BINARY_OP_MULTIPLY_FLOAT, (left, right -- res)) {
STAT_INC(BINARY_OP, hit);
double dres =
Expand Down
Loading

0 comments on commit a6647d1

Please sign in to comment.