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::getSliceMap(unsigned start, unsigned length) const {
498   return AffineMap::get(getNumDims(), getNumSymbols(),
499                         getResults().slice(start, length), getContext());
500 }
501 
502 AffineMap AffineMap::getMajorSubMap(unsigned numResults) const {
503   if (numResults == 0)
504     return AffineMap();
505   if (numResults > getNumResults())
506     return *this;
507   return getSubMap(llvm::to_vector<4>(llvm::seq<unsigned>(0, numResults)));
508 }
509 
510 AffineMap AffineMap::getMinorSubMap(unsigned numResults) const {
511   if (numResults == 0)
512     return AffineMap();
513   if (numResults > getNumResults())
514     return *this;
515   return getSubMap(llvm::to_vector<4>(
516       llvm::seq<unsigned>(getNumResults() - numResults, getNumResults())));
517 }
518 
519 AffineMap mlir::compressDims(AffineMap map,
520                              const llvm::SmallDenseSet<unsigned> &unusedDims) {
521   unsigned numDims = 0;
522   SmallVector<AffineExpr> dimReplacements;
523   dimReplacements.reserve(map.getNumDims());
524   MLIRContext *context = map.getContext();
525   for (unsigned dim = 0, e = map.getNumDims(); dim < e; ++dim) {
526     if (unusedDims.contains(dim))
527       dimReplacements.push_back(getAffineConstantExpr(0, context));
528     else
529       dimReplacements.push_back(getAffineDimExpr(numDims++, context));
530   }
531   SmallVector<AffineExpr> resultExprs;
532   resultExprs.reserve(map.getNumResults());
533   for (auto e : map.getResults())
534     resultExprs.push_back(e.replaceDims(dimReplacements));
535   return AffineMap::get(numDims, map.getNumSymbols(), resultExprs, context);
536 }
537 
538 AffineMap mlir::compressUnusedDims(AffineMap map) {
539   llvm::SmallDenseSet<unsigned> usedDims;
540   map.walkExprs([&](AffineExpr expr) {
541     if (auto dimExpr = expr.dyn_cast<AffineDimExpr>())
542       usedDims.insert(dimExpr.getPosition());
543   });
544   llvm::SmallDenseSet<unsigned> unusedDims;
545   for (unsigned d = 0, e = map.getNumDims(); d != e; ++d)
546     if (!usedDims.contains(d))
547       unusedDims.insert(d);
548   return compressDims(map, unusedDims);
549 }
550 
551 static SmallVector<AffineMap>
552 compressUnusedImpl(ArrayRef<AffineMap> maps,
553                    llvm::function_ref<AffineMap(AffineMap)> compressionFun) {
554   if (maps.empty())
555     return SmallVector<AffineMap>();
556   SmallVector<AffineExpr> allExprs;
557   allExprs.reserve(maps.size() * maps.front().getNumResults());
558   unsigned numDims = maps.front().getNumDims(),
559            numSymbols = maps.front().getNumSymbols();
560   for (auto m : maps) {
561     assert(numDims == m.getNumDims() && numSymbols == m.getNumSymbols() &&
562            "expected maps with same num dims and symbols");
563     llvm::append_range(allExprs, m.getResults());
564   }
565   AffineMap unifiedMap = compressionFun(
566       AffineMap::get(numDims, numSymbols, allExprs, maps.front().getContext()));
567   unsigned unifiedNumDims = unifiedMap.getNumDims(),
568            unifiedNumSymbols = unifiedMap.getNumSymbols();
569   ArrayRef<AffineExpr> unifiedResults = unifiedMap.getResults();
570   SmallVector<AffineMap> res;
571   res.reserve(maps.size());
572   for (auto m : maps) {
573     res.push_back(AffineMap::get(unifiedNumDims, unifiedNumSymbols,
574                                  unifiedResults.take_front(m.getNumResults()),
575                                  m.getContext()));
576     unifiedResults = unifiedResults.drop_front(m.getNumResults());
577   }
578   return res;
579 }
580 
581 SmallVector<AffineMap> mlir::compressUnusedDims(ArrayRef<AffineMap> maps) {
582   return compressUnusedImpl(maps,
583                             [](AffineMap m) { return compressUnusedDims(m); });
584 }
585 
586 AffineMap
587 mlir::compressSymbols(AffineMap map,
588                       const llvm::SmallDenseSet<unsigned> &unusedSymbols) {
589   unsigned numSymbols = 0;
590   SmallVector<AffineExpr> symReplacements;
591   symReplacements.reserve(map.getNumSymbols());
592   MLIRContext *context = map.getContext();
593   for (unsigned sym = 0, e = map.getNumSymbols(); sym < e; ++sym) {
594     if (unusedSymbols.contains(sym))
595       symReplacements.push_back(getAffineConstantExpr(0, context));
596     else
597       symReplacements.push_back(getAffineSymbolExpr(numSymbols++, context));
598   }
599   SmallVector<AffineExpr> resultExprs;
600   resultExprs.reserve(map.getNumResults());
601   for (auto e : map.getResults())
602     resultExprs.push_back(e.replaceSymbols(symReplacements));
603   return AffineMap::get(map.getNumDims(), numSymbols, resultExprs, context);
604 }
605 
606 AffineMap mlir::compressUnusedSymbols(AffineMap map) {
607   llvm::SmallDenseSet<unsigned> usedSymbols;
608   map.walkExprs([&](AffineExpr expr) {
609     if (auto symExpr = expr.dyn_cast<AffineSymbolExpr>())
610       usedSymbols.insert(symExpr.getPosition());
611   });
612   llvm::SmallDenseSet<unsigned> unusedSymbols;
613   for (unsigned d = 0, e = map.getNumSymbols(); d != e; ++d)
614     if (!usedSymbols.contains(d))
615       unusedSymbols.insert(d);
616   return compressSymbols(map, unusedSymbols);
617 }
618 
619 SmallVector<AffineMap> mlir::compressUnusedSymbols(ArrayRef<AffineMap> maps) {
620   return compressUnusedImpl(
621       maps, [](AffineMap m) { return compressUnusedSymbols(m); });
622 }
623 
624 AffineMap mlir::simplifyAffineMap(AffineMap map) {
625   SmallVector<AffineExpr, 8> exprs;
626   for (auto e : map.getResults()) {
627     exprs.push_back(
628         simplifyAffineExpr(e, map.getNumDims(), map.getNumSymbols()));
629   }
630   return AffineMap::get(map.getNumDims(), map.getNumSymbols(), exprs,
631                         map.getContext());
632 }
633 
634 AffineMap mlir::removeDuplicateExprs(AffineMap map) {
635   auto results = map.getResults();
636   SmallVector<AffineExpr, 4> uniqueExprs(results.begin(), results.end());
637   uniqueExprs.erase(std::unique(uniqueExprs.begin(), uniqueExprs.end()),
638                     uniqueExprs.end());
639   return AffineMap::get(map.getNumDims(), map.getNumSymbols(), uniqueExprs,
640                         map.getContext());
641 }
642 
643 AffineMap mlir::inversePermutation(AffineMap map) {
644   if (map.isEmpty())
645     return map;
646   assert(map.getNumSymbols() == 0 && "expected map without symbols");
647   SmallVector<AffineExpr, 4> exprs(map.getNumDims());
648   for (auto en : llvm::enumerate(map.getResults())) {
649     auto expr = en.value();
650     // Skip non-permutations.
651     if (auto d = expr.dyn_cast<AffineDimExpr>()) {
652       if (exprs[d.getPosition()])
653         continue;
654       exprs[d.getPosition()] = getAffineDimExpr(en.index(), d.getContext());
655     }
656   }
657   SmallVector<AffineExpr, 4> seenExprs;
658   seenExprs.reserve(map.getNumDims());
659   for (auto expr : exprs)
660     if (expr)
661       seenExprs.push_back(expr);
662   if (seenExprs.size() != map.getNumInputs())
663     return AffineMap();
664   return AffineMap::get(map.getNumResults(), 0, seenExprs, map.getContext());
665 }
666 
667 AffineMap mlir::concatAffineMaps(ArrayRef<AffineMap> maps) {
668   unsigned numResults = 0, numDims = 0, numSymbols = 0;
669   for (auto m : maps)
670     numResults += m.getNumResults();
671   SmallVector<AffineExpr, 8> results;
672   results.reserve(numResults);
673   for (auto m : maps) {
674     for (auto res : m.getResults())
675       results.push_back(res.shiftSymbols(m.getNumSymbols(), numSymbols));
676 
677     numSymbols += m.getNumSymbols();
678     numDims = std::max(m.getNumDims(), numDims);
679   }
680   return AffineMap::get(numDims, numSymbols, results,
681                         maps.front().getContext());
682 }
683 
684 AffineMap
685 mlir::getProjectedMap(AffineMap map,
686                       const llvm::SmallDenseSet<unsigned> &unusedDims) {
687   return compressUnusedSymbols(compressDims(map, unusedDims));
688 }
689 
690 //===----------------------------------------------------------------------===//
691 // MutableAffineMap.
692 //===----------------------------------------------------------------------===//
693 
694 MutableAffineMap::MutableAffineMap(AffineMap map)
695     : numDims(map.getNumDims()), numSymbols(map.getNumSymbols()),
696       context(map.getContext()) {
697   for (auto result : map.getResults())
698     results.push_back(result);
699 }
700 
701 void MutableAffineMap::reset(AffineMap map) {
702   results.clear();
703   numDims = map.getNumDims();
704   numSymbols = map.getNumSymbols();
705   context = map.getContext();
706   for (auto result : map.getResults())
707     results.push_back(result);
708 }
709 
710 bool MutableAffineMap::isMultipleOf(unsigned idx, int64_t factor) const {
711   if (results[idx].isMultipleOf(factor))
712     return true;
713 
714   // TODO: use simplifyAffineExpr and FlatAffineConstraints to
715   // complete this (for a more powerful analysis).
716   return false;
717 }
718 
719 // Simplifies the result affine expressions of this map. The expressions have to
720 // be pure for the simplification implemented.
721 void MutableAffineMap::simplify() {
722   // Simplify each of the results if possible.
723   // TODO: functional-style map
724   for (unsigned i = 0, e = getNumResults(); i < e; i++) {
725     results[i] = simplifyAffineExpr(getResult(i), numDims, numSymbols);
726   }
727 }
728 
729 AffineMap MutableAffineMap::getAffineMap() const {
730   return AffineMap::get(numDims, numSymbols, results, context);
731 }
732