FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-146381: Constant-fold frozendict subscript lookups via REPLACE_OPCODE_IF_EVALUATES_PURE by corona10 · Pull Request #146490 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (2) .h  (1) .py  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
33 changes: 33 additions & 0 deletions Lib/test/test_capi/test_opt.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
#For test of issue 136154
GLOBAL_136154 = 42

# For frozendict JIT tests
FROZEN_DICT_CONST = frozendict(x=1, y=2)


@contextlib.contextmanager
def clear_executors(func):
# Clear executors in func before and after running a block
Expand Down Expand Up @@ -4155,6 +4159,35 @@ def testfunc(n):
self.assertLessEqual(count_ops(ex, "_POP_TOP_INT"), 1)
self.assertIn("_POP_TOP_NOP", uops)

def test_binary_subscr_frozendict_lowering(self):
def testfunc(n):
x = 0
for _ in range(n):
x += FROZEN_DICT_CONST['x']
return x

res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
self.assertEqual(res, TIER2_THRESHOLD)
self.assertIsNotNone(ex)
uops = get_opnames(ex)
self.assertIn("_INSERT_2_LOAD_CONST_INLINE_BORROW", uops)
self.assertNotIn("_BINARY_OP_SUBSCR_DICT", uops)

def test_binary_subscr_frozendict_const_fold(self):
def testfunc(n):
x = 0
for _ in range(n):
if FROZEN_DICT_CONST['x'] == 1:
x += 1
return x

res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
self.assertEqual(res, TIER2_THRESHOLD)
self.assertIsNotNone(ex)
uops = get_opnames(ex)
# lookup result is folded to constant 1, so comparison is optimized away
self.assertNotIn("_COMPARE_OP_INT", uops)

def test_binary_subscr_list_slice(self):
def testfunc(n):
x = 0
Expand Down
3 changes: 3 additions & 0 deletions Python/optimizer_bytecodes.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,9 @@ dummy_func(void) {
res = sym_new_not_null(ctx);
ds = dict_st;
ss = sub_st;
if (sym_matches_type(dict_st, &PyFrozenDict_Type)) {
REPLACE_OPCODE_IF_EVALUATES_PURE(dict_st, sub_st, res);
}
}

op(_BINARY_OP_SUBSCR_LIST_SLICE, (list_st, sub_st -- res, ls, ss)) {
Expand Down
48 changes: 48 additions & 0 deletions Python/optimizer_cases.c.h

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

3 changes: 2 additions & 1 deletion Python/optimizer_symbols.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,8 @@ _Py_uop_sym_is_safe_const(JitOptContext *ctx, JitOptRef sym)
return (typ == &PyUnicode_Type) ||
(typ == &PyFloat_Type) ||
(typ == &_PyNone_Type) ||
(typ == &PyBool_Type);
(typ == &PyBool_Type) ||
(typ == &PyFrozenDict_Type);
}

void
Expand Down
Loading

Back | FazBrowse Home | New Git URL