Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Fidget-Spinner committed Nov 15, 2024
1 parent 0ca5cd1 commit 5fbc47a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
24 changes: 24 additions & 0 deletions Lib/test/test_capi/test_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -1515,9 +1515,33 @@ def test_jit_error_pops(self):
with self.assertRaises(TypeError):
{item for item in items}

def test_global_guard_hoisted_if_possible(self):
def testfunc(n):
for i in range(n):
# Only works on functions promoted to constants
# The inner global promoted guard should be hoisted out.
global_foo(i)

opt = _testinternalcapi.new_uop_optimizer()
with temporary_optimizer(opt):
testfunc(20)

ex = get_first_executor(testfunc)
self.assertIsNotNone(ex)
uops = get_opnames(ex)
opnames = list(iter_opnames(ex))
self.assertIn("_PUSH_FRAME", uops)
# Hoisted version
self.assertIn("_CHECK_FUNCTION_STACK", uops)
# Only 1 outer guard
self.assertLessEqual(opnames.count("_CHECK_FUNCTION"), 1)

def global_identity(x):
return x

def global_foo(x):
return global_identity(x)


if __name__ == "__main__":
unittest.main()
14 changes: 14 additions & 0 deletions Python/optimizer_bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,21 @@ dummy_func(void) {
// with partial evaluation later on.
(void)func_version;
if (ctx->frame->frame_creation_inst != NULL) {
// Already hoisted. Slot can't be used again.
if ((ctx->frame->frame_creation_inst - 1)->opcode == _CHECK_FUNCTION_STACK) {
break;
}
assert((ctx->frame->frame_creation_inst - 1)->opcode == _NOP);
bool has_interfering_inst = false;
for (_PyUOpInstruction *start = ctx->frame->frame_creation_inst + 1; start < this_instr; start++) {
if (_PyUop_Flags[start->opcode] & HAS_ESCAPES_FLAG) {
has_interfering_inst = true;
break;
}
}
if (has_interfering_inst) {
break;
}
*(ctx->frame->frame_creation_inst - 1) = *this_instr;
(ctx->frame->frame_creation_inst - 1)->opcode = _CHECK_FUNCTION_STACK;
(ctx->frame->frame_creation_inst - 1)->oparg = ctx->frame->frame_creation_inst->oparg;
Expand Down
14 changes: 14 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.

0 comments on commit 5fbc47a

Please sign in to comment.