Removing redundant loops in loop nests?

Hello, is there a way to automatically remove useless loops in a loop nest?
By useless I mean a loop that iterates from 0 to 1 or something like:

#map0 = affine_map<(d0) -> (d0)>
#map2 = affine_map<(d0) -> (d0 + 1)>  
affine.for %arg7 = #map0(%arg4) to #map2(%arg4) {

Is this even a problem, or subsequent transformations will remove the useless code and variables?

This type of code appears often in ML algorithms when dimensions have size 1 or when tiling on only one dimension with mlir-opt, which appears to want to tile all loops, albeit to tiles of size 1…

We are removing single iteration loops with -affine-loop-normalize. We added this transformation there for the time being but we could decouple it from the rest of the normalization transformations, if needed. We decided not to add this as part of the AffineForOp generic canonicalization/folding because we are losing high-level information of the iteration space when we remove a loop from the loop nest, which is not always desirable (some passes may not work properly because of this).

Hopefully that helps!

1 Like