taco icon indicating copy to clipboard operation
taco copied to clipboard

SpTV: index variable used before defined

Open Infinoid opened this issue 4 years ago • 2 comments

While trying to cook up a test case that does this:

$ taco 'y(j) = A(i,j,k) * x(k)' -s='parallelize(j, CPUThread, Atomics)'

I noticed that the C++ API does not have the same default loop order as the command line tool. The command line tool seems to use (i,j,k) order by default, the C++ API seems to use (j,i,k) by default.

When using the (j,i,k) order, Taco generates code that accesses an iterator variable before it has been defined.

$ taco 'y(j) = A(i,j,k) * x(k)' -f=A:sss -s='reorder(j,i,k)'
[...]
  for (int32_t jA = A2_pos[iA]; jA < A2_pos[(iA + 1)]; jA++) {
    int32_t j = A2_crd[jA];
    double tiy_val = 0.0;
    for (int32_t iA = A1_pos[0]; iA < A1_pos[1]; iA++) {

Note, the variable jA is initialized using iA, but iA is defined farther down.

Infinoid avatar Feb 12 '21 13:02 Infinoid

Here is a reproducer that uses the C++ API, demonstrating the difference in default ordering.

https://gist.github.com/Infinoid/d1e8cc05f1b957a8e5966877545bf40c

Uncomment the .reorder line and it works.

Infinoid avatar Feb 12 '21 14:02 Infinoid

I've run into this in different contexts. it seems like just reorders of iterations over sparse levels is enough to cause the bug.

rohany avatar Feb 12 '21 19:02 rohany