-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[llvm][ipsccp/sccp] Strengthen range analysis in SCCPSolver #111716
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
Open
grigorypas
wants to merge
5
commits into
llvm:main
Choose a base branch
from
grigorypas:sccp_tighten_bounds
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b1f03c8
Implementation of GuaranteedBoundsPropagator
grigorypas 634ff93
Adding regression test file
grigorypas e059c6a
Fixed failing test: the pass now eliminates more branches
grigorypas c67a059
Implemented tracking monotonicity
grigorypas d83f18c
Reverting: Implement tracking monotonicity
grigorypas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5 | ||
; RUN: opt < %s -passes=ipsccp -S | FileCheck %s | ||
|
||
define i32 @foo() { | ||
; CHECK-LABEL: @foo( | ||
; CHECK-NEXT: init: | ||
; CHECK-NEXT: br label %[[OUTER_LOOP_CONTROL:.*]] | ||
; CHECK: outer.loop.control: | ||
; CHECK-NEXT: [[X_0:%.*]] = phi i32 [ 0, [[INIT:%.*]] ], [ [[X_OUTER:%.*]], [[OUTER_LOOP_INC:%.*]] ] | ||
; CHECK-NEXT: [[TMP0:%.*]] = icmp slt i32 [[X_0]], 10 | ||
; CHECK-NEXT: br i1 [[TMP0]], label %[[INNER_LOOP_CONTROL:.*]], label %[[EXIT:.*]] | ||
; CHECK: inner.loop.control: | ||
; CHECK-NEXT: br label [[OUTER_LOOP_INC]] | ||
; CHECK: outer.loop.inc: | ||
; CHECK-NEXT: [[X_OUTER]] = add nsw i32 [[X_0]], 2 | ||
; CHECK-NEXT: br label %[[OUTER_LOOP_CONTROL]] | ||
; CHECK: exit: | ||
; CHECK-NEXT: ret i32 [[X_0]] | ||
; | ||
init: | ||
br label %outer.loop.control | ||
|
||
outer.loop.control: ; preds = %init, %outer.loop.inc | ||
%x.0 = phi i32 [ 0, %init ], [ %x.outer, %outer.loop.inc ] | ||
%0 = icmp slt i32 %x.0, 10 | ||
br i1 %0, label %inner.loop.control, label %exit | ||
|
||
inner.loop.control: ; preds = %outer.loop.control, %inner.loop.body | ||
%x.1 = phi i32 [ %x.0, %outer.loop.control ], [ %x.inner, %inner.loop.body ] | ||
%1 = icmp sgt i32 %x.1, 20 | ||
br i1 %1, label %inner.loop.body, label %outer.loop.inc | ||
|
||
inner.loop.body: ; preds = %inner.loop.control | ||
%x.inner = sub nsw i32 %x.1, 1 | ||
br label %inner.loop.control | ||
|
||
outer.loop.inc: ; preds = %inner.loop.control | ||
%x.outer = add nsw i32 %x.1, 2 | ||
br label %outer.loop.control | ||
|
||
exit: ; preds = %1 | ||
ret i32 %x.0 | ||
} |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should eliminate this comparison in ConstraintElimination.
The constraint system
is unsatisfiable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your suggestion. I was under the impression that ConstraintElimination pass cannot handle loops well. I will take a closer look at ConstraintElimination pass.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ConstraintElimination analyses loops and induction variables with SCEV. Related code:
llvm-project/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
Lines 903 to 1049 in 72fb379
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. But in the original issue (#108906) nested while loops are not SCEVable. Do you think that the code below should be handled by ConstraintElimination? My approach does remove the dead loop.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Personally I don't want to pay for the complexity to handle random-generated cases :(
cc @nikic for more guidance.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand and appreciate your comment -- I think, if anything, the testcase shows a hole/gap in the range analysis infra in LLVM. I don't have a strong opinion on how we should fix it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@grigorypas IIRC ConstraintElimination only handles monotonically increasing indvars. It would be easy to support monotonically decreasing cases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Regarding #108906, I agree that we should not try to optimize such cases. If I ever find the time, I'll write a new section for the developer policy that specifies requirements for fuzzer-generated issue.
As for the change itself, I think the idea behind it is fairly reasonable, and I expect that it will help more realistic cases as well. Unfortunately, we're currently pretty bad with bounds checks elimination in loops for a combination of different reasons (SCEV being glacially slow with context sensitive facts, LVI going to overdefined in cycles, ConstraintElimination only having very basic loop reasoning, etc).
I see some code size changes from this patch: http://llvm-compile-time-tracker.com/compare.php?from=eaea5f6f952b6059cebfe87ea9800a3a6516f9ed&to=6358352b3b4a9912e4dd312fad057b02833a7faa&stat=size-text And also significant compile-time regressions, so we'll have to see if we can make it cheap enough: http://llvm-compile-time-tracker.com/compare.php?from=eaea5f6f952b6059cebfe87ea9800a3a6516f9ed&to=6358352b3b4a9912e4dd312fad057b02833a7faa&stat=instructions%3Au I haven't had time to look at the implementation yet.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While I agree that LLVM shouldn't try to optimize crazy cases, I still do believe the code in #108906 is not that unreasonable to optimize. I found out several instances in real codebases where a line of dead code was preventing other optimizations to kick in, causing code bloat.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, on a more general note, I think a range analysis infra shouldn't necessarily be concerned with how the code looks like as input, as long as it has reasonable capabilities to reason about equalities and inequalities. I looked at some point and found out the limitation you point out (for LVI/CE), and I wonder if instead of fixing these problems one by one we might want to rethink the bigger picture.