Newbie Question: Add operation/loop-body between AffineFor

The question is quite naive, I think, and quite appreciate it if anyone could give some hints.

For the AffineFor conversion, it is usually found that the AffineFor can be generated by calling the method buildAffineLoopNestImpl, which will add the bodybuilderFn at the innermost loop.

My curiosity is how to add the bodyBuilderFn between two or even three levels of AffineFor, shown as the following code? Is there any method that can be called?

affine.for %arg0 = 0 to 3 {
     %2 = affine.load %1[%arg1, %arg0] : memref<2x3xf64>
      affine.for %arg1 = 0 to 2 {
        %4 = affine.load %3[%arg1, %arg0] : memref<2x3xf64>
      }
    }

Thanks

Chunjie

I believe the call to mlir::buildAffineLoopNest builds a perfectly nested loop-nest. However you can just decompose in two calls:

buildAffineLoopNest(builder, loc, outer_lbs, outer_ubs, outer_steps, 
  [&](OpBuilder &nestedBuilder, Location loc, ValueRange ivs) {
    // builds the body of the outer loop here
    ...
    // nest a new loop inside the outer loop
    buildAffineLoopNest(nestedBuilder, loc, inner_lbs, inner_ubs, inner_steps, 
      [&](OpBuilder &innerBuilder, Location loc, ValueRange ivs) {
        // builds the body of the inner loop here
        ...
      }
    });

Many thanks for your quick answer. The functionality has been achieved.

An alternative depending on which one you see as simpler is to just construct an affine.for with an empty body; then, set the builder insertion point to the body of affine.for, create the operations for its body including the inner affine.for, and so on.

Adding operations after an AffineForOp This link can be referred, right?