-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[LoopInterchange] Don't consider loops with BTC=0 #167113
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
Merged
+197
−37
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e72522a
[LoopInterchange] Don't consider loops with BTC=0
sjoerdmeijer f51d9f6
Forgto to add test-case
sjoerdmeijer e3abad5
Removed datalayout from test, improved debug message, and clarified c…
sjoerdmeijer 5d9326a
TMP
sjoerdmeijer 56c3e9b
Don't reject the whole loopnest, fixed up and added test case.
sjoerdmeijer 7d68d9c
Remove commented out option
sjoerdmeijer 9d548a5
Removed BTC caching, fixed up test case.
sjoerdmeijer 738e6b6
Addressed last comments
sjoerdmeijer 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,6 +46,7 @@ | |
| #include "llvm/Transforms/Utils/Local.h" | ||
| #include "llvm/Transforms/Utils/LoopUtils.h" | ||
| #include <cassert> | ||
| #include <map> | ||
| #include <utility> | ||
| #include <vector> | ||
|
|
||
|
|
@@ -1462,6 +1463,25 @@ std::optional<bool> LoopInterchangeProfitability::isProfitableForVectorization( | |
| bool LoopInterchangeProfitability::isProfitable( | ||
| const Loop *InnerLoop, const Loop *OuterLoop, unsigned InnerLoopId, | ||
| unsigned OuterLoopId, CharMatrix &DepMatrix, CacheCostManager &CCM) { | ||
| // A loop with a backedge that isn't taken, e.g. an unconditional branch | ||
| // true, isn't really a loop and we don't want to consider it as a | ||
|
||
| // candidate. | ||
| // TODO: when interchange is forced, we should probably also allow | ||
| // interchange for these loops, and thus this logic should be moved just | ||
| // below the cost-model ignore check below. But this check is done first | ||
| // to avoid the issue in #163954. | ||
| const SCEV *InnerBTC = SE->getBackedgeTakenCount(InnerLoop); | ||
| const SCEV *OuterBTC = SE->getBackedgeTakenCount(OuterLoop); | ||
| if (InnerBTC && InnerBTC->isZero()) { | ||
| LLVM_DEBUG(dbgs() << "Inner loop back-edge isn't taken, rejecting " | ||
| "single iteration loop\n"); | ||
| return false; | ||
| } | ||
| if (OuterBTC && OuterBTC->isZero()) { | ||
| LLVM_DEBUG(dbgs() << "Outer loop back-edge isn't taken, rejecting " | ||
| "single iteration loop\n"); | ||
| return false; | ||
| } | ||
|
|
||
| // Return true if interchange is forced and the cost-model ignored. | ||
| if (Profitabilities.size() == 1 && Profitabilities[0] == RuleTy::Ignore) | ||
|
|
||
74 changes: 74 additions & 0 deletions
74
llvm/test/Transforms/LoopInterchange/loopnest-with-outer-btc0.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,74 @@ | ||
| ; RUN: opt < %s -passes=loop-interchange -verify-dom-info -verify-loop-info \ | ||
| ; RUN: -pass-remarks-output=%t -pass-remarks='loop-interchange' -S | ||
| ; RUN: cat %t | FileCheck %s | ||
|
|
||
| target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" | ||
|
|
||
| @D = common global [100 x [100 x [100 x i32]]] zeroinitializer | ||
|
|
||
| ; The outer loop's backedge isn't taken. Check the loop with BTC=0 is considered | ||
| ; unprofitable, but that we still interchange the two inner loops. | ||
| ; | ||
| ; for(int i=0;i<1;i++) | ||
| ; for(int j=0;j<100;j++) | ||
| ; for(int k=0;k<100;k++) | ||
| ; D[i][k][j] = D[i][k][j]+t; | ||
| ; | ||
|
|
||
| ; CHECK: --- !Analysis | ||
| ; CHECK-NEXT: Pass: loop-interchange | ||
| ; CHECK-NEXT: Name: Dependence | ||
| ; CHECK-NEXT: Function: interchange_i_and_j | ||
| ; CHECK-NEXT: Args: | ||
| ; CHECK-NEXT: - String: Computed dependence info, invoking the transform. | ||
| ; CHECK-NEXT: ... | ||
| ; CHECK-NEXT: --- !Passed | ||
| ; CHECK-NEXT: Pass: loop-interchange | ||
| ; CHECK-NEXT: Name: Interchanged | ||
| ; CHECK-NEXT: Function: interchange_i_and_j | ||
| ; CHECK-NEXT: Args: | ||
| ; CHECK-NEXT: - String: Loop interchanged with enclosing loop. | ||
| ; CHECK-NEXT: ... | ||
| ; CHECK-NEXT: --- !Missed | ||
| ; CHECK-NEXT: Pass: loop-interchange | ||
| ; CHECK-NEXT: Name: InterchangeNotProfitable | ||
| ; CHECK-NEXT: Function: interchange_i_and_j | ||
| ; CHECK-NEXT: Args: | ||
| ; CHECK-NEXT: - String: Insufficient information to calculate the cost of loop for interchange. | ||
| ; CHECK-NEXT: ... | ||
|
|
||
| define void @interchange_i_and_j(i32 %t){ | ||
| entry: | ||
| br label %outer.header | ||
|
|
||
| outer.header: | ||
| %i = phi i64 [ 0, %entry ], [ %inc16, %for.inc15 ] | ||
| br label %inner1.header | ||
|
|
||
| inner1.header: | ||
| %j = phi i64 [ 0, %outer.header ], [ %inc13, %for.inc12 ] | ||
| br label %inner2.body | ||
|
|
||
| inner2.body: | ||
| %k = phi i64 [ 0, %inner1.header ], [ %inc, %inner2.body ] | ||
| %arrayidx8 = getelementptr inbounds [100 x [100 x i32]], ptr @D, i64 %i, i64 %k, i64 %j | ||
| %0 = load i32, ptr %arrayidx8 | ||
| %add = add nsw i32 %0, %t | ||
| store i32 %add, ptr %arrayidx8 | ||
| %inc = add nuw nsw i64 %k, 1 | ||
| %exitcond = icmp eq i64 %inc, 100 | ||
| br i1 %exitcond, label %for.inc12, label %inner2.body | ||
|
|
||
| for.inc12: | ||
| %inc13 = add nuw nsw i64 %j, 1 | ||
| %exitcond29 = icmp eq i64 %inc13, 100 | ||
| br i1 %exitcond29, label %for.inc15, label %inner1.header | ||
|
|
||
| for.inc15: | ||
| %inc16 = add nuw nsw i64 %i, 1 | ||
| %exitcond30 = icmp eq i64 %inc16, 1 | ||
| br i1 %exitcond30, label %for.end17, label %outer.header | ||
|
|
||
| for.end17: | ||
| ret void | ||
| } |
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
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,54 @@ | ||
| ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6 | ||
| ; RUN: opt < %s -passes=loop-interchange -loop-interchange-profitabilities=ignore -cache-line-size=64 -verify-dom-info -verify-loop-info -verify-scev -verify-loop-lcssa -S | FileCheck %s | ||
|
|
||
| ; Test case for issue: https://github.com/llvm/llvm-project/issues/163954 | ||
|
|
||
| define void @test() { | ||
| ; CHECK-LABEL: define void @test() { | ||
| ; CHECK-NEXT: [[ENTRY:.*]]: | ||
| ; CHECK-NEXT: br label %[[OUTER_HEADER:.*]] | ||
| ; CHECK: [[OUTER_HEADER]]: | ||
| ; CHECK-NEXT: [[I:%.*]] = phi i8 [ 0, %[[ENTRY]] ], [ [[DOTLCSSA:%.*]], %[[OUTER_LATCH:.*]] ] | ||
| ; CHECK-NEXT: br label %[[INNER_HEADER:.*]] | ||
| ; CHECK: [[INNER_HEADER]]: | ||
| ; CHECK-NEXT: [[J:%.*]] = phi i64 [ 0, %[[OUTER_HEADER]] ], [ [[J_NEXT:%.*]], %[[INNER_LATCH:.*]] ] | ||
| ; CHECK-NEXT: [[TMP0:%.*]] = phi i8 [ [[I]], %[[OUTER_HEADER]] ], [ [[TMP1:%.*]], %[[INNER_LATCH]] ] | ||
| ; CHECK-NEXT: br label %[[INNER_BODY:.*]] | ||
| ; CHECK: [[INNER_BODY]]: | ||
| ; CHECK-NEXT: br i1 true, label %[[INNER_LATCH]], label %[[INNER_BODY]] | ||
| ; CHECK: [[INNER_LATCH]]: | ||
| ; CHECK-NEXT: [[TMP1]] = or i8 [[TMP0]], 0 | ||
| ; CHECK-NEXT: [[J_NEXT]] = add i64 [[J]], 1 | ||
| ; CHECK-NEXT: br i1 true, label %[[OUTER_LATCH]], label %[[INNER_HEADER]] | ||
| ; CHECK: [[OUTER_LATCH]]: | ||
| ; CHECK-NEXT: [[DOTLCSSA]] = phi i8 [ [[TMP1]], %[[INNER_LATCH]] ] | ||
| ; CHECK-NEXT: br i1 true, label %[[EXIT:.*]], label %[[OUTER_HEADER]] | ||
| ; CHECK: [[EXIT]]: | ||
| ; CHECK-NEXT: ret void | ||
| ; | ||
| entry: | ||
| br label %outer.header | ||
|
|
||
| outer.header: | ||
| %i = phi i8 [ 0, %entry ], [ %1, %outer.latch ] | ||
| br label %inner.header | ||
|
|
||
| inner.header: | ||
| %j = phi i64 [ 0, %outer.header ], [ %j.next, %inner.latch ] | ||
| %0 = phi i8 [ %i, %outer.header ], [ %1, %inner.latch ] | ||
| br label %inner.body | ||
|
|
||
| inner.body: | ||
| br i1 true, label %inner.latch, label %inner.body ; another (self) loop, but never taken | ||
|
|
||
| inner.latch: | ||
| %1 = or i8 %0, 0 | ||
| %j.next = add i64 %j, 1 | ||
| br i1 true, label %outer.latch, label %inner.header | ||
|
|
||
| outer.latch: | ||
| br i1 true, label %exit, label %outer.header | ||
|
|
||
| exit: | ||
| ret void | ||
| } |
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.
Maybe unnecessary?