[LoopCacheAnalysis] Cost Value Overflow
LoopCacheAnalysis uses int64_t type to represent the cost of reference groups, but the algorithm tends to produce large values that can overflow 64-bit int types. For example:
#define M 1024*8
void foo(int N, float A[][N][N][N][N], float B[][N][N][N][N], float C[][N][N][N][N]) {
for (int i1 = 0; i1 < M; i1++)
for (int i2 = 0; i2 < M; i2++)
for (int i3 = 0; i3 < M; i3++)
for (int i4 = 0; i4 < M; i4++)
for (int i5 = 0; i5 < M; i5++)
A[i1][i2][i3][i4][i5] += B[i1][i2][i3][i4][i5] + C[i1][i2][i3][i4][i5];
}
$ opt -passes='print<loop-cache-cost>' tmp.simp.ll -disable-output
Loop 'for.body16' has cost = 3458764513820540928
Loop 'for.body' has cost = 0
Loop 'for.body4' has cost = 0
Loop 'for.body8' has cost = 0
Loop 'for.body12' has cost = 0
Possible fixes as discussed in today's LoopOptWG call is to either use APInt (with 128-bit width) or use double type instead of int64_t.
a proposed fix: https://reviews.llvm.org/D124984
a proposed fix: https://reviews.llvm.org/D124984
Looks like D124984 only changes the path changed under D123400, but the test case above fails (gives 0 cost values) even before D123400 landed. @CongzheUalberta could you please clarify?
I added a test, i.e., t4() in LoopnestFixedSize.ll, which has array size of 4096 although not 8192 as in the test case you provided at the top. The problem with size of 8192(2^13) is that, in loop cache analysis we would multiply the tripcounts of all loops anyways and with a loopnest of depth 5, we will always overflow (since 13*5 >64). Therefore, whatever approach we use to fix the current issue 55233, this overflow with size 8192 will always occur unless we do a more fundamental change to the loop cache analysis algorithm.
Would it be possible to normalize the terms somehow to reduce their size or represent the terms symbolically? I assume we are mostly interested in the relative differences across the loop nest?
See also #57087