Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

GH-115480: Reduce guard strength for binary ops when type of one operand is known already #118050

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

Merged
merged 4 commits into from
Apr 22, 2024
Merged
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
Next Next commit
Reduce strength of binary guards when one side is known type.
  • Loading branch information
markshannon committed Apr 17, 2024
commit 646672c41aeb2b795ff1e2cf0a0c979c896bbf82
1 change: 1 addition & 0 deletions 1 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 168 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 16 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 44 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 16 Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,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 @@ -460,6 +468,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
Morty Proxy This is a proxified and sanitized view of the page, visit original site.