pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/python/cpython/pull/148146/files

"https://github.githubassets.com/assets/primer-primitives-10bf9dd67e3d70bd.css" /> gh-100239: Propagate type info through _BINARY_OP_EXTEND in tier 2 by eendebakpt · Pull Request #148146 · python/cpython · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 7 additions & 0 deletions Include/internal/pycore_code.h
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,13 @@ typedef struct {
int oparg;
binaryopguardfunc guard;
binaryopactionfunc action;
/* Static type of the result, or NULL if unknown. Used by the tier 2
optimizer to propagate type information through _BINARY_OP_EXTEND. */
PyTypeObject *result_type;
/* Nonzero iff `action` always returns a freshly allocated object (not
aliased to either operand). Used by the tier 2 optimizer to enable
inplace follow-up ops. */
int result_unique;
Comment on lines +502 to +505
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this always true?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, here it is. In the pr this was factored out from we had more specializations where this would not always be true.

We can remove it here and add back later. I think it would be needed of we use the binary_op_extend as s mechanism for adding more cases to tier 2 without creating more tier 1 opcodes

} _PyBinaryOpSpecializationDescr;

/* Comparison bit masks. */
Expand Down
23 changes: 23 additions & 0 deletions Lib/test/test_capi/test_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -3813,6 +3813,29 @@ def f(n):
self.assertIn("_UNPACK_SEQUENCE_TWO_TUPLE", uops)
self.assertNotIn("_GUARD_TOS_TUPLE", uops)

def test_binary_op_extend_float_result_enables_inplace_multiply(self):
# (2 + x) * y with x, y floats: `2 + x` goes through _BINARY_OP_EXTEND
# (int + float). The result_type/result_unique info should let the
# subsequent float multiply use the inplace variant.
def testfunc(n):
x = 3.5
y = 2.0
res = 0.0
for _ in range(n):
res = (2 + x) * y
return res

res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
self.assertEqual(res, 11.0)
self.assertIsNotNone(ex)
uops = get_opnames(ex)
self.assertIn("_BINARY_OP_EXTEND", uops)
self.assertIn("_BINARY_OP_MULTIPLY_FLOAT_INPLACE", uops)
self.assertNotIn("_BINARY_OP_MULTIPLY_FLOAT", uops)
# NOS guard on the multiply is eliminated because _BINARY_OP_EXTEND
# propagates PyFloat_Type.
self.assertNotIn("_GUARD_NOS_FLOAT", uops)

def test_unary_invert_long_type(self):
def testfunc(n):
for _ in range(n):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Propagate result type and uniqueness information through
``_BINARY_OP_EXTEND`` in the tier 2 optimizer, enabling elimination of
downstream type guards and selection of inplace float operations.
12 changes: 10 additions & 2 deletions Python/optimizer_bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,16 @@ dummy_func(void) {
}

op(_BINARY_OP_EXTEND, (descr/4, left, right -- res, l, r)) {
(void)descr;
res = sym_new_not_null(ctx);
_PyBinaryOpSpecializationDescr *d = (_PyBinaryOpSpecializationDescr *)descr;
if (d != NULL && d->result_type != NULL) {
res = sym_new_type(ctx, d->result_type);
if (d->result_unique) {
res = PyJitRef_MakeUnique(res);
}
}
else {
res = sym_new_not_null(ctx);
}
l = left;
r = right;
}
Expand Down
12 changes: 10 additions & 2 deletions Python/optimizer_cases.c.h

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

28 changes: 14 additions & 14 deletions Python/specialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -2195,24 +2195,24 @@ LONG_FLOAT_ACTION(compactlong_float_true_div, /)

static _PyBinaryOpSpecializationDescr binaryop_extend_descrs[] = {
/* long-long arithmetic */
{NB_OR, compactlongs_guard, compactlongs_or},
{NB_AND, compactlongs_guard, compactlongs_and},
{NB_XOR, compactlongs_guard, compactlongs_xor},
{NB_INPLACE_OR, compactlongs_guard, compactlongs_or},
{NB_INPLACE_AND, compactlongs_guard, compactlongs_and},
{NB_INPLACE_XOR, compactlongs_guard, compactlongs_xor},
{NB_OR, compactlongs_guard, compactlongs_or, &PyLong_Type, 1},
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These can return small int too right? In that case, the result would not be unique

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The contract is that operations making use of the uniqueness can handle small ints. (The inplace versions of BINARY_ADD_INT handle this for example).

{NB_AND, compactlongs_guard, compactlongs_and, &PyLong_Type, 1},
{NB_XOR, compactlongs_guard, compactlongs_xor, &PyLong_Type, 1},
{NB_INPLACE_OR, compactlongs_guard, compactlongs_or, &PyLong_Type, 1},
{NB_INPLACE_AND, compactlongs_guard, compactlongs_and, &PyLong_Type, 1},
{NB_INPLACE_XOR, compactlongs_guard, compactlongs_xor, &PyLong_Type, 1},

/* float-long arithemetic */
{NB_ADD, float_compactlong_guard, float_compactlong_add},
{NB_SUBTRACT, float_compactlong_guard, float_compactlong_subtract},
{NB_TRUE_DIVIDE, nonzero_float_compactlong_guard, float_compactlong_true_div},
{NB_MULTIPLY, float_compactlong_guard, float_compactlong_multiply},
{NB_ADD, float_compactlong_guard, float_compactlong_add, &PyFloat_Type, 1},
{NB_SUBTRACT, float_compactlong_guard, float_compactlong_subtract, &PyFloat_Type, 1},
{NB_TRUE_DIVIDE, nonzero_float_compactlong_guard, float_compactlong_true_div, &PyFloat_Type, 1},
{NB_MULTIPLY, float_compactlong_guard, float_compactlong_multiply, &PyFloat_Type, 1},

/* float-float arithmetic */
{NB_ADD, compactlong_float_guard, compactlong_float_add},
{NB_SUBTRACT, compactlong_float_guard, compactlong_float_subtract},
{NB_TRUE_DIVIDE, nonzero_compactlong_float_guard, compactlong_float_true_div},
{NB_MULTIPLY, compactlong_float_guard, compactlong_float_multiply},
{NB_ADD, compactlong_float_guard, compactlong_float_add, &PyFloat_Type, 1},
{NB_SUBTRACT, compactlong_float_guard, compactlong_float_subtract, &PyFloat_Type, 1},
{NB_TRUE_DIVIDE, nonzero_compactlong_float_guard, compactlong_float_true_div, &PyFloat_Type, 1},
{NB_MULTIPLY, compactlong_float_guard, compactlong_float_multiply, &PyFloat_Type, 1},
};

static int
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy