MLIR  19.0.0git
AffineValueMap.cpp
Go to the documentation of this file.
1 //===- AffineValueMap.cpp - MLIR Affine Value Map Class -------------------===//
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 
11 
12 using namespace mlir;
13 using namespace mlir::affine;
14 
16  ValueRange results)
17  : map(map), operands(operands.begin(), operands.end()),
18  results(results.begin(), results.end()) {}
19 
21  ValueRange results) {
22  this->map.reset(map);
23  this->operands.assign(operands.begin(), operands.end());
24  this->results.assign(results.begin(), results.end());
25 }
26 
28  const AffineValueMap &b, AffineValueMap *res) {
29  assert(a.getNumResults() == b.getNumResults() && "invalid inputs");
30 
31  SmallVector<Value, 4> allOperands;
32  allOperands.reserve(a.getNumOperands() + b.getNumOperands());
33  auto aDims = a.getOperands().take_front(a.getNumDims());
34  auto bDims = b.getOperands().take_front(b.getNumDims());
35  auto aSyms = a.getOperands().take_back(a.getNumSymbols());
36  auto bSyms = b.getOperands().take_back(b.getNumSymbols());
37  allOperands.append(aDims.begin(), aDims.end());
38  allOperands.append(bDims.begin(), bDims.end());
39  allOperands.append(aSyms.begin(), aSyms.end());
40  allOperands.append(bSyms.begin(), bSyms.end());
41 
42  // Shift dims and symbols of b's map.
43  auto bMap = b.getAffineMap()
44  .shiftDims(a.getNumDims())
46 
47  // Construct the difference expressions.
48  auto aMap = a.getAffineMap();
50  diffExprs.reserve(a.getNumResults());
51  for (unsigned i = 0, e = bMap.getNumResults(); i < e; ++i)
52  diffExprs.push_back(aMap.getResult(i) - bMap.getResult(i));
53 
54  auto diffMap = AffineMap::get(bMap.getNumDims(), bMap.getNumSymbols(),
55  diffExprs, bMap.getContext());
56  fullyComposeAffineMapAndOperands(&diffMap, &allOperands);
57  canonicalizeMapAndOperands(&diffMap, &allOperands);
58  diffMap = simplifyAffineMap(diffMap);
59  res->reset(diffMap, allOperands);
60 }
61 
62 // Returns true and sets 'indexOfMatch' if 'valueToMatch' is found in
63 // 'valuesToSearch' beginning at 'indexStart'. Returns false otherwise.
64 static bool findIndex(Value valueToMatch, ArrayRef<Value> valuesToSearch,
65  unsigned indexStart, unsigned *indexOfMatch) {
66  unsigned size = valuesToSearch.size();
67  for (unsigned i = indexStart; i < size; ++i) {
68  if (valueToMatch == valuesToSearch[i]) {
69  *indexOfMatch = i;
70  return true;
71  }
72  }
73  return false;
74 }
75 
76 bool AffineValueMap::isMultipleOf(unsigned idx, int64_t factor) const {
77  return map.isMultipleOf(idx, factor);
78 }
79 
80 /// This method uses the invariant that operands are always positionally aligned
81 /// with the AffineDimExpr in the underlying AffineMap.
82 bool AffineValueMap::isFunctionOf(unsigned idx, Value value) const {
83  unsigned index;
84  if (!findIndex(value, operands, /*indexStart=*/0, &index)) {
85  return false;
86  }
87  auto expr = const_cast<AffineValueMap *>(this)->getAffineMap().getResult(idx);
88  // TODO: this is better implemented on a flattened representation.
89  // At least for now it is conservative.
90  return expr.isFunctionOfDim(index);
91 }
92 
93 Value AffineValueMap::getOperand(unsigned i) const {
94  return static_cast<Value>(operands[i]);
95 }
96 
98  return ArrayRef<Value>(operands);
99 }
100 
102 
static bool findIndex(Value valueToMatch, ArrayRef< Value > valuesToSearch, unsigned indexStart, unsigned *indexOfMatch)
bool isFunctionOfDim(unsigned position) const
Return true if the affine expression involves AffineDimExpr position.
Definition: AffineExpr.cpp:308
A multi-dimensional affine map Affine map's are immutable like Type's, and they are uniqued.
Definition: AffineMap.h:47
static AffineMap get(MLIRContext *context)
Returns a zero result affine map with no dimensions or symbols: () -> ().
AffineMap shiftDims(unsigned shift, unsigned offset=0) const
Replace dims[offset ...
Definition: AffineMap.h:260
AffineMap shiftSymbols(unsigned shift, unsigned offset=0) const
Replace symbols[offset ...
Definition: AffineMap.h:273
AffineExpr getResult(unsigned idx) const
Definition: AffineMap.cpp:397
This class provides an abstraction over the different types of ranges over Values.
Definition: ValueRange.h:378
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition: Value.h:96
An AffineValueMap is an affine map plus its ML value operands and results for analysis purposes.
Value getOperand(unsigned i) const
bool isMultipleOf(unsigned idx, int64_t factor) const
Return true if the idx^th result can be proved to be a multiple of 'factor', false otherwise.
unsigned getNumSymbols() const
ArrayRef< Value > getOperands() const
unsigned getNumOperands() const
bool isFunctionOf(unsigned idx, Value value) const
Return true if the idx^th result depends on 'value', false otherwise.
void reset(AffineMap map, ValueRange operands, ValueRange results={})
unsigned getNumResults() const
static void difference(const AffineValueMap &a, const AffineValueMap &b, AffineValueMap *res)
Return the value map that is the difference of value maps 'a' and 'b', represented as an affine map a...
void fullyComposeAffineMapAndOperands(AffineMap *map, SmallVectorImpl< Value > *operands)
Given an affine map map and its input operands, this method composes into map, maps of AffineApplyOps...
Definition: AffineOps.cpp:1125
void canonicalizeMapAndOperands(AffineMap *map, SmallVectorImpl< Value > *operands)
Modifies both map and operands in-place so as to:
Definition: AffineOps.cpp:1426
Include the generated interface declarations.
AffineMap simplifyAffineMap(AffineMap map)
Simplifies an affine map by simplifying its underlying AffineExpr results.
Definition: AffineMap.cpp:736
AffineMap getAffineMap() const
Get the AffineMap corresponding to this MutableAffineMap.
Definition: AffineMap.cpp:955
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