-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[llvm][ConstraintElimination]Insert ConditionFact into loop header in case of monotonic induction variables #112080
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
7
commits into
llvm:main
Choose a base branch
from
grigorypas:loops_in_constr_elimination
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
7 commits
Select commit
Hold shift + click to select a range
b0da99d
Insert ConditionFact into loop header
grigorypas ef9c027
Fixed regression tests
grigorypas 3a5592a
Added regression test
grigorypas de54a16
Restrict to functions with smaller number of conditional branches
grigorypas 4bc49ac
Added overflow test and fixed Polly test
grigorypas cff3656
Refactored common code into functions
grigorypas bcc8bf4
Removed unnecessary cast
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
98 changes: 98 additions & 0 deletions
98
llvm/test/Transforms/ConstraintElimination/loop-removal.ll
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,98 @@ | ||
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5 | ||
; RUN: opt < %s -passes=constraint-elimination -S | FileCheck %s | ||
|
||
define i32 @foo() { | ||
; CHECK-LABEL: define i32 @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: [[X_1:%.*]] = phi i32 [ [[X_0]], %[[OUTER_LOOP_CONTROL]] ], [ [[X_INNER:%.*]], %[[INNER_LOOP_BODY:.*]] ] | ||
; CHECK-NEXT: br i1 false, label %[[INNER_LOOP_BODY]], label %[[OUTER_LOOP_INC]] | ||
; CHECK: inner.loop.body: | ||
; CHECK-NEXT: [[X_INNER]] = add nsw i32 [[X_1]], -1 | ||
; CHECK-NEXT: br label %[[INNER_LOOP_CONTROL]] | ||
; CHECK: outer.loop.inc: | ||
; CHECK-NEXT: [[X_OUTER]] = add nsw i32 [[X_1]], 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 = add 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 | ||
} | ||
|
||
; int foo_with_overflow(unsigned x, int y){ | ||
; unsigned i = x; | ||
; while(i < 10 || i > 20){ | ||
; if(i > x){ | ||
; y -= 23; | ||
; } | ||
; i -= 1; | ||
; } | ||
; return y; | ||
; } | ||
define dso_local noundef i32 @foo_with_overflow(i32 noundef %x, i32 noundef %y) local_unnamed_addr #0 { | ||
; CHECK-LABEL: i32 @foo_with_overflow( | ||
; CHECK-NEXT: entry: | ||
; CHECK-NEXT: [[TMP0:%.*]] = add i32 [[X:%.*]], -21 | ||
; CHECK-NEXT: [[TMP1:%.*]] = icmp ult i32 [[TMP0]], -11 | ||
; CHECK-NEXT: br i1 [[TMP1]], label %[[WHILE_BODY:.*]], label %[[WHILE_END:.*]] | ||
; CHECK: while.body: | ||
; CHECK-NEXT: [[I_02:%.*]] = phi i32 [ [[SUB3:%.*]], %[[WHILE_BODY]] ], [ [[X]], %[[ENTRY:.*]] ] | ||
; CHECK-NEXT: [[Y_ADDR_01:%.*]] = phi i32 [ [[SPEC_SELECT:%.*]], %[[WHILE_BODY]] ], [ [[Y:%.*]], %[[ENTRY]] ] | ||
; CHECK-NEXT: [[CMP2:%.*]] = icmp ugt i32 [[I_02]], [[X]] | ||
; CHECK-NEXT: [[SUB:%.*]] = add nsw i32 [[Y_ADDR_01]], -23 | ||
; CHECK-NEXT: [[SPEC_SELECT]] = select i1 [[CMP2]], i32 [[SUB]], i32 [[Y_ADDR_01]] | ||
; CHECK-NEXT: [[SUB3]] = add i32 [[I_02]], -1 | ||
; CHECK-NEXT: [[DOTREASS:%.*]] = add i32 [[I_02]], -22 | ||
; CHECK-NEXT: [[TMP2:%.*]] = icmp ult i32 [[DOTREASS]], -11 | ||
; CHECK-NEXT: br i1 [[TMP2]], label %[[WHILE_BODY]], label %[[WHILE_END]] | ||
; CHECK: while.end: | ||
; CHECK-NEXT: [[Y_ADDR_0_LCSSA:%.*]] = phi i32 [ [[Y]], %[[ENTRY]] ], [ [[SPEC_SELECT]], %[[WHILE_BODY]] ] | ||
; CHECK-NEXT: ret i32 [[Y_ADDR_0_LCSSA]] | ||
; | ||
entry: | ||
%0 = add i32 %x, -21 | ||
%1 = icmp ult i32 %0, -11 | ||
br i1 %1, label %while.body, label %while.end | ||
|
||
while.body: ; preds = %entry, %while.body | ||
%i.02 = phi i32 [ %sub3, %while.body ], [ %x, %entry ] | ||
%y.addr.01 = phi i32 [ %spec.select, %while.body ], [ %y, %entry ] | ||
%cmp2 = icmp ugt i32 %i.02, %x | ||
%sub = add nsw i32 %y.addr.01, -23 | ||
%spec.select = select i1 %cmp2, i32 %sub, i32 %y.addr.01 | ||
%sub3 = add i32 %i.02, -1 | ||
%.reass = add i32 %i.02, -22 | ||
%2 = icmp ult i32 %.reass, -11 | ||
br i1 %2, label %while.body, label %while.end | ||
|
||
while.end: ; preds = %while.body, %entry | ||
%y.addr.0.lcssa = phi i32 [ %y, %entry ], [ %spec.select, %while.body ] | ||
ret i32 %y.addr.0.lcssa | ||
} |
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
Oops, something went wrong.
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 was not aware of the existing
MaxRows
mechanism during my earlier review, so that seems to be already enough to avoid compile-time explosion.Doing a whole walk over the function to count conditional branches feels somewhat heavy and we potentially get an amount of facts in the number of
Phi
instructions (which isn't necessarily correlated to number of condjumps). Anyway just trying to make a case that maybe we should just do the simpler thing and rely onMaxRows
limiting things without adding extra conditions here.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.
The reason I added this condition is to avoid regression in some edge cases (see analysis by @dtcxzyw). If we add extra constrains in loop headers and reach MaxRows limit, some constraints are dropped that are not otherwise. I am happy to remove it if we are OK with those regressions.
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.
It just feels like a lot of trouble (or rather a lot of memory read) to go through the whole function just to count the number of conditional branches... So I am wondering if there is ways to limit this without having an extra pass over the whole function...