[LoopInterchange] Enable interchange when the outer loop header has "safe" loads
The following C program is trimmed from a real workload. If we interchange the loopnest, we could observe significant performance improvement. It is clear that with the loopnest interchanged, access to res[j][i] could be faster.
int* data;
int** res;
int *sum;
int maxLocal=0, max_i=0, max_j=0;
void foo() {
for (int i = 0; i < 10000; i++) {
if (data[i] == 1) continue;
for (int j = 0; j < 10; j++) {
int cur = res[j][i] * res[j][i];
sum[j] += cur;
if (cur > maxLocal) {
maxLocal = cur;
max_i = i;
max_j = j;
}
}
}
return;
}
int main () {
foo();
return 0;
}
However, with the current loop interchange pass, we are unable to interchange the loopnest. This is because there is load of data[i] in the original outer loop header. When we check whether the loopnest is tightly nested, we would bail out if we find "unsafe" instructions in the outer loop header, i.e., if containsUnsafeInstructions(OuterLoopHeader) returns true. Load instructions are considered unsafe.
If we would like to enable interchange for such scenarios, my general thought is that it might suffice to make sure data[i] does not change within the loopnest. If that is the case then we can interchange. To be a bit more specific, we could check 1) whether the array index i is a inner loop invariant. For instance, if what we had is data[maxLocal] then it would become illegal to interchange. 2) whether the array data itself changes within the loopnest. If the array values had changed then it would be illegal to interchange, e.g., if we had a store to data[i+1] or data[i] inside the inner loop. This check can be done by leveraging the current dependence analysis in loop interchange, nevertheless we need to be more strict on mem accesses within the outer loop header. This means that even if we just had loop independent dependence on data[i] (suppose we had a store to data[i] inside the inner loop) we need to bail out. Recall that loop independent dependence won't prevent us from doing interchange if the loopnest was tightly nested.
Just to be clear, after interchange the loopnest would become the following
int* data;
int** res;
int *sum;
int maxLocal=0, max_i=0, max_j=0;
void foo() {
for (int j = 0; j < 10; j++) {
for (int i = 0; i < 10000; i++) {
if (data[i] == 1) continue;
int cur = res[j][i] * res[j][i];
sum[j] += cur;
if (cur > maxLocal) {
maxLocal = cur;
max_i = i;
max_j = j;
}
}
}
return;
}
The IR for the original source code (in my first post) that is right before loop interchange is as follows.
target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128"
target triple = "aarch64-unknown-linux-gnu"
@maxLocal = dso_local local_unnamed_addr global i32 0, align 4
@max_i = dso_local local_unnamed_addr global i32 0, align 4
@max_j = dso_local local_unnamed_addr global i32 0, align 4
@data = dso_local local_unnamed_addr global ptr null, align 8
@res = dso_local local_unnamed_addr global ptr null, align 8
@sum = dso_local local_unnamed_addr global ptr null, align 8
; Function Attrs: nofree norecurse nosync nounwind uwtable
define dso_local void @foo() local_unnamed_addr #0 {
%1 = load ptr, ptr @data, align 8, !tbaa !10
%2 = load ptr, ptr @res, align 8
%3 = load ptr, ptr @sum, align 8
br label %5
4: ; preds = %.loopexit
ret void
5: ; preds = %.loopexit, %0
%indvars.iv21 = phi i64 [ 0, %0 ], [ %indvars.iv.next22, %.loopexit ]
%6 = getelementptr inbounds i32, ptr %1, i64 %indvars.iv21
%7 = load i32, ptr %6, align 4, !tbaa !14
%8 = icmp eq i32 %7, 1
br i1 %8, label %.loopexit, label %.preheader.preheader
.preheader.preheader: ; preds = %5
%9 = trunc i64 %indvars.iv21 to i32
br label %.preheader
.preheader: ; preds = %22, %.preheader.preheader
%indvars.iv = phi i64 [ 538, %.preheader.preheader ], [ %indvars.iv.next, %22 ]
%10 = getelementptr inbounds ptr, ptr %2, i64 %indvars.iv
%11 = load ptr, ptr %10, align 8, !tbaa !10
%12 = getelementptr inbounds i32, ptr %11, i64 %indvars.iv21
%13 = load i32, ptr %12, align 4, !tbaa !14
%14 = mul nsw i32 %13, %13
%15 = getelementptr inbounds i32, ptr %3, i64 %indvars.iv
%16 = load i32, ptr %15, align 4, !tbaa !14
%17 = add nsw i32 %16, %14
store i32 %17, ptr %15, align 4, !tbaa !14
%18 = load i32, ptr @maxLocal, align 4, !tbaa !14
%19 = icmp sgt i32 %14, %18
br i1 %19, label %20, label %22
20: ; preds = %.preheader
store i32 %14, ptr @maxLocal, align 4, !tbaa !14
store i32 %9, ptr @max_i, align 4, !tbaa !14
%21 = trunc i64 %indvars.iv to i32
store i32 %21, ptr @max_j, align 4, !tbaa !14
br label %22
22: ; preds = %20, %.preheader
%indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
%exitcond.not = icmp eq i64 %indvars.iv.next, 10000
br i1 %exitcond.not, label %.loopexit.loopexit, label %.preheader, !llvm.loop !16
.loopexit.loopexit: ; preds = %22
br label %.loopexit
.loopexit: ; preds = %.loopexit.loopexit, %5
%indvars.iv.next22 = add nuw nsw i64 %indvars.iv21, 1
%exitcond24.not = icmp eq i64 %indvars.iv.next22, 10000
br i1 %exitcond24.not, label %4, label %5, !llvm.loop !18
}
attributes #0 = { nofree norecurse nosync nounwind uwtable "approx-func-fp-math"="true" "frame-pointer"="non-leaf" "min-legal-vector-width"="0" "no-infs-fp-math"="true" "no-nans-fp-math"="true" "no-signed-zeros-fp-math"="true" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="tsv110" "target-features"="+aes,+crc,+crypto,+dotprod,+fp-armv8,+fp16fml,+fullfp16,+lse,+neon,+outline-atomics,+ras,+rdm,+sha2,+spe,+v8.2a" "unsafe-fp-math"="true" }
!llvm.module.flags = !{!0, !1, !2, !3, !4, !5, !6, !7, !8}
!llvm.ident = !{!9}
!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"branch-target-enforcement", i32 0}
!2 = !{i32 8, !"sign-return-address", i32 0}
!3 = !{i32 8, !"sign-return-address-all", i32 0}
!4 = !{i32 8, !"sign-return-address-with-bkey", i32 0}
!5 = !{i32 7, !"PIC Level", i32 2}
!6 = !{i32 7, !"PIE Level", i32 2}
!7 = !{i32 7, !"uwtable", i32 2}
!8 = !{i32 7, !"frame-pointer", i32 1}
!9 = !{!"clang version 15.0.0 (https://github.com/llvm/llvm-project.git 078174278574761edfd557908af59c67aa7b303e)"}
!10 = !{!11, !11, i64 0}
!11 = !{!"any pointer", !12, i64 0}
!12 = !{!"omnipotent char", !13, i64 0}
!13 = !{!"Simple C/C++ TBAA"}
!14 = !{!15, !15, i64 0}
!15 = !{!"int", !12, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = distinct !{!18, !17}
In the loopopt meeting we've discussed methods of forward code motion that moves the if-condition inside the inner loop, which could make the loop tightly nested. Potential drawbacks might be that if the loops are not interchanged then we'll need to move the if-condition back to outside the inner loop (by other passes like licm or loop unswitch). This phase-ordering problem may cause another level of difficulty.
Profitablity is another concern, since we'll hit the if-condition more times after interchange. For the motivating workload interchange brings better performance. But a more proper and accurate cost analysis is probably needed.
Regarding relaxing the legality for the loop interchange pass as I mentioned in my first post, I'll think twice and possibly post a patch for discussion, if that makes sense.
Any comment on this issue is appreciated :)