19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/ADT/SmallPtrSet.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/raw_ostream.h"
26 #define DEBUG_TYPE "flat-value-constraints"
29 using namespace presburger;
49 AffineExprFlattener(
unsigned nDims,
unsigned nSymbols)
81 struct SemiAffineExprFlattener :
public AffineExprFlattener {
82 using AffineExprFlattener::AffineExprFlattener;
89 assert(succeeded(result) &&
90 "unexpected failure in SimpleAffineExprFlattener");
111 bSubR.insert(bSubR.begin() + rPos, -1);
140 bool addConservativeSemiAffineBounds =
false) {
147 auto flattenExprs = [&](AffineExprFlattener &flattener) -> LogicalResult {
150 for (
auto expr : exprs) {
151 auto flattenResult = flattener.walkPostOrder(expr);
152 if (failed(flattenResult))
156 assert(flattener.operandExprStack.size() == exprs.size());
157 flattenedExprs->clear();
158 flattenedExprs->assign(flattener.operandExprStack.begin(),
159 flattener.operandExprStack.end());
167 if (addConservativeSemiAffineBounds) {
168 SemiAffineExprFlattener flattener(numDims, numSymbols);
169 return flattenExprs(flattener);
172 AffineExprFlattener flattener(numDims, numSymbols);
173 return flattenExprs(flattener);
179 AffineExpr expr,
unsigned numDims,
unsigned numSymbols,
181 bool addConservativeSemiAffineBounds) {
182 std::vector<SmallVector<int64_t, 8>> flattenedExprs;
185 localVarCst, addConservativeSemiAffineBounds);
186 *flattenedExpr = flattenedExprs[0];
203 localVarCst, addConservativeSemiAffineBounds);
228 assert(other.
getNumDims() == getNumDimVars() &&
"dim mismatch");
229 assert(other.
getNumSymbols() == getNumSymbolVars() &&
"symbol mismatch");
231 std::vector<SmallVector<int64_t, 8>> flatExprs;
232 if (failed(flattenAlignedMapAndMergeLocals(other, &flatExprs)))
245 for (
unsigned r = 0, e = flatExprs.size(); r < e; r++) {
246 const auto &flatExpr = flatExprs[r];
254 for (
unsigned i = 0, f = other.
getNumInputs(); i < f; i++) {
256 eqToAdd[e + i] = -flatExpr[i];
259 unsigned j = getNumDimVars() + getNumSymbolVars();
260 unsigned end = flatExpr.size() - 1;
262 eqToAdd[
j] = -flatExpr[i];
266 eqToAdd[getNumCols() - 1] = -flatExpr[flatExpr.size() - 1];
269 addEquality(eqToAdd);
303 unsigned offset,
unsigned num, int64_t lbConst,
310 if (lbConst != 0 || ubConst < 1)
312 int64_t divisor = ubConst + 1;
316 curEquality < numEqualities; curEquality++) {
317 int64_t coefficientAtPos = cst.
atEq64(curEquality, pos);
320 if (coefficientAtPos == 0)
336 unsigned quotientCount = 0;
337 int quotientPosition = -1;
338 int quotientSign = 1;
346 int64_t coefficientOfCurVar = cst.
atEq64(curEquality, curVar);
348 if (coefficientOfCurVar == 0)
351 if (coefficientOfCurVar % (divisor * coefficientAtPos) == 0) {
353 quotientPosition = curVar;
354 quotientSign = (coefficientOfCurVar * coefficientAtPos) > 0 ? 1 : -1;
361 dividendExpr = dividendExpr + memo[curVar] * coefficientOfCurVar;
369 if (coefficientAtPos > 0)
370 dividendExpr = (-dividendExpr).floorDiv(coefficientAtPos);
372 dividendExpr = dividendExpr.floorDiv(-coefficientAtPos);
381 auto dimExpr = dyn_cast<AffineDimExpr>(dividendExpr);
386 if (quotientCount >= 1) {
391 unsigned dimExprPos = dimExpr.getPosition();
392 unsigned dimExprCol = dimExprPos < offset ? dimExprPos : dimExprPos + num;
396 if (ub && *ub < divisor)
399 memo[pos] = dimExpr % divisor;
402 if (quotientCount == 1 && !memo[quotientPosition])
403 memo[quotientPosition] = dimExpr.floorDiv(divisor) * quotientSign;
421 assert(pos < cst.
getNumVars() &&
"invalid position");
425 for (
unsigned i = 0, e = cst.
getNumVars(); i < e; ++i)
439 for (
unsigned c = 0, f = cst.
getNumVars(); c < f; c++)
440 if (dividend[c] != 0)
441 dividendExpr = dividendExpr + dividend[c] * exprs[c];
444 exprs[pos] = dividendExpr.floorDiv(divisor);
449 unsigned pos,
unsigned offset,
unsigned num,
unsigned symStartPos,
451 bool closedUB)
const {
452 assert(pos + offset < getNumDimVars() &&
"invalid dim start pos");
453 assert(symStartPos >= (pos + offset) &&
"invalid sym start pos");
454 assert(getNumLocalVars() == localExprs.size() &&
455 "incorrect local exprs count");
458 getLowerAndUpperBoundIndices(pos + offset, &lbIndices, &ubIndices, &eqIndices,
464 for (
unsigned i = 0, e = a.size(); i < e; ++i) {
465 if (i < offset || i >= offset + num)
472 unsigned dimCount = symStartPos - num;
473 unsigned symCount = getNumDimAndSymbolVars() - symStartPos;
474 lbExprs.reserve(lbIndices.size() + eqIndices.size());
476 for (
auto idx : lbIndices) {
477 auto ineq = getInequality64(idx);
482 std::transform(lb.begin(), lb.end(), lb.begin(), std::negate<int64_t>());
486 int64_t divisor =
std::abs(ineq[pos + offset]);
487 expr = (expr + divisor - 1).floorDiv(divisor);
488 lbExprs.push_back(expr);
492 ubExprs.reserve(ubIndices.size() + eqIndices.size());
494 for (
auto idx : ubIndices) {
495 auto ineq = getInequality64(idx);
500 expr = expr.floorDiv(
std::abs(ineq[pos + offset]));
501 int64_t ubAdjustment = closedUB ? 0 : 1;
502 ubExprs.push_back(expr + ubAdjustment);
507 for (
auto idx : eqIndices) {
508 auto eq = getEquality64(idx);
510 if (eq[pos + offset] > 0)
511 std::transform(b.begin(), b.end(), b.begin(), std::negate<int64_t>());
516 expr = expr.floorDiv(
std::abs(eq[pos + offset]));
518 ubExprs.push_back(expr + 1);
522 expr = expr.ceilDiv(
std::abs(eq[pos + offset]));
523 lbExprs.push_back(expr);
526 auto lbMap =
AffineMap::get(dimCount, symCount, lbExprs, context);
527 auto ubMap =
AffineMap::get(dimCount, symCount, ubExprs, context);
529 return {lbMap, ubMap};
542 assert(offset + num <= getNumDimVars() &&
"invalid range");
545 normalizeConstraintsByGCD();
547 LLVM_DEBUG(llvm::dbgs() <<
"getSliceBounds for first " << num
554 for (
unsigned i = 0, e = getNumDimVars(); i < e; i++) {
557 else if (i >= offset + num)
560 for (
unsigned i = getNumDimVars(), e = getNumDimAndSymbolVars(); i < e; i++)
568 for (
unsigned pos = 0; pos < getNumVars(); pos++) {
574 if (lbConst.has_value() && ubConst.has_value()) {
576 if (*lbConst == *ubConst) {
584 if (
detectAsMod(*
this, pos, offset, num, *lbConst, *ubConst, context,
600 if (!findConstraintWithNonZeroAt(pos,
true, &idx)) {
607 for (
j = 0, e = getNumVars();
j < e; ++
j) {
610 int64_t c = atEq64(idx,
j);
616 expr = expr + memo[
j] * c;
624 expr = expr + atEq64(idx, getNumVars());
625 int64_t vPos = atEq64(idx, pos);
626 assert(vPos != 0 &&
"expected non-zero here");
628 expr = (-expr).floorDiv(vPos);
631 expr = expr.floorDiv(-vPos);
641 int64_t ubAdjustment = closedUB ? 0 : 1;
646 std::optional<FlatLinearConstraints> tmpClone;
647 for (
unsigned pos = 0; pos < num; pos++) {
648 unsigned numMapDims = getNumDimVars() - num;
649 unsigned numMapSymbols = getNumSymbolVars();
659 ubMap =
AffineMap::get(numMapDims, numMapSymbols, expr + ubAdjustment);
664 if (getNumLocalVars() == 0) {
670 tmpClone->removeRedundantInequalities();
672 std::tie(lbMap, ubMap) = tmpClone->getLowerAndUpperBound(
673 pos, offset, num, getNumDimVars(), {}, context,
683 LLVM_DEBUG(llvm::dbgs()
684 <<
"WARNING: Potentially over-approximating slice lb\n");
685 auto lbConst = getConstantBound64(
BoundType::LB, pos + offset);
686 if (lbConst.has_value()) {
692 LLVM_DEBUG(llvm::dbgs()
693 <<
"WARNING: Potentially over-approximating slice ub\n");
694 auto ubConst = getConstantBound64(
BoundType::UB, pos + offset);
695 if (ubConst.has_value()) {
697 numMapDims, numMapSymbols,
702 LLVM_DEBUG(llvm::dbgs()
703 <<
"lb map for pos = " << Twine(pos + offset) <<
", expr: ");
704 LLVM_DEBUG(lbMap.
dump(););
705 LLVM_DEBUG(llvm::dbgs()
706 <<
"ub map for pos = " << Twine(pos + offset) <<
", expr: ");
707 LLVM_DEBUG(ubMap.
dump(););
713 bool addConservativeSemiAffineBounds) {
716 addConservativeSemiAffineBounds))) {
717 LLVM_DEBUG(llvm::dbgs()
718 <<
"composition unimplemented for semi-affine maps\n");
724 unsigned numLocalVars = getNumLocalVars();
740 assert(boundMap.
getNumDims() == getNumDimVars() &&
"dim mismatch");
741 assert(boundMap.
getNumSymbols() == getNumSymbolVars() &&
"symbol mismatch");
742 assert(pos < getNumDimAndSymbolVars() &&
"invalid position");
744 "EQ bound must be closed.");
749 "single result expected");
752 std::vector<SmallVector<int64_t, 8>> flatExprs;
753 if (failed(flattenAlignedMapAndMergeLocals(
754 boundMap, &flatExprs,
755 addSemiAffineBounds == AddConservativeSemiAffineBounds::Yes)))
760 for (
const auto &flatExpr : flatExprs) {
764 ineq[
j] = lower ? -flatExpr[
j] : flatExpr[
j];
771 ineq[pos] = lower ? 1 : -1;
773 unsigned j = getNumDimVars() + getNumSymbolVars();
774 unsigned end = flatExpr.size() - 1;
775 for (
unsigned i = boundMap.
getNumInputs(); i < end; i++,
j++) {
776 ineq[
j] = lower ? -flatExpr[i] : flatExpr[i];
780 int64_t boundAdjustment = (isClosedBound || type ==
BoundType::EQ) ? 0 : -1;
782 ineq[getNumCols() - 1] = (lower ? -flatExpr[flatExpr.size() - 1]
783 : flatExpr[flatExpr.size() - 1]) +
785 type ==
BoundType::EQ ? addEquality(ineq) : addInequality(ineq);
794 return addBound(type, pos, boundMap,
804 unsigned numDims = getNumDimVars();
805 unsigned numSyms = getNumSymbolVars();
808 for (
unsigned i = 0; i < numDims; i++)
810 for (
unsigned i = numDims, e = numDims + numSyms; i < e; i++)
819 for (
unsigned i = 0, e = getNumLocalVars(); i < e; ++i)
820 if (!memo[numDims + numSyms + i] &&
828 llvm::all_of(localExprs, [](
AffineExpr expr) {
return expr; }));
832 if (getNumConstraints() == 0)
841 if (failed(computeLocalVars(memo, context))) {
845 unsigned numDimsSymbols = getNumDimAndSymbolVars();
846 for (
unsigned i = numDimsSymbols, e = getNumVars(); i < e; ++i) {
847 if (!memo[i] && !isColZero(i))
848 noLocalRepVars.push_back(i - numDimsSymbols);
850 if (!noLocalRepVars.empty()) {
852 llvm::dbgs() <<
"local variables at position(s) ";
853 llvm::interleaveComma(noLocalRepVars, llvm::dbgs());
854 llvm::dbgs() <<
" do not have an explicit representation in:\n";
865 unsigned numDims = getNumDimVars();
866 unsigned numSyms = getNumSymbolVars();
869 std::fill(eqFlags.begin(), eqFlags.begin() + getNumEqualities(),
true);
870 std::fill(eqFlags.begin() + getNumEqualities(), eqFlags.end(),
false);
873 exprs.reserve(getNumConstraints());
875 for (
unsigned i = 0, e = getNumEqualities(); i < e; ++i)
877 numSyms, localExprs, context));
878 for (
unsigned i = 0, e = getNumInequalities(); i < e; ++i)
880 numSyms, localExprs, context));
892 set.getNumDims() + set.getNumSymbols() + 1,
893 set.getNumDims(), set.getNumSymbols(),
895 assert((operands.empty() || set.
getNumInputs() == operands.size()) &&
896 "operand count mismatch");
898 for (
unsigned i = 0, e = operands.size(); i < e; ++i)
902 std::vector<SmallVector<int64_t, 8>> flatExprs;
905 assert(
false &&
"flattening unimplemented for semi-affine integer sets");
912 for (
unsigned i = 0, e = flatExprs.size(); i < e; ++i) {
913 const auto &flatExpr = flatExprs[i];
927 return insertVar(VarKind::SetDim, pos, vals);
932 return insertVar(VarKind::Symbol, pos, vals);
937 return insertVar(VarKind::SetDim, pos, vals);
942 return insertVar(VarKind::Symbol, pos, vals);
947 unsigned absolutePos = IntegerPolyhedron::insertVar(kind, pos, num);
954 assert(!vals.empty() &&
"expected ValueRange with Values.");
955 assert(kind != VarKind::Local &&
956 "values cannot be attached to local variables.");
957 unsigned num = vals.size();
958 unsigned absolutePos = IntegerPolyhedron::insertVar(kind, pos, num);
961 for (
unsigned i = 0, e = vals.size(); i < e; ++i)
978 return std::equal(aMaybeValues.begin(), aMaybeValues.end(),
979 bMaybeValues.begin(), bMaybeValues.end());
995 "Start position out of bounds");
1004 maybeValuesAll.data() + end};
1006 for (std::optional<Value> val : maybeValues)
1007 if (val && !uniqueVars.insert(*val).second)
1014 static bool LLVM_ATTRIBUTE_UNUSED
1021 static bool LLVM_ATTRIBUTE_UNUSED
1024 if (kind == VarKind::SetDim)
1026 if (kind == VarKind::Symbol)
1029 llvm_unreachable(
"Unexpected VarKind");
1046 assert(offset <= a->getNumDimVars() && offset <= b->getNumDimVars());
1048 assert(llvm::all_of(
1050 [](
const std::optional<Value> &var) { return var.has_value(); }));
1052 assert(llvm::all_of(
1054 [](
const std::optional<Value> &var) { return var.has_value(); }));
1061 unsigned d = offset;
1062 for (
Value aDimValue : aDimValues) {
1067 if (b->
findVar(aDimValue, &loc, d)) {
1068 assert(loc >= offset &&
"A's dim appears in B's aligned range");
1069 assert(loc < b->getNumDimVars() &&
1070 "A's dim appears in B's non-dim position");
1082 "expected same number of dims");
1111 for (
Value aSymValue : aSymValues) {
1131 "expected same number of symbols");
1135 unsigned varLimit) {
1136 IntegerPolyhedron::removeVarRange(kind, varStart, varLimit);
1142 assert(map.
getNumInputs() == operands.size() &&
"number of inputs mismatch");
1154 for (
unsigned i = 0, e =
getNumVarKind(VarKind::SetDim); i < e; ++i) {
1158 for (
unsigned i = 0, e =
getNumVarKind(VarKind::Symbol); i < e; ++i) {
1166 assert(syms.size() == newSymsPtr->size() &&
"unexpected new/missing symbols");
1167 assert(std::equal(syms.begin(), syms.end(), newSymsPtr->begin()) &&
1168 "unexpected new/missing symbols");
1173 unsigned offset)
const {
1175 for (
unsigned i = offset, e = maybeValues.size(); i < e; ++i)
1176 if (maybeValues[i] && maybeValues[i].value() == val) {
1193 assert(0 &&
"var not found");
1227 bool ret =
findVar(val, &pos);
1239 assert(std::equal(maybeValues.begin(), maybeValues.begin() +
getNumDimVars(),
1240 otherMaybeValues.begin(),
1242 "dim values mismatch");
1243 assert(otherCst.
getNumLocalVars() == 0 &&
"local vars not supported here");
1244 assert(
getNumLocalVars() == 0 &&
"local vars not supported yet here");
1250 return IntegerPolyhedron::unionBoundingBox(otherCopy);
1253 return IntegerPolyhedron::unionBoundingBox(otherCst);
1264 "expected same number of operands and map inputs");
1268 unsigned numSymbols = syms.size();
1272 newSyms->append(syms.begin(), syms.end());
1278 auto dimIt = llvm::find(dims, operand.value());
1279 auto symIt = llvm::find(syms, operand.value());
1280 if (dimIt != dims.end()) {
1283 }
else if (symIt != syms.end()) {
1291 newSyms->push_back(operand.value());
1295 dimReplacements[operand.index()] = replacement;
1297 symReplacements[operand.index() - map.
getNumDims()] = replacement;
1302 dims.size(), numSymbols);
1309 std::vector<SmallVector<int64_t, 8>> flattenedExprs;
1312 if (result.failed())
1317 "AffineMap cannot produce divs without local representation");
1322 for (
unsigned i = 0, e = flattenedExprs.size(); i < e; ++i)
1323 for (
unsigned j = 0, f = flattenedExprs[i].size();
j < f; ++
j)
1324 mat(i,
j) = flattenedExprs[i][
j];
static bool detectAsFloorDiv(const FlatLinearConstraints &cst, unsigned pos, MLIRContext *context, SmallVectorImpl< AffineExpr > &exprs)
Check if the pos^th variable can be expressed as a floordiv of an affine function of other variables ...
static bool detectAsMod(const FlatLinearConstraints &cst, unsigned pos, unsigned offset, unsigned num, int64_t lbConst, int64_t ubConst, MLIRContext *context, SmallVectorImpl< AffineExpr > &memo)
static bool LLVM_ATTRIBUTE_UNUSED areVarsUnique(const FlatLinearValueConstraints &cst, unsigned start, unsigned end)
Checks if the SSA values associated with cst's variables in range [start, end) are unique.
static LogicalResult getFlattenedAffineExprs(ArrayRef< AffineExpr > exprs, unsigned numDims, unsigned numSymbols, std::vector< SmallVector< int64_t, 8 >> *flattenedExprs, FlatLinearConstraints *localVarCst, bool addConservativeSemiAffineBounds=false)
static bool areVarsAligned(const FlatLinearValueConstraints &a, const FlatLinearValueConstraints &b)
Checks if two constraint systems are in the same space, i.e., if they are associated with the same se...
static void mergeAndAlignVars(unsigned offset, FlatLinearValueConstraints *a, FlatLinearValueConstraints *b)
Merge and align the variables of A and B starting at 'offset', so that both constraint systems get th...
Base type for affine expression.
AffineExprKind getKind() const
Return the classification for this type.
A multi-dimensional affine map Affine map's are immutable like Type's, and they are uniqued.
MLIRContext * getContext() const
static AffineMap get(MLIRContext *context)
Returns a zero result affine map with no dimensions or symbols: () -> ().
unsigned getNumSymbols() const
unsigned getNumDims() const
ArrayRef< AffineExpr > getResults() const
unsigned getNumResults() const
AffineMap replaceDimsAndSymbols(ArrayRef< AffineExpr > dimReplacements, ArrayRef< AffineExpr > symReplacements, unsigned numResultDims, unsigned numResultSyms) const
This method substitutes any uses of dimensions and symbols (e.g.
unsigned getNumInputs() const
This class is a general helper class for creating context-global objects like types,...
AffineExpr getAffineSymbolExpr(unsigned position)
AffineExpr getAffineDimExpr(unsigned position)
FlatLinearConstraints is an extension of IntegerPolyhedron.
IntegerSet getAsIntegerSet(MLIRContext *context) const
Returns the constraint system as an integer set.
LogicalResult flattenAlignedMapAndMergeLocals(AffineMap map, std::vector< SmallVector< int64_t, 8 >> *flattenedExprs, bool addConservativeSemiAffineBounds=false)
Given an affine map that is aligned with this constraint system:
unsigned appendLocalVar(unsigned num=1)
void printSpace(raw_ostream &os) const override
Prints the number of constraints, dimensions, symbols and locals in the FlatLinearConstraints.
AddConservativeSemiAffineBounds
Flag to control if conservative semi-affine bounds should be added in addBound().
LogicalResult composeMatchingMap(AffineMap other)
Composes an affine map whose dimensions and symbols match one to one with the dimensions and symbols ...
void getSliceBounds(unsigned offset, unsigned num, MLIRContext *context, SmallVectorImpl< AffineMap > *lbMaps, SmallVectorImpl< AffineMap > *ubMaps, bool closedUB=false)
Computes the lower and upper bounds of the first num dimensional variables (starting at offset) as an...
LogicalResult addBound(presburger::BoundType type, unsigned pos, AffineMap boundMap, bool isClosedBound, AddConservativeSemiAffineBounds=AddConservativeSemiAffineBounds::No)
Adds a bound for the variable at the specified position with constraints being drawn from the specifi...
std::pair< AffineMap, AffineMap > getLowerAndUpperBound(unsigned pos, unsigned offset, unsigned num, unsigned symStartPos, ArrayRef< AffineExpr > localExprs, MLIRContext *context, bool closedUB=false) const
Gets the lower and upper bound of the offset + posth variable treating [0, offset) U [offset + num,...
LogicalResult computeLocalVars(SmallVectorImpl< AffineExpr > &memo, MLIRContext *context) const
Compute an explicit representation for local vars.
FlatLinearValueConstraints represents an extension of FlatLinearConstraints where each non-local vari...
unsigned appendDimVar(unsigned num=1)
Append variables of the specified kind after the last variable of that kind.
unsigned insertDimVar(unsigned pos, ValueRange vals)
LogicalResult unionBoundingBox(const FlatLinearValueConstraints &other)
Updates the constraints to be the smallest bounding (enclosing) box that contains the points of this ...
void mergeAndAlignVarsWithOther(unsigned offset, FlatLinearValueConstraints *other)
Merge and align the variables of this and other starting at offset, so that both constraint systems g...
SmallVector< std::optional< Value > > getMaybeValues() const
unsigned insertSymbolVar(unsigned pos, unsigned num=1)
bool hasValue(unsigned pos) const
Returns true if the pos^th variable has an associated Value.
Value getValue(unsigned pos) const
Returns the Value associated with the pos^th variable.
void printSpace(raw_ostream &os) const override
Prints the number of constraints, dimensions, symbols and locals in the FlatAffineValueConstraints.
void mergeSymbolVars(FlatLinearValueConstraints &other)
Merge and align symbols of this and other such that both get union of of symbols that are unique.
void projectOut(Value val)
Projects out the variable that is associate with Value.
bool containsVar(Value val) const
Returns true if a variable with the specified Value exists, false otherwise.
void removeVarRange(presburger::VarKind kind, unsigned varStart, unsigned varLimit) override
Removes variables in the column range [varStart, varLimit), and copies any remaining valid data into ...
unsigned appendDimVar(ValueRange vals)
unsigned appendSymbolVar(unsigned num=1)
unsigned insertSymbolVar(unsigned pos, ValueRange vals)
bool findVar(Value val, unsigned *pos, unsigned offset=0) const
Looks up the position of the variable with the specified Value starting with variables at offset offs...
LogicalResult addBound(presburger::BoundType type, unsigned pos, AffineMap boundMap, bool isClosedBound, AddConservativeSemiAffineBounds=AddConservativeSemiAffineBounds::No)
Adds a bound for the variable at the specified position with constraints being drawn from the specifi...
bool areVarsAlignedWithOther(const FlatLinearConstraints &other)
Returns true if this constraint system and other are in the same space, i.e., if they are associated ...
AffineMap computeAlignedMap(AffineMap map, ValueRange operands) const
Align map with this constraint system based on operands.
void getValues(unsigned start, unsigned end, SmallVectorImpl< Value > *values) const
Returns the Values associated with variables in range [start, end).
FlatLinearValueConstraints(unsigned numReservedInequalities, unsigned numReservedEqualities, unsigned numReservedCols, unsigned numDims, unsigned numSymbols, unsigned numLocals, ArrayRef< std::optional< Value >> valArgs)
Constructs a constraint system reserving memory for the specified number of constraints and variables...
void setValue(unsigned pos, Value val)
Sets the Value associated with the pos^th variable.
unsigned insertDimVar(unsigned pos, unsigned num=1)
Insert variables of the specified kind at position pos.
unsigned insertVar(presburger::VarKind kind, unsigned pos, unsigned num=1) override
Insert num variables of the specified kind at position pos.
An integer set representing a conjunction of one or more affine equalities and inequalities.
unsigned getNumDims() const
static IntegerSet get(unsigned dimCount, unsigned symbolCount, ArrayRef< AffineExpr > constraints, ArrayRef< bool > eqFlags)
unsigned getNumInputs() const
unsigned getNumConstraints() const
ArrayRef< AffineExpr > getConstraints() const
ArrayRef< bool > getEqFlags() const
Returns the equality bits, which specify whether each of the constraints is an equality or inequality...
unsigned getNumSymbols() const
MLIRContext is the top-level object for a collection of MLIR operations.
virtual void addLocalFloorDivId(ArrayRef< int64_t > dividend, int64_t divisor, AffineExpr localExpr)
virtual LogicalResult addLocalIdSemiAffine(ArrayRef< int64_t > lhs, ArrayRef< int64_t > rhs, AffineExpr localExpr)
Add a local identifier (needed to flatten a mod, floordiv, ceildiv, mul expr) when the rhs is a symbo...
SimpleAffineExprFlattener(unsigned numDims, unsigned numSymbols)
This class provides an abstraction over the different types of ranges over Values.
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Class storing division representation of local variables of a constraint system.
unsigned getNumDivs() const
An Identifier stores a pointer to an object, such as a Value or an Operation.
An IntegerPolyhedron represents the set of points from a PresburgerSpace that satisfy a list of affin...
virtual void swapVar(unsigned posA, unsigned posB)
Swap the posA^th variable with the posB^th variable.
int64_t atEq64(unsigned i, unsigned j) const
The same, but casts to int64_t.
unsigned getVarKindEnd(VarKind kind) const
Return the index at Which the specified kind of vars ends.
unsigned getNumSymbolVars() const
std::optional< int64_t > getConstantBound64(BoundType type, unsigned pos) const
The same, but casts to int64_t.
unsigned getNumVarKind(VarKind kind) const
Get the number of vars of the specified kind.
void addLocalFloorDiv(ArrayRef< DynamicAPInt > dividend, const DynamicAPInt &divisor)
Adds a new local variable as the floordiv of an affine function of other variables,...
unsigned getNumDomainVars() const
unsigned getNumVars() const
void append(const IntegerRelation &other)
Appends constraints from other into this.
void addEquality(ArrayRef< DynamicAPInt > eq)
Adds an equality from the coefficients specified in eq.
unsigned getNumRangeVars() const
unsigned getNumLocalVars() const
unsigned getNumDimAndSymbolVars() const
unsigned getNumCols() const
Returns the number of columns in the constraint system.
DivisionRepr getLocalReprs(std::vector< MaybeLocalRepr > *repr=nullptr) const
Returns a DivisonRepr representing the division representation of local variables in the constraint s...
virtual void clearAndCopyFrom(const IntegerRelation &other)
Replaces the contents of this IntegerRelation with other.
void addInequality(ArrayRef< DynamicAPInt > inEq)
Adds an inequality (>= 0) from the coefficients specified in inEq.
unsigned mergeLocalVars(IntegerRelation &other)
Adds additional local vars to the sets such that they both have the union of the local vars in each s...
unsigned getNumEqualities() const
unsigned getVarKindOffset(VarKind kind) const
Return the index at which the specified kind of vars starts.
unsigned getNumDimVars() const
virtual void fourierMotzkinEliminate(unsigned pos, bool darkShadow=false, bool *isResultIntegerExact=nullptr)
Eliminates the variable at the specified position using Fourier-Motzkin variable elimination,...
This class represents a multi-affine function with the domain as Z^d, where d is the number of domain...
Identifier getId(VarKind kind, unsigned pos) const
Get the identifier of pos^th variable of the specified kind.
static PresburgerSpace getSetSpace(unsigned numDims=0, unsigned numSymbols=0, unsigned numLocals=0)
static void printSpace(std::ostream &os, int count)
constexpr void enumerate(std::tuple< Tys... > &tuple, CallbackT &&callback)
BoundType
The type of bound: equal, lower bound or upper bound.
MaybeLocalRepr computeSingleVarRepr(const IntegerRelation &cst, ArrayRef< bool > foundRepr, unsigned pos, MutableArrayRef< DynamicAPInt > dividend, DynamicAPInt &divisor)
Returns the MaybeLocalRepr struct which contains the indices of the constraints that can be expressed...
Fraction abs(const Fraction &f)
Include the generated interface declarations.
const FrozenRewritePatternSet GreedyRewriteConfig bool * changed
AffineMap alignAffineMapWithValues(AffineMap map, ValueRange operands, ValueRange dims, ValueRange syms, SmallVector< Value > *newSyms=nullptr)
Re-indexes the dimensions and symbols of an affine map with given operands values to align with dims ...
@ Mod
RHS of mod is always a constant or a symbolic expression with a positive value.
AffineExpr getAffineExprFromFlatForm(ArrayRef< int64_t > flatExprs, unsigned numDims, unsigned numSymbols, ArrayRef< AffineExpr > localExprs, MLIRContext *context)
Constructs an affine expression from a flat ArrayRef.
AffineExpr getAffineConstantExpr(int64_t constant, MLIRContext *context)
LogicalResult getFlattenedAffineExpr(AffineExpr expr, unsigned numDims, unsigned numSymbols, SmallVectorImpl< int64_t > *flattenedExpr, FlatLinearConstraints *cst=nullptr, bool addConservativeSemiAffineBounds=false)
Flattens 'expr' into 'flattenedExpr', which contains the coefficients of the dimensions,...
LogicalResult getFlattenedAffineExprs(AffineMap map, std::vector< SmallVector< int64_t, 8 >> *flattenedExprs, FlatLinearConstraints *cst=nullptr, bool addConservativeSemiAffineBounds=false)
Flattens the result expressions of the map to their corresponding flattened forms and set in 'flatten...
AffineExpr simplifyAffineExpr(AffineExpr expr, unsigned numDims, unsigned numSymbols)
Simplify an affine expression by flattening and some amount of simple analysis.
AffineExpr getAffineDimExpr(unsigned position, MLIRContext *context)
These free functions allow clients of the API to not use classes in detail.
LogicalResult getMultiAffineFunctionFromMap(AffineMap map, presburger::MultiAffineFunction &multiAff)
AffineExpr getAffineSymbolExpr(unsigned position, MLIRContext *context)
Eliminates variable at the specified position using Fourier-Motzkin variable elimination.