diff options
| author | Guido van Rossum <guido@python.org> | 2023-07-11 22:13:57 (GMT) |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-07-11 22:13:57 (GMT) |
| commit | da86db56cb595fdbeda8b57a1ec03b1dd80ad1f0 (patch) | |
| tree | 64fe2ff2a6c9dfad7c839dcbde9354d0b3cfedca | |
| parent | d0972c77aa1cd5fe27618e82c10141a2bf157476 (diff) | |
| download | cpython-da86db56cb595fdbeda8b57a1ec03b1dd80ad1f0.zip cpython-da86db56cb595fdbeda8b57a1ec03b1dd80ad1f0.tar.gz cpython-da86db56cb595fdbeda8b57a1ec03b1dd80ad1f0.tar.bz2 | |
gh-106529: Implement JUMP_FORWARD in uops (with test) (#106651)
Note that this may generate two SAVE_IP uops in a row.
Removing unneeded SAVE_IP uops is the optimizer's job.
| -rw-r--r-- | Lib/test/test_capi/test_misc.py | 25 | ||||
| -rw-r--r-- | Python/optimizer.c | 7 |
2 files changed, 32 insertions, 0 deletions
diff --git a/Lib/test/test_capi/test_misc.py b/Lib/test/test_capi/test_misc.py index 9cbd550..9c14a50 100644 --- a/Lib/test/test_capi/test_misc.py +++ b/Lib/test/test_capi/test_misc.py @@ -2553,6 +2553,7 @@ class TestUops(unittest.TestCase): i = 0 while i < n: i += 1 + opt = _testinternalcapi.get_uop_optimizer() with temporary_optimizer(opt): testfunc(10) @@ -2562,6 +2563,30 @@ class TestUops(unittest.TestCase): uops = {opname for opname, _ in ex} self.assertIn("JUMP_TO_TOP", uops) + def test_jump_forward(self): + def testfunc(n): + a = 0 + while a < n: + if a < 0: + a = -a + else: + a = +a + a += 1 + return a + + opt = _testinternalcapi.get_uop_optimizer() + with temporary_optimizer(opt): + testfunc(10) + + ex = get_first_executor(testfunc) + self.assertIsNotNone(ex) + # for i, (opname, oparg) in enumerate(ex): + # print(f"{i:4d}: {opname:<20s} {oparg:4d}") + uops = {opname for opname, _ in ex} + # Since there is no JUMP_FORWARD instruction, + # look for indirect evidence: the += operator + self.assertIn("_BINARY_OP_ADD_INT", uops) + if __name__ == "__main__": unittest.main() diff --git a/Python/optimizer.c b/Python/optimizer.c index e2b92e6..7fc40e6 100644 --- a/Python/optimizer.c +++ b/Python/optimizer.c @@ -472,6 +472,13 @@ translate_bytecode_to_trace( goto done; } + case JUMP_FORWARD: + { + // This will emit two SAVE_IP instructions; leave it to the optimizer + instr += oparg; + break; + } + default: { const struct opcode_macro_expansion *expansion = &_PyOpcode_macro_expansion[opcode]; |
