Skip to content

Commit 0fbb424

Browse files
Better description of simplification, extend to symbolic constant cases
Also address reviewer comment.
1 parent d2f8780 commit 0fbb424

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

mlir/lib/Dialect/Affine/IR/AffineOps.cpp

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,16 +1046,20 @@ simplifyMapWithOperands(AffineMap &map, ArrayRef<Value> operands) {
10461046
map.getContext());
10471047
}
10481048

1049-
/// Assuming `dimOrSym` is a quantity in `map` that is defined by `minOp`,
1050-
/// replaces the patterns:
1049+
/// Assuming `dimOrSym` is a quantity in `map` that is defined by `minOp`.
1050+
/// Assuming that the quantity is of the form:
1051+
/// `affine_min(f(x, y), symbolic_cst)`.
1052+
/// This function checks that `0 < affine_min(f(x, y), symbolic_cst)` and
1053+
/// proceeds with replacing the patterns:
10511054
/// ```
1052-
/// dimOrSym.ceildiv(cst) * cst
1053-
/// (dimOrSym + cst - 1).floordiv(cst) * cst
1055+
/// dimOrSym.ceildiv(symbolic_cst)
1056+
/// (dimOrSym + symbolic_cst - 1).floordiv(symbolic_cst)
10541057
/// ```
1055-
/// by `cst` in `map`.
1056-
/// This simplification is valid iff `minOp` is guaranteed to be nonnegative.
1058+
/// by `1`.
1059+
///
10571060
/// Additionally, allows the caller to pass `affineMinKnownToBeNonNegative` to
10581061
/// inject static information that may not be statically discoverable.
1062+
///
10591063
/// Warning: ValueBoundsConstraintSet::computeConstantBound is needed to check
10601064
/// for the nonnegative case, if `affineMinKnownToBeNonNegative` is false.
10611065
static LogicalResult replaceAffineMinBoundingBoxExpression(
@@ -1071,31 +1075,30 @@ static LogicalResult replaceAffineMinBoundingBoxExpression(
10711075
presburger::BoundType::LB, {row, values},
10721076
/*stopCondition=*/nullptr,
10731077
/*closedUB=*/true);
1074-
if (failed(lowerBound) || lowerBound.value() < 0)
1078+
if (failed(lowerBound) || lowerBound.value() <= 0)
10751079
return failure();
10761080
}
10771081
}
10781082

10791083
AffineMap initialMap = *map;
10801084
for (unsigned i = 0, e = affineMinMap.getNumResults(); i != e; ++i) {
10811085
auto m = affineMinMap.getSubMap(ArrayRef<unsigned>{i});
1082-
// TODO: this should also work with nonnegative symbolic divisors.
1083-
if (!m.isSingleConstant())
1086+
AffineExpr expr = m.getResult(0);
1087+
if (!expr.isSymbolicOrConstant())
10841088
continue;
10851089

1086-
auto cst = m.getSingleConstantResult();
10871090
DenseMap<AffineExpr, AffineExpr> repl;
1088-
// dimOrSym.ceilDiv(cst) * cst -> cst
1089-
repl[dimOrSym.ceilDiv(cst) * cst] =
1090-
getAffineConstantExpr(cst, minOp.getContext());
1091-
// (dimOrSym + cst - 1).floorDiv(cst) * cst -> cst
1092-
repl[(dimOrSym + cst - 1).floorDiv(cst) * cst] =
1093-
getAffineConstantExpr(cst, minOp.getContext());
1091+
// dimOrSym.ceilDiv(expr) -> 1
1092+
repl[dimOrSym.ceilDiv(expr)] = getAffineConstantExpr(1, minOp.getContext());
1093+
// (dimOrSym + expr - 1).floorDiv(expr) -> 1
1094+
repl[(dimOrSym + expr - 1).floorDiv(expr)] =
1095+
getAffineConstantExpr(1, minOp.getContext());
10941096
auto newMap = map->replace(repl);
10951097
if (newMap == *map)
10961098
continue;
10971099
*map = newMap;
10981100
}
1101+
10991102
return success(*map != initialMap);
11001103
}
11011104

@@ -3096,7 +3099,7 @@ void AffineIfOp::build(OpBuilder &builder, OperationState &result,
30963099
/// set constraints.
30973100
static void composeSetAndOperands(IntegerSet &set,
30983101
SmallVectorImpl<Value> &operands,
3099-
bool composeAffineMin) {
3102+
bool composeAffineMin = false) {
31003103
// We will simply reuse the API of the map composition by viewing the LHSs of
31013104
// the equalities and inequalities of `set` as the affine exprs of an affine
31023105
// map. Convert to equivalent map, compose, and convert back to set.
@@ -3116,7 +3119,7 @@ static void composeSetAndOperands(IntegerSet &set,
31163119
LogicalResult AffineIfOp::fold(FoldAdaptor, SmallVectorImpl<OpFoldResult> &) {
31173120
auto set = getIntegerSet();
31183121
SmallVector<Value, 4> operands(getOperands());
3119-
composeSetAndOperands(set, operands, /*composeAffineMin=*/false);
3122+
composeSetAndOperands(set, operands);
31203123
canonicalizeSetAndOperands(&set, &operands);
31213124

31223125
// Check if the canonicalization or composition led to any change.

0 commit comments

Comments
 (0)