Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
11 changes: 11 additions & 0 deletions llvm/lib/Transforms/Scalar/LoopInterchange.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,17 @@ static bool populateDependencyMatrix(CharMatrix &DepMatrix, unsigned Level,
Dep.push_back('I');
}

// If all the elements of any direction vector have only '*', legality
// can't be proven. Exit early to save compile time.
if (all_of(Dep, [](char C) { return C == '*'; })) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be reworded to the following for clarity.

// If all the elements of any direction vector have only '*', legality can't be proven.
// Exit early to save compile time.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed, thanks

ORE->emit([&]() {
return OptimizationRemarkMissed(DEBUG_TYPE, "Dependence",
L->getStartLoc(), L->getHeader())
<< "All loops have dependencies in all directions.";
});
return false;
}

// Test whether the dependency is forward or not.
bool IsKnownForward = true;
if (Src->getParent() != Dst->getParent()) {
Expand Down
44 changes: 44 additions & 0 deletions llvm/test/Transforms/LoopInterchange/bail-out-all-deps.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
; RUN: opt < %s -passes=loop-interchange -pass-remarks-output=%t \
; RUN: -disable-output
; RUN: FileCheck -input-file %t %s

; Check that loop interchange bails out early when finding a direction vector
; with all '*' elements.
;
; for (int i = 0; i < 4; i++)
; for (int j = 0; j < 4; j++)
; A[i & val][j & val] = 0;

; CHECK: --- !Missed
; CHECK-NEXT: Pass: loop-interchange
; CHECK-NEXT: Name: Dependence
; CHECK-NEXT: Function: f
; CHECK-NEXT: Args:
; CHECK-NEXT: - String: All loops have dependencies in all directions.
; CHECK-NEXT: ...
define void @f(ptr %A, i64 %val) {
entry:
br label %for.i.header

for.i.header:
%i = phi i64 [ 0, %entry ], [ %i.next, %for.i.latch ]
br label %for.j

for.j:
%j = phi i64 [ 0, %for.i.header ], [ %j.next, %for.j ]
%subscript.0 = and i64 %i, %val
%subscript.1 = and i64 %j, %val
%idx = getelementptr inbounds [4 x i8], ptr %A, i64 %subscript.0, i64 %subscript.1
store i8 0, ptr %idx
%j.next = add nuw nsw i64 %j, 1
%exit.j = icmp eq i64 %j.next, 4
br i1 %exit.j, label %for.i.latch, label %for.j

for.i.latch:
%i.next = add nuw nsw i64 %i, 1
%exit.i = icmp eq i64 %i.next, 4
br i1 %exit.i, label %exit, label %for.i.header

exit:
ret void
}
16 changes: 10 additions & 6 deletions llvm/test/Transforms/LoopInterchange/confused-dependence.ll
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
; REQUIRES: asserts
; RUN: opt < %s -passes=loop-interchange -verify-dom-info -verify-loop-info \
; RUN: -disable-output -debug 2>&1 | FileCheck %s
; RUN: opt < %s -passes=loop-interchange -pass-remarks-output=%t \
; RUN: -disable-output
; RUN: FileCheck -input-file %t %s

;; In the following case, p0 and p1 may alias, so the direction vector must be [* *].
;;
Expand All @@ -10,9 +10,13 @@
;; p0[4 * i + j] = p1[4 * j + i];
;; }

; CHECK: Dependency matrix before interchange:
; CHECK-NEXT: * *
; CHECK-NEXT: Processing InnerLoopId = 1 and OuterLoopId = 0
; CHECK: --- !Missed
; CHECK-NEXT: Pass: loop-interchange
; CHECK-NEXT: Name: Dependence
; CHECK-NEXT: Function: may_alias
; CHECK-NEXT: Args:
; CHECK-NEXT: - String: All loops have dependencies in all directions.
; CHECK-NEXT: ...
Comment on lines -13 to +19
Copy link
Contributor Author

@kasuga-fj kasuga-fj Jul 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed because the previous CHECK directives check the debug outputs, which are no longer printed due to this change.

