Can't correctly unroll loop using loopUnrollJamByFactor

Hello all!
I’m trying to unroll the following loop using loopUnrollJamByFactor() with unroll factor value =7 (loop to be unrolled is denoted by (*))

#map2 = affine_map<(d0) -> (480, d0 * -480 + 2048)>
... ...
affine.for %arg4 = 0 to 5 {
... ...
    affine.for %arg8 = 0 to min #map2(%arg4) {  (*)
        ... ...
    }
    ... ...
}    

After loopUnrollJamByFactor() is done, in the output IR the following couple of loops is appeared instead of initial single loop (*):

#map2 = affine_map<(d0) -> (476, ((d0 * -480 + 2048) floordiv 7) * 7)>
#map13 = affine_map<(d0) -> (480, d0 * -480 + 2048)>

affine.for %arg4 = 0 to 5 {
    ... ... 
    affine.for %arg8 = 0 to min #map2(%arg4) step 7 {
        ... ...
    }
    affine.for %arg8 = max #map2(%arg4) to min #map13(%arg4) {
         ... ...
    }
}  

The first loop is correct and its upper bound is min #map2(%arg4) (it is divisible by 7), but why the lower bound of the second loop is max #map2(%arg4) while we have finished by value min #map2(%arg4) in the first loop?
It seems, the correct lower bound of the second loop should be the same as the upper bound of the 1-st one, i.e. should be min #map2(%arg4)…

@YOV This is a bug. Can you please file it at bugs.llvm.org? The lower bound for an affine.for can only be a max. The cleanup space here will be non-convex and can’t be represented as a max to min range. To unroll this, either (1) the loop will have to be versioned (if/else), or (2) an affine.execute_region will have to be used, or (3) an scf.for will have to be generated for the cleanup loop.

Done.
https://bugs.llvm.org/show_bug.cgi?id=50784

But what way are you going to fix this bug immediately in MLIR?

By not unrolling it in such a scenario - the check has to be fixed first.

Why it is not possible to make an exclusion for such a case and use min qualifier for defining lower bound of the second AffineFor? It seems reasonable at the first site…

By design the lower bound has to be a max and the upper bound has to be a min: it is otherwise not a conjunction of constraints.
https://mlir.llvm.org/docs/Dialects/Affine/#affinefor-affineforop
If you don’t need this constraint, an scf.for allows arbitrary SSA values for the bounds (just index type).