Skip to content

Commit 6e8128f

Browse files
authored
bpo-41323: Perform 'peephole' optimizations directly on the CFG. (GH-21517)
* Move 'peephole' optimizations into compile.c and perform them directly on the CFG.
1 parent ba18c0b commit 6e8128f

12 files changed

+4358
-4577
lines changed

Lib/test/test_dis.py

-4
Original file line numberDiff line numberDiff line change
@@ -360,8 +360,6 @@ def _tryfinallyconst(b):
360360
16 CALL_FUNCTION 0
361361
18 POP_TOP
362362
20 RERAISE
363-
22 LOAD_CONST 0 (None)
364-
24 RETURN_VALUE
365363
""" % (_tryfinally.__code__.co_firstlineno + 1,
366364
_tryfinally.__code__.co_firstlineno + 2,
367365
_tryfinally.__code__.co_firstlineno + 4,
@@ -385,8 +383,6 @@ def _tryfinallyconst(b):
385383
16 CALL_FUNCTION 0
386384
18 POP_TOP
387385
20 RERAISE
388-
22 LOAD_CONST 0 (None)
389-
24 RETURN_VALUE
390386
""" % (_tryfinallyconst.__code__.co_firstlineno + 1,
391387
_tryfinallyconst.__code__.co_firstlineno + 2,
392388
_tryfinallyconst.__code__.co_firstlineno + 4,

Lib/test/test_peepholer.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -416,9 +416,9 @@ def f(cond1, cond2):
416416
if cond1: return 4
417417
self.assertNotInBytecode(f, 'JUMP_FORWARD')
418418
# There should be one jump for the while loop.
419-
returns = [instr for instr in dis.get_instructions(f)
420-
if instr.opname == 'JUMP_ABSOLUTE']
421-
self.assertEqual(len(returns), 1)
419+
jumps = [instr for instr in dis.get_instructions(f)
420+
if 'JUMP' in instr.opname]
421+
self.assertEqual(len(jumps), 1)
422422
returns = [instr for instr in dis.get_instructions(f)
423423
if instr.opname == 'RETURN_VALUE']
424424
self.assertLessEqual(len(returns), 2)

Lib/test/test_sys_settrace.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -948,7 +948,7 @@ def test_jump_in_nested_finally_3(output):
948948
output.append(11)
949949
output.append(12)
950950

951-
@jump_test(5, 11, [2, 4], (ValueError, 'unreachable'))
951+
@jump_test(5, 11, [2, 4], (ValueError, 'after'))
952952
def test_no_jump_over_return_try_finally_in_finally_block(output):
953953
try:
954954
output.append(2)
@@ -1457,7 +1457,7 @@ async def test_jump_between_async_with_blocks(output):
14571457
async with asynctracecontext(output, 4):
14581458
output.append(5)
14591459

1460-
@jump_test(5, 7, [2, 4], (ValueError, "unreachable"))
1460+
@jump_test(5, 7, [2, 4], (ValueError, "after"))
14611461
def test_no_jump_over_return_out_of_finally_block(output):
14621462
try:
14631463
output.append(2)

Makefile.pre.in

-1
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,6 @@ PYTHON_OBJS= \
354354
Python/mysnprintf.o \
355355
Python/mystrtoul.o \
356356
Python/pathconfig.o \
357-
Python/peephole.o \
358357
Python/preconfig.o \
359358
Python/pyarena.o \
360359
Python/pyctype.o \
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Bytecode optimizations are performed directly on the control flow graph.
2+
This will result in slightly more compact code objects in some
3+
circumstances.

PCbuild/pythoncore.vcxproj

-1
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,6 @@
460460
<ClCompile Include="..\Python\mysnprintf.c" />
461461
<ClCompile Include="..\Python\mystrtoul.c" />
462462
<ClCompile Include="..\Python\pathconfig.c" />
463-
<ClCompile Include="..\Python\peephole.c" />
464463
<ClCompile Include="..\Python\preconfig.c" />
465464
<ClCompile Include="..\Python\pyarena.c" />
466465
<ClCompile Include="..\Python\pyctype.c" />

PCbuild/pythoncore.vcxproj.filters

+1-4
Original file line numberDiff line numberDiff line change
@@ -1028,9 +1028,6 @@
10281028
<ClCompile Include="..\Python\pathconfig.c">
10291029
<Filter>Python</Filter>
10301030
</ClCompile>
1031-
<ClCompile Include="..\Python\peephole.c">
1032-
<Filter>Python</Filter>
1033-
</ClCompile>
10341031
<ClCompile Include="..\Python\preconfig.c">
10351032
<Filter>Python</Filter>
10361033
</ClCompile>
@@ -1184,4 +1181,4 @@
11841181
<Filter>Resource Files</Filter>
11851182
</ResourceCompile>
11861183
</ItemGroup>
1187-
</Project>
1184+
</Project>

0 commit comments

Comments
 (0)