define void @may_alias(ptr %p0, ptr %p1) {
entry:
br label %for.i.header
Expand Down
10 changes: 5 additions & 5 deletions llvm/test/Transforms/LoopInterchange/legality-for-scalar-deps.ll
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
; CHECK-NEXT: Name: Dependence
; CHECK-NEXT: Function: issue46867
; CHECK-NEXT: Args:
; CHECK-NEXT: - String: Cannot interchange loops due to dependences.
; CHECK-NEXT: - String: All loops have dependencies in all directions.
; CHECK: --- !Missed
; CHECK-NEXT: Pass: loop-interchange
; CHECK-NEXT: Name: Dependence
; CHECK-NEXT: Function: issue46867
; CHECK-NEXT: Args:
; CHECK-NEXT: - String: Cannot interchange loops due to dependences.
; CHECK-NEXT: - String: All loops have dependencies in all directions.
define void @issue46867(ptr noundef captures(none) %s, i32 noundef %c, ptr noundef readonly captures(none) %ff) {
entry:
%tobool7.not = icmp eq i32 %c, 0
Expand Down Expand Up @@ -121,7 +121,7 @@ land.end:
; CHECK-NEXT: Name: Dependence
; CHECK-NEXT: Function: issue47401
; CHECK-NEXT: Args:
; CHECK-NEXT: - String: Cannot interchange loops due to dependences.
; CHECK-NEXT: - String: All loops have dependencies in all directions.
define void @issue47401(ptr noundef writeonly captures(none) %e, ptr noundef readonly captures(none) %bb) {
entry:
br label %for.cond1.preheader
Expand Down Expand Up @@ -175,7 +175,7 @@ land.end:
; CHECK-NEXT: Name: Dependence
; CHECK-NEXT: Function: issue47295
; CHECK-NEXT: Args:
; CHECK-NEXT: - String: Cannot interchange loops due to dependences.
; CHECK-NEXT: - String: All loops have dependencies in all directions.
define void @issue47295(ptr noundef captures(none) %f, ptr noundef writeonly captures(none) %cc) {
entry:
br label %for.cond1.preheader
Expand Down Expand Up @@ -221,7 +221,7 @@ for.body4:
; CHECK-NEXT: Name: Dependence
; CHECK-NEXT: Function: issue54176
; CHECK-NEXT: Args:
; CHECK-NEXT: - String: Cannot interchange loops due to dependences.
; CHECK-NEXT: - String: All loops have dependencies in all directions.
define void @issue54176(i32 noundef %n, i32 noundef %m, ptr noundef captures(none) %aa, ptr noundef readonly captures(none) %bb, ptr noundef writeonly captures(none) %cc) {

entry:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ for.end19:
; CHECK-NEXT: Name: Dependence
; CHECK-NEXT: Function: test01
; CHECK-NEXT: Args:
; CHECK-NEXT: - String: Cannot interchange loops due to dependences.
; CHECK-NEXT: - String: All loops have dependencies in all directions.
; CHECK-NEXT: ...

; DELIN: --- !Analysis
Expand Down Expand Up @@ -147,7 +147,7 @@ define void @test02(i32 %k, i32 %N) {
; CHECK-NEXT: Name: Dependence
; CHECK-NEXT: Function: test02
; CHECK-NEXT: Args:
; CHECK-NEXT: - String: Cannot interchange loops due to dependences.
; CHECK-NEXT: - String: All loops have dependencies in all directions.
; CHECK-NEXT: ...

; DELIN: --- !Analysis
Expand Down Expand Up @@ -290,7 +290,7 @@ for.end17:
; CHECK-NEXT: Name: Dependence
; CHECK-NEXT: Function: test04
; CHECK-NEXT: Args:
; CHECK-NEXT: - String: Cannot interchange loops due to dependences.
; CHECK-NEXT: - String: All loops have dependencies in all directions.
; CHECK-NEXT: ...

; DELIN: --- !Missed
Expand Down
3 changes: 1 addition & 2 deletions llvm/test/Transforms/LoopInterchange/unique-dep-matrix.ll
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
; RUN: opt < %s -passes=loop-interchange -S -debug 2>&1 | FileCheck %s

; CHECK: Dependency matrix before interchange:
; CHECK-NEXT: * *
; CHECK-NEXT: = *
; CHECK-NEXT: < *
; CHECK-NEXT: Processing InnerLoopId

; This example is taken from github issue #54176
;
define void @foo(i32 noundef %n, i32 noundef %m, ptr nocapture noundef %aa, ptr nocapture noundef readonly %bb, ptr nocapture noundef writeonly %cc) {
define void @foo(i32 noundef %n, i32 noundef %m, ptr nocapture noundef noalias %aa, ptr nocapture noundef readonly noalias %bb, ptr nocapture noundef writeonly noalias %cc) {
Comment on lines -5 to +11
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added noalias to all ptr arguments to eliminate the * * dependency.

entry:
%arrayidx7 = getelementptr inbounds i8, ptr %aa, i64 512
br label %for.cond1.preheader
Expand Down