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

gh-131798: JIT optimizer: Support custom binary op and property frames by Fidget-Spinner · Pull Request #143735 · 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  (1) .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
36 changes: 36 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 @@ -3538,6 +3538,42 @@ def testfunc(n):
# _POP_TOP_NOP is a sign the optimizer ran and didn't hit bottom.
self.assertGreaterEqual(count_ops(ex, "_POP_TOP_NOP"), 1)

def test_binary_op_subscr_init_frame(self):
class B:
def __getitem__(self, other):
return other + 1
def testfunc(*args):
n, b = args[0]
for _ in range(n):
y = b[2]

res, ex = self._run_with_optimizer(testfunc, (TIER2_THRESHOLD, B()))
self.assertIsNotNone(ex)
uops = get_opnames(ex)

self.assertIn("_BINARY_OP_SUBSCR_INIT_CALL", uops)
# _POP_TOP_NOP is a sign the optimizer ran and didn't hit contradiction.
self.assertGreaterEqual(count_ops(ex, "_POP_TOP_NOP"), 1)

def test_load_attr_property_frame(self):
class B:
@property
def prop(self):
return 3
def testfunc(*args):
n, b = args[0]
for _ in range(n):
y = b.prop + b.prop

testfunc((3, B()))
res, ex = self._run_with_optimizer(testfunc, (TIER2_THRESHOLD, B()))
self.assertIsNotNone(ex)
uops = get_opnames(ex)

self.assertIn("_LOAD_ATTR_PROPERTY_FRAME", uops)
# This is a sign the optimizer ran and didn't hit contradiction.
self.assertIn("_INSERT_2_LOAD_CONST_INLINE_BORROW", uops)

def test_unary_negative(self):
def testfunc(n):
a = 3
Expand Down
33 changes: 27 additions & 6 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 @@ -327,9 +327,20 @@ dummy_func(void) {
GETLOCAL(this_instr->operand0) = sym_new_null(ctx);
}

op(_BINARY_OP_SUBSCR_INIT_CALL, (container, sub, getitem -- new_frame)) {
new_frame = PyJitRef_NULL;
ctx->done = true;
op(_BINARY_OP_SUBSCR_INIT_CALL, (container, sub, getitem -- new_frame)) {
assert((this_instr + 1)->opcode == _PUSH_FRAME);
PyCodeObject *co = get_code_with_logging(this_instr + 1);
if (co == NULL) {
ctx->done = true;
break;
}
_Py_UOpsAbstractFrame *f = frame_new(ctx, co, 0, NULL, 0);
if (f == NULL) {
break;
}
f->locals[0] = container;
f->locals[1] = sub;
new_frame = PyJitRef_Wrap((JitOptSymbol *)f);
}

op(_BINARY_OP_SUBSCR_STR_INT, (str_st, sub_st -- res, s, i)) {
Expand Down Expand Up @@ -759,9 +770,19 @@ dummy_func(void) {
}

op(_LOAD_ATTR_PROPERTY_FRAME, (fget/4, owner -- new_frame)) {
(void)fget;
new_frame = PyJitRef_NULL;
ctx->done = true;
// + 1 for _SAVE_RETURN_OFFSET
assert((this_instr + 2)->opcode == _PUSH_FRAME);
PyCodeObject *co = get_code_with_logging(this_instr + 2);
Comment thread
savannahostrowski marked this conversation as resolved.
if (co == NULL) {
ctx->done = true;
break;
}
_Py_UOpsAbstractFrame *f = frame_new(ctx, co, 0, NULL, 0);
if (f == NULL) {
break;
}
f->locals[0] = owner;
Comment thread
savannahostrowski marked this conversation as resolved.
new_frame = PyJitRef_Wrap((JitOptSymbol *)f);
}

op(_INIT_CALL_BOUND_METHOD_EXACT_ARGS, (callable, self_or_null, unused[oparg] -- callable, self_or_null, unused[oparg])) {
Expand Down
36 changes: 31 additions & 5 deletions Python/optimizer_cases.c.h

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

Loading

Back | FazBrowse Home | New Git URL