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