MLIR  18.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 = std::max_element(permutation.begin(), permutation.end());
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 template <typename AffineExprContainer>
278  assert(!exprsList.empty());
279  assert(!exprsList[0].empty());
280  auto context = exprsList[0][0].getContext();
281  int64_t maxDim = -1, maxSym = -1;
282  getMaxDimAndSymbol(exprsList, maxDim, maxSym);
284  maps.reserve(exprsList.size());
285  for (const auto &exprs : exprsList)
286  maps.push_back(AffineMap::get(/*dimCount=*/maxDim + 1,
287  /*symbolCount=*/maxSym + 1, exprs, context));
288  return maps;
289 }
290 
293  return ::inferFromExprList(exprsList);
294 }
295 
298  return ::inferFromExprList(exprsList);
299 }
300 
302  uint64_t gcd = 0;
303  for (AffineExpr resultExpr : getResults()) {
304  uint64_t thisGcd = resultExpr.getLargestKnownDivisor();
305  gcd = std::gcd(gcd, thisGcd);
306  }
307  if (gcd == 0)
309  return gcd;
310 }
311 
313  MLIRContext *context) {
315  dimExprs.reserve(numDims);
316  for (unsigned i = 0; i < numDims; ++i)
317  dimExprs.push_back(mlir::getAffineDimExpr(i, context));
318  return get(/*dimCount=*/numDims, /*symbolCount=*/0, dimExprs, context);
319 }
320 
321 MLIRContext *AffineMap::getContext() const { return map->context; }
322 
323 bool AffineMap::isIdentity() const {
324  if (getNumDims() != getNumResults())
325  return false;
326  ArrayRef<AffineExpr> results = getResults();
327  for (unsigned i = 0, numDims = getNumDims(); i < numDims; ++i) {
328  auto expr = dyn_cast<AffineDimExpr>(results[i]);
329  if (!expr || expr.getPosition() != i)
330  return false;
331  }
332  return true;
333 }
334 
336  if (getNumSymbols() != getNumResults())
337  return false;
338  ArrayRef<AffineExpr> results = getResults();
339  for (unsigned i = 0, numSymbols = getNumSymbols(); i < numSymbols; ++i) {
340  auto expr = dyn_cast<AffineDimExpr>(results[i]);
341  if (!expr || expr.getPosition() != i)
342  return false;
343  }
344  return true;
345 }
346 
347 bool AffineMap::isEmpty() const {
348  return getNumDims() == 0 && getNumSymbols() == 0 && getNumResults() == 0;
349 }
350 
352  return getNumResults() == 1 && isa<AffineConstantExpr>(getResult(0));
353 }
354 
355 bool AffineMap::isConstant() const {
356  return llvm::all_of(getResults(), [](AffineExpr expr) {
357  return isa<AffineConstantExpr>(expr);
358  });
359 }
360 
362  assert(isSingleConstant() && "map must have a single constant result");
363  return cast<AffineConstantExpr>(getResult(0)).getValue();
364 }
365 
367  assert(isConstant() && "map must have only constant results");
368  SmallVector<int64_t> result;
369  for (auto expr : getResults())
370  result.emplace_back(cast<AffineConstantExpr>(expr).getValue());
371  return result;
372 }
373 
374 unsigned AffineMap::getNumDims() const {
375  assert(map && "uninitialized map storage");
376  return map->numDims;
377 }
378 unsigned AffineMap::getNumSymbols() const {
379  assert(map && "uninitialized map storage");
380  return map->numSymbols;
381 }
382 unsigned AffineMap::getNumResults() const { return getResults().size(); }
383 unsigned AffineMap::getNumInputs() const {
384  assert(map && "uninitialized map storage");
385  return map->numDims + map->numSymbols;
386 }
388  assert(map && "uninitialized map storage");
389  return map->results();
390 }
391 AffineExpr AffineMap::getResult(unsigned idx) const {
392  return getResults()[idx];
393 }
394 
395 unsigned AffineMap::getDimPosition(unsigned idx) const {
396  return cast<AffineDimExpr>(getResult(idx)).getPosition();
397 }
398 
399 std::optional<unsigned> AffineMap::getResultPosition(AffineExpr input) const {
400  if (!isa<AffineDimExpr>(input))
401  return std::nullopt;
402 
403  for (unsigned i = 0, numResults = getNumResults(); i < numResults; i++) {
404  if (getResult(i) == input)
405  return i;
406  }
407 
408  return std::nullopt;
409 }
410 
411 /// Folds the results of the application of an affine map on the provided
412 /// operands to a constant if possible. Returns false if the folding happens,
413 /// true otherwise.
416  bool *hasPoison) const {
417  // Attempt partial folding.
418  SmallVector<int64_t, 2> integers;
419  partialConstantFold(operandConstants, &integers, hasPoison);
420 
421  // If all expressions folded to a constant, populate results with attributes
422  // containing those constants.
423  if (integers.empty())
424  return failure();
425 
426  auto range = llvm::map_range(integers, [this](int64_t i) {
428  });
429  results.append(range.begin(), range.end());
430  return success();
431 }
432 
434  SmallVectorImpl<int64_t> *results,
435  bool *hasPoison) const {
436  assert(getNumInputs() == operandConstants.size());
437 
438  // Fold each of the result expressions.
439  AffineExprConstantFolder exprFolder(getNumDims(), operandConstants);
441  exprs.reserve(getNumResults());
442 
443  for (auto expr : getResults()) {
444  auto folded = exprFolder.constantFold(expr);
445  if (exprFolder.hasPoison() && hasPoison) {
446  *hasPoison = true;
447  return {};
448  }
449  // If did not fold to a constant, keep the original expression, and clear
450  // the integer results vector.
451  if (folded) {
452  exprs.push_back(
453  getAffineConstantExpr(folded.getInt(), folded.getContext()));
454  if (results)
455  results->push_back(folded.getInt());
456  } else {
457  exprs.push_back(expr);
458  if (results) {
459  results->clear();
460  results = nullptr;
461  }
462  }
463  }
464 
465  return get(getNumDims(), getNumSymbols(), exprs, getContext());
466 }
467 
468 /// Walk all of the AffineExpr's in this mapping. Each node in an expression
469 /// tree is visited in postorder.
471  for (auto expr : getResults())
472  expr.walk(callback);
473 }
474 
475 /// This method substitutes any uses of dimensions and symbols (e.g.
476 /// dim#0 with dimReplacements[0]) in subexpressions and returns the modified
477 /// expression mapping. Because this can be used to eliminate dims and
478 /// symbols, the client needs to specify the number of dims and symbols in
479 /// the result. The returned map always has the same number of results.
481  ArrayRef<AffineExpr> symReplacements,
482  unsigned numResultDims,
483  unsigned numResultSyms) const {
485  results.reserve(getNumResults());
486  for (auto expr : getResults())
487  results.push_back(
488  expr.replaceDimsAndSymbols(dimReplacements, symReplacements));
489  return get(numResultDims, numResultSyms, results, getContext());
490 }
491 
492 /// Sparse replace method. Apply AffineExpr::replace(`expr`, `replacement`) to
493 /// each of the results and return a new AffineMap with the new results and
494 /// with the specified number of dims and symbols.
496  unsigned numResultDims,
497  unsigned numResultSyms) const {
498  SmallVector<AffineExpr, 4> newResults;
499  newResults.reserve(getNumResults());
500  for (AffineExpr e : getResults())
501  newResults.push_back(e.replace(expr, replacement));
502  return AffineMap::get(numResultDims, numResultSyms, newResults, getContext());
503 }
504 
505 /// Sparse replace method. Apply AffineExpr::replace(`map`) to each of the
506 /// results and return a new AffineMap with the new results and with the
507 /// specified number of dims and symbols.
509  unsigned numResultDims,
510  unsigned numResultSyms) const {
511  SmallVector<AffineExpr, 4> newResults;
512  newResults.reserve(getNumResults());
513  for (AffineExpr e : getResults())
514  newResults.push_back(e.replace(map));
515  return AffineMap::get(numResultDims, numResultSyms, newResults, getContext());
516 }
517 
518 AffineMap
520  SmallVector<AffineExpr, 4> newResults;
521  newResults.reserve(getNumResults());
522  for (AffineExpr e : getResults())
523  newResults.push_back(e.replace(map));
524  return AffineMap::inferFromExprList(newResults).front();
525 }
526 
527 AffineMap AffineMap::dropResults(const llvm::SmallBitVector &positions) const {
528  auto exprs = llvm::to_vector<4>(getResults());
529  // TODO: this is a pretty terrible API .. is there anything better?
530  for (auto pos = positions.find_last(); pos != -1;
531  pos = positions.find_prev(pos))
532  exprs.erase(exprs.begin() + pos);
533  return AffineMap::get(getNumDims(), getNumSymbols(), exprs, getContext());
534 }
535 
537  assert(getNumDims() == map.getNumResults() && "Number of results mismatch");
538  // Prepare `map` by concatenating the symbols and rewriting its exprs.
539  unsigned numDims = map.getNumDims();
540  unsigned numSymbolsThisMap = getNumSymbols();
541  unsigned numSymbols = numSymbolsThisMap + map.getNumSymbols();
542  SmallVector<AffineExpr, 8> newDims(numDims);
543  for (unsigned idx = 0; idx < numDims; ++idx) {
544  newDims[idx] = getAffineDimExpr(idx, getContext());
545  }
546  SmallVector<AffineExpr, 8> newSymbols(numSymbols - numSymbolsThisMap);
547  for (unsigned idx = numSymbolsThisMap; idx < numSymbols; ++idx) {
548  newSymbols[idx - numSymbolsThisMap] =
550  }
551  auto newMap =
552  map.replaceDimsAndSymbols(newDims, newSymbols, numDims, numSymbols);
554  exprs.reserve(getResults().size());
555  for (auto expr : getResults())
556  exprs.push_back(expr.compose(newMap));
557  return AffineMap::get(numDims, numSymbols, exprs, map.getContext());
558 }
559 
561  assert(getNumSymbols() == 0 && "Expected symbol-less map");
563  exprs.reserve(values.size());
564  MLIRContext *ctx = getContext();
565  for (auto v : values)
566  exprs.push_back(getAffineConstantExpr(v, ctx));
567  auto resMap = compose(AffineMap::get(0, 0, exprs, ctx));
569  res.reserve(resMap.getNumResults());
570  for (auto e : resMap.getResults())
571  res.push_back(cast<AffineConstantExpr>(e).getValue());
572  return res;
573 }
574 
575 bool AffineMap::isProjectedPermutation(bool allowZeroInResults) const {
576  if (getNumSymbols() > 0)
577  return false;
578 
579  // Having more results than inputs means that results have duplicated dims or
580  // zeros that can't be mapped to input dims.
581  if (getNumResults() > getNumInputs())
582  return false;
583 
584  SmallVector<bool, 8> seen(getNumInputs(), false);
585  // A projected permutation can have, at most, only one instance of each input
586  // dimension in the result expressions. Zeros are allowed as long as the
587  // number of result expressions is lower or equal than the number of input
588  // expressions.
589  for (auto expr : getResults()) {
590  if (auto dim = dyn_cast<AffineDimExpr>(expr)) {
591  if (seen[dim.getPosition()])
592  return false;
593  seen[dim.getPosition()] = true;
594  } else {
595  auto constExpr = dyn_cast<AffineConstantExpr>(expr);
596  if (!allowZeroInResults || !constExpr || constExpr.getValue() != 0)
597  return false;
598  }
599  }
600 
601  // Results are either dims or zeros and zeros can be mapped to input dims.
602  return true;
603 }
604 
606  if (getNumDims() != getNumResults())
607  return false;
608  return isProjectedPermutation();
609 }
610 
613  exprs.reserve(resultPos.size());
614  for (auto idx : resultPos)
615  exprs.push_back(getResult(idx));
616  return AffineMap::get(getNumDims(), getNumSymbols(), exprs, getContext());
617 }
618 
619 AffineMap AffineMap::getSliceMap(unsigned start, unsigned length) const {
621  getResults().slice(start, length), getContext());
622 }
623 
624 AffineMap AffineMap::getMajorSubMap(unsigned numResults) const {
625  if (numResults == 0)
626  return AffineMap();
627  if (numResults > getNumResults())
628  return *this;
629  return getSliceMap(0, numResults);
630 }
631 
632 AffineMap AffineMap::getMinorSubMap(unsigned numResults) const {
633  if (numResults == 0)
634  return AffineMap();
635  if (numResults > getNumResults())
636  return *this;
637  return getSliceMap(getNumResults() - numResults, numResults);
638 }
639 
640 /// Implementation detail to compress multiple affine maps with a compressionFun
641 /// that is expected to be either compressUnusedDims or compressUnusedSymbols.
642 /// The implementation keeps track of num dims and symbols across the different
643 /// affine maps.
645  ArrayRef<AffineMap> maps,
646  llvm::function_ref<AffineMap(AffineMap)> compressionFun) {
647  if (maps.empty())
648  return SmallVector<AffineMap>();
649  SmallVector<AffineExpr> allExprs;
650  allExprs.reserve(maps.size() * maps.front().getNumResults());
651  unsigned numDims = maps.front().getNumDims(),
652  numSymbols = maps.front().getNumSymbols();
653  for (auto m : maps) {
654  assert(numDims == m.getNumDims() && numSymbols == m.getNumSymbols() &&
655  "expected maps with same num dims and symbols");
656  llvm::append_range(allExprs, m.getResults());
657  }
658  AffineMap unifiedMap = compressionFun(
659  AffineMap::get(numDims, numSymbols, allExprs, maps.front().getContext()));
660  unsigned unifiedNumDims = unifiedMap.getNumDims(),
661  unifiedNumSymbols = unifiedMap.getNumSymbols();
662  ArrayRef<AffineExpr> unifiedResults = unifiedMap.getResults();
664  res.reserve(maps.size());
665  for (auto m : maps) {
666  res.push_back(AffineMap::get(unifiedNumDims, unifiedNumSymbols,
667  unifiedResults.take_front(m.getNumResults()),
668  m.getContext()));
669  unifiedResults = unifiedResults.drop_front(m.getNumResults());
670  }
671  return res;
672 }
673 
675  const llvm::SmallBitVector &unusedDims) {
676  return projectDims(map, unusedDims, /*compressDimsFlag=*/true);
677 }
678 
680  return compressDims(map, getUnusedDimsBitVector({map}));
681 }
682 
684  return compressUnusedListImpl(
685  maps, [](AffineMap m) { return compressUnusedDims(m); });
686 }
687 
689  const llvm::SmallBitVector &unusedSymbols) {
690  return projectSymbols(map, unusedSymbols, /*compressSymbolsFlag=*/true);
691 }
692 
694  return compressSymbols(map, getUnusedSymbolsBitVector({map}));
695 }
696 
698  return compressUnusedListImpl(
699  maps, [](AffineMap m) { return compressUnusedSymbols(m); });
700 }
701 
703  ArrayRef<OpFoldResult> operands,
704  SmallVector<Value> &remainingValues) {
705  SmallVector<AffineExpr> dimReplacements, symReplacements;
706  int64_t numDims = 0;
707  for (int64_t i = 0; i < map.getNumDims(); ++i) {
708  if (auto attr = operands[i].dyn_cast<Attribute>()) {
709  dimReplacements.push_back(
710  b.getAffineConstantExpr(attr.cast<IntegerAttr>().getInt()));
711  } else {
712  dimReplacements.push_back(b.getAffineDimExpr(numDims++));
713  remainingValues.push_back(operands[i].get<Value>());
714  }
715  }
716  int64_t numSymbols = 0;
717  for (int64_t i = 0; i < map.getNumSymbols(); ++i) {
718  if (auto attr = operands[i + map.getNumDims()].dyn_cast<Attribute>()) {
719  symReplacements.push_back(
720  b.getAffineConstantExpr(attr.cast<IntegerAttr>().getInt()));
721  } else {
722  symReplacements.push_back(b.getAffineSymbolExpr(numSymbols++));
723  remainingValues.push_back(operands[i + map.getNumDims()].get<Value>());
724  }
725  }
726  return map.replaceDimsAndSymbols(dimReplacements, symReplacements, numDims,
727  numSymbols);
728 }
729 
732  for (auto e : map.getResults()) {
733  exprs.push_back(
734  simplifyAffineExpr(e, map.getNumDims(), map.getNumSymbols()));
735  }
736  return AffineMap::get(map.getNumDims(), map.getNumSymbols(), exprs,
737  map.getContext());
738 }
739 
741  auto results = map.getResults();
742  SmallVector<AffineExpr, 4> uniqueExprs(results.begin(), results.end());
743  uniqueExprs.erase(std::unique(uniqueExprs.begin(), uniqueExprs.end()),
744  uniqueExprs.end());
745  return AffineMap::get(map.getNumDims(), map.getNumSymbols(), uniqueExprs,
746  map.getContext());
747 }
748 
750  if (map.isEmpty())
751  return map;
752  assert(map.getNumSymbols() == 0 && "expected map without symbols");
754  for (const auto &en : llvm::enumerate(map.getResults())) {
755  auto expr = en.value();
756  // Skip non-permutations.
757  if (auto d = dyn_cast<AffineDimExpr>(expr)) {
758  if (exprs[d.getPosition()])
759  continue;
760  exprs[d.getPosition()] = getAffineDimExpr(en.index(), d.getContext());
761  }
762  }
763  SmallVector<AffineExpr, 4> seenExprs;
764  seenExprs.reserve(map.getNumDims());
765  for (auto expr : exprs)
766  if (expr)
767  seenExprs.push_back(expr);
768  if (seenExprs.size() != map.getNumInputs())
769  return AffineMap();
770  return AffineMap::get(map.getNumResults(), 0, seenExprs, map.getContext());
771 }
772 
774  assert(map.isProjectedPermutation(/*allowZeroInResults=*/true));
775  MLIRContext *context = map.getContext();
776  AffineExpr zero = mlir::getAffineConstantExpr(0, context);
777  // Start with all the results as 0.
778  SmallVector<AffineExpr, 4> exprs(map.getNumInputs(), zero);
779  for (unsigned i : llvm::seq(unsigned(0), map.getNumResults())) {
780  // Skip zeros from input map. 'exprs' is already initialized to zero.
781  if (auto constExpr = dyn_cast<AffineConstantExpr>(map.getResult(i))) {
782  assert(constExpr.getValue() == 0 &&
783  "Unexpected constant in projected permutation");
784  (void)constExpr;
785  continue;
786  }
787 
788  // Reverse each dimension existing in the original map result.
789  exprs[map.getDimPosition(i)] = getAffineDimExpr(i, context);
790  }
791  return AffineMap::get(map.getNumResults(), /*symbolCount=*/0, exprs, context);
792 }
793 
795  unsigned numResults = 0, numDims = 0, numSymbols = 0;
796  for (auto m : maps)
797  numResults += m.getNumResults();
799  results.reserve(numResults);
800  for (auto m : maps) {
801  for (auto res : m.getResults())
802  results.push_back(res.shiftSymbols(m.getNumSymbols(), numSymbols));
803 
804  numSymbols += m.getNumSymbols();
805  numDims = std::max(m.getNumDims(), numDims);
806  }
807  return AffineMap::get(numDims, numSymbols, results,
808  maps.front().getContext());
809 }
810 
811 /// Common implementation to project out dimensions or symbols from an affine
812 /// map based on the template type.
813 /// Additionally, if 'compress' is true, the projected out dimensions or symbols
814 /// are also dropped from the resulting map.
815 template <typename AffineDimOrSymExpr>
817  const llvm::SmallBitVector &toProject,
818  bool compress) {
819  static_assert(llvm::is_one_of<AffineDimOrSymExpr, AffineDimExpr,
820  AffineSymbolExpr>::value,
821  "expected AffineDimExpr or AffineSymbolExpr");
822 
823  constexpr bool isDim = std::is_same<AffineDimOrSymExpr, AffineDimExpr>::value;
824  int64_t numDimOrSym = (isDim) ? map.getNumDims() : map.getNumSymbols();
825  SmallVector<AffineExpr> replacements;
826  replacements.reserve(numDimOrSym);
827 
828  auto createNewDimOrSym = (isDim) ? getAffineDimExpr : getAffineSymbolExpr;
829 
830  using replace_fn_ty =
831  std::function<AffineExpr(AffineExpr, ArrayRef<AffineExpr>)>;
832  replace_fn_ty replaceDims = [](AffineExpr e,
833  ArrayRef<AffineExpr> replacements) {
834  return e.replaceDims(replacements);
835  };
836  replace_fn_ty replaceSymbols = [](AffineExpr e,
837  ArrayRef<AffineExpr> replacements) {
838  return e.replaceSymbols(replacements);
839  };
840  replace_fn_ty replaceNewDimOrSym = (isDim) ? replaceDims : replaceSymbols;
841 
842  MLIRContext *context = map.getContext();
843  int64_t newNumDimOrSym = 0;
844  for (unsigned dimOrSym = 0; dimOrSym < numDimOrSym; ++dimOrSym) {
845  if (toProject.test(dimOrSym)) {
846  replacements.push_back(getAffineConstantExpr(0, context));
847  continue;
848  }
849  int64_t newPos = compress ? newNumDimOrSym++ : dimOrSym;
850  replacements.push_back(createNewDimOrSym(newPos, context));
851  }
852  SmallVector<AffineExpr> resultExprs;
853  resultExprs.reserve(map.getNumResults());
854  for (auto e : map.getResults())
855  resultExprs.push_back(replaceNewDimOrSym(e, replacements));
856 
857  int64_t numDims = (compress && isDim) ? newNumDimOrSym : map.getNumDims();
858  int64_t numSyms = (compress && !isDim) ? newNumDimOrSym : map.getNumSymbols();
859  return AffineMap::get(numDims, numSyms, resultExprs, context);
860 }
861 
863  const llvm::SmallBitVector &projectedDimensions,
864  bool compressDimsFlag) {
865  return projectCommonImpl<AffineDimExpr>(map, projectedDimensions,
866  compressDimsFlag);
867 }
868 
870  const llvm::SmallBitVector &projectedSymbols,
871  bool compressSymbolsFlag) {
872  return projectCommonImpl<AffineSymbolExpr>(map, projectedSymbols,
873  compressSymbolsFlag);
874 }
875 
877  const llvm::SmallBitVector &projectedDimensions,
878  bool compressDimsFlag,
879  bool compressSymbolsFlag) {
880  map = projectDims(map, projectedDimensions, compressDimsFlag);
881  if (compressSymbolsFlag)
882  map = compressUnusedSymbols(map);
883  return map;
884 }
885 
887  unsigned numDims = maps[0].getNumDims();
888  llvm::SmallBitVector numDimsBitVector(numDims, true);
889  for (AffineMap m : maps) {
890  for (unsigned i = 0; i < numDims; ++i) {
891  if (m.isFunctionOfDim(i))
892  numDimsBitVector.reset(i);
893  }
894  }
895  return numDimsBitVector;
896 }
897 
899  unsigned numSymbols = maps[0].getNumSymbols();
900  llvm::SmallBitVector numSymbolsBitVector(numSymbols, true);
901  for (AffineMap m : maps) {
902  for (unsigned i = 0; i < numSymbols; ++i) {
903  if (m.isFunctionOfSymbol(i))
904  numSymbolsBitVector.reset(i);
905  }
906  }
907  return numSymbolsBitVector;
908 }
909 
910 AffineMap
912  const llvm::SmallBitVector &projectedDimensions) {
913  auto id = AffineMap::getMultiDimIdentityMap(rank, map.getContext());
914  AffineMap proj = id.dropResults(projectedDimensions);
915  return map.compose(proj);
916 }
917 
918 //===----------------------------------------------------------------------===//
919 // MutableAffineMap.
920 //===----------------------------------------------------------------------===//
921 
923  : results(map.getResults().begin(), map.getResults().end()),
924  numDims(map.getNumDims()), numSymbols(map.getNumSymbols()),
925  context(map.getContext()) {}
926 
928  results.clear();
929  numDims = map.getNumDims();
930  numSymbols = map.getNumSymbols();
931  context = map.getContext();
932  llvm::append_range(results, map.getResults());
933 }
934 
935 bool MutableAffineMap::isMultipleOf(unsigned idx, int64_t factor) const {
936  return results[idx].isMultipleOf(factor);
937 }
938 
939 // Simplifies the result affine expressions of this map. The expressions
940 // have to be pure for the simplification implemented.
942  // Simplify each of the results if possible.
943  // TODO: functional-style map
944  for (unsigned i = 0, e = getNumResults(); i < e; i++) {
945  results[i] = simplifyAffineExpr(getResult(i), numDims, numSymbols);
946  }
947 }
948 
950  return AffineMap::get(numDims, numSymbols, results, context);
951 }
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:644
static SmallVector< AffineMap, 4 > inferFromExprList(ArrayRef< AffineExprContainer > exprsList)
Definition: AffineMap.cpp:277
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:816
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:222
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:66
void walk(std::function< void(AffineExpr)> callback) const
Walk all of the AffineExpr's in this expression in postorder.
Definition: AffineExpr.cpp:30
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:978
AffineExpr replaceDims(ArrayRef< AffineExpr > dimReplacements) const
Dim-only version of replaceDimsAndSymbols.
Definition: AffineExpr.cpp:99
MLIRContext * getContext() const
Definition: AffineExpr.cpp:25
AffineExpr replaceSymbols(ArrayRef< AffineExpr > symReplacements) const
Symbol-only version of replaceDimsAndSymbols.
Definition: AffineExpr.cpp:104
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:361
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:290
AffineMap getSliceMap(unsigned start, unsigned length) const
Returns the map consisting of length expressions starting from start.
Definition: AffineMap.cpp:619
AffineMap getMajorSubMap(unsigned numResults) const
Returns the map consisting of the most major numResults results.
Definition: AffineMap.cpp:624
MLIRContext * getContext() const
Definition: AffineMap.cpp:321
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:395
bool isConstant() const
Returns true if this affine map has only constant results.
Definition: AffineMap.cpp:355
static AffineMap getMultiDimIdentityMap(unsigned numDims, MLIRContext *context)
Returns an AffineMap with 'numDims' identity result dim exprs.
Definition: AffineMap.cpp:312
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:351
bool isProjectedPermutation(bool allowZeroInResults=false) const
Returns true if the AffineMap represents a subset (i.e.
Definition: AffineMap.cpp:575
AffineMap getMinorSubMap(unsigned numResults) const
Returns the map consisting of the most minor numResults results.
Definition: AffineMap.cpp:632
uint64_t getLargestKnownDivisorOfMapExprs()
Get the largest known divisor of all map expressions.
Definition: AffineMap.cpp:301
constexpr AffineMap()=default
bool isEmpty() const
Returns true if this affine map is an empty map, i.e., () -> ().
Definition: AffineMap.cpp:347
std::optional< unsigned > getResultPosition(AffineExpr input) const
Extracts the first result position where input dimension resides.
Definition: AffineMap.cpp:399
unsigned getNumSymbols() const
Definition: AffineMap.cpp:378
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:374
ArrayRef< AffineExpr > getResults() const
Definition: AffineMap.cpp:387
SmallVector< int64_t > getConstantResults() const
Returns the constant results of this map.
Definition: AffineMap.cpp:366
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:335
unsigned getNumResults() const
Definition: AffineMap.cpp:382
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:480
unsigned getNumInputs() const
Definition: AffineMap.cpp:383
static SmallVector< AffineMap, 4 > inferFromExprList(ArrayRef< ArrayRef< AffineExpr >> exprsList)
Returns a vector of AffineMaps; each with as many results as exprs.size(), as many dims as the larges...
Definition: AffineMap.cpp:292
AffineExpr getResult(unsigned idx) const
Definition: AffineMap.cpp:391
static AffineMap getFilteredIdentityMap(MLIRContext *ctx, unsigned numDims, llvm::function_ref< bool(AffineDimExpr)> keepDimFilter)
Returns an identity affine map witn 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:495
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:470
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:433
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:611
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:414
AffineMap compose(AffineMap map) const
Returns the AffineMap resulting from composing this with map.
Definition: AffineMap.cpp:536
bool isIdentity() const
Returns true if this affine map is an identity affine map.
Definition: AffineMap.cpp:323
bool isPermutation() const
Returns true if the AffineMap represents a symbol-less permutation map.
Definition: AffineMap.cpp:605
A symbolic identifier appearing in an affine expression.
Definition: AffineExpr.h:230
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:357
AffineExpr getAffineConstantExpr(int64_t constant)
Definition: Builders.cpp:361
AffineExpr getAffineDimExpr(unsigned position)
Definition: Builders.cpp:353
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:730
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:911
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:740
llvm::SmallBitVector getUnusedSymbolsBitVector(ArrayRef< AffineMap > maps)
Definition: AffineMap.cpp:898
AffineMap inverseAndBroadcastProjectedPermutation(AffineMap map)
Return the reverse map of a projected permutation where the projected dimensions are transformed into...
Definition: AffineMap.cpp:773
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:749
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:794
@ 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:688
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:670
AffineMap compressUnusedDims(AffineMap map)
Drop the dims that are not used.
Definition: AffineMap.cpp:679
AffineMap compressDims(AffineMap map, const llvm::SmallBitVector &unusedDims)
Drop the dims that are listed in unusedDims.
Definition: AffineMap.cpp:674
AffineExpr getAffineConstantExpr(int64_t constant, MLIRContext *context)
Definition: AffineExpr.cpp:608
AffineMap getProjectedMap(AffineMap map, const llvm::SmallBitVector &projectedDimensions, bool compressDimsFlag=true, bool compressSymbolsFlag=true)
Calls projectDims(map, projectedDimensions, compressDimsFlag).
Definition: AffineMap.cpp:876
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:886
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:584
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:862
AffineMap compressUnusedSymbols(AffineMap map)
Drop the symbols that are not used.
Definition: AffineMap.cpp:693
AffineMap projectSymbols(AffineMap map, const llvm::SmallBitVector &projectedSymbols, bool compressSymbolsFlag=false)
Symbol counterpart of projectDims.
Definition: AffineMap.cpp:869
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:702
AffineExpr getAffineSymbolExpr(unsigned position, MLIRContext *context)
Definition: AffineExpr.cpp:594
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:927
AffineMap getAffineMap() const
Get the AffineMap corresponding to this MutableAffineMap.
Definition: AffineMap.cpp:949
AffineExpr getResult(unsigned idx) const
Definition: AffineMap.h:409
bool isMultipleOf(unsigned idx, int64_t factor) const
Returns true if the idx'th result expression is a multiple of factor.
Definition: AffineMap.cpp:935
unsigned getNumResults() const
Definition: AffineMap.h:411
void simplify()
Simplify the (result) expressions in this map using analysis (used by.
Definition: AffineMap.cpp:941
ArrayRef< AffineExpr > results() const
The affine expressions for this (multi-dimensional) map.