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