1 //===- AffineMap.cpp - MLIR Affine Map Classes ----------------------------===//
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 #include "mlir/IR/AffineMap.h"
10 #include "AffineMapDetail.h"
11 #include "mlir/IR/BuiltinAttributes.h"
12 #include "mlir/IR/BuiltinTypes.h"
13 #include "mlir/Support/LogicalResult.h"
14 #include "mlir/Support/MathExtras.h"
15 #include "llvm/ADT/SmallBitVector.h"
16 #include "llvm/ADT/SmallSet.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/Support/raw_ostream.h"
19 
20 using namespace mlir;
21 
22 namespace {
23 
24 // AffineExprConstantFolder evaluates an affine expression using constant
25 // operands passed in 'operandConsts'. Returns an IntegerAttr attribute
26 // representing the constant value of the affine expression evaluated on
27 // constant 'operandConsts', or nullptr if it can't be folded.
28 class AffineExprConstantFolder {
29 public:
30   AffineExprConstantFolder(unsigned numDims, ArrayRef<Attribute> operandConsts)
31       : numDims(numDims), operandConsts(operandConsts) {}
32 
33   /// Attempt to constant fold the specified affine expr, or return null on
34   /// failure.
35   IntegerAttr constantFold(AffineExpr expr) {
36     if (auto result = constantFoldImpl(expr))
37       return IntegerAttr::get(IndexType::get(expr.getContext()), *result);
38     return nullptr;
39   }
40 
41 private:
42   Optional<int64_t> constantFoldImpl(AffineExpr expr) {
43     switch (expr.getKind()) {
44     case AffineExprKind::Add:
45       return constantFoldBinExpr(
46           expr, [](int64_t lhs, int64_t rhs) { return lhs + rhs; });
47     case AffineExprKind::Mul:
48       return constantFoldBinExpr(
49           expr, [](int64_t lhs, int64_t rhs) { return lhs * rhs; });
50     case AffineExprKind::Mod:
51       return constantFoldBinExpr(
52           expr, [](int64_t lhs, int64_t rhs) { return mod(lhs, rhs); });
53     case AffineExprKind::FloorDiv:
54       return constantFoldBinExpr(
55           expr, [](int64_t lhs, int64_t rhs) { return floorDiv(lhs, rhs); });
56     case AffineExprKind::CeilDiv:
57       return constantFoldBinExpr(
58           expr, [](int64_t lhs, int64_t rhs) { return ceilDiv(lhs, rhs); });
59     case AffineExprKind::Constant:
60       return expr.cast<AffineConstantExpr>().getValue();
61     case AffineExprKind::DimId:
62       if (auto attr = operandConsts[expr.cast<AffineDimExpr>().getPosition()]
63                           .dyn_cast_or_null<IntegerAttr>())
64         return attr.getInt();
65       return llvm::None;
66     case AffineExprKind::SymbolId:
67       if (auto attr = operandConsts[numDims +
68                                     expr.cast<AffineSymbolExpr>().getPosition()]
69                           .dyn_cast_or_null<IntegerAttr>())
70         return attr.getInt();
71       return llvm::None;
72     }
73     llvm_unreachable("Unknown AffineExpr");
74   }
75 
76   // TODO: Change these to operate on APInts too.
77   Optional<int64_t> constantFoldBinExpr(AffineExpr expr,
78                                         int64_t (*op)(int64_t, int64_t)) {
79     auto binOpExpr = expr.cast<AffineBinaryOpExpr>();
80     if (auto lhs = constantFoldImpl(binOpExpr.getLHS()))
81       if (auto rhs = constantFoldImpl(binOpExpr.getRHS()))
82         return op(*lhs, *rhs);
83     return llvm::None;
84   }
85 
86   // The number of dimension operands in AffineMap containing this expression.
87   unsigned numDims;
88   // The constant valued operands used to evaluate this AffineExpr.
89   ArrayRef<Attribute> operandConsts;
90 };
91 
92 } // end anonymous namespace
93 
94 /// Returns a single constant result affine map.
95 AffineMap AffineMap::getConstantMap(int64_t val, MLIRContext *context) {
96   return get(/*dimCount=*/0, /*symbolCount=*/0,
97              {getAffineConstantExpr(val, context)});
98 }
99 
100 /// Returns an identity affine map (d0, ..., dn) -> (dp, ..., dn) on the most
101 /// minor dimensions.
102 AffineMap AffineMap::getMinorIdentityMap(unsigned dims, unsigned results,
103                                          MLIRContext *context) {
104   assert(dims >= results && "Dimension mismatch");
105   auto id = AffineMap::getMultiDimIdentityMap(dims, context);
106   return AffineMap::get(dims, 0, id.getResults().take_back(results), context);
107 }
108 
109 bool AffineMap::isMinorIdentity() const {
110   return getNumDims() >= getNumResults() &&
111          *this ==
112              getMinorIdentityMap(getNumDims(), getNumResults(), getContext());
113 }
114 
115 /// Returns true if this affine map is a minor identity up to broadcasted
116 /// dimensions which are indicated by value 0 in the result.
117 bool AffineMap::isMinorIdentityWithBroadcasting(
118     SmallVectorImpl<unsigned> *broadcastedDims) const {
119   if (broadcastedDims)
120     broadcastedDims->clear();
121   if (getNumDims() < getNumResults())
122     return false;
123   unsigned suffixStart = getNumDims() - getNumResults();
124   for (auto idxAndExpr : llvm::enumerate(getResults())) {
125     unsigned resIdx = idxAndExpr.index();
126     AffineExpr expr = idxAndExpr.value();
127     if (auto constExpr = expr.dyn_cast<AffineConstantExpr>()) {
128       // Each result may be either a constant 0 (broadcasted dimension).
129       if (constExpr.getValue() != 0)
130         return false;
131       if (broadcastedDims)
132         broadcastedDims->push_back(resIdx);
133     } else if (auto dimExpr = expr.dyn_cast<AffineDimExpr>()) {
134       // Or it may be the input dimension corresponding to this result position.
135       if (dimExpr.getPosition() != suffixStart + resIdx)
136         return false;
137     } else {
138       return false;
139     }
140   }
141   return true;
142 }
143 
144 /// Return true if this affine map can be converted to a minor identity with
145 /// broadcast by doing a permute. Return a permutation (there may be
146 /// several) to apply to get to a minor identity with broadcasts.
147 /// Ex:
148 ///  * (d0, d1, d2) -> (0, d1) maps to minor identity (d1, 0 = d2) with
149 ///  perm = [1, 0] and broadcast d2
150 ///  * (d0, d1, d2) -> (d0, 0) cannot be mapped to a minor identity by
151 ///  permutation + broadcast
152 ///  * (d0, d1, d2, d3) -> (0, d1, d3) maps to minor identity (d1, 0 = d2, d3)
153 ///  with perm = [1, 0, 2] and broadcast d2
154 ///  * (d0, d1) -> (d1, 0, 0, d0) maps to minor identity (d0, d1) with extra
155 ///  leading broadcat dimensions. The map returned would be (0, 0, d0, d1) with
156 ///  perm = [3, 0, 1, 2]
157 bool AffineMap::isPermutationOfMinorIdentityWithBroadcasting(
158     SmallVectorImpl<unsigned> &permutedDims) const {
159   unsigned projectionStart =
160       getNumResults() < getNumInputs() ? getNumInputs() - getNumResults() : 0;
161   permutedDims.clear();
162   SmallVector<unsigned> broadcastDims;
163   permutedDims.resize(getNumResults(), 0);
164   // If there are more results than input dimensions we want the new map to
165   // start with broadcast dimensions in order to be a minor identity with
166   // broadcasting.
167   unsigned leadingBroadcast =
168       getNumResults() > getNumInputs() ? getNumResults() - getNumInputs() : 0;
169   llvm::SmallBitVector dimFound(std::max(getNumInputs(), getNumResults()),
170                                 false);
171   for (auto idxAndExpr : llvm::enumerate(getResults())) {
172     unsigned resIdx = idxAndExpr.index();
173     AffineExpr expr = idxAndExpr.value();
174     // Each result may be either a constant 0 (broadcast dimension) or a
175     // dimension.
176     if (auto constExpr = expr.dyn_cast<AffineConstantExpr>()) {
177       if (constExpr.getValue() != 0)
178         return false;
179       broadcastDims.push_back(resIdx);
180     } else if (auto dimExpr = expr.dyn_cast<AffineDimExpr>()) {
181       if (dimExpr.getPosition() < projectionStart)
182         return false;
183       unsigned newPosition =
184           dimExpr.getPosition() - projectionStart + leadingBroadcast;
185       permutedDims[resIdx] = newPosition;
186       dimFound[newPosition] = true;
187     } else {
188       return false;
189     }
190   }
191   // Find a permuation for the broadcast dimension. Since they are broadcasted
192   // any valid permutation is acceptable. We just permute the dim into a slot
193   // without an existing dimension.
194   unsigned pos = 0;
195   for (auto dim : broadcastDims) {
196     while (pos < dimFound.size() && dimFound[pos]) {
197       pos++;
198     }
199     permutedDims[dim] = pos++;
200   }
201   return true;
202 }
203 
204 /// Returns an AffineMap representing a permutation.
205 AffineMap AffineMap::getPermutationMap(ArrayRef<unsigned> permutation,
206                                        MLIRContext *context) {
207   assert(!permutation.empty() &&
208          "Cannot create permutation map from empty permutation vector");
209   SmallVector<AffineExpr, 4> affExprs;
210   for (auto index : permutation)
211     affExprs.push_back(getAffineDimExpr(index, context));
212   auto m = std::max_element(permutation.begin(), permutation.end());
213   auto permutationMap = AffineMap::get(*m + 1, 0, affExprs, context);
214   assert(permutationMap.isPermutation() && "Invalid permutation vector");
215   return permutationMap;
216 }
217 
218 template <typename AffineExprContainer>
219 static void getMaxDimAndSymbol(ArrayRef<AffineExprContainer> exprsList,
220                                int64_t &maxDim, int64_t &maxSym) {
221   for (const auto &exprs : exprsList) {
222     for (auto expr : exprs) {
223       expr.walk([&maxDim, &maxSym](AffineExpr e) {
224         if (auto d = e.dyn_cast<AffineDimExpr>())
225           maxDim = std::max(maxDim, static_cast<int64_t>(d.getPosition()));
226         if (auto s = e.dyn_cast<AffineSymbolExpr>())
227           maxSym = std::max(maxSym, static_cast<int64_t>(s.getPosition()));
228       });
229     }
230   }
231 }
232 
233 template <typename AffineExprContainer>
234 static SmallVector<AffineMap, 4>
235 inferFromExprList(ArrayRef<AffineExprContainer> exprsList) {
236   assert(!exprsList.empty());
237   assert(!exprsList[0].empty());
238   auto context = exprsList[0][0].getContext();
239   int64_t maxDim = -1, maxSym = -1;
240   getMaxDimAndSymbol(exprsList, maxDim, maxSym);
241   SmallVector<AffineMap, 4> maps;
242   maps.reserve(exprsList.size());
243   for (const auto &exprs : exprsList)
244     maps.push_back(AffineMap::get(/*dimCount=*/maxDim + 1,
245                                   /*symbolCount=*/maxSym + 1, exprs, context));
246   return maps;
247 }
248 
249 SmallVector<AffineMap, 4>
250 AffineMap::inferFromExprList(ArrayRef<ArrayRef<AffineExpr>> exprsList) {
251   return ::inferFromExprList(exprsList);
252 }
253 
254 SmallVector<AffineMap, 4>
255 AffineMap::inferFromExprList(ArrayRef<SmallVector<AffineExpr, 4>> exprsList) {
256   return ::inferFromExprList(exprsList);
257 }
258 
259 AffineMap AffineMap::getMultiDimIdentityMap(unsigned numDims,
260                                             MLIRContext *context) {
261   SmallVector<AffineExpr, 4> dimExprs;
262   dimExprs.reserve(numDims);
263   for (unsigned i = 0; i < numDims; ++i)
264     dimExprs.push_back(mlir::getAffineDimExpr(i, context));
265   return get(/*dimCount=*/numDims, /*symbolCount=*/0, dimExprs, context);
266 }
267 
268 MLIRContext *AffineMap::getContext() const { return map->context; }
269 
270 bool AffineMap::isIdentity() const {
271   if (getNumDims() != getNumResults())
272     return false;
273   ArrayRef<AffineExpr> results = getResults();
274   for (unsigned i = 0, numDims = getNumDims(); i < numDims; ++i) {
275     auto expr = results[i].dyn_cast<AffineDimExpr>();
276     if (!expr || expr.getPosition() != i)
277       return false;
278   }
279   return true;
280 }
281 
282 bool AffineMap::isEmpty() const {
283   return getNumDims() == 0 && getNumSymbols() == 0 && getNumResults() == 0;
284 }
285 
286 bool AffineMap::isSingleConstant() const {
287   return getNumResults() == 1 && getResult(0).isa<AffineConstantExpr>();
288 }
289 
290 int64_t AffineMap::getSingleConstantResult() const {
291   assert(isSingleConstant() && "map must have a single constant result");
292   return getResult(0).cast<AffineConstantExpr>().getValue();
293 }
294 
295 unsigned AffineMap::getNumDims() const {
296   assert(map && "uninitialized map storage");
297   return map->numDims;
298 }
299 unsigned AffineMap::getNumSymbols() const {
300   assert(map && "uninitialized map storage");
301   return map->numSymbols;
302 }
303 unsigned AffineMap::getNumResults() const {
304   assert(map && "uninitialized map storage");
305   return map->results.size();
306 }
307 unsigned AffineMap::getNumInputs() const {
308   assert(map && "uninitialized map storage");
309   return map->numDims + map->numSymbols;
310 }
311 
312 ArrayRef<AffineExpr> AffineMap::getResults() const {
313   assert(map && "uninitialized map storage");
314   return map->results;
315 }
316 AffineExpr AffineMap::getResult(unsigned idx) const {
317   assert(map && "uninitialized map storage");
318   return map->results[idx];
319 }
320 
321 unsigned AffineMap::getDimPosition(unsigned idx) const {
322   return getResult(idx).cast<AffineDimExpr>().getPosition();
323 }
324 
325 /// Folds the results of the application of an affine map on the provided
326 /// operands to a constant if possible. Returns false if the folding happens,
327 /// true otherwise.
328 LogicalResult
329 AffineMap::constantFold(ArrayRef<Attribute> operandConstants,
330                         SmallVectorImpl<Attribute> &results) const {
331   // Attempt partial folding.
332   SmallVector<int64_t, 2> integers;
333   partialConstantFold(operandConstants, &integers);
334 
335   // If all expressions folded to a constant, populate results with attributes
336   // containing those constants.
337   if (integers.empty())
338     return failure();
339 
340   auto range = llvm::map_range(integers, [this](int64_t i) {
341     return IntegerAttr::get(IndexType::get(getContext()), i);
342   });
343   results.append(range.begin(), range.end());
344   return success();
345 }
346 
347 AffineMap
348 AffineMap::partialConstantFold(ArrayRef<Attribute> operandConstants,
349                                SmallVectorImpl<int64_t> *results) const {
350   assert(getNumInputs() == operandConstants.size());
351 
352   // Fold each of the result expressions.
353   AffineExprConstantFolder exprFolder(getNumDims(), operandConstants);
354   SmallVector<AffineExpr, 4> exprs;
355   exprs.reserve(getNumResults());
356 
357   for (auto expr : getResults()) {
358     auto folded = exprFolder.constantFold(expr);
359     // If did not fold to a constant, keep the original expression, and clear
360     // the integer results vector.
361     if (folded) {
362       exprs.push_back(
363           getAffineConstantExpr(folded.getInt(), folded.getContext()));
364       if (results)
365         results->push_back(folded.getInt());
366     } else {
367       exprs.push_back(expr);
368       if (results) {
369         results->clear();
370         results = nullptr;
371       }
372     }
373   }
374 
375   return get(getNumDims(), getNumSymbols(), exprs, getContext());
376 }
377 
378 /// Walk all of the AffineExpr's in this mapping. Each node in an expression
379 /// tree is visited in postorder.
380 void AffineMap::walkExprs(std::function<void(AffineExpr)> callback) const {
381   for (auto expr : getResults())
382     expr.walk(callback);
383 }
384 
385 /// This method substitutes any uses of dimensions and symbols (e.g.
386 /// dim#0 with dimReplacements[0]) in subexpressions and returns the modified
387 /// expression mapping.  Because this can be used to eliminate dims and
388 /// symbols, the client needs to specify the number of dims and symbols in
389 /// the result.  The returned map always has the same number of results.
390 AffineMap AffineMap::replaceDimsAndSymbols(ArrayRef<AffineExpr> dimReplacements,
391                                            ArrayRef<AffineExpr> symReplacements,
392                                            unsigned numResultDims,
393                                            unsigned numResultSyms) const {
394   SmallVector<AffineExpr, 8> results;
395   results.reserve(getNumResults());
396   for (auto expr : getResults())
397     results.push_back(
398         expr.replaceDimsAndSymbols(dimReplacements, symReplacements));
399   return get(numResultDims, numResultSyms, results, getContext());
400 }
401 
402 /// Sparse replace method. Apply AffineExpr::replace(`expr`, `replacement`) to
403 /// each of the results and return a new AffineMap with the new results and
404 /// with the specified number of dims and symbols.
405 AffineMap AffineMap::replace(AffineExpr expr, AffineExpr replacement,
406                              unsigned numResultDims,
407                              unsigned numResultSyms) const {
408   SmallVector<AffineExpr, 4> newResults;
409   newResults.reserve(getNumResults());
410   for (AffineExpr e : getResults())
411     newResults.push_back(e.replace(expr, replacement));
412   return AffineMap::get(numResultDims, numResultSyms, newResults, getContext());
413 }
414 
415 /// Sparse replace method. Apply AffineExpr::replace(`map`) to each of the
416 /// results and return a new AffineMap with the new results and with the
417 /// specified number of dims and symbols.
418 AffineMap AffineMap::replace(const DenseMap<AffineExpr, AffineExpr> &map,
419                              unsigned numResultDims,
420                              unsigned numResultSyms) const {
421   SmallVector<AffineExpr, 4> newResults;
422   newResults.reserve(getNumResults());
423   for (AffineExpr e : getResults())
424     newResults.push_back(e.replace(map));
425   return AffineMap::get(numResultDims, numResultSyms, newResults, getContext());
426 }
427 
428 AffineMap AffineMap::compose(AffineMap map) const {
429   assert(getNumDims() == map.getNumResults() && "Number of results mismatch");
430   // Prepare `map` by concatenating the symbols and rewriting its exprs.
431   unsigned numDims = map.getNumDims();
432   unsigned numSymbolsThisMap = getNumSymbols();
433   unsigned numSymbols = numSymbolsThisMap + map.getNumSymbols();
434   SmallVector<AffineExpr, 8> newDims(numDims);
435   for (unsigned idx = 0; idx < numDims; ++idx) {
436     newDims[idx] = getAffineDimExpr(idx, getContext());
437   }
438   SmallVector<AffineExpr, 8> newSymbols(numSymbols - numSymbolsThisMap);
439   for (unsigned idx = numSymbolsThisMap; idx < numSymbols; ++idx) {
440     newSymbols[idx - numSymbolsThisMap] =
441         getAffineSymbolExpr(idx, getContext());
442   }
443   auto newMap =
444       map.replaceDimsAndSymbols(newDims, newSymbols, numDims, numSymbols);
445   SmallVector<AffineExpr, 8> exprs;
446   exprs.reserve(getResults().size());
447   for (auto expr : getResults())
448     exprs.push_back(expr.compose(newMap));
449   return AffineMap::get(numDims, numSymbols, exprs, map.getContext());
450 }
451 
452 SmallVector<int64_t, 4> AffineMap::compose(ArrayRef<int64_t> values) const {
453   assert(getNumSymbols() == 0 && "Expected symbol-less map");
454   SmallVector<AffineExpr, 4> exprs;
455   exprs.reserve(values.size());
456   MLIRContext *ctx = getContext();
457   for (auto v : values)
458     exprs.push_back(getAffineConstantExpr(v, ctx));
459   auto resMap = compose(AffineMap::get(0, 0, exprs, ctx));
460   SmallVector<int64_t, 4> res;
461   res.reserve(resMap.getNumResults());
462   for (auto e : resMap.getResults())
463     res.push_back(e.cast<AffineConstantExpr>().getValue());
464   return res;
465 }
466 
467 bool AffineMap::isProjectedPermutation() const {
468   if (getNumSymbols() > 0)
469     return false;
470   SmallVector<bool, 8> seen(getNumInputs(), false);
471   for (auto expr : getResults()) {
472     if (auto dim = expr.dyn_cast<AffineDimExpr>()) {
473       if (seen[dim.getPosition()])
474         return false;
475       seen[dim.getPosition()] = true;
476       continue;
477     }
478     return false;
479   }
480   return true;
481 }
482 
483 bool AffineMap::isPermutation() const {
484   if (getNumDims() != getNumResults())
485     return false;
486   return isProjectedPermutation();
487 }
488 
489 AffineMap AffineMap::getSubMap(ArrayRef<unsigned> resultPos) const {
490   SmallVector<AffineExpr, 4> exprs;
491   exprs.reserve(resultPos.size());
492   for (auto idx : resultPos)
493     exprs.push_back(getResult(idx));
494   return AffineMap::get(getNumDims(), getNumSymbols(), exprs, getContext());
495 }
496 
497 AffineMap AffineMap::getMajorSubMap(unsigned numResults) const {
498   if (numResults == 0)
499     return AffineMap();
500   if (numResults > getNumResults())
501     return *this;
502   return getSubMap(llvm::to_vector<4>(llvm::seq<unsigned>(0, numResults)));
503 }
504 
505 AffineMap AffineMap::getMinorSubMap(unsigned numResults) const {
506   if (numResults == 0)
507     return AffineMap();
508   if (numResults > getNumResults())
509     return *this;
510   return getSubMap(llvm::to_vector<4>(
511       llvm::seq<unsigned>(getNumResults() - numResults, getNumResults())));
512 }
513 
514 AffineMap mlir::compressDims(AffineMap map,
515                              const llvm::SmallDenseSet<unsigned> &unusedDims) {
516   unsigned numDims = 0;
517   SmallVector<AffineExpr> dimReplacements;
518   dimReplacements.reserve(map.getNumDims());
519   MLIRContext *context = map.getContext();
520   for (unsigned dim = 0, e = map.getNumDims(); dim < e; ++dim) {
521     if (unusedDims.contains(dim))
522       dimReplacements.push_back(getAffineConstantExpr(0, context));
523     else
524       dimReplacements.push_back(getAffineDimExpr(numDims++, context));
525   }
526   SmallVector<AffineExpr> resultExprs;
527   resultExprs.reserve(map.getNumResults());
528   for (auto e : map.getResults())
529     resultExprs.push_back(e.replaceDims(dimReplacements));
530   return AffineMap::get(numDims, map.getNumSymbols(), resultExprs, context);
531 }
532 
533 AffineMap mlir::compressUnusedDims(AffineMap map) {
534   llvm::SmallDenseSet<unsigned> usedDims;
535   map.walkExprs([&](AffineExpr expr) {
536     if (auto dimExpr = expr.dyn_cast<AffineDimExpr>())
537       usedDims.insert(dimExpr.getPosition());
538   });
539   llvm::SmallDenseSet<unsigned> unusedDims;
540   for (unsigned d = 0, e = map.getNumDims(); d != e; ++d)
541     if (!usedDims.contains(d))
542       unusedDims.insert(d);
543   return compressDims(map, unusedDims);
544 }
545 
546 AffineMap
547 mlir::compressSymbols(AffineMap map,
548                       const llvm::SmallDenseSet<unsigned> &unusedSymbols) {
549   unsigned numSymbols = 0;
550   SmallVector<AffineExpr> symReplacements;
551   symReplacements.reserve(map.getNumSymbols());
552   MLIRContext *context = map.getContext();
553   for (unsigned sym = 0, e = map.getNumSymbols(); sym < e; ++sym) {
554     if (unusedSymbols.contains(sym))
555       symReplacements.push_back(getAffineConstantExpr(0, context));
556     else
557       symReplacements.push_back(getAffineSymbolExpr(numSymbols++, context));
558   }
559   SmallVector<AffineExpr> resultExprs;
560   resultExprs.reserve(map.getNumResults());
561   for (auto e : map.getResults())
562     resultExprs.push_back(e.replaceSymbols(symReplacements));
563   return AffineMap::get(map.getNumDims(), numSymbols, resultExprs, context);
564 }
565 
566 AffineMap mlir::compressUnusedSymbols(AffineMap map) {
567   llvm::SmallDenseSet<unsigned> usedSymbols;
568   map.walkExprs([&](AffineExpr expr) {
569     if (auto symExpr = expr.dyn_cast<AffineSymbolExpr>())
570       usedSymbols.insert(symExpr.getPosition());
571   });
572   llvm::SmallDenseSet<unsigned> unusedSymbols;
573   for (unsigned d = 0, e = map.getNumSymbols(); d != e; ++d)
574     if (!usedSymbols.contains(d))
575       unusedSymbols.insert(d);
576   return compressSymbols(map, unusedSymbols);
577 }
578 
579 AffineMap mlir::simplifyAffineMap(AffineMap map) {
580   SmallVector<AffineExpr, 8> exprs;
581   for (auto e : map.getResults()) {
582     exprs.push_back(
583         simplifyAffineExpr(e, map.getNumDims(), map.getNumSymbols()));
584   }
585   return AffineMap::get(map.getNumDims(), map.getNumSymbols(), exprs,
586                         map.getContext());
587 }
588 
589 AffineMap mlir::removeDuplicateExprs(AffineMap map) {
590   auto results = map.getResults();
591   SmallVector<AffineExpr, 4> uniqueExprs(results.begin(), results.end());
592   uniqueExprs.erase(std::unique(uniqueExprs.begin(), uniqueExprs.end()),
593                     uniqueExprs.end());
594   return AffineMap::get(map.getNumDims(), map.getNumSymbols(), uniqueExprs,
595                         map.getContext());
596 }
597 
598 AffineMap mlir::inversePermutation(AffineMap map) {
599   if (map.isEmpty())
600     return map;
601   assert(map.getNumSymbols() == 0 && "expected map without symbols");
602   SmallVector<AffineExpr, 4> exprs(map.getNumDims());
603   for (auto en : llvm::enumerate(map.getResults())) {
604     auto expr = en.value();
605     // Skip non-permutations.
606     if (auto d = expr.dyn_cast<AffineDimExpr>()) {
607       if (exprs[d.getPosition()])
608         continue;
609       exprs[d.getPosition()] = getAffineDimExpr(en.index(), d.getContext());
610     }
611   }
612   SmallVector<AffineExpr, 4> seenExprs;
613   seenExprs.reserve(map.getNumDims());
614   for (auto expr : exprs)
615     if (expr)
616       seenExprs.push_back(expr);
617   if (seenExprs.size() != map.getNumInputs())
618     return AffineMap();
619   return AffineMap::get(map.getNumResults(), 0, seenExprs, map.getContext());
620 }
621 
622 AffineMap mlir::concatAffineMaps(ArrayRef<AffineMap> maps) {
623   unsigned numResults = 0, numDims = 0, numSymbols = 0;
624   for (auto m : maps)
625     numResults += m.getNumResults();
626   SmallVector<AffineExpr, 8> results;
627   results.reserve(numResults);
628   for (auto m : maps) {
629     for (auto res : m.getResults())
630       results.push_back(res.shiftSymbols(m.getNumSymbols(), numSymbols));
631 
632     numSymbols += m.getNumSymbols();
633     numDims = std::max(m.getNumDims(), numDims);
634   }
635   return AffineMap::get(numDims, numSymbols, results,
636                         maps.front().getContext());
637 }
638 
639 AffineMap
640 mlir::getProjectedMap(AffineMap map,
641                       const llvm::SmallDenseSet<unsigned> &unusedDims) {
642   return compressUnusedSymbols(compressDims(map, unusedDims));
643 }
644 
645 //===----------------------------------------------------------------------===//
646 // MutableAffineMap.
647 //===----------------------------------------------------------------------===//
648 
649 MutableAffineMap::MutableAffineMap(AffineMap map)
650     : numDims(map.getNumDims()), numSymbols(map.getNumSymbols()),
651       context(map.getContext()) {
652   for (auto result : map.getResults())
653     results.push_back(result);
654 }
655 
656 void MutableAffineMap::reset(AffineMap map) {
657   results.clear();
658   numDims = map.getNumDims();
659   numSymbols = map.getNumSymbols();
660   context = map.getContext();
661   for (auto result : map.getResults())
662     results.push_back(result);
663 }
664 
665 bool MutableAffineMap::isMultipleOf(unsigned idx, int64_t factor) const {
666   if (results[idx].isMultipleOf(factor))
667     return true;
668 
669   // TODO: use simplifyAffineExpr and FlatAffineConstraints to
670   // complete this (for a more powerful analysis).
671   return false;
672 }
673 
674 // Simplifies the result affine expressions of this map. The expressions have to
675 // be pure for the simplification implemented.
676 void MutableAffineMap::simplify() {
677   // Simplify each of the results if possible.
678   // TODO: functional-style map
679   for (unsigned i = 0, e = getNumResults(); i < e; i++) {
680     results[i] = simplifyAffineExpr(getResult(i), numDims, numSymbols);
681   }
682 }
683 
684 AffineMap MutableAffineMap::getAffineMap() const {
685   return AffineMap::get(numDims, numSymbols, results, context);
686 }
687