llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[LoopCacheAnalysis] Cost Value Overflow

Open bmahjour opened this issue 4 years ago • 5 comments

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

bmahjour avatar May 02 '22 18:05 bmahjour

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.

bmahjour avatar May 04 '22 15:05 bmahjour

a proposed fix: https://reviews.llvm.org/D124984

CongzheUalberta avatar May 13 '22 05:05 CongzheUalberta

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?

bmahjour avatar May 16 '22 17:05 bmahjour

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.

CongzheUalberta avatar May 18 '22 14:05 CongzheUalberta

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

fhahn avatar Aug 14 '22 16:08 fhahn