classification
Title: Dead code in wordcode
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.7, Python 3.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: serhiy.storchaka Nosy List: Demur Rumed, gregory.p.smith, matrixise, python-dev, rhettinger, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2016-10-24 05:16 by serhiy.storchaka, last changed 2018-11-08 21:09 by gregory.p.smith. This issue is now closed.

Files
File name Uploaded Description Edit
peephole_remove_unreachable_code.patch serhiy.storchaka, 2016-10-24 07:37 review
Pull Requests
URL Status Linked Edit
PR 552 closed dstufft, 2017-03-31 16:36
Messages (8)
msg279297 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-10-24 05:16
>>> def func(test):
...     if test == 1:
...         return 1
...     elif test == 2:
...         return 2
...     return 3
... 
>>> import dis
>>> dis.dis(func)

Python 3.5:

  2           0 LOAD_FAST                0 (test)
              3 LOAD_CONST               1 (1)
              6 COMPARE_OP               2 (==)
              9 POP_JUMP_IF_FALSE       16

  3          12 LOAD_CONST               1 (1)
             15 RETURN_VALUE

  4     >>   16 LOAD_FAST                0 (test)
             19 LOAD_CONST               2 (2)
             22 COMPARE_OP               2 (==)
             25 POP_JUMP_IF_FALSE       32

  5          28 LOAD_CONST               2 (2)
             31 RETURN_VALUE

  6     >>   32 LOAD_CONST               3 (3)
             35 RETURN_VALUE

Python 3.6:

  2           0 LOAD_FAST                0 (test)
              2 LOAD_CONST               1 (1)
              4 COMPARE_OP               2 (==)
              6 POP_JUMP_IF_FALSE       14

  3           8 LOAD_CONST               1 (1)
             10 RETURN_VALUE
             12 JUMP_FORWARD            12 (to 26)

  4     >>   14 LOAD_FAST                0 (test)
             16 LOAD_CONST               2 (2)
             18 COMPARE_OP               2 (==)
             20 POP_JUMP_IF_FALSE       26

  5          22 LOAD_CONST               2 (2)
             24 RETURN_VALUE

  6     >>   26 LOAD_CONST               3 (3)
             28 RETURN_VALUE

Note JUMP_FORWARD after RETURN_VALUE in 3.6 listing.
msg279298 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-10-24 07:37
Proposed patch fixes removing unreachable code after RETURN_VALUE in peephole optimizer.
msg279320 - (view) Author: Stéphane Wirtel (matrixise) * (Python committer) Date: 2016-10-24 16:56
Hi Serhiy,

Could you help me, because I don't understand your patch ? because a RETURN_VALUE will go to the end of the block, and in this case, I don't understand why there is a JUMP_FORWARD at 12 in Python 3.6.
msg279348 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2016-10-25 02:54
+0 The situation this addresses isn't common and the patch will rarely produce a perceptable benefit (just making the disassembly look a little nicer).  That said, change looks simple enough and doesn't add any overhead.
msg279359 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2016-10-25 06:42
New changeset 5784cc37b5f4 by Serhiy Storchaka in branch '3.6':
Issue #28517: Fixed of-by-one error in the peephole optimizer that caused
https://hg.python.org/cpython/rev/5784cc37b5f4

New changeset 8d571fab4d66 by Serhiy Storchaka in branch 'default':
Issue #28517: Fixed of-by-one error in the peephole optimizer that caused
https://hg.python.org/cpython/rev/8d571fab4d66
msg279366 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-10-25 07:03
The main problem was that this bug caused unnecessary breakage of tests in Victor's bytecode [1].

Stéphane, the number of opcodes to the end of the block was counted incorrectly. Just off-by-one error. One unreachable opcode always was left.

[1] https://github.com/haypo/bytecode
msg279367 - (view) Author: Stéphane Wirtel (matrixise) * (Python committer) Date: 2016-10-25 07:10
ah ok, thanks for the explanation.
msg329486 - (view) Author: Gregory P. Smith (gregory.p.smith) * (Python committer) Date: 2018-11-08 21:09
the off by one error fix here introduced a new off by one error.  PR coming, follow https://bugs.python.org/issue35193 for that.
History
Date User Action Args
2018-11-08 21:09:05gregory.p.smithsetnosy: + gregory.p.smith
messages: + msg329486
2017-03-31 16:36:35dstufftsetpull_requests: + pull_request1084
2016-10-25 07:20:39serhiy.storchakasetstatus: open -> closed
2016-10-25 07:10:03matrixisesetmessages: + msg279367
2016-10-25 07:03:40serhiy.storchakasetresolution: fixed
messages: + msg279366
stage: patch review -> resolved
2016-10-25 06:42:30python-devsetnosy: + python-dev
messages: + msg279359
2016-10-25 02:54:53rhettingersetnosy: + rhettinger
messages: + msg279348
2016-10-24 16:56:44matrixisesetnosy: + matrixise
messages: + msg279320
2016-10-24 07:37:27serhiy.storchakasetfiles: + peephole_remove_unreachable_code.patch
messages: + msg279298

assignee: serhiy.storchaka
keywords: + patch
stage: patch review
2016-10-24 05:16:39serhiy.storchakacreate