This is another attempt to land this patch. The patch proposed to use a new cost model for loop interchange, which is obtained from loop cache analysis. Given a loopnest, what loop cache analysis returns is a vector of loops [loop0, loop1, loop2, ...] where loop0 should be replaced as the outermost loop, loop1 should be placed one more level inside, and loop2 one more level inside, etc. What loop cache analysis does is not only more comprehensive than the current cost model, it is also a "one-shot" query which means that we only need to query it once during the entire loop interchange pass, which is better than the current cost model where we query it every time we check whether it is profitable to interchange two loops. Thus complexity is reduced, especially after D120386 where we do more interchanges to get the globally optimal loop access pattern. Updates made to test cases are mostly minor changes and some corrections. One change that applies to all tests is that we added an option `-cache-line-size=64` to the RUN lines. This is ensure that loop cache analysis receives a valid number of cache line size for correct analysis. Test coverage for loop interchange is not reduced. Currently we did not completely remove the legacy cost model, but keep it as fall-back in case the new cost model did not run successfully. This is because currently we have some limitations in delinearization, which sometimes makes loop cache analysis bail out. The longer term goal is to enhance delinearization and eventually remove the legacy cost model compeletely. Reviewed By: bmahjour, #loopoptwg Differential Revision: https://reviews.llvm.org/D124926
143 lines
4.3 KiB
LLVM
143 lines
4.3 KiB
LLVM
; REQUIRES: asserts
|
|
; RUN: opt < %s -basic-aa -loop-interchange -cache-line-size=64 -verify-dom-info -verify-loop-info \
|
|
; RUN: -S -debug 2>&1 | FileCheck %s
|
|
|
|
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
|
|
|
|
@A = common global [100 x [100 x i32]] zeroinitializer
|
|
@B = common global [100 x i32] zeroinitializer
|
|
@C = common global [100 x [100 x i32]] zeroinitializer
|
|
@D = common global [100 x [100 x [100 x i32]]] zeroinitializer
|
|
|
|
;; Loops not tightly nested are not interchanged
|
|
;; for(int j=0;j<N;j++) {
|
|
;; B[j] = j+k;
|
|
;; for(int i=0;i<N;i++)
|
|
;; A[j][i] = A[j][i]+B[j];
|
|
;; }
|
|
|
|
; CHECK: Not interchanging loops. Cannot prove legality.
|
|
|
|
define void @interchange_05(i32 %k, i32 %N){
|
|
entry:
|
|
%cmp30 = icmp sgt i32 %N, 0
|
|
br i1 %cmp30, label %for.body.lr.ph, label %for.end17
|
|
|
|
for.body.lr.ph:
|
|
%0 = add i32 %N, -1
|
|
%1 = zext i32 %k to i64
|
|
br label %for.body
|
|
|
|
for.body:
|
|
%indvars.iv32 = phi i64 [ 0, %for.body.lr.ph ], [ %indvars.iv.next33, %for.inc15 ]
|
|
%2 = add nsw i64 %indvars.iv32, %1
|
|
%arrayidx = getelementptr inbounds [100 x i32], [100 x i32]* @B, i64 0, i64 %indvars.iv32
|
|
%3 = trunc i64 %2 to i32
|
|
store i32 %3, i32* %arrayidx
|
|
br label %for.body3
|
|
|
|
for.body3:
|
|
%indvars.iv = phi i64 [ 0, %for.body ], [ %indvars.iv.next, %for.body3 ]
|
|
%arrayidx7 = getelementptr inbounds [100 x [100 x i32]], [100 x [100 x i32]]* @A, i64 0, i64 %indvars.iv32, i64 %indvars.iv
|
|
%4 = load i32, i32* %arrayidx7
|
|
%add10 = add nsw i32 %3, %4
|
|
store i32 %add10, i32* %arrayidx7
|
|
%indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
|
|
%lftr.wideiv = trunc i64 %indvars.iv to i32
|
|
%exitcond = icmp eq i32 %lftr.wideiv, %0
|
|
br i1 %exitcond, label %for.inc15, label %for.body3
|
|
|
|
for.inc15:
|
|
%indvars.iv.next33 = add nuw nsw i64 %indvars.iv32, 1
|
|
%lftr.wideiv35 = trunc i64 %indvars.iv32 to i32
|
|
%exitcond36 = icmp eq i32 %lftr.wideiv35, %0
|
|
br i1 %exitcond36, label %for.end17, label %for.body
|
|
|
|
for.end17:
|
|
ret void
|
|
}
|
|
|
|
declare void @foo(...) readnone
|
|
|
|
;; Loops not tightly nested are not interchanged
|
|
;; for(int j=0;j<N;j++) {
|
|
;; foo();
|
|
;; for(int i=2;i<N;i++)
|
|
;; A[j][i] = A[j][i]+k;
|
|
;; }
|
|
|
|
; CHECK: Not interchanging loops. Cannot prove legality.
|
|
|
|
define void @interchange_06(i32 %k, i32 %N) {
|
|
entry:
|
|
%cmp22 = icmp sgt i32 %N, 0
|
|
br i1 %cmp22, label %for.body.lr.ph, label %for.end12
|
|
|
|
for.body.lr.ph:
|
|
%0 = add i32 %N, -1
|
|
br label %for.body
|
|
|
|
for.body:
|
|
%indvars.iv24 = phi i64 [ 0, %for.body.lr.ph ], [ %indvars.iv.next25, %for.inc10 ]
|
|
tail call void (...) @foo()
|
|
br label %for.body3
|
|
|
|
for.body3:
|
|
%indvars.iv = phi i64 [ %indvars.iv.next, %for.body3 ], [ 2, %for.body ]
|
|
%arrayidx5 = getelementptr inbounds [100 x [100 x i32]], [100 x [100 x i32]]* @A, i64 0, i64 %indvars.iv24, i64 %indvars.iv
|
|
%1 = load i32, i32* %arrayidx5
|
|
%add = add nsw i32 %1, %k
|
|
store i32 %add, i32* %arrayidx5
|
|
%indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
|
|
%lftr.wideiv = trunc i64 %indvars.iv to i32
|
|
%exitcond = icmp eq i32 %lftr.wideiv, %0
|
|
br i1 %exitcond, label %for.inc10, label %for.body3
|
|
|
|
for.inc10:
|
|
%indvars.iv.next25 = add nuw nsw i64 %indvars.iv24, 1
|
|
%lftr.wideiv26 = trunc i64 %indvars.iv24 to i32
|
|
%exitcond27 = icmp eq i32 %lftr.wideiv26, %0
|
|
br i1 %exitcond27, label %for.end12, label %for.body
|
|
|
|
for.end12:
|
|
ret void
|
|
}
|
|
|
|
;; The following Loop is not considered tightly nested and is not interchanged.
|
|
;; The outer loop header does not branch to the inner loop preheader, or the
|
|
;; inner loop header, or the outer loop latch.
|
|
; CHECK: Not interchanging loops. Cannot prove legality.
|
|
define void @interchange_07(i32 %k, i32 %N, i64 %ny) {
|
|
entry:
|
|
br label %for1.header
|
|
|
|
for1.header:
|
|
%j23 = phi i64 [ 0, %entry ], [ %j.next24, %for1.inc10 ]
|
|
%cmp21 = icmp slt i64 0, %ny
|
|
br label %singleSucc
|
|
|
|
singleSucc:
|
|
br i1 %cmp21, label %preheader.j, label %for1.inc10
|
|
|
|
preheader.j:
|
|
br label %for2
|
|
|
|
for2:
|
|
%j = phi i64 [ %j.next, %for2 ], [ 0, %preheader.j ]
|
|
%arrayidx5 = getelementptr inbounds [100 x [100 x i32]], [100 x [100 x i32]]* @A, i64 0, i64 %j, i64 %j23
|
|
%lv = load i32, i32* %arrayidx5
|
|
%add = add nsw i32 %lv, %k
|
|
store i32 %add, i32* %arrayidx5
|
|
%j.next = add nuw nsw i64 %j, 1
|
|
%exitcond = icmp eq i64 %j, 99
|
|
br i1 %exitcond, label %for1.inc10, label %for2
|
|
|
|
for1.inc10:
|
|
%j.next24 = add nuw nsw i64 %j23, 1
|
|
%exitcond26 = icmp eq i64 %j23, 99
|
|
br i1 %exitcond26, label %for.end12, label %for1.header
|
|
|
|
for.end12:
|
|
ret void
|
|
}
|