1 //===- AffineStructures.cpp - MLIR Affine Structures Class-----------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Structures for affine/polyhedral analysis of affine dialect ops.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "mlir/Dialect/Affine/Analysis/AffineStructures.h"
14 #include "mlir/Analysis/Presburger/LinearTransform.h"
15 #include "mlir/Analysis/Presburger/Simplex.h"
16 #include "mlir/Analysis/Presburger/Utils.h"
17 #include "mlir/Dialect/Affine/IR/AffineOps.h"
18 #include "mlir/Dialect/Affine/IR/AffineValueMap.h"
19 #include "mlir/Dialect/Arithmetic/IR/Arithmetic.h"
20 #include "mlir/Dialect/StandardOps/IR/Ops.h"
21 #include "mlir/IR/AffineExprVisitor.h"
22 #include "mlir/IR/IntegerSet.h"
23 #include "mlir/Support/LLVM.h"
24 #include "mlir/Support/MathExtras.h"
25 #include "llvm/ADT/STLExtras.h"
26 #include "llvm/ADT/SmallPtrSet.h"
27 #include "llvm/ADT/SmallVector.h"
28 #include "llvm/Support/Debug.h"
29 #include "llvm/Support/raw_ostream.h"
30 
31 #define DEBUG_TYPE "affine-structures"
32 
33 using namespace mlir;
34 
35 namespace {
36 
37 // See comments for SimpleAffineExprFlattener.
38 // An AffineExprFlattener extends a SimpleAffineExprFlattener by recording
39 // constraint information associated with mod's, floordiv's, and ceildiv's
40 // in FlatAffineConstraints 'localVarCst'.
41 struct AffineExprFlattener : public SimpleAffineExprFlattener {
42 public:
43   // Constraints connecting newly introduced local variables (for mod's and
44   // div's) to existing (dimensional and symbolic) ones. These are always
45   // inequalities.
46   FlatAffineConstraints localVarCst;
47 
48   AffineExprFlattener(unsigned nDims, unsigned nSymbols)
49       : SimpleAffineExprFlattener(nDims, nSymbols) {
50     localVarCst.reset(nDims, nSymbols, /*numLocals=*/0);
51   }
52 
53 private:
54   // Add a local identifier (needed to flatten a mod, floordiv, ceildiv expr).
55   // The local identifier added is always a floordiv of a pure add/mul affine
56   // function of other identifiers, coefficients of which are specified in
57   // `dividend' and with respect to the positive constant `divisor'. localExpr
58   // is the simplified tree expression (AffineExpr) corresponding to the
59   // quantifier.
60   void addLocalFloorDivId(ArrayRef<int64_t> dividend, int64_t divisor,
61                           AffineExpr localExpr) override {
62     SimpleAffineExprFlattener::addLocalFloorDivId(dividend, divisor, localExpr);
63     // Update localVarCst.
64     localVarCst.addLocalFloorDiv(dividend, divisor);
65   }
66 };
67 
68 } // namespace
69 
70 // Flattens the expressions in map. Returns failure if 'expr' was unable to be
71 // flattened (i.e., semi-affine expressions not handled yet).
72 static LogicalResult
73 getFlattenedAffineExprs(ArrayRef<AffineExpr> exprs, unsigned numDims,
74                         unsigned numSymbols,
75                         std::vector<SmallVector<int64_t, 8>> *flattenedExprs,
76                         FlatAffineConstraints *localVarCst) {
77   if (exprs.empty()) {
78     localVarCst->reset(numDims, numSymbols);
79     return success();
80   }
81 
82   AffineExprFlattener flattener(numDims, numSymbols);
83   // Use the same flattener to simplify each expression successively. This way
84   // local identifiers / expressions are shared.
85   for (auto expr : exprs) {
86     if (!expr.isPureAffine())
87       return failure();
88 
89     flattener.walkPostOrder(expr);
90   }
91 
92   assert(flattener.operandExprStack.size() == exprs.size());
93   flattenedExprs->clear();
94   flattenedExprs->assign(flattener.operandExprStack.begin(),
95                          flattener.operandExprStack.end());
96 
97   if (localVarCst)
98     localVarCst->clearAndCopyFrom(flattener.localVarCst);
99 
100   return success();
101 }
102 
103 // Flattens 'expr' into 'flattenedExpr'. Returns failure if 'expr' was unable to
104 // be flattened (semi-affine expressions not handled yet).
105 LogicalResult
106 mlir::getFlattenedAffineExpr(AffineExpr expr, unsigned numDims,
107                              unsigned numSymbols,
108                              SmallVectorImpl<int64_t> *flattenedExpr,
109                              FlatAffineConstraints *localVarCst) {
110   std::vector<SmallVector<int64_t, 8>> flattenedExprs;
111   LogicalResult ret = ::getFlattenedAffineExprs({expr}, numDims, numSymbols,
112                                                 &flattenedExprs, localVarCst);
113   *flattenedExpr = flattenedExprs[0];
114   return ret;
115 }
116 
117 /// Flattens the expressions in map. Returns failure if 'expr' was unable to be
118 /// flattened (i.e., semi-affine expressions not handled yet).
119 LogicalResult mlir::getFlattenedAffineExprs(
120     AffineMap map, std::vector<SmallVector<int64_t, 8>> *flattenedExprs,
121     FlatAffineConstraints *localVarCst) {
122   if (map.getNumResults() == 0) {
123     localVarCst->reset(map.getNumDims(), map.getNumSymbols());
124     return success();
125   }
126   return ::getFlattenedAffineExprs(map.getResults(), map.getNumDims(),
127                                    map.getNumSymbols(), flattenedExprs,
128                                    localVarCst);
129 }
130 
131 LogicalResult mlir::getFlattenedAffineExprs(
132     IntegerSet set, std::vector<SmallVector<int64_t, 8>> *flattenedExprs,
133     FlatAffineConstraints *localVarCst) {
134   if (set.getNumConstraints() == 0) {
135     localVarCst->reset(set.getNumDims(), set.getNumSymbols());
136     return success();
137   }
138   return ::getFlattenedAffineExprs(set.getConstraints(), set.getNumDims(),
139                                    set.getNumSymbols(), flattenedExprs,
140                                    localVarCst);
141 }
142 
143 //===----------------------------------------------------------------------===//
144 // FlatAffineConstraints / FlatAffineValueConstraints.
145 //===----------------------------------------------------------------------===//
146 
147 // Clones this object.
148 std::unique_ptr<FlatAffineConstraints> FlatAffineConstraints::clone() const {
149   return std::make_unique<FlatAffineConstraints>(*this);
150 }
151 
152 std::unique_ptr<FlatAffineValueConstraints>
153 FlatAffineValueConstraints::clone() const {
154   return std::make_unique<FlatAffineValueConstraints>(*this);
155 }
156 
157 // Construct from an IntegerSet.
158 FlatAffineConstraints::FlatAffineConstraints(IntegerSet set)
159     : IntegerPolyhedron(set.getNumInequalities(), set.getNumEqualities(),
160                         set.getNumDims() + set.getNumSymbols() + 1,
161                         set.getNumDims(), set.getNumSymbols(),
162                         /*numLocals=*/0) {
163 
164   // Flatten expressions and add them to the constraint system.
165   std::vector<SmallVector<int64_t, 8>> flatExprs;
166   FlatAffineConstraints localVarCst;
167   if (failed(getFlattenedAffineExprs(set, &flatExprs, &localVarCst))) {
168     assert(false && "flattening unimplemented for semi-affine integer sets");
169     return;
170   }
171   assert(flatExprs.size() == set.getNumConstraints());
172   appendLocalId(/*num=*/localVarCst.getNumLocalIds());
173 
174   for (unsigned i = 0, e = flatExprs.size(); i < e; ++i) {
175     const auto &flatExpr = flatExprs[i];
176     assert(flatExpr.size() == getNumCols());
177     if (set.getEqFlags()[i]) {
178       addEquality(flatExpr);
179     } else {
180       addInequality(flatExpr);
181     }
182   }
183   // Add the other constraints involving local id's from flattening.
184   append(localVarCst);
185 }
186 
187 // Construct from an IntegerSet.
188 FlatAffineValueConstraints::FlatAffineValueConstraints(IntegerSet set)
189     : FlatAffineConstraints(set) {
190   values.resize(numIds, None);
191 }
192 
193 // Construct a hyperrectangular constraint set from ValueRanges that represent
194 // induction variables, lower and upper bounds. `ivs`, `lbs` and `ubs` are
195 // expected to match one to one. The order of variables and constraints is:
196 //
197 // ivs | lbs | ubs | eq/ineq
198 // ----+-----+-----+---------
199 //   1   -1     0      >= 0
200 // ----+-----+-----+---------
201 //  -1    0     1      >= 0
202 //
203 // All dimensions as set as DimId.
204 FlatAffineValueConstraints
205 FlatAffineValueConstraints::getHyperrectangular(ValueRange ivs, ValueRange lbs,
206                                                 ValueRange ubs) {
207   FlatAffineValueConstraints res;
208   unsigned nIvs = ivs.size();
209   assert(nIvs == lbs.size() && "expected as many lower bounds as ivs");
210   assert(nIvs == ubs.size() && "expected as many upper bounds as ivs");
211 
212   if (nIvs == 0)
213     return res;
214 
215   res.appendDimId(ivs);
216   unsigned lbsStart = res.appendDimId(lbs);
217   unsigned ubsStart = res.appendDimId(ubs);
218 
219   MLIRContext *ctx = ivs.front().getContext();
220   for (int ivIdx = 0, e = nIvs; ivIdx < e; ++ivIdx) {
221     // iv - lb >= 0
222     AffineMap lb = AffineMap::get(/*dimCount=*/3 * nIvs, /*symbolCount=*/0,
223                                   getAffineDimExpr(lbsStart + ivIdx, ctx));
224     if (failed(res.addBound(BoundType::LB, ivIdx, lb)))
225       llvm_unreachable("Unexpected FlatAffineValueConstraints creation error");
226     // -iv + ub >= 0
227     AffineMap ub = AffineMap::get(/*dimCount=*/3 * nIvs, /*symbolCount=*/0,
228                                   getAffineDimExpr(ubsStart + ivIdx, ctx));
229     if (failed(res.addBound(BoundType::UB, ivIdx, ub)))
230       llvm_unreachable("Unexpected FlatAffineValueConstraints creation error");
231   }
232   return res;
233 }
234 
235 void FlatAffineValueConstraints::reset(unsigned numReservedInequalities,
236                                        unsigned numReservedEqualities,
237                                        unsigned newNumReservedCols,
238                                        unsigned newNumDims,
239                                        unsigned newNumSymbols,
240                                        unsigned newNumLocals) {
241   reset(numReservedInequalities, numReservedEqualities, newNumReservedCols,
242         newNumDims, newNumSymbols, newNumLocals, /*valArgs=*/{});
243 }
244 
245 void FlatAffineValueConstraints::reset(
246     unsigned numReservedInequalities, unsigned numReservedEqualities,
247     unsigned newNumReservedCols, unsigned newNumDims, unsigned newNumSymbols,
248     unsigned newNumLocals, ArrayRef<Value> valArgs) {
249   assert(newNumReservedCols >= newNumDims + newNumSymbols + newNumLocals + 1 &&
250          "minimum 1 column");
251   SmallVector<Optional<Value>, 8> newVals;
252   if (!valArgs.empty())
253     newVals.assign(valArgs.begin(), valArgs.end());
254 
255   *this = FlatAffineValueConstraints(
256       numReservedInequalities, numReservedEqualities, newNumReservedCols,
257       newNumDims, newNumSymbols, newNumLocals, newVals);
258 }
259 
260 void FlatAffineValueConstraints::reset(unsigned newNumDims,
261                                        unsigned newNumSymbols,
262                                        unsigned newNumLocals,
263                                        ArrayRef<Value> valArgs) {
264   reset(0, 0, newNumDims + newNumSymbols + newNumLocals + 1, newNumDims,
265         newNumSymbols, newNumLocals, valArgs);
266 }
267 
268 unsigned FlatAffineValueConstraints::appendDimId(ValueRange vals) {
269   unsigned pos = getNumDimIds();
270   insertId(IdKind::Dimension, pos, vals);
271   return pos;
272 }
273 
274 unsigned FlatAffineValueConstraints::appendSymbolId(ValueRange vals) {
275   unsigned pos = getNumSymbolIds();
276   insertId(IdKind::Symbol, pos, vals);
277   return pos;
278 }
279 
280 unsigned FlatAffineValueConstraints::insertDimId(unsigned pos,
281                                                  ValueRange vals) {
282   return insertId(IdKind::Dimension, pos, vals);
283 }
284 
285 unsigned FlatAffineValueConstraints::insertSymbolId(unsigned pos,
286                                                     ValueRange vals) {
287   return insertId(IdKind::Symbol, pos, vals);
288 }
289 
290 unsigned FlatAffineValueConstraints::insertId(IdKind kind, unsigned pos,
291                                               unsigned num) {
292   unsigned absolutePos = FlatAffineConstraints::insertId(kind, pos, num);
293   values.insert(values.begin() + absolutePos, num, None);
294   assert(values.size() == getNumIds());
295   return absolutePos;
296 }
297 
298 unsigned FlatAffineValueConstraints::insertId(IdKind kind, unsigned pos,
299                                               ValueRange vals) {
300   assert(!vals.empty() && "expected ValueRange with Values");
301   unsigned num = vals.size();
302   unsigned absolutePos = FlatAffineConstraints::insertId(kind, pos, num);
303 
304   // If a Value is provided, insert it; otherwise use None.
305   for (unsigned i = 0; i < num; ++i)
306     values.insert(values.begin() + absolutePos + i,
307                   vals[i] ? Optional<Value>(vals[i]) : None);
308 
309   assert(values.size() == getNumIds());
310   return absolutePos;
311 }
312 
313 bool FlatAffineValueConstraints::hasValues() const {
314   return llvm::find_if(values, [](Optional<Value> id) {
315            return id.hasValue();
316          }) != values.end();
317 }
318 
319 /// Checks if two constraint systems are in the same space, i.e., if they are
320 /// associated with the same set of identifiers, appearing in the same order.
321 static bool areIdsAligned(const FlatAffineValueConstraints &a,
322                           const FlatAffineValueConstraints &b) {
323   return a.getNumDimIds() == b.getNumDimIds() &&
324          a.getNumSymbolIds() == b.getNumSymbolIds() &&
325          a.getNumIds() == b.getNumIds() &&
326          a.getMaybeValues().equals(b.getMaybeValues());
327 }
328 
329 /// Calls areIdsAligned to check if two constraint systems have the same set
330 /// of identifiers in the same order.
331 bool FlatAffineValueConstraints::areIdsAlignedWithOther(
332     const FlatAffineValueConstraints &other) {
333   return areIdsAligned(*this, other);
334 }
335 
336 /// Checks if the SSA values associated with `cst`'s identifiers in range
337 /// [start, end) are unique.
338 static bool LLVM_ATTRIBUTE_UNUSED areIdsUnique(
339     const FlatAffineValueConstraints &cst, unsigned start, unsigned end) {
340 
341   assert(start <= cst.getNumIds() && "Start position out of bounds");
342   assert(end <= cst.getNumIds() && "End position out of bounds");
343 
344   if (start >= end)
345     return true;
346 
347   SmallPtrSet<Value, 8> uniqueIds;
348   ArrayRef<Optional<Value>> maybeValues = cst.getMaybeValues();
349   for (Optional<Value> val : maybeValues) {
350     if (val.hasValue() && !uniqueIds.insert(val.getValue()).second)
351       return false;
352   }
353   return true;
354 }
355 
356 /// Checks if the SSA values associated with `cst`'s identifiers are unique.
357 static bool LLVM_ATTRIBUTE_UNUSED
358 areIdsUnique(const FlatAffineConstraints &cst) {
359   return areIdsUnique(cst, 0, cst.getNumIds());
360 }
361 
362 /// Checks if the SSA values associated with `cst`'s identifiers of kind `kind`
363 /// are unique.
364 static bool LLVM_ATTRIBUTE_UNUSED areIdsUnique(
365     const FlatAffineValueConstraints &cst, FlatAffineConstraints::IdKind kind) {
366 
367   if (kind == FlatAffineConstraints::IdKind::Dimension)
368     return areIdsUnique(cst, 0, cst.getNumDimIds());
369   if (kind == FlatAffineConstraints::IdKind::Symbol)
370     return areIdsUnique(cst, cst.getNumDimIds(), cst.getNumDimAndSymbolIds());
371   if (kind == FlatAffineConstraints::IdKind::Local)
372     return areIdsUnique(cst, cst.getNumDimAndSymbolIds(), cst.getNumIds());
373   llvm_unreachable("Unexpected IdKind");
374 }
375 
376 /// Merge and align the identifiers of A and B starting at 'offset', so that
377 /// both constraint systems get the union of the contained identifiers that is
378 /// dimension-wise and symbol-wise unique; both constraint systems are updated
379 /// so that they have the union of all identifiers, with A's original
380 /// identifiers appearing first followed by any of B's identifiers that didn't
381 /// appear in A. Local identifiers in B that have the same division
382 /// representation as local identifiers in A are merged into one.
383 //  E.g.: Input: A has ((%i, %j) [%M, %N]) and B has (%k, %j) [%P, %N, %M])
384 //        Output: both A, B have (%i, %j, %k) [%M, %N, %P]
385 static void mergeAndAlignIds(unsigned offset, FlatAffineValueConstraints *a,
386                              FlatAffineValueConstraints *b) {
387   assert(offset <= a->getNumDimIds() && offset <= b->getNumDimIds());
388   // A merge/align isn't meaningful if a cst's ids aren't distinct.
389   assert(areIdsUnique(*a) && "A's values aren't unique");
390   assert(areIdsUnique(*b) && "B's values aren't unique");
391 
392   assert(std::all_of(a->getMaybeValues().begin() + offset,
393                      a->getMaybeValues().begin() + a->getNumDimAndSymbolIds(),
394                      [](Optional<Value> id) { return id.hasValue(); }));
395 
396   assert(std::all_of(b->getMaybeValues().begin() + offset,
397                      b->getMaybeValues().begin() + b->getNumDimAndSymbolIds(),
398                      [](Optional<Value> id) { return id.hasValue(); }));
399 
400   SmallVector<Value, 4> aDimValues;
401   a->getValues(offset, a->getNumDimIds(), &aDimValues);
402 
403   {
404     // Merge dims from A into B.
405     unsigned d = offset;
406     for (auto aDimValue : aDimValues) {
407       unsigned loc;
408       if (b->findId(aDimValue, &loc)) {
409         assert(loc >= offset && "A's dim appears in B's aligned range");
410         assert(loc < b->getNumDimIds() &&
411                "A's dim appears in B's non-dim position");
412         b->swapId(d, loc);
413       } else {
414         b->insertDimId(d, aDimValue);
415       }
416       d++;
417     }
418     // Dimensions that are in B, but not in A, are added at the end.
419     for (unsigned t = a->getNumDimIds(), e = b->getNumDimIds(); t < e; t++) {
420       a->appendDimId(b->getValue(t));
421     }
422     assert(a->getNumDimIds() == b->getNumDimIds() &&
423            "expected same number of dims");
424   }
425 
426   // Merge and align symbols of A and B
427   a->mergeSymbolIds(*b);
428   // Merge and align local ids of A and B
429   a->mergeLocalIds(*b);
430 
431   assert(areIdsAligned(*a, *b) && "IDs expected to be aligned");
432 }
433 
434 // Call 'mergeAndAlignIds' to align constraint systems of 'this' and 'other'.
435 void FlatAffineValueConstraints::mergeAndAlignIdsWithOther(
436     unsigned offset, FlatAffineValueConstraints *other) {
437   mergeAndAlignIds(offset, this, other);
438 }
439 
440 LogicalResult
441 FlatAffineValueConstraints::composeMap(const AffineValueMap *vMap) {
442   return composeMatchingMap(
443       computeAlignedMap(vMap->getAffineMap(), vMap->getOperands()));
444 }
445 
446 // Similar to `composeMap` except that no Values need be associated with the
447 // constraint system nor are they looked at -- the dimensions and symbols of
448 // `other` are expected to correspond 1:1 to `this` system.
449 LogicalResult FlatAffineConstraints::composeMatchingMap(AffineMap other) {
450   assert(other.getNumDims() == getNumDimIds() && "dim mismatch");
451   assert(other.getNumSymbols() == getNumSymbolIds() && "symbol mismatch");
452 
453   std::vector<SmallVector<int64_t, 8>> flatExprs;
454   if (failed(flattenAlignedMapAndMergeLocals(other, &flatExprs)))
455     return failure();
456   assert(flatExprs.size() == other.getNumResults());
457 
458   // Add dimensions corresponding to the map's results.
459   insertDimId(/*pos=*/0, /*num=*/other.getNumResults());
460 
461   // We add one equality for each result connecting the result dim of the map to
462   // the other identifiers.
463   // E.g.: if the expression is 16*i0 + i1, and this is the r^th
464   // iteration/result of the value map, we are adding the equality:
465   // d_r - 16*i0 - i1 = 0. Similarly, when flattening (i0 + 1, i0 + 8*i2), we
466   // add two equalities: d_0 - i0 - 1 == 0, d1 - i0 - 8*i2 == 0.
467   for (unsigned r = 0, e = flatExprs.size(); r < e; r++) {
468     const auto &flatExpr = flatExprs[r];
469     assert(flatExpr.size() >= other.getNumInputs() + 1);
470 
471     SmallVector<int64_t, 8> eqToAdd(getNumCols(), 0);
472     // Set the coefficient for this result to one.
473     eqToAdd[r] = 1;
474 
475     // Dims and symbols.
476     for (unsigned i = 0, f = other.getNumInputs(); i < f; i++) {
477       // Negate `eq[r]` since the newly added dimension will be set to this one.
478       eqToAdd[e + i] = -flatExpr[i];
479     }
480     // Local columns of `eq` are at the beginning.
481     unsigned j = getNumDimIds() + getNumSymbolIds();
482     unsigned end = flatExpr.size() - 1;
483     for (unsigned i = other.getNumInputs(); i < end; i++, j++) {
484       eqToAdd[j] = -flatExpr[i];
485     }
486 
487     // Constant term.
488     eqToAdd[getNumCols() - 1] = -flatExpr[flatExpr.size() - 1];
489 
490     // Add the equality connecting the result of the map to this constraint set.
491     addEquality(eqToAdd);
492   }
493 
494   return success();
495 }
496 
497 // Turn a symbol into a dimension.
498 static void turnSymbolIntoDim(FlatAffineValueConstraints *cst, Value id) {
499   unsigned pos;
500   if (cst->findId(id, &pos) && pos >= cst->getNumDimIds() &&
501       pos < cst->getNumDimAndSymbolIds()) {
502     cst->swapId(pos, cst->getNumDimIds());
503     cst->setDimSymbolSeparation(cst->getNumSymbolIds() - 1);
504   }
505 }
506 
507 /// Merge and align symbols of `this` and `other` such that both get union of
508 /// of symbols that are unique. Symbols in `this` and `other` should be
509 /// unique. Symbols with Value as `None` are considered to be inequal to all
510 /// other symbols.
511 void FlatAffineValueConstraints::mergeSymbolIds(
512     FlatAffineValueConstraints &other) {
513 
514   assert(areIdsUnique(*this, IdKind::Symbol) && "Symbol ids are not unique");
515   assert(areIdsUnique(other, IdKind::Symbol) && "Symbol ids are not unique");
516 
517   SmallVector<Value, 4> aSymValues;
518   getValues(getNumDimIds(), getNumDimAndSymbolIds(), &aSymValues);
519 
520   // Merge symbols: merge symbols into `other` first from `this`.
521   unsigned s = other.getNumDimIds();
522   for (Value aSymValue : aSymValues) {
523     unsigned loc;
524     // If the id is a symbol in `other`, then align it, otherwise assume that
525     // it is a new symbol
526     if (other.findId(aSymValue, &loc) && loc >= other.getNumDimIds() &&
527         loc < other.getNumDimAndSymbolIds())
528       other.swapId(s, loc);
529     else
530       other.insertSymbolId(s - other.getNumDimIds(), aSymValue);
531     s++;
532   }
533 
534   // Symbols that are in other, but not in this, are added at the end.
535   for (unsigned t = other.getNumDimIds() + getNumSymbolIds(),
536                 e = other.getNumDimAndSymbolIds();
537        t < e; t++)
538     insertSymbolId(getNumSymbolIds(), other.getValue(t));
539 
540   assert(getNumSymbolIds() == other.getNumSymbolIds() &&
541          "expected same number of symbols");
542   assert(areIdsUnique(*this, IdKind::Symbol) && "Symbol ids are not unique");
543   assert(areIdsUnique(other, IdKind::Symbol) && "Symbol ids are not unique");
544 }
545 
546 // Changes all symbol identifiers which are loop IVs to dim identifiers.
547 void FlatAffineValueConstraints::convertLoopIVSymbolsToDims() {
548   // Gather all symbols which are loop IVs.
549   SmallVector<Value, 4> loopIVs;
550   for (unsigned i = getNumDimIds(), e = getNumDimAndSymbolIds(); i < e; i++) {
551     if (hasValue(i) && getForInductionVarOwner(getValue(i)))
552       loopIVs.push_back(getValue(i));
553   }
554   // Turn each symbol in 'loopIVs' into a dim identifier.
555   for (auto iv : loopIVs) {
556     turnSymbolIntoDim(this, iv);
557   }
558 }
559 
560 void FlatAffineValueConstraints::addInductionVarOrTerminalSymbol(Value val) {
561   if (containsId(val))
562     return;
563 
564   // Caller is expected to fully compose map/operands if necessary.
565   assert((isTopLevelValue(val) || isForInductionVar(val)) &&
566          "non-terminal symbol / loop IV expected");
567   // Outer loop IVs could be used in forOp's bounds.
568   if (auto loop = getForInductionVarOwner(val)) {
569     appendDimId(val);
570     if (failed(this->addAffineForOpDomain(loop)))
571       LLVM_DEBUG(
572           loop.emitWarning("failed to add domain info to constraint system"));
573     return;
574   }
575   // Add top level symbol.
576   appendSymbolId(val);
577   // Check if the symbol is a constant.
578   if (auto constOp = val.getDefiningOp<arith::ConstantIndexOp>())
579     addBound(BoundType::EQ, val, constOp.value());
580 }
581 
582 LogicalResult
583 FlatAffineValueConstraints::addAffineForOpDomain(AffineForOp forOp) {
584   unsigned pos;
585   // Pre-condition for this method.
586   if (!findId(forOp.getInductionVar(), &pos)) {
587     assert(false && "Value not found");
588     return failure();
589   }
590 
591   int64_t step = forOp.getStep();
592   if (step != 1) {
593     if (!forOp.hasConstantLowerBound())
594       LLVM_DEBUG(forOp.emitWarning("domain conservatively approximated"));
595     else {
596       // Add constraints for the stride.
597       // (iv - lb) % step = 0 can be written as:
598       // (iv - lb) - step * q = 0 where q = (iv - lb) / step.
599       // Add local variable 'q' and add the above equality.
600       // The first constraint is q = (iv - lb) floordiv step
601       SmallVector<int64_t, 8> dividend(getNumCols(), 0);
602       int64_t lb = forOp.getConstantLowerBound();
603       dividend[pos] = 1;
604       dividend.back() -= lb;
605       addLocalFloorDiv(dividend, step);
606       // Second constraint: (iv - lb) - step * q = 0.
607       SmallVector<int64_t, 8> eq(getNumCols(), 0);
608       eq[pos] = 1;
609       eq.back() -= lb;
610       // For the local var just added above.
611       eq[getNumCols() - 2] = -step;
612       addEquality(eq);
613     }
614   }
615 
616   if (forOp.hasConstantLowerBound()) {
617     addBound(BoundType::LB, pos, forOp.getConstantLowerBound());
618   } else {
619     // Non-constant lower bound case.
620     if (failed(addBound(BoundType::LB, pos, forOp.getLowerBoundMap(),
621                         forOp.getLowerBoundOperands())))
622       return failure();
623   }
624 
625   if (forOp.hasConstantUpperBound()) {
626     addBound(BoundType::UB, pos, forOp.getConstantUpperBound() - 1);
627     return success();
628   }
629   // Non-constant upper bound case.
630   return addBound(BoundType::UB, pos, forOp.getUpperBoundMap(),
631                   forOp.getUpperBoundOperands());
632 }
633 
634 LogicalResult
635 FlatAffineValueConstraints::addDomainFromSliceMaps(ArrayRef<AffineMap> lbMaps,
636                                                    ArrayRef<AffineMap> ubMaps,
637                                                    ArrayRef<Value> operands) {
638   assert(lbMaps.size() == ubMaps.size());
639   assert(lbMaps.size() <= getNumDimIds());
640 
641   for (unsigned i = 0, e = lbMaps.size(); i < e; ++i) {
642     AffineMap lbMap = lbMaps[i];
643     AffineMap ubMap = ubMaps[i];
644     assert(!lbMap || lbMap.getNumInputs() == operands.size());
645     assert(!ubMap || ubMap.getNumInputs() == operands.size());
646 
647     // Check if this slice is just an equality along this dimension. If so,
648     // retrieve the existing loop it equates to and add it to the system.
649     if (lbMap && ubMap && lbMap.getNumResults() == 1 &&
650         ubMap.getNumResults() == 1 &&
651         lbMap.getResult(0) + 1 == ubMap.getResult(0) &&
652         // The condition above will be true for maps describing a single
653         // iteration (e.g., lbMap.getResult(0) = 0, ubMap.getResult(0) = 1).
654         // Make sure we skip those cases by checking that the lb result is not
655         // just a constant.
656         !lbMap.getResult(0).isa<AffineConstantExpr>()) {
657       // Limited support: we expect the lb result to be just a loop dimension.
658       // Not supported otherwise for now.
659       AffineDimExpr result = lbMap.getResult(0).dyn_cast<AffineDimExpr>();
660       if (!result)
661         return failure();
662 
663       AffineForOp loop =
664           getForInductionVarOwner(operands[result.getPosition()]);
665       if (!loop)
666         return failure();
667 
668       if (failed(addAffineForOpDomain(loop)))
669         return failure();
670       continue;
671     }
672 
673     // This slice refers to a loop that doesn't exist in the IR yet. Add its
674     // bounds to the system assuming its dimension identifier position is the
675     // same as the position of the loop in the loop nest.
676     if (lbMap && failed(addBound(BoundType::LB, i, lbMap, operands)))
677       return failure();
678     if (ubMap && failed(addBound(BoundType::UB, i, ubMap, operands)))
679       return failure();
680   }
681   return success();
682 }
683 
684 void FlatAffineValueConstraints::addAffineIfOpDomain(AffineIfOp ifOp) {
685   // Create the base constraints from the integer set attached to ifOp.
686   FlatAffineValueConstraints cst(ifOp.getIntegerSet());
687 
688   // Bind ids in the constraints to ifOp operands.
689   SmallVector<Value, 4> operands = ifOp.getOperands();
690   cst.setValues(0, cst.getNumDimAndSymbolIds(), operands);
691 
692   // Merge the constraints from ifOp to the current domain. We need first merge
693   // and align the IDs from both constraints, and then append the constraints
694   // from the ifOp into the current one.
695   mergeAndAlignIdsWithOther(0, &cst);
696   append(cst);
697 }
698 
699 bool FlatAffineValueConstraints::hasConsistentState() const {
700   return FlatAffineConstraints::hasConsistentState() &&
701          values.size() == getNumIds();
702 }
703 
704 void FlatAffineValueConstraints::removeIdRange(unsigned idStart,
705                                                unsigned idLimit) {
706   FlatAffineConstraints::removeIdRange(idStart, idLimit);
707   values.erase(values.begin() + idStart, values.begin() + idLimit);
708 }
709 
710 // Determine whether the identifier at 'pos' (say id_r) can be expressed as
711 // modulo of another known identifier (say id_n) w.r.t a constant. For example,
712 // if the following constraints hold true:
713 // ```
714 // 0 <= id_r <= divisor - 1
715 // id_n - (divisor * q_expr) = id_r
716 // ```
717 // where `id_n` is a known identifier (called dividend), and `q_expr` is an
718 // `AffineExpr` (called the quotient expression), `id_r` can be written as:
719 //
720 // `id_r = id_n mod divisor`.
721 //
722 // Additionally, in a special case of the above constaints where `q_expr` is an
723 // identifier itself that is not yet known (say `id_q`), it can be written as a
724 // floordiv in the following way:
725 //
726 // `id_q = id_n floordiv divisor`.
727 //
728 // Returns true if the above mod or floordiv are detected, updating 'memo' with
729 // these new expressions. Returns false otherwise.
730 static bool detectAsMod(const FlatAffineConstraints &cst, unsigned pos,
731                         int64_t lbConst, int64_t ubConst,
732                         SmallVectorImpl<AffineExpr> &memo,
733                         MLIRContext *context) {
734   assert(pos < cst.getNumIds() && "invalid position");
735 
736   // Check if a divisor satisfying the condition `0 <= id_r <= divisor - 1` can
737   // be determined.
738   if (lbConst != 0 || ubConst < 1)
739     return false;
740   int64_t divisor = ubConst + 1;
741 
742   // Check for the aforementioned conditions in each equality.
743   for (unsigned curEquality = 0, numEqualities = cst.getNumEqualities();
744        curEquality < numEqualities; curEquality++) {
745     int64_t coefficientAtPos = cst.atEq(curEquality, pos);
746     // If current equality does not involve `id_r`, continue to the next
747     // equality.
748     if (coefficientAtPos == 0)
749       continue;
750 
751     // Constant term should be 0 in this equality.
752     if (cst.atEq(curEquality, cst.getNumCols() - 1) != 0)
753       continue;
754 
755     // Traverse through the equality and construct the dividend expression
756     // `dividendExpr`, to contain all the identifiers which are known and are
757     // not divisible by `(coefficientAtPos * divisor)`. Hope here is that the
758     // `dividendExpr` gets simplified into a single identifier `id_n` discussed
759     // above.
760     auto dividendExpr = getAffineConstantExpr(0, context);
761 
762     // Track the terms that go into quotient expression, later used to detect
763     // additional floordiv.
764     unsigned quotientCount = 0;
765     int quotientPosition = -1;
766     int quotientSign = 1;
767 
768     // Consider each term in the current equality.
769     unsigned curId, e;
770     for (curId = 0, e = cst.getNumDimAndSymbolIds(); curId < e; ++curId) {
771       // Ignore id_r.
772       if (curId == pos)
773         continue;
774       int64_t coefficientOfCurId = cst.atEq(curEquality, curId);
775       // Ignore ids that do not contribute to the current equality.
776       if (coefficientOfCurId == 0)
777         continue;
778       // Check if the current id goes into the quotient expression.
779       if (coefficientOfCurId % (divisor * coefficientAtPos) == 0) {
780         quotientCount++;
781         quotientPosition = curId;
782         quotientSign = (coefficientOfCurId * coefficientAtPos) > 0 ? 1 : -1;
783         continue;
784       }
785       // Identifiers that are part of dividendExpr should be known.
786       if (!memo[curId])
787         break;
788       // Append the current identifier to the dividend expression.
789       dividendExpr = dividendExpr + memo[curId] * coefficientOfCurId;
790     }
791 
792     // Can't construct expression as it depends on a yet uncomputed id.
793     if (curId < e)
794       continue;
795 
796     // Express `id_r` in terms of the other ids collected so far.
797     if (coefficientAtPos > 0)
798       dividendExpr = (-dividendExpr).floorDiv(coefficientAtPos);
799     else
800       dividendExpr = dividendExpr.floorDiv(-coefficientAtPos);
801 
802     // Simplify the expression.
803     dividendExpr = simplifyAffineExpr(dividendExpr, cst.getNumDimIds(),
804                                       cst.getNumSymbolIds());
805     // Only if the final dividend expression is just a single id (which we call
806     // `id_n`), we can proceed.
807     // TODO: Handle AffineSymbolExpr as well. There is no reason to restrict it
808     // to dims themselves.
809     auto dimExpr = dividendExpr.dyn_cast<AffineDimExpr>();
810     if (!dimExpr)
811       continue;
812 
813     // Express `id_r` as `id_n % divisor` and store the expression in `memo`.
814     if (quotientCount >= 1) {
815       auto ub = cst.getConstantBound(FlatAffineConstraints::BoundType::UB,
816                                      dimExpr.getPosition());
817       // If `id_n` has an upperbound that is less than the divisor, mod can be
818       // eliminated altogether.
819       if (ub.hasValue() && ub.getValue() < divisor)
820         memo[pos] = dimExpr;
821       else
822         memo[pos] = dimExpr % divisor;
823       // If a unique quotient `id_q` was seen, it can be expressed as
824       // `id_n floordiv divisor`.
825       if (quotientCount == 1 && !memo[quotientPosition])
826         memo[quotientPosition] = dimExpr.floorDiv(divisor) * quotientSign;
827 
828       return true;
829     }
830   }
831   return false;
832 }
833 
834 /// Check if the pos^th identifier can be expressed as a floordiv of an affine
835 /// function of other identifiers (where the divisor is a positive constant)
836 /// given the initial set of expressions in `exprs`. If it can be, the
837 /// corresponding position in `exprs` is set as the detected affine expr. For
838 /// eg: 4q <= i + j <= 4q + 3   <=>   q = (i + j) floordiv 4. An equality can
839 /// also yield a floordiv: eg.  4q = i + j <=> q = (i + j) floordiv 4. 32q + 28
840 /// <= i <= 32q + 31 => q = i floordiv 32.
841 static bool detectAsFloorDiv(const FlatAffineConstraints &cst, unsigned pos,
842                              MLIRContext *context,
843                              SmallVectorImpl<AffineExpr> &exprs) {
844   assert(pos < cst.getNumIds() && "invalid position");
845 
846   // Get upper-lower bound pair for this variable.
847   SmallVector<bool, 8> foundRepr(cst.getNumIds(), false);
848   for (unsigned i = 0, e = cst.getNumIds(); i < e; ++i)
849     if (exprs[i])
850       foundRepr[i] = true;
851 
852   SmallVector<int64_t, 8> dividend;
853   unsigned divisor;
854   auto ulPair = presburger_utils::computeSingleVarRepr(cst, foundRepr, pos,
855                                                        dividend, divisor);
856 
857   // No upper-lower bound pair found for this var.
858   if (!ulPair)
859     return false;
860 
861   // Construct the dividend expression.
862   auto dividendExpr = getAffineConstantExpr(dividend.back(), context);
863   for (unsigned c = 0, f = cst.getNumIds(); c < f; c++)
864     if (dividend[c] != 0)
865       dividendExpr = dividendExpr + dividend[c] * exprs[c];
866 
867   // Successfully detected the floordiv.
868   exprs[pos] = dividendExpr.floorDiv(divisor);
869   return true;
870 }
871 
872 std::pair<AffineMap, AffineMap> FlatAffineConstraints::getLowerAndUpperBound(
873     unsigned pos, unsigned offset, unsigned num, unsigned symStartPos,
874     ArrayRef<AffineExpr> localExprs, MLIRContext *context) const {
875   assert(pos + offset < getNumDimIds() && "invalid dim start pos");
876   assert(symStartPos >= (pos + offset) && "invalid sym start pos");
877   assert(getNumLocalIds() == localExprs.size() &&
878          "incorrect local exprs count");
879 
880   SmallVector<unsigned, 4> lbIndices, ubIndices, eqIndices;
881   getLowerAndUpperBoundIndices(pos + offset, &lbIndices, &ubIndices, &eqIndices,
882                                offset, num);
883 
884   /// Add to 'b' from 'a' in set [0, offset) U [offset + num, symbStartPos).
885   auto addCoeffs = [&](ArrayRef<int64_t> a, SmallVectorImpl<int64_t> &b) {
886     b.clear();
887     for (unsigned i = 0, e = a.size(); i < e; ++i) {
888       if (i < offset || i >= offset + num)
889         b.push_back(a[i]);
890     }
891   };
892 
893   SmallVector<int64_t, 8> lb, ub;
894   SmallVector<AffineExpr, 4> lbExprs;
895   unsigned dimCount = symStartPos - num;
896   unsigned symCount = getNumDimAndSymbolIds() - symStartPos;
897   lbExprs.reserve(lbIndices.size() + eqIndices.size());
898   // Lower bound expressions.
899   for (auto idx : lbIndices) {
900     auto ineq = getInequality(idx);
901     // Extract the lower bound (in terms of other coeff's + const), i.e., if
902     // i - j + 1 >= 0 is the constraint, 'pos' is for i the lower bound is j
903     // - 1.
904     addCoeffs(ineq, lb);
905     std::transform(lb.begin(), lb.end(), lb.begin(), std::negate<int64_t>());
906     auto expr =
907         getAffineExprFromFlatForm(lb, dimCount, symCount, localExprs, context);
908     // expr ceildiv divisor is (expr + divisor - 1) floordiv divisor
909     int64_t divisor = std::abs(ineq[pos + offset]);
910     expr = (expr + divisor - 1).floorDiv(divisor);
911     lbExprs.push_back(expr);
912   }
913 
914   SmallVector<AffineExpr, 4> ubExprs;
915   ubExprs.reserve(ubIndices.size() + eqIndices.size());
916   // Upper bound expressions.
917   for (auto idx : ubIndices) {
918     auto ineq = getInequality(idx);
919     // Extract the upper bound (in terms of other coeff's + const).
920     addCoeffs(ineq, ub);
921     auto expr =
922         getAffineExprFromFlatForm(ub, dimCount, symCount, localExprs, context);
923     expr = expr.floorDiv(std::abs(ineq[pos + offset]));
924     // Upper bound is exclusive.
925     ubExprs.push_back(expr + 1);
926   }
927 
928   // Equalities. It's both a lower and a upper bound.
929   SmallVector<int64_t, 4> b;
930   for (auto idx : eqIndices) {
931     auto eq = getEquality(idx);
932     addCoeffs(eq, b);
933     if (eq[pos + offset] > 0)
934       std::transform(b.begin(), b.end(), b.begin(), std::negate<int64_t>());
935 
936     // Extract the upper bound (in terms of other coeff's + const).
937     auto expr =
938         getAffineExprFromFlatForm(b, dimCount, symCount, localExprs, context);
939     expr = expr.floorDiv(std::abs(eq[pos + offset]));
940     // Upper bound is exclusive.
941     ubExprs.push_back(expr + 1);
942     // Lower bound.
943     expr =
944         getAffineExprFromFlatForm(b, dimCount, symCount, localExprs, context);
945     expr = expr.ceilDiv(std::abs(eq[pos + offset]));
946     lbExprs.push_back(expr);
947   }
948 
949   auto lbMap = AffineMap::get(dimCount, symCount, lbExprs, context);
950   auto ubMap = AffineMap::get(dimCount, symCount, ubExprs, context);
951 
952   return {lbMap, ubMap};
953 }
954 
955 /// Computes the lower and upper bounds of the first 'num' dimensional
956 /// identifiers (starting at 'offset') as affine maps of the remaining
957 /// identifiers (dimensional and symbolic identifiers). Local identifiers are
958 /// themselves explicitly computed as affine functions of other identifiers in
959 /// this process if needed.
960 void FlatAffineConstraints::getSliceBounds(unsigned offset, unsigned num,
961                                            MLIRContext *context,
962                                            SmallVectorImpl<AffineMap> *lbMaps,
963                                            SmallVectorImpl<AffineMap> *ubMaps) {
964   assert(num < getNumDimIds() && "invalid range");
965 
966   // Basic simplification.
967   normalizeConstraintsByGCD();
968 
969   LLVM_DEBUG(llvm::dbgs() << "getSliceBounds for first " << num
970                           << " identifiers\n");
971   LLVM_DEBUG(dump());
972 
973   // Record computed/detected identifiers.
974   SmallVector<AffineExpr, 8> memo(getNumIds());
975   // Initialize dimensional and symbolic identifiers.
976   for (unsigned i = 0, e = getNumDimIds(); i < e; i++) {
977     if (i < offset)
978       memo[i] = getAffineDimExpr(i, context);
979     else if (i >= offset + num)
980       memo[i] = getAffineDimExpr(i - num, context);
981   }
982   for (unsigned i = getNumDimIds(), e = getNumDimAndSymbolIds(); i < e; i++)
983     memo[i] = getAffineSymbolExpr(i - getNumDimIds(), context);
984 
985   bool changed;
986   do {
987     changed = false;
988     // Identify yet unknown identifiers as constants or mod's / floordiv's of
989     // other identifiers if possible.
990     for (unsigned pos = 0; pos < getNumIds(); pos++) {
991       if (memo[pos])
992         continue;
993 
994       auto lbConst = getConstantBound(BoundType::LB, pos);
995       auto ubConst = getConstantBound(BoundType::UB, pos);
996       if (lbConst.hasValue() && ubConst.hasValue()) {
997         // Detect equality to a constant.
998         if (lbConst.getValue() == ubConst.getValue()) {
999           memo[pos] = getAffineConstantExpr(lbConst.getValue(), context);
1000           changed = true;
1001           continue;
1002         }
1003 
1004         // Detect an identifier as modulo of another identifier w.r.t a
1005         // constant.
1006         if (detectAsMod(*this, pos, lbConst.getValue(), ubConst.getValue(),
1007                         memo, context)) {
1008           changed = true;
1009           continue;
1010         }
1011       }
1012 
1013       // Detect an identifier as a floordiv of an affine function of other
1014       // identifiers (divisor is a positive constant).
1015       if (detectAsFloorDiv(*this, pos, context, memo)) {
1016         changed = true;
1017         continue;
1018       }
1019 
1020       // Detect an identifier as an expression of other identifiers.
1021       unsigned idx;
1022       if (!findConstraintWithNonZeroAt(pos, /*isEq=*/true, &idx)) {
1023         continue;
1024       }
1025 
1026       // Build AffineExpr solving for identifier 'pos' in terms of all others.
1027       auto expr = getAffineConstantExpr(0, context);
1028       unsigned j, e;
1029       for (j = 0, e = getNumIds(); j < e; ++j) {
1030         if (j == pos)
1031           continue;
1032         int64_t c = atEq(idx, j);
1033         if (c == 0)
1034           continue;
1035         // If any of the involved IDs hasn't been found yet, we can't proceed.
1036         if (!memo[j])
1037           break;
1038         expr = expr + memo[j] * c;
1039       }
1040       if (j < e)
1041         // Can't construct expression as it depends on a yet uncomputed
1042         // identifier.
1043         continue;
1044 
1045       // Add constant term to AffineExpr.
1046       expr = expr + atEq(idx, getNumIds());
1047       int64_t vPos = atEq(idx, pos);
1048       assert(vPos != 0 && "expected non-zero here");
1049       if (vPos > 0)
1050         expr = (-expr).floorDiv(vPos);
1051       else
1052         // vPos < 0.
1053         expr = expr.floorDiv(-vPos);
1054       // Successfully constructed expression.
1055       memo[pos] = expr;
1056       changed = true;
1057     }
1058     // This loop is guaranteed to reach a fixed point - since once an
1059     // identifier's explicit form is computed (in memo[pos]), it's not updated
1060     // again.
1061   } while (changed);
1062 
1063   // Set the lower and upper bound maps for all the identifiers that were
1064   // computed as affine expressions of the rest as the "detected expr" and
1065   // "detected expr + 1" respectively; set the undetected ones to null.
1066   Optional<FlatAffineConstraints> tmpClone;
1067   for (unsigned pos = 0; pos < num; pos++) {
1068     unsigned numMapDims = getNumDimIds() - num;
1069     unsigned numMapSymbols = getNumSymbolIds();
1070     AffineExpr expr = memo[pos + offset];
1071     if (expr)
1072       expr = simplifyAffineExpr(expr, numMapDims, numMapSymbols);
1073 
1074     AffineMap &lbMap = (*lbMaps)[pos];
1075     AffineMap &ubMap = (*ubMaps)[pos];
1076 
1077     if (expr) {
1078       lbMap = AffineMap::get(numMapDims, numMapSymbols, expr);
1079       ubMap = AffineMap::get(numMapDims, numMapSymbols, expr + 1);
1080     } else {
1081       // TODO: Whenever there are local identifiers in the dependence
1082       // constraints, we'll conservatively over-approximate, since we don't
1083       // always explicitly compute them above (in the while loop).
1084       if (getNumLocalIds() == 0) {
1085         // Work on a copy so that we don't update this constraint system.
1086         if (!tmpClone) {
1087           tmpClone.emplace(FlatAffineConstraints(*this));
1088           // Removing redundant inequalities is necessary so that we don't get
1089           // redundant loop bounds.
1090           tmpClone->removeRedundantInequalities();
1091         }
1092         std::tie(lbMap, ubMap) = tmpClone->getLowerAndUpperBound(
1093             pos, offset, num, getNumDimIds(), /*localExprs=*/{}, context);
1094       }
1095 
1096       // If the above fails, we'll just use the constant lower bound and the
1097       // constant upper bound (if they exist) as the slice bounds.
1098       // TODO: being conservative for the moment in cases that
1099       // lead to multiple bounds - until getConstDifference in LoopFusion.cpp is
1100       // fixed (b/126426796).
1101       if (!lbMap || lbMap.getNumResults() > 1) {
1102         LLVM_DEBUG(llvm::dbgs()
1103                    << "WARNING: Potentially over-approximating slice lb\n");
1104         auto lbConst = getConstantBound(BoundType::LB, pos + offset);
1105         if (lbConst.hasValue()) {
1106           lbMap = AffineMap::get(
1107               numMapDims, numMapSymbols,
1108               getAffineConstantExpr(lbConst.getValue(), context));
1109         }
1110       }
1111       if (!ubMap || ubMap.getNumResults() > 1) {
1112         LLVM_DEBUG(llvm::dbgs()
1113                    << "WARNING: Potentially over-approximating slice ub\n");
1114         auto ubConst = getConstantBound(BoundType::UB, pos + offset);
1115         if (ubConst.hasValue()) {
1116           (ubMap) = AffineMap::get(
1117               numMapDims, numMapSymbols,
1118               getAffineConstantExpr(ubConst.getValue() + 1, context));
1119         }
1120       }
1121     }
1122     LLVM_DEBUG(llvm::dbgs()
1123                << "lb map for pos = " << Twine(pos + offset) << ", expr: ");
1124     LLVM_DEBUG(lbMap.dump(););
1125     LLVM_DEBUG(llvm::dbgs()
1126                << "ub map for pos = " << Twine(pos + offset) << ", expr: ");
1127     LLVM_DEBUG(ubMap.dump(););
1128   }
1129 }
1130 
1131 LogicalResult FlatAffineConstraints::flattenAlignedMapAndMergeLocals(
1132     AffineMap map, std::vector<SmallVector<int64_t, 8>> *flattenedExprs) {
1133   FlatAffineConstraints localCst;
1134   if (failed(getFlattenedAffineExprs(map, flattenedExprs, &localCst))) {
1135     LLVM_DEBUG(llvm::dbgs()
1136                << "composition unimplemented for semi-affine maps\n");
1137     return failure();
1138   }
1139 
1140   // Add localCst information.
1141   if (localCst.getNumLocalIds() > 0) {
1142     unsigned numLocalIds = getNumLocalIds();
1143     // Insert local dims of localCst at the beginning.
1144     insertLocalId(/*pos=*/0, /*num=*/localCst.getNumLocalIds());
1145     // Insert local dims of `this` at the end of localCst.
1146     localCst.appendLocalId(/*num=*/numLocalIds);
1147     // Dimensions of localCst and this constraint set match. Append localCst to
1148     // this constraint set.
1149     append(localCst);
1150   }
1151 
1152   return success();
1153 }
1154 
1155 LogicalResult FlatAffineConstraints::addBound(BoundType type, unsigned pos,
1156                                               AffineMap boundMap) {
1157   assert(boundMap.getNumDims() == getNumDimIds() && "dim mismatch");
1158   assert(boundMap.getNumSymbols() == getNumSymbolIds() && "symbol mismatch");
1159   assert(pos < getNumDimAndSymbolIds() && "invalid position");
1160 
1161   // Equality follows the logic of lower bound except that we add an equality
1162   // instead of an inequality.
1163   assert((type != BoundType::EQ || boundMap.getNumResults() == 1) &&
1164          "single result expected");
1165   bool lower = type == BoundType::LB || type == BoundType::EQ;
1166 
1167   std::vector<SmallVector<int64_t, 8>> flatExprs;
1168   if (failed(flattenAlignedMapAndMergeLocals(boundMap, &flatExprs)))
1169     return failure();
1170   assert(flatExprs.size() == boundMap.getNumResults());
1171 
1172   // Add one (in)equality for each result.
1173   for (const auto &flatExpr : flatExprs) {
1174     SmallVector<int64_t> ineq(getNumCols(), 0);
1175     // Dims and symbols.
1176     for (unsigned j = 0, e = boundMap.getNumInputs(); j < e; j++) {
1177       ineq[j] = lower ? -flatExpr[j] : flatExpr[j];
1178     }
1179     // Invalid bound: pos appears in `boundMap`.
1180     // TODO: This should be an assertion. Fix `addDomainFromSliceMaps` and/or
1181     // its callers to prevent invalid bounds from being added.
1182     if (ineq[pos] != 0)
1183       continue;
1184     ineq[pos] = lower ? 1 : -1;
1185     // Local columns of `ineq` are at the beginning.
1186     unsigned j = getNumDimIds() + getNumSymbolIds();
1187     unsigned end = flatExpr.size() - 1;
1188     for (unsigned i = boundMap.getNumInputs(); i < end; i++, j++) {
1189       ineq[j] = lower ? -flatExpr[i] : flatExpr[i];
1190     }
1191     // Constant term.
1192     ineq[getNumCols() - 1] =
1193         lower ? -flatExpr[flatExpr.size() - 1]
1194               // Upper bound in flattenedExpr is an exclusive one.
1195               : flatExpr[flatExpr.size() - 1] - 1;
1196     type == BoundType::EQ ? addEquality(ineq) : addInequality(ineq);
1197   }
1198 
1199   return success();
1200 }
1201 
1202 AffineMap
1203 FlatAffineValueConstraints::computeAlignedMap(AffineMap map,
1204                                               ValueRange operands) const {
1205   assert(map.getNumInputs() == operands.size() && "number of inputs mismatch");
1206 
1207   SmallVector<Value> dims, syms;
1208 #ifndef NDEBUG
1209   SmallVector<Value> newSyms;
1210   SmallVector<Value> *newSymsPtr = &newSyms;
1211 #else
1212   SmallVector<Value> *newSymsPtr = nullptr;
1213 #endif // NDEBUG
1214 
1215   dims.reserve(numDims);
1216   syms.reserve(numSymbols);
1217   for (unsigned i = 0; i < numDims; ++i)
1218     dims.push_back(values[i] ? *values[i] : Value());
1219   for (unsigned i = numDims, e = numDims + numSymbols; i < e; ++i)
1220     syms.push_back(values[i] ? *values[i] : Value());
1221 
1222   AffineMap alignedMap =
1223       alignAffineMapWithValues(map, operands, dims, syms, newSymsPtr);
1224   // All symbols are already part of this FlatAffineConstraints.
1225   assert(syms.size() == newSymsPtr->size() && "unexpected new/missing symbols");
1226   assert(std::equal(syms.begin(), syms.end(), newSymsPtr->begin()) &&
1227          "unexpected new/missing symbols");
1228   return alignedMap;
1229 }
1230 
1231 LogicalResult FlatAffineValueConstraints::addBound(BoundType type, unsigned pos,
1232                                                    AffineMap boundMap,
1233                                                    ValueRange boundOperands) {
1234   // Fully compose map and operands; canonicalize and simplify so that we
1235   // transitively get to terminal symbols or loop IVs.
1236   auto map = boundMap;
1237   SmallVector<Value, 4> operands(boundOperands.begin(), boundOperands.end());
1238   fullyComposeAffineMapAndOperands(&map, &operands);
1239   map = simplifyAffineMap(map);
1240   canonicalizeMapAndOperands(&map, &operands);
1241   for (auto operand : operands)
1242     addInductionVarOrTerminalSymbol(operand);
1243   return addBound(type, pos, computeAlignedMap(map, operands));
1244 }
1245 
1246 // Adds slice lower bounds represented by lower bounds in 'lbMaps' and upper
1247 // bounds in 'ubMaps' to each value in `values' that appears in the constraint
1248 // system. Note that both lower/upper bounds share the same operand list
1249 // 'operands'.
1250 // This function assumes 'values.size' == 'lbMaps.size' == 'ubMaps.size', and
1251 // skips any null AffineMaps in 'lbMaps' or 'ubMaps'.
1252 // Note that both lower/upper bounds use operands from 'operands'.
1253 // Returns failure for unimplemented cases such as semi-affine expressions or
1254 // expressions with mod/floordiv.
1255 LogicalResult FlatAffineValueConstraints::addSliceBounds(
1256     ArrayRef<Value> values, ArrayRef<AffineMap> lbMaps,
1257     ArrayRef<AffineMap> ubMaps, ArrayRef<Value> operands) {
1258   assert(values.size() == lbMaps.size());
1259   assert(lbMaps.size() == ubMaps.size());
1260 
1261   for (unsigned i = 0, e = lbMaps.size(); i < e; ++i) {
1262     unsigned pos;
1263     if (!findId(values[i], &pos))
1264       continue;
1265 
1266     AffineMap lbMap = lbMaps[i];
1267     AffineMap ubMap = ubMaps[i];
1268     assert(!lbMap || lbMap.getNumInputs() == operands.size());
1269     assert(!ubMap || ubMap.getNumInputs() == operands.size());
1270 
1271     // Check if this slice is just an equality along this dimension.
1272     if (lbMap && ubMap && lbMap.getNumResults() == 1 &&
1273         ubMap.getNumResults() == 1 &&
1274         lbMap.getResult(0) + 1 == ubMap.getResult(0)) {
1275       if (failed(addBound(BoundType::EQ, pos, lbMap, operands)))
1276         return failure();
1277       continue;
1278     }
1279 
1280     // If lower or upper bound maps are null or provide no results, it implies
1281     // that the source loop was not at all sliced, and the entire loop will be a
1282     // part of the slice.
1283     if (lbMap && lbMap.getNumResults() != 0 && ubMap &&
1284         ubMap.getNumResults() != 0) {
1285       if (failed(addBound(BoundType::LB, pos, lbMap, operands)))
1286         return failure();
1287       if (failed(addBound(BoundType::UB, pos, ubMap, operands)))
1288         return failure();
1289     } else {
1290       auto loop = getForInductionVarOwner(values[i]);
1291       if (failed(this->addAffineForOpDomain(loop)))
1292         return failure();
1293     }
1294   }
1295   return success();
1296 }
1297 
1298 bool FlatAffineValueConstraints::findId(Value val, unsigned *pos) const {
1299   unsigned i = 0;
1300   for (const auto &mayBeId : values) {
1301     if (mayBeId.hasValue() && mayBeId.getValue() == val) {
1302       *pos = i;
1303       return true;
1304     }
1305     i++;
1306   }
1307   return false;
1308 }
1309 
1310 bool FlatAffineValueConstraints::containsId(Value val) const {
1311   return llvm::any_of(values, [&](const Optional<Value> &mayBeId) {
1312     return mayBeId.hasValue() && mayBeId.getValue() == val;
1313   });
1314 }
1315 
1316 void FlatAffineValueConstraints::swapId(unsigned posA, unsigned posB) {
1317   FlatAffineConstraints::swapId(posA, posB);
1318   std::swap(values[posA], values[posB]);
1319 }
1320 
1321 void FlatAffineValueConstraints::addBound(BoundType type, Value val,
1322                                           int64_t value) {
1323   unsigned pos;
1324   if (!findId(val, &pos))
1325     // This is a pre-condition for this method.
1326     assert(0 && "id not found");
1327   addBound(type, pos, value);
1328 }
1329 
1330 void FlatAffineConstraints::printSpace(raw_ostream &os) const {
1331   IntegerPolyhedron::printSpace(os);
1332   os << "(";
1333   for (unsigned i = 0, e = getNumIds(); i < e; i++) {
1334     if (auto *valueCstr = dyn_cast<const FlatAffineValueConstraints>(this)) {
1335       if (valueCstr->hasValue(i))
1336         os << "Value ";
1337       else
1338         os << "None ";
1339     } else {
1340       os << "None ";
1341     }
1342   }
1343   os << " const)\n";
1344 }
1345 
1346 void FlatAffineConstraints::clearAndCopyFrom(const IntegerPolyhedron &other) {
1347   if (auto *otherValueSet = dyn_cast<const FlatAffineValueConstraints>(&other))
1348     assert(!otherValueSet->hasValues() &&
1349            "cannot copy associated Values into FlatAffineConstraints");
1350 
1351   // Note: Assigment operator does not vtable pointer, so kind does not
1352   // change.
1353   if (auto *otherValueSet = dyn_cast<const FlatAffineConstraints>(&other))
1354     *this = *otherValueSet;
1355   else
1356     *static_cast<IntegerPolyhedron *>(this) = other;
1357 }
1358 
1359 void FlatAffineValueConstraints::clearAndCopyFrom(
1360     const IntegerPolyhedron &other) {
1361 
1362   if (auto *otherValueSet =
1363           dyn_cast<const FlatAffineValueConstraints>(&other)) {
1364     *this = *otherValueSet;
1365     return;
1366   }
1367 
1368   if (auto *otherValueSet = dyn_cast<const FlatAffineValueConstraints>(&other))
1369     *static_cast<FlatAffineConstraints *>(this) = *otherValueSet;
1370   else
1371     *static_cast<IntegerPolyhedron *>(this) = other;
1372 
1373   values.clear();
1374   values.resize(numIds, None);
1375 }
1376 
1377 void FlatAffineValueConstraints::fourierMotzkinEliminate(
1378     unsigned pos, bool darkShadow, bool *isResultIntegerExact) {
1379   SmallVector<Optional<Value>, 8> newVals;
1380   newVals.reserve(numIds - 1);
1381   newVals.append(values.begin(), values.begin() + pos);
1382   newVals.append(values.begin() + pos + 1, values.end());
1383   // Note: Base implementation discards all associated Values.
1384   FlatAffineConstraints::fourierMotzkinEliminate(pos, darkShadow,
1385                                                  isResultIntegerExact);
1386   values = newVals;
1387   assert(values.size() == getNumIds());
1388 }
1389 
1390 void FlatAffineValueConstraints::projectOut(Value val) {
1391   unsigned pos;
1392   bool ret = findId(val, &pos);
1393   assert(ret);
1394   (void)ret;
1395   fourierMotzkinEliminate(pos);
1396 }
1397 
1398 LogicalResult FlatAffineValueConstraints::unionBoundingBox(
1399     const FlatAffineValueConstraints &otherCst) {
1400   assert(otherCst.getNumDimIds() == numDims && "dims mismatch");
1401   assert(otherCst.getMaybeValues()
1402              .slice(0, getNumDimIds())
1403              .equals(getMaybeValues().slice(0, getNumDimIds())) &&
1404          "dim values mismatch");
1405   assert(otherCst.getNumLocalIds() == 0 && "local ids not supported here");
1406   assert(getNumLocalIds() == 0 && "local ids not supported yet here");
1407 
1408   // Align `other` to this.
1409   if (!areIdsAligned(*this, otherCst)) {
1410     FlatAffineValueConstraints otherCopy(otherCst);
1411     mergeAndAlignIds(/*offset=*/numDims, this, &otherCopy);
1412     return FlatAffineConstraints::unionBoundingBox(otherCopy);
1413   }
1414 
1415   return FlatAffineConstraints::unionBoundingBox(otherCst);
1416 }
1417 
1418 /// Compute an explicit representation for local vars. For all systems coming
1419 /// from MLIR integer sets, maps, or expressions where local vars were
1420 /// introduced to model floordivs and mods, this always succeeds.
1421 static LogicalResult computeLocalVars(const FlatAffineConstraints &cst,
1422                                       SmallVectorImpl<AffineExpr> &memo,
1423                                       MLIRContext *context) {
1424   unsigned numDims = cst.getNumDimIds();
1425   unsigned numSyms = cst.getNumSymbolIds();
1426 
1427   // Initialize dimensional and symbolic identifiers.
1428   for (unsigned i = 0; i < numDims; i++)
1429     memo[i] = getAffineDimExpr(i, context);
1430   for (unsigned i = numDims, e = numDims + numSyms; i < e; i++)
1431     memo[i] = getAffineSymbolExpr(i - numDims, context);
1432 
1433   bool changed;
1434   do {
1435     // Each time `changed` is true at the end of this iteration, one or more
1436     // local vars would have been detected as floordivs and set in memo; so the
1437     // number of null entries in memo[...] strictly reduces; so this converges.
1438     changed = false;
1439     for (unsigned i = 0, e = cst.getNumLocalIds(); i < e; ++i)
1440       if (!memo[numDims + numSyms + i] &&
1441           detectAsFloorDiv(cst, /*pos=*/numDims + numSyms + i, context, memo))
1442         changed = true;
1443   } while (changed);
1444 
1445   ArrayRef<AffineExpr> localExprs =
1446       ArrayRef<AffineExpr>(memo).take_back(cst.getNumLocalIds());
1447   return success(
1448       llvm::all_of(localExprs, [](AffineExpr expr) { return expr; }));
1449 }
1450 
1451 void FlatAffineValueConstraints::getIneqAsAffineValueMap(
1452     unsigned pos, unsigned ineqPos, AffineValueMap &vmap,
1453     MLIRContext *context) const {
1454   unsigned numDims = getNumDimIds();
1455   unsigned numSyms = getNumSymbolIds();
1456 
1457   assert(pos < numDims && "invalid position");
1458   assert(ineqPos < getNumInequalities() && "invalid inequality position");
1459 
1460   // Get expressions for local vars.
1461   SmallVector<AffineExpr, 8> memo(getNumIds(), AffineExpr());
1462   if (failed(computeLocalVars(*this, memo, context)))
1463     assert(false &&
1464            "one or more local exprs do not have an explicit representation");
1465   auto localExprs = ArrayRef<AffineExpr>(memo).take_back(getNumLocalIds());
1466 
1467   // Compute the AffineExpr lower/upper bound for this inequality.
1468   ArrayRef<int64_t> inequality = getInequality(ineqPos);
1469   SmallVector<int64_t, 8> bound;
1470   bound.reserve(getNumCols() - 1);
1471   // Everything other than the coefficient at `pos`.
1472   bound.append(inequality.begin(), inequality.begin() + pos);
1473   bound.append(inequality.begin() + pos + 1, inequality.end());
1474 
1475   if (inequality[pos] > 0)
1476     // Lower bound.
1477     std::transform(bound.begin(), bound.end(), bound.begin(),
1478                    std::negate<int64_t>());
1479   else
1480     // Upper bound (which is exclusive).
1481     bound.back() += 1;
1482 
1483   // Convert to AffineExpr (tree) form.
1484   auto boundExpr = getAffineExprFromFlatForm(bound, numDims - 1, numSyms,
1485                                              localExprs, context);
1486 
1487   // Get the values to bind to this affine expr (all dims and symbols).
1488   SmallVector<Value, 4> operands;
1489   getValues(0, pos, &operands);
1490   SmallVector<Value, 4> trailingOperands;
1491   getValues(pos + 1, getNumDimAndSymbolIds(), &trailingOperands);
1492   operands.append(trailingOperands.begin(), trailingOperands.end());
1493   vmap.reset(AffineMap::get(numDims - 1, numSyms, boundExpr), operands);
1494 }
1495 
1496 IntegerSet FlatAffineConstraints::getAsIntegerSet(MLIRContext *context) const {
1497   if (getNumConstraints() == 0)
1498     // Return universal set (always true): 0 == 0.
1499     return IntegerSet::get(getNumDimIds(), getNumSymbolIds(),
1500                            getAffineConstantExpr(/*constant=*/0, context),
1501                            /*eqFlags=*/true);
1502 
1503   // Construct local references.
1504   SmallVector<AffineExpr, 8> memo(getNumIds(), AffineExpr());
1505 
1506   if (failed(computeLocalVars(*this, memo, context))) {
1507     // Check if the local variables without an explicit representation have
1508     // zero coefficients everywhere.
1509     SmallVector<unsigned> noLocalRepVars;
1510     unsigned numDimsSymbols = getNumDimAndSymbolIds();
1511     for (unsigned i = numDimsSymbols, e = getNumIds(); i < e; ++i) {
1512       if (!memo[i] && !isColZero(/*pos=*/i))
1513         noLocalRepVars.push_back(i - numDimsSymbols);
1514     }
1515     if (!noLocalRepVars.empty()) {
1516       LLVM_DEBUG({
1517         llvm::dbgs() << "local variables at position(s) ";
1518         llvm::interleaveComma(noLocalRepVars, llvm::dbgs());
1519         llvm::dbgs() << " do not have an explicit representation in:\n";
1520         this->dump();
1521       });
1522       return IntegerSet();
1523     }
1524   }
1525 
1526   ArrayRef<AffineExpr> localExprs =
1527       ArrayRef<AffineExpr>(memo).take_back(getNumLocalIds());
1528 
1529   // Construct the IntegerSet from the equalities/inequalities.
1530   unsigned numDims = getNumDimIds();
1531   unsigned numSyms = getNumSymbolIds();
1532 
1533   SmallVector<bool, 16> eqFlags(getNumConstraints());
1534   std::fill(eqFlags.begin(), eqFlags.begin() + getNumEqualities(), true);
1535   std::fill(eqFlags.begin() + getNumEqualities(), eqFlags.end(), false);
1536 
1537   SmallVector<AffineExpr, 8> exprs;
1538   exprs.reserve(getNumConstraints());
1539 
1540   for (unsigned i = 0, e = getNumEqualities(); i < e; ++i)
1541     exprs.push_back(getAffineExprFromFlatForm(getEquality(i), numDims, numSyms,
1542                                               localExprs, context));
1543   for (unsigned i = 0, e = getNumInequalities(); i < e; ++i)
1544     exprs.push_back(getAffineExprFromFlatForm(getInequality(i), numDims,
1545                                               numSyms, localExprs, context));
1546   return IntegerSet::get(numDims, numSyms, exprs, eqFlags);
1547 }
1548 
1549 AffineMap mlir::alignAffineMapWithValues(AffineMap map, ValueRange operands,
1550                                          ValueRange dims, ValueRange syms,
1551                                          SmallVector<Value> *newSyms) {
1552   assert(operands.size() == map.getNumInputs() &&
1553          "expected same number of operands and map inputs");
1554   MLIRContext *ctx = map.getContext();
1555   Builder builder(ctx);
1556   SmallVector<AffineExpr> dimReplacements(map.getNumDims(), {});
1557   unsigned numSymbols = syms.size();
1558   SmallVector<AffineExpr> symReplacements(map.getNumSymbols(), {});
1559   if (newSyms) {
1560     newSyms->clear();
1561     newSyms->append(syms.begin(), syms.end());
1562   }
1563 
1564   for (const auto &operand : llvm::enumerate(operands)) {
1565     // Compute replacement dim/sym of operand.
1566     AffineExpr replacement;
1567     auto dimIt = std::find(dims.begin(), dims.end(), operand.value());
1568     auto symIt = std::find(syms.begin(), syms.end(), operand.value());
1569     if (dimIt != dims.end()) {
1570       replacement =
1571           builder.getAffineDimExpr(std::distance(dims.begin(), dimIt));
1572     } else if (symIt != syms.end()) {
1573       replacement =
1574           builder.getAffineSymbolExpr(std::distance(syms.begin(), symIt));
1575     } else {
1576       // This operand is neither a dimension nor a symbol. Add it as a new
1577       // symbol.
1578       replacement = builder.getAffineSymbolExpr(numSymbols++);
1579       if (newSyms)
1580         newSyms->push_back(operand.value());
1581     }
1582     // Add to corresponding replacements vector.
1583     if (operand.index() < map.getNumDims()) {
1584       dimReplacements[operand.index()] = replacement;
1585     } else {
1586       symReplacements[operand.index() - map.getNumDims()] = replacement;
1587     }
1588   }
1589 
1590   return map.replaceDimsAndSymbols(dimReplacements, symReplacements,
1591                                    dims.size(), numSymbols);
1592 }
1593 
1594 FlatAffineValueConstraints FlatAffineRelation::getDomainSet() const {
1595   FlatAffineValueConstraints domain = *this;
1596   // Convert all range variables to local variables.
1597   domain.convertDimToLocal(getNumDomainDims(),
1598                            getNumDomainDims() + getNumRangeDims());
1599   return domain;
1600 }
1601 
1602 FlatAffineValueConstraints FlatAffineRelation::getRangeSet() const {
1603   FlatAffineValueConstraints range = *this;
1604   // Convert all domain variables to local variables.
1605   range.convertDimToLocal(0, getNumDomainDims());
1606   return range;
1607 }
1608 
1609 void FlatAffineRelation::compose(const FlatAffineRelation &other) {
1610   assert(getNumDomainDims() == other.getNumRangeDims() &&
1611          "Domain of this and range of other do not match");
1612   assert(std::equal(values.begin(), values.begin() + getNumDomainDims(),
1613                     other.values.begin() + other.getNumDomainDims()) &&
1614          "Domain of this and range of other do not match");
1615 
1616   FlatAffineRelation rel = other;
1617 
1618   // Convert `rel` from
1619   //    [otherDomain] -> [otherRange]
1620   // to
1621   //    [otherDomain] -> [otherRange thisRange]
1622   // and `this` from
1623   //    [thisDomain] -> [thisRange]
1624   // to
1625   //    [otherDomain thisDomain] -> [thisRange].
1626   unsigned removeDims = rel.getNumRangeDims();
1627   insertDomainId(0, rel.getNumDomainDims());
1628   rel.appendRangeId(getNumRangeDims());
1629 
1630   // Merge symbol and local identifiers.
1631   mergeSymbolIds(rel);
1632   mergeLocalIds(rel);
1633 
1634   // Convert `rel` from [otherDomain] -> [otherRange thisRange] to
1635   // [otherDomain] -> [thisRange] by converting first otherRange range ids
1636   // to local ids.
1637   rel.convertDimToLocal(rel.getNumDomainDims(),
1638                         rel.getNumDomainDims() + removeDims);
1639   // Convert `this` from [otherDomain thisDomain] -> [thisRange] to
1640   // [otherDomain] -> [thisRange] by converting last thisDomain domain ids
1641   // to local ids.
1642   convertDimToLocal(getNumDomainDims() - removeDims, getNumDomainDims());
1643 
1644   auto thisMaybeValues = getMaybeDimValues();
1645   auto relMaybeValues = rel.getMaybeDimValues();
1646 
1647   // Add and match domain of `rel` to domain of `this`.
1648   for (unsigned i = 0, e = rel.getNumDomainDims(); i < e; ++i)
1649     if (relMaybeValues[i].hasValue())
1650       setValue(i, relMaybeValues[i].getValue());
1651   // Add and match range of `this` to range of `rel`.
1652   for (unsigned i = 0, e = getNumRangeDims(); i < e; ++i) {
1653     unsigned rangeIdx = rel.getNumDomainDims() + i;
1654     if (thisMaybeValues[rangeIdx].hasValue())
1655       rel.setValue(rangeIdx, thisMaybeValues[rangeIdx].getValue());
1656   }
1657 
1658   // Append `this` to `rel` and simplify constraints.
1659   rel.append(*this);
1660   rel.removeRedundantLocalVars();
1661 
1662   *this = rel;
1663 }
1664 
1665 void FlatAffineRelation::inverse() {
1666   unsigned oldDomain = getNumDomainDims();
1667   unsigned oldRange = getNumRangeDims();
1668   // Add new range ids.
1669   appendRangeId(oldDomain);
1670   // Swap new ids with domain.
1671   for (unsigned i = 0; i < oldDomain; ++i)
1672     swapId(i, oldDomain + oldRange + i);
1673   // Remove the swapped domain.
1674   removeIdRange(0, oldDomain);
1675   // Set domain and range as inverse.
1676   numDomainDims = oldRange;
1677   numRangeDims = oldDomain;
1678 }
1679 
1680 void FlatAffineRelation::insertDomainId(unsigned pos, unsigned num) {
1681   assert(pos <= getNumDomainDims() &&
1682          "Id cannot be inserted at invalid position");
1683   insertDimId(pos, num);
1684   numDomainDims += num;
1685 }
1686 
1687 void FlatAffineRelation::insertRangeId(unsigned pos, unsigned num) {
1688   assert(pos <= getNumRangeDims() &&
1689          "Id cannot be inserted at invalid position");
1690   insertDimId(getNumDomainDims() + pos, num);
1691   numRangeDims += num;
1692 }
1693 
1694 void FlatAffineRelation::appendDomainId(unsigned num) {
1695   insertDimId(getNumDomainDims(), num);
1696   numDomainDims += num;
1697 }
1698 
1699 void FlatAffineRelation::appendRangeId(unsigned num) {
1700   insertDimId(getNumDimIds(), num);
1701   numRangeDims += num;
1702 }
1703 
1704 void FlatAffineRelation::removeIdRange(unsigned idStart, unsigned idLimit) {
1705   if (idStart >= idLimit)
1706     return;
1707 
1708   // Compute number of domain and range identifiers to remove. This is done by
1709   // intersecting the range of domain/range ids with range of ids to remove.
1710   unsigned intersectDomainLHS = std::min(idLimit, getNumDomainDims());
1711   unsigned intersectDomainRHS = idStart;
1712   unsigned intersectRangeLHS = std::min(idLimit, getNumDimIds());
1713   unsigned intersectRangeRHS = std::max(idStart, getNumDomainDims());
1714 
1715   FlatAffineValueConstraints::removeIdRange(idStart, idLimit);
1716 
1717   if (intersectDomainLHS > intersectDomainRHS)
1718     numDomainDims -= intersectDomainLHS - intersectDomainRHS;
1719   if (intersectRangeLHS > intersectRangeRHS)
1720     numRangeDims -= intersectRangeLHS - intersectRangeRHS;
1721 }
1722 
1723 LogicalResult mlir::getRelationFromMap(AffineMap &map,
1724                                        FlatAffineRelation &rel) {
1725   // Get flattened affine expressions.
1726   std::vector<SmallVector<int64_t, 8>> flatExprs;
1727   FlatAffineConstraints localVarCst;
1728   if (failed(getFlattenedAffineExprs(map, &flatExprs, &localVarCst)))
1729     return failure();
1730 
1731   unsigned oldDimNum = localVarCst.getNumDimIds();
1732   unsigned oldCols = localVarCst.getNumCols();
1733   unsigned numRangeIds = map.getNumResults();
1734   unsigned numDomainIds = map.getNumDims();
1735 
1736   // Add range as the new expressions.
1737   localVarCst.appendDimId(numRangeIds);
1738 
1739   // Add equalities between source and range.
1740   SmallVector<int64_t, 8> eq(localVarCst.getNumCols());
1741   for (unsigned i = 0, e = map.getNumResults(); i < e; ++i) {
1742     // Zero fill.
1743     std::fill(eq.begin(), eq.end(), 0);
1744     // Fill equality.
1745     for (unsigned j = 0, f = oldDimNum; j < f; ++j)
1746       eq[j] = flatExprs[i][j];
1747     for (unsigned j = oldDimNum, f = oldCols; j < f; ++j)
1748       eq[j + numRangeIds] = flatExprs[i][j];
1749     // Set this dimension to -1 to equate lhs and rhs and add equality.
1750     eq[numDomainIds + i] = -1;
1751     localVarCst.addEquality(eq);
1752   }
1753 
1754   // Create relation and return success.
1755   rel = FlatAffineRelation(numDomainIds, numRangeIds, localVarCst);
1756   return success();
1757 }
1758 
1759 LogicalResult mlir::getRelationFromMap(const AffineValueMap &map,
1760                                        FlatAffineRelation &rel) {
1761 
1762   AffineMap affineMap = map.getAffineMap();
1763   if (failed(getRelationFromMap(affineMap, rel)))
1764     return failure();
1765 
1766   // Set symbol values for domain dimensions and symbols.
1767   for (unsigned i = 0, e = rel.getNumDomainDims(); i < e; ++i)
1768     rel.setValue(i, map.getOperand(i));
1769   for (unsigned i = rel.getNumDimIds(), e = rel.getNumDimAndSymbolIds(); i < e;
1770        ++i)
1771     rel.setValue(i, map.getOperand(i - rel.getNumRangeDims()));
1772 
1773   return success();
1774 }
1775