-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
Conditional backward edges should help "warm up" code #93554
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
Comments
Darn. Switching over to forward-only conditional jumps makes sense to me then. |
If it's easier to just revert #93229 for now, that is okay as well. |
@brandtbucher the assembler adds a basicblock with a branch here . |
This would need to happen later though, right? Like, at the last possible moment, after we've ordered the blocks and we're actually emitting concrete jump instructions? The bummer is that I don't see a way to make this play nicely with the peepholer's jump elimination, unless we add a separate jump-to-jump elimination pass there. We may just need to accept that some optimization opportunities will slip through the cracks. |
Couldn't we do jump-to-jump-elimination first, then afterward convert conditional backward jumps to (conditional forward jump + |
Most jump elimination will still work fine, but I'm anticipating weird edge-cases with this new transformation. I'm picturing something like |
It's related to the issue with |
Would that be solved by having separate cases like this?
In the first case, Jump-To-Jump elimination will have already folded |
Can we move jump elimination later, just before we emit the code or is there a reason it needs to be earlier? |
From what I can gather, jump-threading can mess up directions of conditional jumps, but (assuming we have special logic for conditional-followed-by-unconditional like my other comment) inverting conditional jumps can't introduce any new opportunities for jump-threading. I'm imagining a situation where the front-end makes a monster like this:
Conditional-Jump-Inversion, then Jump-ThreadingIf we attempt to do conditional jump inversion first, there's nothing to do: the conditional jump is already forward. Then when we go to do jump threading, we get Jump-Inversion (nothing to do), followed by Jump-Threading
Then we're left with a backward conditional jump, which is the issue. I suppose one alternative would be to limit the power of jump-threading so that forward jumps always remain forward jumps. It would probably be safe to point the conditional jump at the last instruction in the chain that is forward of it instead (X in this case). That might be okay, but I think we might then need a different algorithm for jump threading, maybe using Floyd's tortoise/hare or something. Or maybe such long messy chains are so rare that it wouldn't be too bad to just thread jumps as long as things are forward, and then give up and accept any remaining jumps to jumps. Jump-Threading, then Conditional-Jump-InversionAfter Jump-Threading first (same as today):
Then Conditional-Jump-Inversion afterward:
(In either case, we can then remove unreachable blocks and no-op jumps.) One other thing I'd note is that we currently don't fold Apologies for the wall of text 😅 |
We still need to add some tests for quicken of code in loops, as described here: #96318 (comment) |
…conditional statement
…conditional statement
…conditional statement (python#119485)
…conditional statement (python#119485)
#93229 introduced a regression in how aggressively we quicken some
for
loops. Minimal example:f(True)
will quicken this code, butf(False)
will not, even though both contain the same number of back edges. The issue is that we only quicken on unconditional backwards jumps, not on conditional ones.We've known about this limitation for some time, in particular with regard to
while
loops. Since we check the loop condition at the bottom ofwhile
loops, one call is not enough to quickenw
:@markshannon has expressed a preference for having all branches be forward (i.e. replacing backward
POP_JUMP_IF_FALSE(x)
instructions withPOP_JUMP_FORWARD_IF_TRUE(1); JUMP_BACKWARD(x)
in the assembler). @iritkatriel believes that this shouldn't be too difficult, based on recent assembler rewrites.CC @sweeneyde
Linked PRs
The text was updated successfully, but these errors were encountered: