Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-109889: fix compiler's redundant NOP detection to look past NOPs with no lineno when looking for the next instruction's lineno #109987

Merged
merged 2 commits into from
Sep 28, 2023

Conversation

iritkatriel
Copy link
Member

@iritkatriel iritkatriel commented Sep 27, 2023

…o lineno when looking for the next instruction's lineno
@@ -1017,7 +1017,17 @@ remove_redundant_nops(basicblock *bb) {
}
/* or if last instruction in BB and next BB has same line number */
if (next) {
if (lineno == next->b_instr[0].i_loc.lineno) {
location next_loc = NO_LOCATION;
for (int next_i=0; next_i < next->b_iused; next_i++) {
Copy link
Member

@carljm carljm Sep 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initially, I thought it would be simpler to have the same behavior whether the next instruction is in the same basicblock or the next one; i.e. it would be clearer to just match the behavior above on lines 1004-1011. If the next instruction has no location, set it to this NOP's location and remove this NOP.

But then I realized that this is not actually equivalent in the case where we are crossing to the next basicblock, because the next basicblock may be a jump target, and that jump should not report the location from the current NOP. So we can't safely use the "copy location to next instr without location" approach across the basicblock boundary.

I'm not sure if this subtlety will be obvious to future readers/modifiers of this code?

We could safely use this new behavior (skip NOP without lineno that will be removed) in all cases, and restructure the code so that first we find the next "relevant" instruction (whether in current or next basic block), and then compare its location with the current NOP. I think this would be somewhat clearer and simpler, but it's up to you whether you agree / think it's worth it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could safely use this new behavior (skip NOP without lineno that will be removed) in all cases

I think we are a bit more aggressive when it's within the same basic block: we will hand the location on to any instruction that doesn't have a line number, not just a NOP.

@markshannon
Copy link
Member

markshannon commented Sep 28, 2023

If I remove the assert statement on main and compile the test function, I see no NOPs:

Disassembly of <code object f at 0x7f1a9a409800, file "<stdin>", line 3>:
  3           0 RESUME                   0

  4           2 LOAD_GLOBAL              0 (b)
             12 TO_BOOL
             20 POP_JUMP_IF_FALSE        7 (to 38)
             24 LOAD_GLOBAL              4 (a)
             34 POP_TOP
             36 RETURN_CONST             0 (None)
        >>   38 LOAD_GLOBAL              2 (c)
             48 TO_BOOL
             56 POP_JUMP_IF_FALSE        7 (to 74)
             60 LOAD_GLOBAL              4 (a)
             70 POP_TOP
             72 RETURN_CONST             0 (None)
        >>   74 LOAD_GLOBAL              6 (d)
             84 POP_TOP
             86 RETURN_CONST             0 (None)

Is the change to the optimizer necessary, is there a case where it generates better output?
If not, then maybe just remove the assert?

@iritkatriel
Copy link
Member Author

If that's the case then we should try to understand where the NOP gets removed. I wouldn't remove the assertion before I understand what's going on.

@iritkatriel iritkatriel merged commit f580edc into python:main Sep 28, 2023
@miss-islington
Copy link
Contributor

Thanks @iritkatriel for the PR 🌮🎉.. I'm working now to backport this PR to: 3.12.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Sep 28, 2023
…NOPs with no lineno when looking for the next instruction's lineno (pythonGH-109987)

(cherry picked from commit f580edc)

Co-authored-by: Irit Katriel <[email protected]>
@bedevere-app
Copy link

bedevere-app bot commented Sep 28, 2023

GH-110048 is a backport of this pull request to the 3.12 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.12 bug and security fixes label Sep 28, 2023
@iritkatriel
Copy link
Member Author

I don't know why we do this in the optimizer and not in the assembler, after line numbers are propagated so it's easier to see what's redundant and what's not.

@bedevere-bot
Copy link

⚠️⚠️⚠️ Buildbot failure ⚠️⚠️⚠️

Hi! The buildbot AMD64 Debian root 3.x has failed when building commit f580edc.

What do you need to do:

  1. Don't panic.
  2. Check the buildbot page in the devguide if you don't know what the buildbots are or how they work.
  3. Go to the page of the buildbot that failed (https://buildbot.python.org/all/#builders/345/builds/5951) and take a look at the build logs.
  4. Check if the failure is related to this commit (f580edc) or if it is a false positive.
  5. If the failure is related to this commit, please, reflect that on the issue and make a new Pull Request with a fix.

You can take a look at the buildbot page here:

https://buildbot.python.org/all/#builders/345/builds/5951

Failed tests:

  • test_signal

Summary of the results of the build (if available):

==

Click to see traceback logs
Traceback (most recent call last):
  File "/root/buildarea/3.x.angelico-debian-amd64/build/Lib/test/test_signal.py", line 1287, in test_stress_delivery_dependent
    self.assertEqual(len(sigs), N, "Some signals were lost")
AssertionError: 4726 != 10000 : Some signals were lost

csm10495 pushed a commit to csm10495/cpython that referenced this pull request Sep 29, 2023
…NOPs with no lineno when looking for the next instruction's lineno (python#109987)
Yhg1s pushed a commit that referenced this pull request Oct 2, 2023
… NOPs with no lineno when looking for the next instruction's lineno (GH-109987) (#110048)

gh-109889: fix compiler's redundant NOP detection to look past NOPs with no lineno when looking for the next instruction's lineno (GH-109987)
(cherry picked from commit f580edc)

Co-authored-by: Irit Katriel <[email protected]>
Glyphack pushed a commit to Glyphack/cpython that referenced this pull request Sep 2, 2024
…NOPs with no lineno when looking for the next instruction's lineno (python#109987)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) performance Performance or resource usage
Projects
None yet
Development

Successfully merging this pull request may close these issues.

flowgraph optimize_cfg Assertion for if-else-expression with if-else-expression as condition
5 participants