Skip to content

Commit 2032c4d

Browse files
author
git apple-llvm automerger
committed
Merge commit '29a925abb660' from llvm.org/main into next
2 parents 6fa6f19 + 29a925a commit 2032c4d

File tree

9 files changed

+275
-86
lines changed

9 files changed

+275
-86
lines changed

mlir/include/mlir/Analysis/FlatLinearValueConstraints.h

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ class FlatLinearConstraints : public presburger::IntegerPolyhedron {
6666
/// Return the kind of this object.
6767
Kind getKind() const override { return Kind::FlatLinearConstraints; }
6868

69+
/// Flag to control if conservative semi-affine bounds should be added in
70+
/// `addBound()`.
71+
enum class AddConservativeSemiAffineBounds { No = 0, Yes };
72+
6973
/// Adds a bound for the variable at the specified position with constraints
7074
/// being drawn from the specified bound map. In case of an EQ bound, the
7175
/// bound map is expected to have exactly one result. In case of a LB/UB, the
@@ -77,21 +81,39 @@ class FlatLinearConstraints : public presburger::IntegerPolyhedron {
7781
/// as a closed bound by +1/-1 respectively. In case of an EQ bound, it can
7882
/// only be added as a closed bound.
7983
///
84+
/// Conservative bounds for semi-affine expressions will be added if
85+
/// `AddConservativeSemiAffineBounds` is set to `Yes`. This currently only
86+
/// covers semi-affine `mod` expressions, so `addBound()` will still fail if
87+
/// it encounters a semi-affine `floordiv`, `ceildiv`, or `mul`. Note: If
88+
/// enabled it is possible for the resulting constraint set to become empty if
89+
/// a precondition of a conservative bound is found not to hold.
90+
///
8091
/// Note: The dimensions/symbols of this FlatLinearConstraints must match the
8192
/// dimensions/symbols of the affine map.
82-
LogicalResult addBound(presburger::BoundType type, unsigned pos,
83-
AffineMap boundMap, bool isClosedBound);
93+
LogicalResult addBound(
94+
presburger::BoundType type, unsigned pos, AffineMap boundMap,
95+
bool isClosedBound,
96+
AddConservativeSemiAffineBounds = AddConservativeSemiAffineBounds::No);
8497

8598
/// Adds a bound for the variable at the specified position with constraints
8699
/// being drawn from the specified bound map. In case of an EQ bound, the
87100
/// bound map is expected to have exactly one result. In case of a LB/UB, the
88101
/// bound map may have more than one result, for each of which an inequality
89102
/// is added.
103+
///
104+
/// Conservative bounds for semi-affine expressions will be added if
105+
/// `AddConservativeSemiAffineBounds` is set to `Yes`. This currently only
106+
/// covers semi-affine `mod` expressions, so `addBound()` will still fail if
107+
/// it encounters a semi-affine `floordiv`, `ceildiv`, or `mul`. Note: If
108+
/// enabled it is possible for the resulting constraint set to become empty if
109+
/// a precondition of a conservative bound is found not to hold.
110+
///
90111
/// Note: The dimensions/symbols of this FlatLinearConstraints must match the
91112
/// dimensions/symbols of the affine map. By default the lower bound is closed
92113
/// and the upper bound is open.
93-
LogicalResult addBound(presburger::BoundType type, unsigned pos,
94-
AffineMap boundMap);
114+
LogicalResult addBound(
115+
presburger::BoundType type, unsigned pos, AffineMap boundMap,
116+
AddConservativeSemiAffineBounds = AddConservativeSemiAffineBounds::No);
95117

96118
/// The `addBound` overload above hides the inherited overloads by default, so
97119
/// we explicitly introduce them here.
@@ -193,7 +215,8 @@ class FlatLinearConstraints : public presburger::IntegerPolyhedron {
193215
/// Note: This is a shared helper function of `addLowerOrUpperBound` and
194216
/// `composeMatchingMap`.
195217
LogicalResult flattenAlignedMapAndMergeLocals(
196-
AffineMap map, std::vector<SmallVector<int64_t, 8>> *flattenedExprs);
218+
AffineMap map, std::vector<SmallVector<int64_t, 8>> *flattenedExprs,
219+
bool addConservativeSemiAffineBounds = false);
197220

198221
/// Prints the number of constraints, dimensions, symbols and locals in the
199222
/// FlatLinearConstraints. Also, prints for each variable whether there is
@@ -468,18 +491,19 @@ class FlatLinearValueConstraints : public FlatLinearConstraints {
468491
/// Flattens 'expr' into 'flattenedExpr', which contains the coefficients of the
469492
/// dimensions, symbols, and additional variables that represent floor divisions
470493
/// of dimensions, symbols, and in turn other floor divisions. Returns failure
471-
/// if 'expr' could not be flattened (i.e., semi-affine is not yet handled).
494+
/// if 'expr' could not be flattened (i.e., an unhandled semi-affine was found).
472495
/// 'cst' contains constraints that connect newly introduced local variables
473496
/// to existing dimensional and symbolic variables. See documentation for
474497
/// AffineExprFlattener on how mod's and div's are flattened.
475-
LogicalResult getFlattenedAffineExpr(AffineExpr expr, unsigned numDims,
476-
unsigned numSymbols,
477-
SmallVectorImpl<int64_t> *flattenedExpr,
478-
FlatLinearConstraints *cst = nullptr);
498+
LogicalResult
499+
getFlattenedAffineExpr(AffineExpr expr, unsigned numDims, unsigned numSymbols,
500+
SmallVectorImpl<int64_t> *flattenedExpr,
501+
FlatLinearConstraints *cst = nullptr,
502+
bool addConservativeSemiAffineBounds = false);
479503

480504
/// Flattens the result expressions of the map to their corresponding flattened
481505
/// forms and set in 'flattenedExprs'. Returns failure if any expression in the
482-
/// map could not be flattened (i.e., semi-affine is not yet handled). 'cst'
506+
/// map could not be flattened (i.e., an unhandled semi-affine was found). 'cst'
483507
/// contains constraints that connect newly introduced local variables to
484508
/// existing dimensional and / symbolic variables. See documentation for
485509
/// AffineExprFlattener on how mod's and div's are flattened. For all affine
@@ -490,7 +514,8 @@ LogicalResult getFlattenedAffineExpr(AffineExpr expr, unsigned numDims,
490514
LogicalResult
491515
getFlattenedAffineExprs(AffineMap map,
492516
std::vector<SmallVector<int64_t, 8>> *flattenedExprs,
493-
FlatLinearConstraints *cst = nullptr);
517+
FlatLinearConstraints *cst = nullptr,
518+
bool addConservativeSemiAffineBounds = false);
494519
LogicalResult
495520
getFlattenedAffineExprs(IntegerSet set,
496521
std::vector<SmallVector<int64_t, 8>> *flattenedExprs,

mlir/include/mlir/Dialect/Vector/IR/ScalableValueBoundsConstraintSet.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ struct ScalableValueBoundsConstraintSet
3333
MLIRContext *context,
3434
ValueBoundsConstraintSet::StopConditionFn stopCondition,
3535
unsigned vscaleMin, unsigned vscaleMax)
36-
: RTTIExtends(context, stopCondition), vscaleMin(vscaleMin),
37-
vscaleMax(vscaleMax) {};
36+
: RTTIExtends(context, stopCondition,
37+
/*addConservativeSemiAffineBounds=*/true),
38+
vscaleMin(vscaleMin), vscaleMax(vscaleMax) {};
3839

3940
using RTTIExtends::bound;
4041
using RTTIExtends::StopConditionFn;

mlir/include/mlir/IR/AffineExprVisitor.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -413,18 +413,22 @@ class SimpleAffineExprFlattener
413413
/// lhs of the mod, floordiv, ceildiv or mul expression and with respect to a
414414
/// symbolic rhs expression. `localExpr` is the simplified tree expression
415415
/// (AffineExpr) corresponding to the quantifier.
416-
virtual void addLocalIdSemiAffine(AffineExpr localExpr);
416+
virtual LogicalResult addLocalIdSemiAffine(ArrayRef<int64_t> lhs,
417+
ArrayRef<int64_t> rhs,
418+
AffineExpr localExpr);
417419

418420
private:
419-
/// Adds `expr`, which may be mod, ceildiv, floordiv or mod expression
421+
/// Adds `localExpr`, which may be mod, ceildiv, floordiv or mod expression
420422
/// representing the affine expression corresponding to the quantifier
421-
/// introduced as the local variable corresponding to `expr`. If the
423+
/// introduced as the local variable corresponding to `localExpr`. If the
422424
/// quantifier is already present, we put the coefficient in the proper index
423425
/// of `result`, otherwise we add a new local variable and put the coefficient
424426
/// there.
425-
void addLocalVariableSemiAffine(AffineExpr expr,
426-
SmallVectorImpl<int64_t> &result,
427-
unsigned long resultSize);
427+
LogicalResult addLocalVariableSemiAffine(ArrayRef<int64_t> lhs,
428+
ArrayRef<int64_t> rhs,
429+
AffineExpr localExpr,
430+
SmallVectorImpl<int64_t> &result,
431+
unsigned long resultSize);
428432

429433
// t = expr floordiv c <=> t = q, c * q <= expr <= c * q + c - 1
430434
// A floordiv is thus flattened by introducing a new local variable q, and

mlir/include/mlir/Interfaces/ValueBoundsOpInterface.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,8 @@ class ValueBoundsConstraintSet
313313
/// An index-typed value or the dimension of a shaped-type value.
314314
using ValueDim = std::pair<Value, int64_t>;
315315

316-
ValueBoundsConstraintSet(MLIRContext *ctx, StopConditionFn stopCondition);
316+
ValueBoundsConstraintSet(MLIRContext *ctx, StopConditionFn stopCondition,
317+
bool addConservativeSemiAffineBounds = false);
317318

318319
/// Return "true" if, based on the current state of the constraint system,
319320
/// "lhs cmp rhs" was proven to hold. Return "false" if the specified relation
@@ -404,6 +405,9 @@ class ValueBoundsConstraintSet
404405

405406
/// The current stop condition function.
406407
StopConditionFn stopCondition = nullptr;
408+
409+
/// Should conservative bounds be added for semi-affine expressions.
410+
bool addConservativeSemiAffineBounds = false;
407411
};
408412

409413
} // namespace mlir

0 commit comments

Comments
 (0)