Skip to content

[LoopInterchange] Interchange potentially breaks the program correctness #123920

@kasuga-fj

Description

@kasuga-fj

In the following program, LoopInterchange incorrectly interchanges the innermost two loops.

#define N 4
int a[N*N][N*N][N*N];
void f() {
  for (int i = 0; i < N; i++)
    for (int j = 1; j < 2*N; j++)
      for (int k = 1; k < 2*N; k++)
        a[2*i][k+1][j-1] -= a[i+N-1][k][j];
}

See also: https://godbolt.org/z/rfev9drn7

Note that the current LoopInterchange interchanges these loops twice, therefore the order returns to the original one. So this case works fine.

The problem here is that the LoopInterchange regards Dependence::DVEntry::LE as '<'. As a result, the direction vector becomes [< < >]. Swapping the innermost loops changes this to [< > <], which passes the legality check. In fact, we must also consider the direction vector such as [= < >].

Metadata

Metadata

Assignees

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions