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