MLIR  20.0.0git
AffineMap.cpp
Go to the documentation of this file.
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/AffineExpr.h"
12 #include "mlir/IR/Builders.h"
14 #include "mlir/IR/BuiltinTypes.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/SmallBitVector.h"
17 #include "llvm/ADT/SmallSet.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/Support/MathExtras.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include <iterator>
23 #include <numeric>
24 #include <optional>
25 #include <type_traits>
26 
27 using namespace mlir;
28 
29 using llvm::divideCeilSigned;
30 using llvm::divideFloorSigned;
31 using llvm::mod;
32 
33 namespace {
34 
35 // AffineExprConstantFolder evaluates an affine expression using constant
36 // operands passed in 'operandConsts'. Returns an IntegerAttr attribute
37 // representing the constant value of the affine expression evaluated on
38 // constant 'operandConsts', or nullptr if it can't be folded.
39 class AffineExprConstantFolder {
40 public:
41  AffineExprConstantFolder(unsigned numDims, ArrayRef<Attribute> operandConsts)
42  : numDims(numDims), operandConsts(operandConsts) {}
43 
44  /// Attempt to constant fold the specified affine expr, or return null on
45  /// failure.
46  IntegerAttr constantFold(AffineExpr expr) {
47  if (auto result = constantFoldImpl(expr))
48  return IntegerAttr::get(IndexType::get(expr.getContext()), *result);
49  return nullptr;
50  }
51 
52  bool hasPoison() const { return hasPoison_; }
53 
54 private:
55  std::optional<int64_t> constantFoldImpl(AffineExpr expr) {
56  switch (expr.getKind()) {
58  return constantFoldBinExpr(
59  expr, [](int64_t lhs, int64_t rhs) { return lhs + rhs; });
61  return constantFoldBinExpr(
62  expr, [](int64_t lhs, int64_t rhs) { return lhs * rhs; });
64  return constantFoldBinExpr(
65  expr, [this](int64_t lhs, int64_t rhs) -> std::optional<int64_t> {
66  if (rhs < 1) {
67  hasPoison_ = true;
68  return std::nullopt;
69  }
70  return mod(lhs, rhs);
71  });
73  return constantFoldBinExpr(
74  expr, [this](int64_t lhs, int64_t rhs) -> std::optional<int64_t> {
75  if (rhs == 0) {
76  hasPoison_ = true;
77  return std::nullopt;
78  }
79  return divideFloorSigned(lhs, rhs);
80  });
82  return constantFoldBinExpr(
83  expr, [this](int64_t lhs, int64_t rhs) -> std::optional<int64_t> {
84  if (rhs == 0) {
85  hasPoison_ = true;
86  return std::nullopt;
87  }
88  return divideCeilSigned(lhs, rhs);
89  });
91  return cast<AffineConstantExpr>(expr).getValue();
93  if (auto attr = llvm::dyn_cast_or_null<IntegerAttr>(
94  operandConsts[cast<AffineDimExpr>(expr).getPosition()]))
95  return attr.getInt();
96  return std::nullopt;
98  if (auto attr = llvm::dyn_cast_or_null<IntegerAttr>(
99  operandConsts[numDims +
100  cast<AffineSymbolExpr>(expr).getPosition()]))
101  return attr.getInt();
102  return std::nullopt;
103  }
104  llvm_unreachable("Unknown AffineExpr");
105  }
106 
107  // TODO: Change these to operate on APInts too.
108  std::optional<int64_t> constantFoldBinExpr(
109  AffineExpr expr,
110  llvm::function_ref<std::optional<int64_t>(int64_t, int64_t)> op) {
111  auto binOpExpr = cast<AffineBinaryOpExpr>(expr);
112  if (auto lhs = constantFoldImpl(binOpExpr.getLHS()))
113  if (auto rhs = constantFoldImpl(binOpExpr.getRHS()))
114  return op(*lhs, *rhs);
115  return std::nullopt;
116  }
117 
118  // The number of dimension operands in AffineMap containing this expression.
119  unsigned numDims;
120  // The constant valued operands used to evaluate this AffineExpr.
121  ArrayRef<Attribute> operandConsts;
122  bool hasPoison_{false};
123 };
124 
125 } // namespace
126 
127 /// Returns a single constant result affine map.
129  return get(/*dimCount=*/0, /*symbolCount=*/0,
130  {getAffineConstantExpr(val, context)});
131 }
132 
133 /// Returns an identity affine map (d0, ..., dn) -> (dp, ..., dn) on the most
134 /// minor dimensions.
135 AffineMap AffineMap::getMinorIdentityMap(unsigned dims, unsigned results,
136  MLIRContext *context) {
137  assert(dims >= results && "Dimension mismatch");
138  auto id = AffineMap::getMultiDimIdentityMap(dims, context);
139  return AffineMap::get(dims, 0, id.getResults().take_back(results), context);
140 }
141 
143  MLIRContext *ctx, unsigned numDims,
144  llvm::function_ref<bool(AffineDimExpr)> keepDimFilter) {
145  auto identityMap = getMultiDimIdentityMap(numDims, ctx);
146 
147  // Apply filter to results.
148  llvm::SmallBitVector dropDimResults(numDims);
149  for (auto [idx, resultExpr] : llvm::enumerate(identityMap.getResults()))
150  dropDimResults[idx] = !keepDimFilter(cast<AffineDimExpr>(resultExpr));
151 
152  return identityMap.dropResults(dropDimResults);
153 }
154 
156  return getNumDims() >= getNumResults() &&
157  *this ==
159 }
160 
162  SmallVector<unsigned> broadcastedDims;
163  for (const auto &[resIdx, expr] : llvm::enumerate(getResults())) {
164  if (auto constExpr = dyn_cast<AffineConstantExpr>(expr)) {
165  if (constExpr.getValue() != 0)
166  continue;
167  broadcastedDims.push_back(resIdx);
168  }
169  }
170 
171  return broadcastedDims;
172 }
173 
174 /// Returns true if this affine map is a minor identity up to broadcasted
175 /// dimensions which are indicated by value 0 in the result.
177  SmallVectorImpl<unsigned> *broadcastedDims) const {
178  if (broadcastedDims)
179  broadcastedDims->clear();
180  if (getNumDims() < getNumResults())
181  return false;
182  unsigned suffixStart = getNumDims() - getNumResults();
183  for (const auto &idxAndExpr : llvm::enumerate(getResults())) {
184  unsigned resIdx = idxAndExpr.index();
185  AffineExpr expr = idxAndExpr.value();
186  if (auto constExpr = dyn_cast<AffineConstantExpr>(expr)) {
187  // Each result may be either a constant 0 (broadcasted dimension).
188  if (constExpr.getValue() != 0)
189  return false;
190  if (broadcastedDims)
191  broadcastedDims->push_back(resIdx);
192  } else if (auto dimExpr = dyn_cast<AffineDimExpr>(expr)) {
193  // Or it may be the input dimension corresponding to this result position.
194  if (dimExpr.getPosition() != suffixStart + resIdx)
195  return false;
196  } else {
197  return false;
198  }
199  }
200  return true;
201 }
202 
203 /// Return true if this affine map can be converted to a minor identity with
204 /// broadcast by doing a permute. Return a permutation (there may be
205 /// several) to apply to get to a minor identity with broadcasts.
206 /// Ex:
207 /// * (d0, d1, d2) -> (0, d1) maps to minor identity (d1, 0 = d2) with
208 /// perm = [1, 0] and broadcast d2
209 /// * (d0, d1, d2) -> (d0, 0) cannot be mapped to a minor identity by
210 /// permutation + broadcast
211 /// * (d0, d1, d2, d3) -> (0, d1, d3) maps to minor identity (d1, 0 = d2, d3)
212 /// with perm = [1, 0, 2] and broadcast d2
213 /// * (d0, d1) -> (d1, 0, 0, d0) maps to minor identity (d0, d1) with extra
214 /// leading broadcat dimensions. The map returned would be (0, 0, d0, d1) with
215 /// perm = [3, 0, 1, 2]
217  SmallVectorImpl<unsigned> &permutedDims) const {
218  unsigned projectionStart =
220  permutedDims.clear();
221  SmallVector<unsigned> broadcastDims;
222  permutedDims.resize(getNumResults(), 0);
223  // If there are more results than input dimensions we want the new map to
224  // start with broadcast dimensions in order to be a minor identity with
225  // broadcasting.
226  unsigned leadingBroadcast =
228  llvm::SmallBitVector dimFound(std::max(getNumInputs(), getNumResults()),
229  false);
230  for (const auto &idxAndExpr : llvm::enumerate(getResults())) {
231  unsigned resIdx = idxAndExpr.index();
232  AffineExpr expr = idxAndExpr.value();
233  // Each result may be either a constant 0 (broadcast dimension) or a
234  // dimension.
235  if (auto constExpr = dyn_cast<AffineConstantExpr>(expr)) {
236  if (constExpr.getValue() != 0)
237  return false;
238  broadcastDims.push_back(resIdx);
239  } else if (auto dimExpr = dyn_cast<AffineDimExpr>(expr)) {
240  if (dimExpr.getPosition() < projectionStart)
241  return false;
242  unsigned newPosition =
243  dimExpr.getPosition() - projectionStart + leadingBroadcast;
244  permutedDims[resIdx] = newPosition;
245  dimFound[newPosition] = true;
246  } else {
247  return false;
248  }
249  }
250  // Find a permuation for the broadcast dimension. Since they are broadcasted
251  // any valid permutation is acceptable. We just permute the dim into a slot
252  // without an existing dimension.
253  unsigned pos = 0;
254  for (auto dim : broadcastDims) {
255  while (pos < dimFound.size() && dimFound[pos]) {
256  pos++;
257  }
258  permutedDims[dim] = pos++;
259  }
260  return true;
261 }
262 
263 /// Returns an AffineMap representing a permutation.
265  MLIRContext *context) {
266  assert(!permutation.empty() &&
267  "Cannot create permutation map from empty permutation vector");
268  const auto *m = llvm::max_element(permutation);
269  auto permutationMap = getMultiDimMapWithTargets(*m + 1, permutation, context);
270  assert(permutationMap.isPermutation() && "Invalid permutation vector");
271  return permutationMap;
272 }
274  MLIRContext *context) {
275  SmallVector<unsigned> perm = llvm::map_to_vector(
276  permutation, [](int64_t i) { return static_cast<unsigned>(i); });
277  return AffineMap::getPermutationMap(perm, context);
278 }
279 
281  ArrayRef<unsigned> targets,
282  MLIRContext *context) {
284  for (unsigned t : targets)
285  affExprs.push_back(getAffineDimExpr(t, context));
286  AffineMap result = AffineMap::get(/*dimCount=*/numDims, /*symbolCount=*/0,
287  affExprs, context);
288  return result;
289 }
290 
291 /// Creates an affine map each for each list of AffineExpr's in `exprsList`
292 /// while inferring the right number of dimensional and symbolic inputs needed
293 /// based on the maximum dimensional and symbolic identifier appearing in the
294 /// expressions.
295 template <typename AffineExprContainer>
298  MLIRContext *context) {
299  if (exprsList.empty())
300  return {};
301  int64_t maxDim = -1, maxSym = -1;
302  getMaxDimAndSymbol(exprsList, maxDim, maxSym);
304  maps.reserve(exprsList.size());
305  for (const auto &exprs : exprsList)
306  maps.push_back(AffineMap::get(/*dimCount=*/maxDim + 1,
307  /*symbolCount=*/maxSym + 1, exprs, context));
308  return maps;
309 }
310 
313  MLIRContext *context) {
314  return ::inferFromExprList(exprsList, context);
315 }
316 
319  MLIRContext *context) {
320  return ::inferFromExprList(exprsList, context);
321 }
322 
324  uint64_t gcd = 0;
325  for (AffineExpr resultExpr : getResults()) {
326  uint64_t thisGcd = resultExpr.getLargestKnownDivisor();
327  gcd = std::gcd(gcd, thisGcd);
328  }
329  if (gcd == 0)
331  return gcd;
332 }
333 
335  MLIRContext *context) {
337  dimExprs.reserve(numDims);
338  for (unsigned i = 0; i < numDims; ++i)
339  dimExprs.push_back(mlir::getAffineDimExpr(i, context));
340  return get(/*dimCount=*/numDims, /*symbolCount=*/0, dimExprs, context);
341 }
342 
343 MLIRContext *AffineMap::getContext() const { return map->context; }
344 
345 bool AffineMap::isIdentity() const {
346  if (getNumDims() != getNumResults())
347  return false;
348  ArrayRef<AffineExpr> results = getResults();
349  for (unsigned i = 0, numDims = getNumDims(); i < numDims; ++i) {
350  auto expr = dyn_cast<AffineDimExpr>(results[i]);
351  if (!expr || expr.getPosition() != i)
352  return false;
353  }
354  return true;
355 }
356 
358  if (getNumSymbols() != getNumResults())
359  return false;
360  ArrayRef<AffineExpr> results = getResults();
361  for (unsigned i = 0, numSymbols = getNumSymbols(); i < numSymbols; ++i) {
362  auto expr = dyn_cast<AffineDimExpr>(results[i]);
363  if (!expr || expr.getPosition() != i)
364  return false;
365  }
366  return true;
367 }
368 
369 bool AffineMap::isEmpty() const {
370  return getNumDims() == 0 && getNumSymbols() == 0 && getNumResults() == 0;
371 }
372 
374  return getNumResults() == 1 && isa<AffineConstantExpr>(getResult(0));
375 }
376 
377 bool AffineMap::isConstant() const {
378  return llvm::all_of(getResults(), llvm::IsaPred<AffineConstantExpr>);
379 }
380 
382  assert(isSingleConstant() && "map must have a single constant result");
383  return cast<AffineConstantExpr>(getResult(0)).getValue();
384 }
385 
387  assert(isConstant() && "map must have only constant results");
388  SmallVector<int64_t> result;
389  for (auto expr : getResults())
390  result.emplace_back(cast<AffineConstantExpr>(expr).getValue());
391  return result;
392 }
393 
394 unsigned AffineMap::getNumDims() const {
395  assert(map && "uninitialized map storage");
396  return map->numDims;
397 }
398 unsigned AffineMap::getNumSymbols() const {
399  assert(map && "uninitialized map storage");
400  return map->numSymbols;
401 }
402 unsigned AffineMap::getNumResults() const { return getResults().size(); }
403 unsigned AffineMap::getNumInputs() const {
404  assert(map && "uninitialized map storage");
405  return map->numDims + map->numSymbols;
406 }
408  assert(map && "uninitialized map storage");
409  return map->results();
410 }
411 AffineExpr AffineMap::getResult(unsigned idx) const {
412  return getResults()[idx];
413 }
414 
415 unsigned AffineMap::getDimPosition(unsigned idx) const {
416  return cast<AffineDimExpr>(getResult(idx)).getPosition();
417 }
418 
419 std::optional<unsigned> AffineMap::getResultPosition(AffineExpr input) const {
420  if (!isa<AffineDimExpr>(input))
421  return std::nullopt;
422 
423  for (unsigned i = 0, numResults = getNumResults(); i < numResults; i++) {
424  if (getResult(i) == input)
425  return i;
426  }
427 
428  return std::nullopt;
429 }
430 
431 /// Folds the results of the application of an affine map on the provided
432 /// operands to a constant if possible. Returns false if the folding happens,
433 /// true otherwise.
434 LogicalResult AffineMap::constantFold(ArrayRef<Attribute> operandConstants,
436  bool *hasPoison) const {
437  // Attempt partial folding.
438  SmallVector<int64_t, 2> integers;
439  partialConstantFold(operandConstants, &integers, hasPoison);
440 
441  // If all expressions folded to a constant, populate results with attributes
442  // containing those constants.
443  if (integers.empty())
444  return failure();
445 
446  auto range = llvm::map_range(integers, [this](int64_t i) {
448  });
449  results.append(range.begin(), range.end());
450  return success();
451 }
452 
454  SmallVectorImpl<int64_t> *results,
455  bool *hasPoison) const {
456  assert(getNumInputs() == operandConstants.size());
457 
458  // Fold each of the result expressions.
459  AffineExprConstantFolder exprFolder(getNumDims(), operandConstants);
461  exprs.reserve(getNumResults());
462 
463  for (auto expr : getResults()) {
464  auto folded = exprFolder.constantFold(expr);
465  if (exprFolder.hasPoison() && hasPoison) {
466  *hasPoison = true;
467  return {};
468  }
469  // If did not fold to a constant, keep the original expression, and clear
470  // the integer results vector.
471  if (folded) {
472  exprs.push_back(
473  getAffineConstantExpr(folded.getInt(), folded.getContext()));
474  if (results)
475  results->push_back(folded.getInt());
476  } else {
477  exprs.push_back(expr);
478  if (results) {
479  results->clear();
480  results = nullptr;
481  }
482  }
483  }
484 
485  return get(getNumDims(), getNumSymbols(), exprs, getContext());
486 }
487 
488 /// Walk all of the AffineExpr's in this mapping. Each node in an expression
489 /// tree is visited in postorder.
491  for (auto expr : getResults())
492  expr.walk(callback);
493 }
494 
495 /// This method substitutes any uses of dimensions and symbols (e.g.
496 /// dim#0 with dimReplacements[0]) in subexpressions and returns the modified
497 /// expression mapping. Because this can be used to eliminate dims and
498 /// symbols, the client needs to specify the number of dims and symbols in
499 /// the result. The returned map always has the same number of results.
501  ArrayRef<AffineExpr> symReplacements,
502  unsigned numResultDims,
503  unsigned numResultSyms) const {
505  results.reserve(getNumResults());
506  for (auto expr : getResults())
507  results.push_back(
508  expr.replaceDimsAndSymbols(dimReplacements, symReplacements));
509  return get(numResultDims, numResultSyms, results, getContext());
510 }
511 
512 /// Sparse replace method. Apply AffineExpr::replace(`expr`, `replacement`) to
513 /// each of the results and return a new AffineMap with the new results and
514 /// with the specified number of dims and symbols.
516  unsigned numResultDims,
517  unsigned numResultSyms) const {
518  SmallVector<AffineExpr, 4> newResults;
519  newResults.reserve(getNumResults());
520  for (AffineExpr e : getResults())
521  newResults.push_back(e.replace(expr, replacement));
522  return AffineMap::get(numResultDims, numResultSyms, newResults, getContext());
523 }
524 
525 /// Sparse replace method. Apply AffineExpr::replace(`map`) to each of the
526 /// results and return a new AffineMap with the new results and with the
527 /// specified number of dims and symbols.
529  unsigned numResultDims,
530  unsigned numResultSyms) const {
531  SmallVector<AffineExpr, 4> newResults;
532  newResults.reserve(getNumResults());
533  for (AffineExpr e : getResults())
534  newResults.push_back(e.replace(map));
535  return AffineMap::get(numResultDims, numResultSyms, newResults, getContext());
536 }
537 
538 AffineMap
540  SmallVector<AffineExpr, 4> newResults;
541  newResults.reserve(getNumResults());
542  for (AffineExpr e : getResults())
543  newResults.push_back(e.replace(map));
544  return AffineMap::inferFromExprList(newResults, getContext()).front();
545 }
546 
547 AffineMap AffineMap::dropResults(const llvm::SmallBitVector &positions) const {
548  auto exprs = llvm::to_vector<4>(getResults());
549  // TODO: this is a pretty terrible API .. is there anything better?
550  for (auto pos = positions.find_last(); pos != -1;
551  pos = positions.find_prev(pos))
552  exprs.erase(exprs.begin() + pos);
553  return AffineMap::get(getNumDims(), getNumSymbols(), exprs, getContext());
554 }
555 
557  assert(getNumDims() == map.getNumResults() && "Number of results mismatch");
558  // Prepare `map` by concatenating the symbols and rewriting its exprs.
559  unsigned numDims = map.getNumDims();
560  unsigned numSymbolsThisMap = getNumSymbols();
561  unsigned numSymbols = numSymbolsThisMap + map.getNumSymbols();
562  SmallVector<AffineExpr, 8> newDims(numDims);
563  for (unsigned idx = 0; idx < numDims; ++idx) {
564  newDims[idx] = getAffineDimExpr(idx, getContext());
565  }
566  SmallVector<AffineExpr, 8> newSymbols(numSymbols - numSymbolsThisMap);
567  for (unsigned idx = numSymbolsThisMap; idx < numSymbols; ++idx) {
568  newSymbols[idx - numSymbolsThisMap] =
570  }
571  auto newMap =
572  map.replaceDimsAndSymbols(newDims, newSymbols, numDims, numSymbols);
574  exprs.reserve(getResults().size());
575  for (auto expr : getResults())
576  exprs.push_back(expr.compose(newMap));
577  return AffineMap::get(numDims, numSymbols, exprs, map.getContext());
578 }
579 
581  assert(getNumSymbols() == 0 && "Expected symbol-less map");
583  exprs.reserve(values.size());
584  MLIRContext *ctx = getContext();
585  for (auto v : values)
586  exprs.push_back(getAffineConstantExpr(v, ctx));
587  auto resMap = compose(AffineMap::get(0, 0, exprs, ctx));
589  res.reserve(resMap.getNumResults());
590  for (auto e : resMap.getResults())
591  res.push_back(cast<AffineConstantExpr>(e).getValue());
592  return res;
593 }
594 
595 bool AffineMap::isProjectedPermutation(bool allowZeroInResults) const {
596  if (getNumSymbols() > 0)
597  return false;
598 
599  // Having more results than inputs means that results have duplicated dims or
600  // zeros that can't be mapped to input dims.
601  if (getNumResults() > getNumInputs())
602  return false;
603 
604  SmallVector<bool, 8> seen(getNumInputs(), false);
605  // A projected permutation can have, at most, only one instance of each input
606  // dimension in the result expressions. Zeros are allowed as long as the
607  // number of result expressions is lower or equal than the number of input
608  // expressions.
609  for (auto expr : getResults()) {
610  if (auto dim = dyn_cast<AffineDimExpr>(expr)) {
611  if (seen[dim.getPosition()])
612  return false;
613  seen[dim.getPosition()] = true;
614  } else {
615  auto constExpr = dyn_cast<AffineConstantExpr>(expr);
616  if (!allowZeroInResults || !constExpr || constExpr.getValue() != 0)
617  return false;
618  }
619  }
620 
621  // Results are either dims or zeros and zeros can be mapped to input dims.
622  return true;
623 }
624 
626  if (getNumDims() != getNumResults())
627  return false;
628  return isProjectedPermutation();
629 }
630 
633  exprs.reserve(resultPos.size());
634  for (auto idx : resultPos)
635  exprs.push_back(getResult(idx));
636  return AffineMap::get(getNumDims(), getNumSymbols(), exprs, getContext());
637 }
638 
639 AffineMap AffineMap::getSliceMap(unsigned start, unsigned length) const {
641  getResults().slice(start, length), getContext());
642 }
643 
644 AffineMap AffineMap::getMajorSubMap(unsigned numResults) const {
645  if (numResults == 0)
646  return AffineMap();
647  if (numResults > getNumResults())
648  return *this;
649  return getSliceMap(0, numResults);
650 }
651 
652 AffineMap AffineMap::getMinorSubMap(unsigned numResults) const {
653  if (numResults == 0)
654  return AffineMap();
655  if (numResults > getNumResults())
656  return *this;
657  return getSliceMap(getNumResults() - numResults, numResults);
658 }
659 
660 /// Implementation detail to compress multiple affine maps with a compressionFun
661 /// that is expected to be either compressUnusedDims or compressUnusedSymbols.
662 /// The implementation keeps track of num dims and symbols across the different
663 /// affine maps.
665  ArrayRef<AffineMap> maps,
666  llvm::function_ref<AffineMap(AffineMap)> compressionFun) {
667  if (maps.empty())
668  return SmallVector<AffineMap>();
669  SmallVector<AffineExpr> allExprs;
670  allExprs.reserve(maps.size() * maps.front().getNumResults());
671  unsigned numDims = maps.front().getNumDims(),
672  numSymbols = maps.front().getNumSymbols();
673  for (auto m : maps) {
674  assert(numDims == m.getNumDims() && numSymbols == m.getNumSymbols() &&
675  "expected maps with same num dims and symbols");
676  llvm::append_range(allExprs, m.getResults());
677  }
678  AffineMap unifiedMap = compressionFun(
679  AffineMap::get(numDims, numSymbols, allExprs, maps.front().getContext()));
680  unsigned unifiedNumDims = unifiedMap.getNumDims(),
681  unifiedNumSymbols = unifiedMap.getNumSymbols();
682  ArrayRef<AffineExpr> unifiedResults = unifiedMap.getResults();
684  res.reserve(maps.size());
685  for (auto m : maps) {
686  res.push_back(AffineMap::get(unifiedNumDims, unifiedNumSymbols,
687  unifiedResults.take_front(m.getNumResults()),
688  m.getContext()));
689  unifiedResults = unifiedResults.drop_front(m.getNumResults());
690  }
691  return res;
692 }
693 
695  const llvm::SmallBitVector &unusedDims) {
696  return projectDims(map, unusedDims, /*compressDimsFlag=*/true);
697 }
698 
700  return compressDims(map, getUnusedDimsBitVector({map}));
701 }
702 
704  return compressUnusedListImpl(
705  maps, [](AffineMap m) { return compressUnusedDims(m); });
706 }
707 
709  const llvm::SmallBitVector &unusedSymbols) {
710  return projectSymbols(map, unusedSymbols, /*compressSymbolsFlag=*/true);
711 }
712 
714  return compressSymbols(map, getUnusedSymbolsBitVector({map}));
715 }
716 
718  return compressUnusedListImpl(
719  maps, [](AffineMap m) { return compressUnusedSymbols(m); });
720 }
721 
723  ArrayRef<OpFoldResult> operands,
724  SmallVector<Value> &remainingValues) {
725  SmallVector<AffineExpr> dimReplacements, symReplacements;
726  int64_t numDims = 0;
727  for (int64_t i = 0; i < map.getNumDims(); ++i) {
728  if (auto attr = operands[i].dyn_cast<Attribute>()) {
729  dimReplacements.push_back(
730  b.getAffineConstantExpr(cast<IntegerAttr>(attr).getInt()));
731  } else {
732  dimReplacements.push_back(b.getAffineDimExpr(numDims++));
733  remainingValues.push_back(operands[i].get<Value>());
734  }
735  }
736  int64_t numSymbols = 0;
737  for (int64_t i = 0; i < map.getNumSymbols(); ++i) {
738  if (auto attr = operands[i + map.getNumDims()].dyn_cast<Attribute>()) {
739  symReplacements.push_back(
740  b.getAffineConstantExpr(cast<IntegerAttr>(attr).getInt()));
741  } else {
742  symReplacements.push_back(b.getAffineSymbolExpr(numSymbols++));
743  remainingValues.push_back(operands[i + map.getNumDims()].get<Value>());
744  }
745  }
746  return map.replaceDimsAndSymbols(dimReplacements, symReplacements, numDims,
747  numSymbols);
748 }
749 
752  for (auto e : map.getResults()) {
753  exprs.push_back(
754  simplifyAffineExpr(e, map.getNumDims(), map.getNumSymbols()));
755  }
756  return AffineMap::get(map.getNumDims(), map.getNumSymbols(), exprs,
757  map.getContext());
758 }
759 
761  auto results = map.getResults();
762  SmallVector<AffineExpr, 4> uniqueExprs(results.begin(), results.end());
763  uniqueExprs.erase(llvm::unique(uniqueExprs), uniqueExprs.end());
764  return AffineMap::get(map.getNumDims(), map.getNumSymbols(), uniqueExprs,
765  map.getContext());
766 }
767 
769  if (map.isEmpty())
770  return map;
771  assert(map.getNumSymbols() == 0 && "expected map without symbols");
773  for (const auto &en : llvm::enumerate(map.getResults())) {
774  auto expr = en.value();
775  // Skip non-permutations.
776  if (auto d = dyn_cast<AffineDimExpr>(expr)) {
777  if (exprs[d.getPosition()])
778  continue;
779  exprs[d.getPosition()] = getAffineDimExpr(en.index(), d.getContext());
780  }
781  }
782  SmallVector<AffineExpr, 4> seenExprs;
783  seenExprs.reserve(map.getNumDims());
784  for (auto expr : exprs)
785  if (expr)
786  seenExprs.push_back(expr);
787  if (seenExprs.size() != map.getNumInputs())
788  return AffineMap();
789  return AffineMap::get(map.getNumResults(), 0, seenExprs, map.getContext());
790 }
791 
793  assert(map.isProjectedPermutation(/*allowZeroInResults=*/true));
794  MLIRContext *context = map.getContext();
795  AffineExpr zero = mlir::getAffineConstantExpr(0, context);
796  // Start with all the results as 0.
797  SmallVector<AffineExpr, 4> exprs(map.getNumInputs(), zero);
798  for (unsigned i : llvm::seq(unsigned(0), map.getNumResults())) {
799  // Skip zeros from input map. 'exprs' is already initialized to zero.
800  if (auto constExpr = dyn_cast<AffineConstantExpr>(map.getResult(i))) {
801  assert(constExpr.getValue() == 0 &&
802  "Unexpected constant in projected permutation");
803  (void)constExpr;
804  continue;
805  }
806 
807  // Reverse each dimension existing in the original map result.
808  exprs[map.getDimPosition(i)] = getAffineDimExpr(i, context);
809  }
810  return AffineMap::get(map.getNumResults(), /*symbolCount=*/0, exprs, context);
811 }
812 
814  unsigned numResults = 0, numDims = 0, numSymbols = 0;
815  for (auto m : maps)
816  numResults += m.getNumResults();
818  results.reserve(numResults);
819  for (auto m : maps) {
820  for (auto res : m.getResults())
821  results.push_back(res.shiftSymbols(m.getNumSymbols(), numSymbols));
822 
823  numSymbols += m.getNumSymbols();
824  numDims = std::max(m.getNumDims(), numDims);
825  }
826  return AffineMap::get(numDims, numSymbols, results,
827  maps.front().getContext());
828 }
829 
830 /// Common implementation to project out dimensions or symbols from an affine
831 /// map based on the template type.
832 /// Additionally, if 'compress' is true, the projected out dimensions or symbols
833 /// are also dropped from the resulting map.
834 template <typename AffineDimOrSymExpr>
836  const llvm::SmallBitVector &toProject,
837  bool compress) {
838  static_assert(llvm::is_one_of<AffineDimOrSymExpr, AffineDimExpr,
839  AffineSymbolExpr>::value,
840  "expected AffineDimExpr or AffineSymbolExpr");
841 
842  constexpr bool isDim = std::is_same<AffineDimOrSymExpr, AffineDimExpr>::value;
843  int64_t numDimOrSym = (isDim) ? map.getNumDims() : map.getNumSymbols();
844  SmallVector<AffineExpr> replacements;
845  replacements.reserve(numDimOrSym);
846 
847  auto createNewDimOrSym = (isDim) ? getAffineDimExpr : getAffineSymbolExpr;
848 
849  using replace_fn_ty =
850  std::function<AffineExpr(AffineExpr, ArrayRef<AffineExpr>)>;
851  replace_fn_ty replaceDims = [](AffineExpr e,
852  ArrayRef<AffineExpr> replacements) {
853  return e.replaceDims(replacements);
854  };
855  replace_fn_ty replaceSymbols = [](AffineExpr e,
856  ArrayRef<AffineExpr> replacements) {
857  return e.replaceSymbols(replacements);
858  };
859  replace_fn_ty replaceNewDimOrSym = (isDim) ? replaceDims : replaceSymbols;
860 
861  MLIRContext *context = map.getContext();
862  int64_t newNumDimOrSym = 0;
863  for (unsigned dimOrSym = 0; dimOrSym < numDimOrSym; ++dimOrSym) {
864  if (toProject.test(dimOrSym)) {
865  replacements.push_back(getAffineConstantExpr(0, context));
866  continue;
867  }
868  int64_t newPos = compress ? newNumDimOrSym++ : dimOrSym;
869  replacements.push_back(createNewDimOrSym(newPos, context));
870  }
871  SmallVector<AffineExpr> resultExprs;
872  resultExprs.reserve(map.getNumResults());
873  for (auto e : map.getResults())
874  resultExprs.push_back(replaceNewDimOrSym(e, replacements));
875 
876  int64_t numDims = (compress && isDim) ? newNumDimOrSym : map.getNumDims();
877  int64_t numSyms = (compress && !isDim) ? newNumDimOrSym : map.getNumSymbols();
878  return AffineMap::get(numDims, numSyms, resultExprs, context);
879 }
880 
882  const llvm::SmallBitVector &projectedDimensions,
883  bool compressDimsFlag) {
884  return projectCommonImpl<AffineDimExpr>(map, projectedDimensions,
885  compressDimsFlag);
886 }
887 
889  const llvm::SmallBitVector &projectedSymbols,
890  bool compressSymbolsFlag) {
891  return projectCommonImpl<AffineSymbolExpr>(map, projectedSymbols,
892  compressSymbolsFlag);
893 }
894 
896  const llvm::SmallBitVector &projectedDimensions,
897  bool compressDimsFlag,
898  bool compressSymbolsFlag) {
899  map = projectDims(map, projectedDimensions, compressDimsFlag);
900  if (compressSymbolsFlag)
901  map = compressUnusedSymbols(map);
902  return map;
903 }
904 
906  unsigned numDims = maps[0].getNumDims();
907  llvm::SmallBitVector numDimsBitVector(numDims, true);
908  for (AffineMap m : maps) {
909  for (unsigned i = 0; i < numDims; ++i) {
910  if (m.isFunctionOfDim(i))
911  numDimsBitVector.reset(i);
912  }
913  }
914  return numDimsBitVector;
915 }
916 
918  unsigned numSymbols = maps[0].getNumSymbols();
919  llvm::SmallBitVector numSymbolsBitVector(numSymbols, true);
920  for (AffineMap m : maps) {
921  for (unsigned i = 0; i < numSymbols; ++i) {
922  if (m.isFunctionOfSymbol(i))
923  numSymbolsBitVector.reset(i);
924  }
925  }
926  return numSymbolsBitVector;
927 }
928 
929 AffineMap
931  const llvm::SmallBitVector &projectedDimensions) {
932  auto id = AffineMap::getMultiDimIdentityMap(rank, map.getContext());
933  AffineMap proj = id.dropResults(projectedDimensions);
934  return map.compose(proj);
935 }
936 
937 //===----------------------------------------------------------------------===//
938 // MutableAffineMap.
939 //===----------------------------------------------------------------------===//
940 
942  : results(map.getResults().begin(), map.getResults().end()),
943  numDims(map.getNumDims()), numSymbols(map.getNumSymbols()),
944  context(map.getContext()) {}
945 
947  results.clear();
948  numDims = map.getNumDims();
949  numSymbols = map.getNumSymbols();
950  context = map.getContext();
951  llvm::append_range(results, map.getResults());
952 }
953 
954 bool MutableAffineMap::isMultipleOf(unsigned idx, int64_t factor) const {
955  return results[idx].isMultipleOf(factor);
956 }
957 
958 // Simplifies the result affine expressions of this map. The expressions
959 // have to be pure for the simplification implemented.
961  // Simplify each of the results if possible.
962  // TODO: functional-style map
963  for (unsigned i = 0, e = getNumResults(); i < e; i++) {
964  results[i] = simplifyAffineExpr(getResult(i), numDims, numSymbols);
965  }
966 }
967 
969  return AffineMap::get(numDims, numSymbols, results, context);
970 }
static SmallVector< AffineMap > compressUnusedListImpl(ArrayRef< AffineMap > maps, llvm::function_ref< AffineMap(AffineMap)> compressionFun)
Implementation detail to compress multiple affine maps with a compressionFun that is expected to be e...
Definition: AffineMap.cpp:664
static SmallVector< AffineMap, 4 > inferFromExprList(ArrayRef< AffineExprContainer > exprsList, MLIRContext *context)
Creates an affine map each for each list of AffineExpr's in exprsList while inferring the right numbe...
Definition: AffineMap.cpp:297
static AffineMap projectCommonImpl(AffineMap map, const llvm::SmallBitVector &toProject, bool compress)
Common implementation to project out dimensions or symbols from an affine map based on the template t...
Definition: AffineMap.cpp:835
static MLIRContext * getContext(OpFoldResult val)
static Value max(ImplicitLocOpBuilder &builder, Value value, Value bound)
A dimensional identifier appearing in an affine expression.
Definition: AffineExpr.h:236
Base type for affine expression.
Definition: AffineExpr.h:68
AffineExpr replaceDimsAndSymbols(ArrayRef< AffineExpr > dimReplacements, ArrayRef< AffineExpr > symReplacements) const
This method substitutes any uses of dimensions and symbols (e.g.
Definition: AffineExpr.cpp:89
RetT walk(FnT &&callback) const
Walk all of the AffineExpr's in this expression in postorder.
Definition: AffineExpr.h:130
AffineExprKind getKind() const
Return the classification for this type.
Definition: AffineExpr.cpp:35
AffineExpr compose(AffineMap map) const
Compose with an AffineMap.
AffineExpr replaceDims(ArrayRef< AffineExpr > dimReplacements) const
Dim-only version of replaceDimsAndSymbols.
Definition: AffineExpr.cpp:122
MLIRContext * getContext() const
Definition: AffineExpr.cpp:33
AffineExpr replaceSymbols(ArrayRef< AffineExpr > symReplacements) const
Symbol-only version of replaceDimsAndSymbols.
Definition: AffineExpr.cpp:127
A multi-dimensional affine map Affine map's are immutable like Type's, and they are uniqued.
Definition: AffineMap.h:46
int64_t getSingleConstantResult() const
Returns the constant result of this map.
Definition: AffineMap.cpp:381
static AffineMap getMinorIdentityMap(unsigned dims, unsigned results, MLIRContext *context)
Returns an identity affine map (d0, ..., dn) -> (dp, ..., dn) on the most minor dimensions.
Definition: AffineMap.cpp:135
AffineMap dropResults(ArrayRef< int64_t > positions) const
Definition: AffineMap.h:299
AffineMap getSliceMap(unsigned start, unsigned length) const
Returns the map consisting of length expressions starting from start.
Definition: AffineMap.cpp:639
AffineMap getMajorSubMap(unsigned numResults) const
Returns the map consisting of the most major numResults results.
Definition: AffineMap.cpp:644
MLIRContext * getContext() const
Definition: AffineMap.cpp:343
bool isMinorIdentity() const
Returns true if this affine map is a minor identity, i.e.
Definition: AffineMap.cpp:155
unsigned getDimPosition(unsigned idx) const
Extracts the position of the dimensional expression at the given result, when the caller knows it is ...
Definition: AffineMap.cpp:415
bool isConstant() const
Returns true if this affine map has only constant results.
Definition: AffineMap.cpp:377
static AffineMap getMultiDimIdentityMap(unsigned numDims, MLIRContext *context)
Returns an AffineMap with 'numDims' identity result dim exprs.
Definition: AffineMap.cpp:334
static AffineMap get(MLIRContext *context)
Returns a zero result affine map with no dimensions or symbols: () -> ().
bool isSingleConstant() const
Returns true if this affine map is a single result constant function.
Definition: AffineMap.cpp:373
bool isProjectedPermutation(bool allowZeroInResults=false) const
Returns true if the AffineMap represents a subset (i.e.
Definition: AffineMap.cpp:595
AffineMap getMinorSubMap(unsigned numResults) const
Returns the map consisting of the most minor numResults results.
Definition: AffineMap.cpp:652
uint64_t getLargestKnownDivisorOfMapExprs()
Get the largest known divisor of all map expressions.
Definition: AffineMap.cpp:323
constexpr AffineMap()=default
bool isEmpty() const
Returns true if this affine map is an empty map, i.e., () -> ().
Definition: AffineMap.cpp:369
std::optional< unsigned > getResultPosition(AffineExpr input) const
Extracts the first result position where input dimension resides.
Definition: AffineMap.cpp:419
unsigned getNumSymbols() const
Definition: AffineMap.cpp:398
bool isMinorIdentityWithBroadcasting(SmallVectorImpl< unsigned > *broadcastedDims=nullptr) const
Returns true if this affine map is a minor identity up to broadcasted dimensions which are indicated ...
Definition: AffineMap.cpp:176
unsigned getNumDims() const
Definition: AffineMap.cpp:394
ArrayRef< AffineExpr > getResults() const
Definition: AffineMap.cpp:407
SmallVector< int64_t > getConstantResults() const
Returns the constant results of this map.
Definition: AffineMap.cpp:386
bool isPermutationOfMinorIdentityWithBroadcasting(SmallVectorImpl< unsigned > &permutedDims) const
Return true if this affine map can be converted to a minor identity with broadcast by doing a permute...
Definition: AffineMap.cpp:216
bool isSymbolIdentity() const
Returns true if this affine map is an identity affine map on the symbol identifiers.
Definition: AffineMap.cpp:357
unsigned getNumResults() const
Definition: AffineMap.cpp:402
AffineMap replaceDimsAndSymbols(ArrayRef< AffineExpr > dimReplacements, ArrayRef< AffineExpr > symReplacements, unsigned numResultDims, unsigned numResultSyms) const
This method substitutes any uses of dimensions and symbols (e.g.
Definition: AffineMap.cpp:500
unsigned getNumInputs() const
Definition: AffineMap.cpp:403
AffineExpr getResult(unsigned idx) const
Definition: AffineMap.cpp:411
static AffineMap getFilteredIdentityMap(MLIRContext *ctx, unsigned numDims, llvm::function_ref< bool(AffineDimExpr)> keepDimFilter)
Returns an identity affine map with numDims input dimensions and filtered results using keepDimFilter...
Definition: AffineMap.cpp:142
AffineMap replace(AffineExpr expr, AffineExpr replacement, unsigned numResultDims, unsigned numResultSyms) const
Sparse replace method.
Definition: AffineMap.cpp:515
static AffineMap getPermutationMap(ArrayRef< unsigned > permutation, MLIRContext *context)
Returns an AffineMap representing a permutation.
Definition: AffineMap.cpp:264
SmallVector< unsigned > getBroadcastDims() const
Returns the list of broadcast dimensions (i.e.
Definition: AffineMap.cpp:161
void walkExprs(llvm::function_ref< void(AffineExpr)> callback) const
Walk all of the AffineExpr's in this mapping.
Definition: AffineMap.cpp:490
AffineMap partialConstantFold(ArrayRef< Attribute > operandConstants, SmallVectorImpl< int64_t > *results=nullptr, bool *hasPoison=nullptr) const
Propagates the constant operands into this affine map.
Definition: AffineMap.cpp:453
static AffineMap getConstantMap(int64_t val, MLIRContext *context)
Returns a single constant result affine map.
Definition: AffineMap.cpp:128
static AffineMap getMultiDimMapWithTargets(unsigned numDims, ArrayRef< unsigned > targets, MLIRContext *context)
Returns an affine map with numDims input dimensions and results specified by targets.
Definition: AffineMap.cpp:280
AffineMap getSubMap(ArrayRef< unsigned > resultPos) const
Returns the map consisting of the resultPos subset.
Definition: AffineMap.cpp:631
LogicalResult constantFold(ArrayRef< Attribute > operandConstants, SmallVectorImpl< Attribute > &results, bool *hasPoison=nullptr) const
Folds the results of the application of an affine map on the provided operands to a constant if possi...
Definition: AffineMap.cpp:434
AffineMap compose(AffineMap map) const
Returns the AffineMap resulting from composing this with map.
Definition: AffineMap.cpp:556
bool isIdentity() const
Returns true if this affine map is an identity affine map.
Definition: AffineMap.cpp:345
bool isPermutation() const
Returns true if the AffineMap represents a symbol-less permutation map.
Definition: AffineMap.cpp:625
static SmallVector< AffineMap, 4 > inferFromExprList(ArrayRef< ArrayRef< AffineExpr >> exprsList, MLIRContext *context)
Returns a vector of AffineMaps; each with as many results as exprs.size(), as many dims as the larges...
Definition: AffineMap.cpp:312
A symbolic identifier appearing in an affine expression.
Definition: AffineExpr.h:244
Attributes are known-constant values of operations.
Definition: Attributes.h:25
This class is a general helper class for creating context-global objects like types,...
Definition: Builders.h:50
AffineExpr getAffineSymbolExpr(unsigned position)
Definition: Builders.cpp:379
AffineExpr getAffineConstantExpr(int64_t constant)
Definition: Builders.cpp:383
AffineExpr getAffineDimExpr(unsigned position)
Definition: Builders.cpp:375
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:60
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition: Value.h:96
constexpr void enumerate(std::tuple< Tys... > &tuple, CallbackT &&callback)
Definition: Matchers.h:285
Include the generated interface declarations.
AffineMap simplifyAffineMap(AffineMap map)
Simplifies an affine map by simplifying its underlying AffineExpr results.
Definition: AffineMap.cpp:750
AffineMap expandDimsToRank(AffineMap map, int64_t rank, const llvm::SmallBitVector &projectedDimensions)
Expand map to operate on rank dims while projecting out the dims in projectedDimensions.
Definition: AffineMap.cpp:930
AffineMap removeDuplicateExprs(AffineMap map)
Returns a map with the same dimension and symbol count as map, but whose results are the unique affin...
Definition: AffineMap.cpp:760
llvm::SmallBitVector getUnusedSymbolsBitVector(ArrayRef< AffineMap > maps)
Definition: AffineMap.cpp:917
AffineMap inverseAndBroadcastProjectedPermutation(AffineMap map)
Return the reverse map of a projected permutation where the projected dimensions are transformed into...
Definition: AffineMap.cpp:792
AffineMap inversePermutation(AffineMap map)
Returns a map of codomain to domain dimensions such that the first codomain dimension for a particula...
Definition: AffineMap.cpp:768
AffineMap concatAffineMaps(ArrayRef< AffineMap > maps)
Concatenates a list of maps into a single AffineMap, stepping over potentially empty maps.
Definition: AffineMap.cpp:813
@ CeilDiv
RHS of ceildiv is always a constant or a symbolic expression.
@ Mul
RHS of mul is always a constant or a symbolic expression.
@ Mod
RHS of mod is always a constant or a symbolic expression with a positive value.
@ DimId
Dimensional identifier.
@ FloorDiv
RHS of floordiv is always a constant or a symbolic expression.
@ Constant
Constant integer.
@ SymbolId
Symbolic identifier.
AffineMap compressSymbols(AffineMap map, const llvm::SmallBitVector &unusedSymbols)
Drop the symbols that are listed in unusedSymbols.
Definition: AffineMap.cpp:708
static void getMaxDimAndSymbol(ArrayRef< AffineExprContainer > exprsList, int64_t &maxDim, int64_t &maxSym)
Calculates maximum dimension and symbol positions from the expressions in exprsLists and stores them ...
Definition: AffineMap.h:679
AffineMap compressUnusedDims(AffineMap map)
Drop the dims that are not used.
Definition: AffineMap.cpp:699
AffineMap compressDims(AffineMap map, const llvm::SmallBitVector &unusedDims)
Drop the dims that are listed in unusedDims.
Definition: AffineMap.cpp:694
AffineExpr getAffineConstantExpr(int64_t constant, MLIRContext *context)
Definition: AffineExpr.cpp:631
AffineMap getProjectedMap(AffineMap map, const llvm::SmallBitVector &projectedDimensions, bool compressDimsFlag=true, bool compressSymbolsFlag=true)
Calls projectDims(map, projectedDimensions, compressDimsFlag).
Definition: AffineMap.cpp:895
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
llvm::SmallBitVector getUnusedDimsBitVector(ArrayRef< AffineMap > maps)
Definition: AffineMap.cpp:905
AffineExpr simplifyAffineExpr(AffineExpr expr, unsigned numDims, unsigned numSymbols)
Simplify an affine expression by flattening and some amount of simple analysis.
AffineExpr getAffineDimExpr(unsigned position, MLIRContext *context)
These free functions allow clients of the API to not use classes in detail.
Definition: AffineExpr.cpp:607
AffineMap projectDims(AffineMap map, const llvm::SmallBitVector &projectedDimensions, bool compressDimsFlag=false)
Returns the map that results from projecting out the dimensions specified in projectedDimensions.
Definition: AffineMap.cpp:881
AffineMap compressUnusedSymbols(AffineMap map)
Drop the symbols that are not used.
Definition: AffineMap.cpp:713
AffineMap projectSymbols(AffineMap map, const llvm::SmallBitVector &projectedSymbols, bool compressSymbolsFlag=false)
Symbol counterpart of projectDims.
Definition: AffineMap.cpp:888
AffineMap foldAttributesIntoMap(Builder &b, AffineMap map, ArrayRef< OpFoldResult > operands, SmallVector< Value > &remainingValues)
Fold all attributes among the given operands into the affine map.
Definition: AffineMap.cpp:722
AffineExpr getAffineSymbolExpr(unsigned position, MLIRContext *context)
Definition: AffineExpr.cpp:617
void reset(AffineMap map)
Resets this MutableAffineMap with 'map'.
Definition: AffineMap.cpp:946
AffineMap getAffineMap() const
Get the AffineMap corresponding to this MutableAffineMap.
Definition: AffineMap.cpp:968
AffineExpr getResult(unsigned idx) const
Definition: AffineMap.h:418
bool isMultipleOf(unsigned idx, int64_t factor) const
Returns true if the idx'th result expression is a multiple of factor.
Definition: AffineMap.cpp:954
unsigned getNumResults() const
Definition: AffineMap.h:420
void simplify()
Simplify the (result) expressions in this map using analysis (used by.
Definition: AffineMap.cpp:960
ArrayRef< AffineExpr > results() const
The affine expressions for this (multi-dimensional) map.