Affine For loop with multiple index variables


Is it possible to generate Affine for loop with multiple index variables. Using below code, able to generate affine for loop with just one index variable.

buildAffineLoopNest(rewriter, loc, ValueRange({startValue.getResult()}) , ValueRange({endValue.getResult()}), steps,
[&](OpBuilder &nestedBuilder, Location loc, ValueRange ivs) {

Requirement is use one index variable to load the value and other index variable to store the value.

Any help regarding the same is appreciated.

  • M

If you just want a different index, affine.for can have secondary induction variables via its iter_args, but you’ll have to increment or otherwise update them yourself. I also don’t recall if they are considered as dimensions per affine value classification or not.

If you want a “multifor” loop nest, it is not possible with affine.for but is possible with affine.parallel provided all loops are indeed parallel.

An affine.for is a single (one-dimensional) loop. You’ll need nested affine.for ops for a multi-dimensional nest.

Can this single 1D loop have multiple index variables to iterate like in C

for (i=0, j=0; i < a, j < b; i++, j++) { // 2 Index variables i & j



Assuming you wanted to write i < a && j < b instead of using the operator comma in the condition, such as loop will have to be an scf.while. Neither Affine nor SCF support such bounds.

You could also write the secondary induction variables in terms of the induction variable of the for loop. In the example you give, you could rewrite it as:

for (i = 0; i < min(a, b); i++) {
  j = i;

I believe you can do this in general:

for (i = l1, j = l2; i < u1, j < u2; i+= s1, j += s2) {

s1 * i + l1 = s2 * j + l2

This is the way I would do it if you really want to use affine loop nests.