@@ -1046,16 +1046,20 @@ simplifyMapWithOperands(AffineMap &map, ArrayRef<Value> operands) {
1046
1046
map.getContext ());
1047
1047
}
1048
1048
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:
1051
1054
// / ```
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)
1054
1057
// / ```
1055
- // / by `cst` in `map `.
1056
- // / This simplification is valid iff `minOp` is guaranteed to be nonnegative.
1058
+ // / by `1 `.
1059
+ // /
1057
1060
// / Additionally, allows the caller to pass `affineMinKnownToBeNonNegative` to
1058
1061
// / inject static information that may not be statically discoverable.
1062
+ // /
1059
1063
// / Warning: ValueBoundsConstraintSet::computeConstantBound is needed to check
1060
1064
// / for the nonnegative case, if `affineMinKnownToBeNonNegative` is false.
1061
1065
static LogicalResult replaceAffineMinBoundingBoxExpression (
@@ -1071,31 +1075,30 @@ static LogicalResult replaceAffineMinBoundingBoxExpression(
1071
1075
presburger::BoundType::LB, {row, values},
1072
1076
/* stopCondition=*/ nullptr ,
1073
1077
/* closedUB=*/ true );
1074
- if (failed (lowerBound) || lowerBound.value () < 0 )
1078
+ if (failed (lowerBound) || lowerBound.value () <= 0 )
1075
1079
return failure ();
1076
1080
}
1077
1081
}
1078
1082
1079
1083
AffineMap initialMap = *map;
1080
1084
for (unsigned i = 0 , e = affineMinMap.getNumResults (); i != e; ++i) {
1081
1085
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 ())
1084
1088
continue ;
1085
1089
1086
- auto cst = m.getSingleConstantResult ();
1087
1090
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 ());
1094
1096
auto newMap = map->replace (repl);
1095
1097
if (newMap == *map)
1096
1098
continue ;
1097
1099
*map = newMap;
1098
1100
}
1101
+
1099
1102
return success (*map != initialMap);
1100
1103
}
1101
1104
@@ -3096,7 +3099,7 @@ void AffineIfOp::build(OpBuilder &builder, OperationState &result,
3096
3099
// / set constraints.
3097
3100
static void composeSetAndOperands (IntegerSet &set,
3098
3101
SmallVectorImpl<Value> &operands,
3099
- bool composeAffineMin) {
3102
+ bool composeAffineMin = false ) {
3100
3103
// We will simply reuse the API of the map composition by viewing the LHSs of
3101
3104
// the equalities and inequalities of `set` as the affine exprs of an affine
3102
3105
// map. Convert to equivalent map, compose, and convert back to set.
@@ -3116,7 +3119,7 @@ static void composeSetAndOperands(IntegerSet &set,
3116
3119
LogicalResult AffineIfOp::fold (FoldAdaptor, SmallVectorImpl<OpFoldResult> &) {
3117
3120
auto set = getIntegerSet ();
3118
3121
SmallVector<Value, 4 > operands (getOperands ());
3119
- composeSetAndOperands (set, operands, /* composeAffineMin= */ false );
3122
+ composeSetAndOperands (set, operands);
3120
3123
canonicalizeSetAndOperands (&set, &operands);
3121
3124
3122
3125
// Check if the canonicalization or composition led to any change.
0 commit comments