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 bool AffineMap::isConstant() const {
291   return llvm::all_of(getResults(), [](AffineExpr expr) {
292     return expr.isa<AffineConstantExpr>();
293   });
294 }
295 
296 int64_t AffineMap::getSingleConstantResult() const {
297   assert(isSingleConstant() && "map must have a single constant result");
298   return getResult(0).cast<AffineConstantExpr>().getValue();
299 }
300 
301 SmallVector<int64_t> AffineMap::getConstantResults() const {
302   assert(isConstant() && "map must have only constant results");
303   SmallVector<int64_t> result;
304   for (auto expr : getResults())
305     result.emplace_back(expr.cast<AffineConstantExpr>().getValue());
306   return result;
307 }
308 
309 unsigned AffineMap::getNumDims() const {
310   assert(map && "uninitialized map storage");
311   return map->numDims;
312 }
313 unsigned AffineMap::getNumSymbols() const {
314   assert(map && "uninitialized map storage");
315   return map->numSymbols;
316 }
317 unsigned AffineMap::getNumResults() const {
318   assert(map && "uninitialized map storage");
319   return map->results.size();
320 }
321 unsigned AffineMap::getNumInputs() const {
322   assert(map && "uninitialized map storage");
323   return map->numDims + map->numSymbols;
324 }
325 
326 ArrayRef<AffineExpr> AffineMap::getResults() const {
327   assert(map && "uninitialized map storage");
328   return map->results;
329 }
330 AffineExpr AffineMap::getResult(unsigned idx) const {
331   assert(map && "uninitialized map storage");
332   return map->results[idx];
333 }
334 
335 unsigned AffineMap::getDimPosition(unsigned idx) const {
336   return getResult(idx).cast<AffineDimExpr>().getPosition();
337 }
338 
339 /// Folds the results of the application of an affine map on the provided
340 /// operands to a constant if possible. Returns false if the folding happens,
341 /// true otherwise.
342 LogicalResult
343 AffineMap::constantFold(ArrayRef<Attribute> operandConstants,
344                         SmallVectorImpl<Attribute> &results) const {
345   // Attempt partial folding.
346   SmallVector<int64_t, 2> integers;
347   partialConstantFold(operandConstants, &integers);
348 
349   // If all expressions folded to a constant, populate results with attributes
350   // containing those constants.
351   if (integers.empty())
352     return failure();
353 
354   auto range = llvm::map_range(integers, [this](int64_t i) {
355     return IntegerAttr::get(IndexType::get(getContext()), i);
356   });
357   results.append(range.begin(), range.end());
358   return success();
359 }
360 
361 AffineMap
362 AffineMap::partialConstantFold(ArrayRef<Attribute> operandConstants,
363                                SmallVectorImpl<int64_t> *results) const {
364   assert(getNumInputs() == operandConstants.size());
365 
366   // Fold each of the result expressions.
367   AffineExprConstantFolder exprFolder(getNumDims(), operandConstants);
368   SmallVector<AffineExpr, 4> exprs;
369   exprs.reserve(getNumResults());
370 
371   for (auto expr : getResults()) {
372     auto folded = exprFolder.constantFold(expr);
373     // If did not fold to a constant, keep the original expression, and clear
374     // the integer results vector.
375     if (folded) {
376       exprs.push_back(
377           getAffineConstantExpr(folded.getInt(), folded.getContext()));
378       if (results)
379         results->push_back(folded.getInt());
380     } else {
381       exprs.push_back(expr);
382       if (results) {
383         results->clear();
384         results = nullptr;
385       }
386     }
387   }
388 
389   return get(getNumDims(), getNumSymbols(), exprs, getContext());
390 }
391 
392 /// Walk all of the AffineExpr's in this mapping. Each node in an expression
393 /// tree is visited in postorder.
394 void AffineMap::walkExprs(std::function<void(AffineExpr)> callback) const {
395   for (auto expr : getResults())
396     expr.walk(callback);
397 }
398 
399 /// This method substitutes any uses of dimensions and symbols (e.g.
400 /// dim#0 with dimReplacements[0]) in subexpressions and returns the modified
401 /// expression mapping.  Because this can be used to eliminate dims and
402 /// symbols, the client needs to specify the number of dims and symbols in
403 /// the result.  The returned map always has the same number of results.
404 AffineMap AffineMap::replaceDimsAndSymbols(ArrayRef<AffineExpr> dimReplacements,
405                                            ArrayRef<AffineExpr> symReplacements,
406                                            unsigned numResultDims,
407                                            unsigned numResultSyms) const {
408   SmallVector<AffineExpr, 8> results;
409   results.reserve(getNumResults());
410   for (auto expr : getResults())
411     results.push_back(
412         expr.replaceDimsAndSymbols(dimReplacements, symReplacements));
413   return get(numResultDims, numResultSyms, results, getContext());
414 }
415 
416 /// Sparse replace method. Apply AffineExpr::replace(`expr`, `replacement`) to
417 /// each of the results and return a new AffineMap with the new results and
418 /// with the specified number of dims and symbols.
419 AffineMap AffineMap::replace(AffineExpr expr, AffineExpr replacement,
420                              unsigned numResultDims,
421                              unsigned numResultSyms) const {
422   SmallVector<AffineExpr, 4> newResults;
423   newResults.reserve(getNumResults());
424   for (AffineExpr e : getResults())
425     newResults.push_back(e.replace(expr, replacement));
426   return AffineMap::get(numResultDims, numResultSyms, newResults, getContext());
427 }
428 
429 /// Sparse replace method. Apply AffineExpr::replace(`map`) to each of the
430 /// results and return a new AffineMap with the new results and with the
431 /// specified number of dims and symbols.
432 AffineMap AffineMap::replace(const DenseMap<AffineExpr, AffineExpr> &map,
433                              unsigned numResultDims,
434                              unsigned numResultSyms) const {
435   SmallVector<AffineExpr, 4> newResults;
436   newResults.reserve(getNumResults());
437   for (AffineExpr e : getResults())
438     newResults.push_back(e.replace(map));
439   return AffineMap::get(numResultDims, numResultSyms, newResults, getContext());
440 }
441 
442 AffineMap AffineMap::compose(AffineMap map) const {
443   assert(getNumDims() == map.getNumResults() && "Number of results mismatch");
444   // Prepare `map` by concatenating the symbols and rewriting its exprs.
445   unsigned numDims = map.getNumDims();
446   unsigned numSymbolsThisMap = getNumSymbols();
447   unsigned numSymbols = numSymbolsThisMap + map.getNumSymbols();
448   SmallVector<AffineExpr, 8> newDims(numDims);
449   for (unsigned idx = 0; idx < numDims; ++idx) {
450     newDims[idx] = getAffineDimExpr(idx, getContext());
451   }
452   SmallVector<AffineExpr, 8> newSymbols(numSymbols - numSymbolsThisMap);
453   for (unsigned idx = numSymbolsThisMap; idx < numSymbols; ++idx) {
454     newSymbols[idx - numSymbolsThisMap] =
455         getAffineSymbolExpr(idx, getContext());
456   }
457   auto newMap =
458       map.replaceDimsAndSymbols(newDims, newSymbols, numDims, numSymbols);
459   SmallVector<AffineExpr, 8> exprs;
460   exprs.reserve(getResults().size());
461   for (auto expr : getResults())
462     exprs.push_back(expr.compose(newMap));
463   return AffineMap::get(numDims, numSymbols, exprs, map.getContext());
464 }
465 
466 SmallVector<int64_t, 4> AffineMap::compose(ArrayRef<int64_t> values) const {
467   assert(getNumSymbols() == 0 && "Expected symbol-less map");
468   SmallVector<AffineExpr, 4> exprs;
469   exprs.reserve(values.size());
470   MLIRContext *ctx = getContext();
471   for (auto v : values)
472     exprs.push_back(getAffineConstantExpr(v, ctx));
473   auto resMap = compose(AffineMap::get(0, 0, exprs, ctx));
474   SmallVector<int64_t, 4> res;
475   res.reserve(resMap.getNumResults());
476   for (auto e : resMap.getResults())
477     res.push_back(e.cast<AffineConstantExpr>().getValue());
478   return res;
479 }
480 
481 bool AffineMap::isProjectedPermutation() const {
482   if (getNumSymbols() > 0)
483     return false;
484   SmallVector<bool, 8> seen(getNumInputs(), false);
485   for (auto expr : getResults()) {
486     if (auto dim = expr.dyn_cast<AffineDimExpr>()) {
487       if (seen[dim.getPosition()])
488         return false;
489       seen[dim.getPosition()] = true;
490       continue;
491     }
492     return false;
493   }
494   return true;
495 }
496 
497 bool AffineMap::isPermutation() const {
498   if (getNumDims() != getNumResults())
499     return false;
500   return isProjectedPermutation();
501 }
502 
503 AffineMap AffineMap::getSubMap(ArrayRef<unsigned> resultPos) const {
504   SmallVector<AffineExpr, 4> exprs;
505   exprs.reserve(resultPos.size());
506   for (auto idx : resultPos)
507     exprs.push_back(getResult(idx));
508   return AffineMap::get(getNumDims(), getNumSymbols(), exprs, getContext());
509 }
510 
511 AffineMap AffineMap::getSliceMap(unsigned start, unsigned length) const {
512   return AffineMap::get(getNumDims(), getNumSymbols(),
513                         getResults().slice(start, length), getContext());
514 }
515 
516 AffineMap AffineMap::getMajorSubMap(unsigned numResults) const {
517   if (numResults == 0)
518     return AffineMap();
519   if (numResults > getNumResults())
520     return *this;
521   return getSubMap(llvm::to_vector<4>(llvm::seq<unsigned>(0, numResults)));
522 }
523 
524 AffineMap AffineMap::getMinorSubMap(unsigned numResults) const {
525   if (numResults == 0)
526     return AffineMap();
527   if (numResults > getNumResults())
528     return *this;
529   return getSubMap(llvm::to_vector<4>(
530       llvm::seq<unsigned>(getNumResults() - numResults, getNumResults())));
531 }
532 
533 AffineMap mlir::compressDims(AffineMap map,
534                              const llvm::SmallDenseSet<unsigned> &unusedDims) {
535   unsigned numDims = 0;
536   SmallVector<AffineExpr> dimReplacements;
537   dimReplacements.reserve(map.getNumDims());
538   MLIRContext *context = map.getContext();
539   for (unsigned dim = 0, e = map.getNumDims(); dim < e; ++dim) {
540     if (unusedDims.contains(dim))
541       dimReplacements.push_back(getAffineConstantExpr(0, context));
542     else
543       dimReplacements.push_back(getAffineDimExpr(numDims++, context));
544   }
545   SmallVector<AffineExpr> resultExprs;
546   resultExprs.reserve(map.getNumResults());
547   for (auto e : map.getResults())
548     resultExprs.push_back(e.replaceDims(dimReplacements));
549   return AffineMap::get(numDims, map.getNumSymbols(), resultExprs, context);
550 }
551 
552 AffineMap mlir::compressUnusedDims(AffineMap map) {
553   llvm::SmallDenseSet<unsigned> usedDims;
554   map.walkExprs([&](AffineExpr expr) {
555     if (auto dimExpr = expr.dyn_cast<AffineDimExpr>())
556       usedDims.insert(dimExpr.getPosition());
557   });
558   llvm::SmallDenseSet<unsigned> unusedDims;
559   for (unsigned d = 0, e = map.getNumDims(); d != e; ++d)
560     if (!usedDims.contains(d))
561       unusedDims.insert(d);
562   return compressDims(map, unusedDims);
563 }
564 
565 static SmallVector<AffineMap>
566 compressUnusedImpl(ArrayRef<AffineMap> maps,
567                    llvm::function_ref<AffineMap(AffineMap)> compressionFun) {
568   if (maps.empty())
569     return SmallVector<AffineMap>();
570   SmallVector<AffineExpr> allExprs;
571   allExprs.reserve(maps.size() * maps.front().getNumResults());
572   unsigned numDims = maps.front().getNumDims(),
573            numSymbols = maps.front().getNumSymbols();
574   for (auto m : maps) {
575     assert(numDims == m.getNumDims() && numSymbols == m.getNumSymbols() &&
576            "expected maps with same num dims and symbols");
577     llvm::append_range(allExprs, m.getResults());
578   }
579   AffineMap unifiedMap = compressionFun(
580       AffineMap::get(numDims, numSymbols, allExprs, maps.front().getContext()));
581   unsigned unifiedNumDims = unifiedMap.getNumDims(),
582            unifiedNumSymbols = unifiedMap.getNumSymbols();
583   ArrayRef<AffineExpr> unifiedResults = unifiedMap.getResults();
584   SmallVector<AffineMap> res;
585   res.reserve(maps.size());
586   for (auto m : maps) {
587     res.push_back(AffineMap::get(unifiedNumDims, unifiedNumSymbols,
588                                  unifiedResults.take_front(m.getNumResults()),
589                                  m.getContext()));
590     unifiedResults = unifiedResults.drop_front(m.getNumResults());
591   }
592   return res;
593 }
594 
595 SmallVector<AffineMap> mlir::compressUnusedDims(ArrayRef<AffineMap> maps) {
596   return compressUnusedImpl(maps,
597                             [](AffineMap m) { return compressUnusedDims(m); });
598 }
599 
600 AffineMap
601 mlir::compressSymbols(AffineMap map,
602                       const llvm::SmallDenseSet<unsigned> &unusedSymbols) {
603   unsigned numSymbols = 0;
604   SmallVector<AffineExpr> symReplacements;
605   symReplacements.reserve(map.getNumSymbols());
606   MLIRContext *context = map.getContext();
607   for (unsigned sym = 0, e = map.getNumSymbols(); sym < e; ++sym) {
608     if (unusedSymbols.contains(sym))
609       symReplacements.push_back(getAffineConstantExpr(0, context));
610     else
611       symReplacements.push_back(getAffineSymbolExpr(numSymbols++, context));
612   }
613   SmallVector<AffineExpr> resultExprs;
614   resultExprs.reserve(map.getNumResults());
615   for (auto e : map.getResults())
616     resultExprs.push_back(e.replaceSymbols(symReplacements));
617   return AffineMap::get(map.getNumDims(), numSymbols, resultExprs, context);
618 }
619 
620 AffineMap mlir::compressUnusedSymbols(AffineMap map) {
621   llvm::SmallDenseSet<unsigned> usedSymbols;
622   map.walkExprs([&](AffineExpr expr) {
623     if (auto symExpr = expr.dyn_cast<AffineSymbolExpr>())
624       usedSymbols.insert(symExpr.getPosition());
625   });
626   llvm::SmallDenseSet<unsigned> unusedSymbols;
627   for (unsigned d = 0, e = map.getNumSymbols(); d != e; ++d)
628     if (!usedSymbols.contains(d))
629       unusedSymbols.insert(d);
630   return compressSymbols(map, unusedSymbols);
631 }
632 
633 SmallVector<AffineMap> mlir::compressUnusedSymbols(ArrayRef<AffineMap> maps) {
634   return compressUnusedImpl(
635       maps, [](AffineMap m) { return compressUnusedSymbols(m); });
636 }
637 
638 AffineMap mlir::simplifyAffineMap(AffineMap map) {
639   SmallVector<AffineExpr, 8> exprs;
640   for (auto e : map.getResults()) {
641     exprs.push_back(
642         simplifyAffineExpr(e, map.getNumDims(), map.getNumSymbols()));
643   }
644   return AffineMap::get(map.getNumDims(), map.getNumSymbols(), exprs,
645                         map.getContext());
646 }
647 
648 AffineMap mlir::removeDuplicateExprs(AffineMap map) {
649   auto results = map.getResults();
650   SmallVector<AffineExpr, 4> uniqueExprs(results.begin(), results.end());
651   uniqueExprs.erase(std::unique(uniqueExprs.begin(), uniqueExprs.end()),
652                     uniqueExprs.end());
653   return AffineMap::get(map.getNumDims(), map.getNumSymbols(), uniqueExprs,
654                         map.getContext());
655 }
656 
657 AffineMap mlir::inversePermutation(AffineMap map) {
658   if (map.isEmpty())
659     return map;
660   assert(map.getNumSymbols() == 0 && "expected map without symbols");
661   SmallVector<AffineExpr, 4> exprs(map.getNumDims());
662   for (auto en : llvm::enumerate(map.getResults())) {
663     auto expr = en.value();
664     // Skip non-permutations.
665     if (auto d = expr.dyn_cast<AffineDimExpr>()) {
666       if (exprs[d.getPosition()])
667         continue;
668       exprs[d.getPosition()] = getAffineDimExpr(en.index(), d.getContext());
669     }
670   }
671   SmallVector<AffineExpr, 4> seenExprs;
672   seenExprs.reserve(map.getNumDims());
673   for (auto expr : exprs)
674     if (expr)
675       seenExprs.push_back(expr);
676   if (seenExprs.size() != map.getNumInputs())
677     return AffineMap();
678   return AffineMap::get(map.getNumResults(), 0, seenExprs, map.getContext());
679 }
680 
681 AffineMap mlir::inverseAndBroadcastProjectedPermuation(AffineMap map) {
682   assert(map.isProjectedPermutation());
683   MLIRContext *context = map.getContext();
684   AffineExpr zero = mlir::getAffineConstantExpr(0, context);
685   // Start with all the results as 0.
686   SmallVector<AffineExpr, 4> exprs(map.getNumInputs(), zero);
687   for (unsigned i : llvm::seq(unsigned(0), map.getNumResults())) {
688     // Reverse each dimension existing in the oringal map result.
689     exprs[map.getDimPosition(i)] = getAffineDimExpr(i, context);
690   }
691   return AffineMap::get(map.getNumResults(), /*symbolCount=*/0, exprs, context);
692 }
693 
694 AffineMap mlir::concatAffineMaps(ArrayRef<AffineMap> maps) {
695   unsigned numResults = 0, numDims = 0, numSymbols = 0;
696   for (auto m : maps)
697     numResults += m.getNumResults();
698   SmallVector<AffineExpr, 8> results;
699   results.reserve(numResults);
700   for (auto m : maps) {
701     for (auto res : m.getResults())
702       results.push_back(res.shiftSymbols(m.getNumSymbols(), numSymbols));
703 
704     numSymbols += m.getNumSymbols();
705     numDims = std::max(m.getNumDims(), numDims);
706   }
707   return AffineMap::get(numDims, numSymbols, results,
708                         maps.front().getContext());
709 }
710 
711 AffineMap
712 mlir::getProjectedMap(AffineMap map,
713                       const llvm::SmallDenseSet<unsigned> &unusedDims) {
714   return compressUnusedSymbols(compressDims(map, unusedDims));
715 }
716 
717 //===----------------------------------------------------------------------===//
718 // MutableAffineMap.
719 //===----------------------------------------------------------------------===//
720 
721 MutableAffineMap::MutableAffineMap(AffineMap map)
722     : numDims(map.getNumDims()), numSymbols(map.getNumSymbols()),
723       context(map.getContext()) {
724   for (auto result : map.getResults())
725     results.push_back(result);
726 }
727 
728 void MutableAffineMap::reset(AffineMap map) {
729   results.clear();
730   numDims = map.getNumDims();
731   numSymbols = map.getNumSymbols();
732   context = map.getContext();
733   for (auto result : map.getResults())
734     results.push_back(result);
735 }
736 
737 bool MutableAffineMap::isMultipleOf(unsigned idx, int64_t factor) const {
738   if (results[idx].isMultipleOf(factor))
739     return true;
740 
741   // TODO: use simplifyAffineExpr and FlatAffineConstraints to
742   // complete this (for a more powerful analysis).
743   return false;
744 }
745 
746 // Simplifies the result affine expressions of this map. The expressions have to
747 // be pure for the simplification implemented.
748 void MutableAffineMap::simplify() {
749   // Simplify each of the results if possible.
750   // TODO: functional-style map
751   for (unsigned i = 0, e = getNumResults(); i < e; i++) {
752     results[i] = simplifyAffineExpr(getResult(i), numDims, numSymbols);
753   }
754 }
755 
756 AffineMap MutableAffineMap::getAffineMap() const {
757   return AffineMap::get(numDims, numSymbols, results, context);
758 }
759