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
67 lines
2.5 KiB
LLVM
67 lines
2.5 KiB
LLVM
; REQUIRES: asserts
|
|
; RUN: opt < %s -loop-interchange -cache-line-size=64 -simplifycfg -simplifycfg-require-and-preserve-domtree=1 -pass-remarks-output=%t \
|
|
; RUN: -pass-remarks=loop-interchange -pass-remarks-missed=loop-interchange -stats -S 2>&1 \
|
|
; RUN: | FileCheck -check-prefix=STATS %s
|
|
; RUN: FileCheck -input-file %t %s
|
|
|
|
|
|
; no_deps_interchange just accesses a single nested array and can be interchange.
|
|
; CHECK: Name: Interchanged
|
|
; CHECK-NEXT: Function: no_deps_interchange
|
|
define i32 @no_deps_interchange([1024 x i32]* nocapture %Arr) local_unnamed_addr #0 {
|
|
entry:
|
|
br label %for1.header
|
|
|
|
for1.header: ; preds = %entry, %for1.inc
|
|
%indvars.iv19 = phi i64 [ 0, %entry ], [ %indvars.iv.next20, %for1.inc ]
|
|
br label %for2
|
|
|
|
for2: ; preds = %for1.header, %for2
|
|
%indvars.iv = phi i64 [ 0, %for1.header ], [ %indvars.iv.next, %for2 ]
|
|
%arrayidx6 = getelementptr inbounds [1024 x i32], [1024 x i32]* %Arr, i64 %indvars.iv, i64 %indvars.iv19
|
|
store i32 0, i32* %arrayidx6, align 4
|
|
%indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
|
|
%exitcond = icmp ne i64 %indvars.iv.next, 1024
|
|
br i1 %exitcond, label %for2, label %for1.inc
|
|
|
|
for1.inc:
|
|
%indvars.iv.next20 = add nuw nsw i64 %indvars.iv19, 1
|
|
%exitcond21 = icmp ne i64 %indvars.iv.next20, 1024
|
|
br i1 %exitcond21, label %for1.header, label %exit
|
|
|
|
exit: ; preds = %for1.inc
|
|
ret i32 0
|
|
|
|
}
|
|
|
|
; No memory access using any induction variables, interchanging not beneficial.
|
|
; CHECK: Name: InterchangeNotProfitable
|
|
; CHECK-NEXT: Function: no_mem_instrs
|
|
define i32 @no_mem_instrs(i64* %ptr) {
|
|
entry:
|
|
br label %for1.header
|
|
|
|
for1.header: ; preds = %entry, %for1.inc
|
|
%indvars.iv19 = phi i64 [ 0, %entry ], [ %indvars.iv.next20, %for1.inc ]
|
|
br label %for2
|
|
|
|
for2: ; preds = %for1.header, %for2
|
|
%indvars.iv = phi i64 [ 0, %for1.header ], [ %indvars.iv.next, %for2 ]
|
|
store i64 %indvars.iv, i64* %ptr, align 4
|
|
%indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
|
|
%exitcond = icmp ne i64 %indvars.iv.next, 1024
|
|
br i1 %exitcond, label %for2, label %for1.inc
|
|
|
|
for1.inc:
|
|
%indvars.iv.next20 = add nuw nsw i64 %indvars.iv19, 1
|
|
%exitcond21 = icmp ne i64 %indvars.iv.next20, 1024
|
|
br i1 %exitcond21, label %for1.header, label %exit
|
|
|
|
exit: ; preds = %for1.inc
|
|
ret i32 0
|
|
}
|
|
|
|
|
|
; Check stats, we interchanged 1 out of 3 loops.
|
|
; STATS: 1 loop-interchange - Number of loops interchanged
|