[Mlir-commits] [mlir] [mlir] Extend affine.min/max ValueBoundsOpInterfaceImpls (PR #118840)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Fri Jan 24 12:28:08 PST 2025
================
@@ -67,6 +67,27 @@ struct AffineMinOpInterface
expr.replaceDimsAndSymbols(dimReplacements, symReplacements);
cstr.bound(value) <= bound;
}
+ // Get all constant lower bounds, choose minimum, and set lower bound to it.
+ MLIRContext *ctx = op->getContext();
+ AffineMap map = minOp.getAffineMap();
+ SmallVector<Value> mapOperands = minOp.getOperands();
+ std::optional<int64_t> minBound;
+ for (AffineExpr expr : map.getResults()) {
+ auto exprMap =
+ AffineMap::get(map.getNumDims(), map.getNumSymbols(), expr, ctx);
+ ValueBoundsConstraintSet::Variable exprVar(exprMap, mapOperands);
+ FailureOr<int64_t> exprBound =
+ cstr.computeConstantBound(presburger::BoundType::LB, exprVar,
----------------
Max191 wrote:
@matthias-springer I tried the `cstr.populateAndCompare` approach, but I don't think it can achieve the same result. Consider the example from the test:
```
func.func @affine_min_const_lb(%a: index) -> (index) {
%0 = affine.max affine_map<(d0) -> (d0, 0)>(%a)
%1 = affine.min affine_map<(d0) -> (d0, 2)>(%0)
%2 = "test.reify_bound"(%1) {type = "LB"}: (index) -> (index)
return %2 : index
}
```
We want the LB of `%2`, so we try to find the minimum result expr of the affine.min map. Comparing `d0` with `2` gives an uncertain result, because `d0` is only constrained to be `>= 0`. This means `d0` could be less than, equal to, or greater than 2, so we can't determine the min expr in this way. In fact, if we were able to determine a min expr this way, then the affine.min op could be folded away all together.
The difference in what this PR is trying to achieve is that it is computing the lower bounds of each expr separately, and then comparing the bounds to each other. So in this example, even though `d0` could be less than or greater than `2`, the lower bounds of each result expr can be shown to be greater than or equal to 0, and the lower bound of `%2` is 0.
For what this PR wants to do, I think we need to be computing the full bound of each expr. I don't have a good simple solution that avoids extra computation right now, but having this additional analysis can open additional optimizations in some cases. To make this work without the extra computation, I think we would need to make the `computeConstantBound` function non-static, and somehow track state of the bounds of each Value in the `ValueBoundsConstraintSet`.
Do you have any thoughts on what the best option is?
https://github.com/llvm/llvm-project/pull/118840
More information about the Mlir-commits
mailing list