Issue 238197
Summary [MLIR][Affine] `affine-simplify-min-max` has compile time $O(n^4)$ on a chain of `n`-long `arith.divsi` operations
Labels mlir
Assignees
Reporter pingshiyu
    Reproduced with LLVM/MLIR 23.1.4, optimized build.

On prorgams formed of a chain of divisions of the form below, as an example:
```mlir
#map = affine_map<()[s0] -> (1, s0)>
module {
  func.func @test(%arg0: index) -> index {
    %c2 = arith.constant 2 : index
    %0 = arith.divsi %arg0, %c2 : index
    %1 = arith.divsi %0, %c2 : index
    %2 = arith.divsi %1, %c2 : index
    %3 = arith.divsi %2, %c2 : index
    %4 = arith.divsi %3, %c2 : index
    %5 = arith.divsi %4, %c2 : index
    %6 = arith.divsi %5, %c2 : index
    %7 = arith.divsi %6, %c2 : index
    %8 = arith.divsi %7, %c2 : index
    %9 = arith.divsi %8, %c2 : index
    %10 = arith.divsi %9, %c2 : index
    %11 = arith.divsi %10, %c2 : index
    %12 = arith.divsi %11, %c2 : index
    %13 = arith.divsi %12, %c2 : index
    %14 = arith.divsi %13, %c2 : index
    %15 = arith.divsi %14, %c2 : index
    %16 = arith.divsi %15, %c2 : index
    %17 = arith.divsi %16, %c2 : index
    %18 = arith.divsi %17, %c2 : index
    %19 = arith.divsi %18, %c2 : index
    %20 = arith.divsi %19, %c2 : index
    %21 = arith.divsi %20, %c2 : index
 %22 = arith.divsi %21, %c2 : index
    %23 = arith.divsi %22, %c2 : index
 %24 = arith.divsi %23, %c2 : index
    %25 = arith.divsi %24, %c2 : index
    %26 = arith.divsi %25, %c2 : index
    %27 = arith.divsi %26, %c2 : index
    %28 = arith.divsi %27, %c2 : index
    %29 = arith.divsi %28, %c2 : index
    %30 = arith.divsi %29, %c2 : index
    %31 = arith.divsi %30, %c2 : index
    %32 = arith.divsi %31, %c2 : index
    %33 = arith.divsi %32, %c2 : index
    %34 = arith.divsi %33, %c2 : index
    %35 = arith.divsi %34, %c2 : index
    %36 = arith.divsi %35, %c2 : index
 %37 = arith.divsi %36, %c2 : index
    %38 = affine.min #map()[%37]
 return %38 : index
  }
}

```

## Scaling and controls

The following measurements use the same binary running single-threaded. The 40--56-division inputs are passed to `mlir-opt` on standard input. 
| Input | Bytes | Time/status |
|---|---:|---:|
| 20 divisions | 949 | 1.09 s, status 0 |
| 32 divisions | 1,417 | 6.69 s, status 0 |
| 34 divisions | 1,495 | 8.45 s, status 0 |
| 36 divisions | 1,573 | 10.59 s, status 0 |
| 38 divisions | 1,651 | 12.08 s mean, status 0 |
| 40 divisions | 1,729 | 14.77 s, status 0 |
| 42 divisions | 1,807 | 17.83 s, status 0 |
| 44 divisions | 1,885 | 21.36 s, status 0 |
| 48 divisions | 2,041 | 30.26 s, status 0 |
| 52 divisions | 2,197 | 41.77 s, status 0 |
| 56 divisions | 2,353 | 55.52 s, status 0 |

A log-log least-squares fit over the directly measured 38--56-division series gives:
```text
time_seconds ~= 7.03e-6 * divisions^3.946
```
With a log-space RMSE is 0.0036. 

This scaling quickly becomes a problem as increasing the chain from 38 to 56 divisions grows the input by only 42.5% but makes the pass 4.60 times slower. 

Peak memory consumption at 56 divisions was about 68 MiB, so the additional time remains CPU work rather than memory pressure. 

## What is going on

`affine-simplify-min-max` spends the majority of the time attempting one ordering proof between the two map results. An LLDB sample on the 32-division sibling shows repeated layers of:

```text
IntegerRelation::isEmpty
ValueBoundsConstraintSet::comparePos
ValueBoundsConstraintSet::populateAndCompare
ValueBoundsConstraintSet::isProvablyNonNegative
arith::DivSIOpInterface::populateBoundsForIndexValue
ValueBoundsConstraintSet::processWorklist
... multiple repeated layers down the division chain ...
ValueBoundsConstraintSet::strongCompare
affine::simplifyAffineMinOp
SimplifyAffineMinMaxPass::runOnOperation
```

## Expected result

This optional simplification should finish in reasonable time, e.g. O(n^2). 

## LLM-Suggested Cause

The affine simplifier constructs one `ValueBoundsConstraintSet::Variable` for each map result and invokes [`strongCompare` for each candidate pair](https://github.com/llvm/llvm-project/blob/384a71b8a2d99b5deb82792c23202c2b82b8ed82/mlir/lib/Dialect/Affine/Transforms/SimplifyAffineMinMax.cpp#L43-L90). This map has only two results, so the outer pair count is one.

Each [`DivSI` ValueBounds model](https://github.com/llvm/llvm-project/blob/384a71b8a2d99b5deb82792c23202c2b82b8ed82/mlir/lib/Dialect/Arith/IR/ValueBoundsOpInterfaceImpl.cpp#L87-L127) asks four sign questions while constraints are being populated. Those questions recursively enter further population/comparison work, and each comparison can run [Presburger emptiness checks](https://github.com/llvm/llvm-project/blob/384a71b8a2d99b5deb82792c23202c2b82b8ed82/mlir/lib/Interfaces/ValueBoundsOpInterface.cpp#L743-L870). The sampled stack shows this re-entrant layer repeated down the linear division chain.

The likely performance issue is a missing analysis budget, memoization, or re-entrancy guard. The implicated paths remain present in source at main commit `384a71b8a2d99b5deb82792c23202c2b82b8ed82`; I did not have a current-main executable for runtime confirmation.

## LLM Declaration

LLM was used to prepare the bug report, and the bug itself was found and verified by a human.

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to