ConstraintAnalysis: Optimize loops - #8980
Open
kripken wants to merge 132 commits into
Open
Conversation
tlively
reviewed
Aug 10, 2026
tlively
left a comment
Member
There was a problem hiding this comment.
Nice!
I'm concerned we'll still need a more conservative fallback, though. Consider this loop:
x = 0
y = 100
while (x < y) {
++x;
++x;
++y;
}
This loop will also execute 100 times, and hopefully we are able to analyze all the individual operations, but the loop condition does not contain a constant, so this particular widening heuristic will not apply.
| // comment). | ||
| if (auto* M = std::get_if<Literal>(&branch.constraint.term)) { | ||
| auto localConstraints = constraints.get(branch.local); | ||
| if (localConstraints.size() == 1 && |
Member
There was a problem hiding this comment.
Probably worth a comment about why we only handle the case where there is only a single constraint.
It also might be easier to read if we invert the conditions and early return false when our expectations are not met.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
One Weird Trick is enough: eagerly extend ranges of constants:
This is simpler than the typical approach used in Abstract Interpretation,
as we do it eagerly (immediately on a branch). This eagerness might
lose some precision, but not in cases we care about, I don't think:
if
xis a constant and we branch on it, then it must be a loop variablethat will increment (if it isn't in a loop, the constant
xwould have beenpropagated to the branch by other passes).