1 //===- PWMAFunction.cpp - MLIR PWMAFunction 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 9 #include "mlir/Analysis/Presburger/PWMAFunction.h" 10 #include "mlir/Analysis/Presburger/Simplex.h" 11 12 using namespace mlir; 13 using namespace presburger; 14 15 // Return the result of subtracting the two given vectors pointwise. 16 // The vectors must be of the same size. 17 // e.g., [3, 4, 6] - [2, 5, 1] = [1, -1, 5]. 18 static SmallVector<int64_t, 8> subtract(ArrayRef<int64_t> vecA, 19 ArrayRef<int64_t> vecB) { 20 assert(vecA.size() == vecB.size() && 21 "Cannot subtract vectors of differing lengths!"); 22 SmallVector<int64_t, 8> result; 23 result.reserve(vecA.size()); 24 for (unsigned i = 0, e = vecA.size(); i < e; ++i) 25 result.push_back(vecA[i] - vecB[i]); 26 return result; 27 } 28 29 PresburgerSet PWMAFunction::getDomain() const { 30 PresburgerSet domain = PresburgerSet::getEmpty(getSpace()); 31 for (const MultiAffineFunction &piece : pieces) 32 domain.unionInPlace(piece.getDomain()); 33 return domain; 34 } 35 36 Optional<SmallVector<int64_t, 8>> 37 MultiAffineFunction::valueAt(ArrayRef<int64_t> point) const { 38 assert(point.size() == getNumDimAndSymbolIds() && 39 "Point has incorrect dimensionality!"); 40 41 Optional<SmallVector<int64_t, 8>> maybeLocalValues = 42 getDomain().containsPointNoLocal(point); 43 if (!maybeLocalValues) 44 return {}; 45 46 // The point lies in the domain, so we need to compute the output value. 47 SmallVector<int64_t, 8> pointHomogenous{llvm::to_vector(point)}; 48 // The given point didn't include the values of locals which the output is a 49 // function of; we have computed one possible set of values and use them 50 // here. The function is not allowed to have local ids that take more than 51 // one possible value. 52 pointHomogenous.append(*maybeLocalValues); 53 // The matrix `output` has an affine expression in the ith row, corresponding 54 // to the expression for the ith value in the output vector. The last column 55 // of the matrix contains the constant term. Let v be the input point with 56 // a 1 appended at the end. We can see that output * v gives the desired 57 // output vector. 58 pointHomogenous.push_back(1); 59 SmallVector<int64_t, 8> result = 60 output.postMultiplyWithColumn(pointHomogenous); 61 assert(result.size() == getNumOutputs()); 62 return result; 63 } 64 65 Optional<SmallVector<int64_t, 8>> 66 PWMAFunction::valueAt(ArrayRef<int64_t> point) const { 67 assert(point.size() == getNumInputs() && 68 "Point has incorrect dimensionality!"); 69 for (const MultiAffineFunction &piece : pieces) 70 if (Optional<SmallVector<int64_t, 8>> output = piece.valueAt(point)) 71 return output; 72 return {}; 73 } 74 75 void MultiAffineFunction::print(raw_ostream &os) const { 76 os << "Domain:"; 77 IntegerPolyhedron::print(os); 78 os << "Output:\n"; 79 output.print(os); 80 os << "\n"; 81 } 82 83 void MultiAffineFunction::dump() const { print(llvm::errs()); } 84 85 bool MultiAffineFunction::isEqual(const MultiAffineFunction &other) const { 86 return isSpaceCompatible(other) && getDomain().isEqual(other.getDomain()) && 87 isEqualWhereDomainsOverlap(other); 88 } 89 90 unsigned MultiAffineFunction::insertId(IdKind kind, unsigned pos, 91 unsigned num) { 92 assert((kind != IdKind::Domain || num == 0) && 93 "Domain has to be zero in a set"); 94 unsigned absolutePos = getIdKindOffset(kind) + pos; 95 output.insertColumns(absolutePos, num); 96 return IntegerPolyhedron::insertId(kind, pos, num); 97 } 98 99 void MultiAffineFunction::swapId(unsigned posA, unsigned posB) { 100 output.swapColumns(posA, posB); 101 IntegerPolyhedron::swapId(posA, posB); 102 } 103 104 void MultiAffineFunction::removeIdRange(IdKind kind, unsigned idStart, 105 unsigned idLimit) { 106 output.removeColumns(idStart + getIdKindOffset(kind), idLimit - idStart); 107 IntegerPolyhedron::removeIdRange(kind, idStart, idLimit); 108 } 109 110 void MultiAffineFunction::eliminateRedundantLocalId(unsigned posA, 111 unsigned posB) { 112 unsigned localOffset = getIdKindOffset(IdKind::Local); 113 output.addToColumn(localOffset + posB, localOffset + posA, /*scale=*/1); 114 IntegerPolyhedron::eliminateRedundantLocalId(posA, posB); 115 } 116 117 void MultiAffineFunction::truncateOutput(unsigned count) { 118 assert(count <= output.getNumRows()); 119 output.resizeVertically(count); 120 } 121 122 void PWMAFunction::truncateOutput(unsigned count) { 123 assert(count <= numOutputs); 124 for (MultiAffineFunction &piece : pieces) 125 piece.truncateOutput(count); 126 numOutputs = count; 127 } 128 129 bool MultiAffineFunction::isEqualWhereDomainsOverlap( 130 MultiAffineFunction other) const { 131 if (!isSpaceCompatible(other)) 132 return false; 133 134 // `commonFunc` has the same output as `this`. 135 MultiAffineFunction commonFunc = *this; 136 // After this merge, `commonFunc` and `other` have the same local ids; they 137 // are merged. 138 commonFunc.mergeLocalIds(other); 139 // After this, the domain of `commonFunc` will be the intersection of the 140 // domains of `this` and `other`. 141 commonFunc.IntegerPolyhedron::append(other); 142 143 // `commonDomainMatching` contains the subset of the common domain 144 // where the outputs of `this` and `other` match. 145 // 146 // We want to add constraints equating the outputs of `this` and `other`. 147 // However, `this` may have difference local ids from `other`, whereas we 148 // need both to have the same locals. Accordingly, we use `commonFunc.output` 149 // in place of `this->output`, since `commonFunc` has the same output but also 150 // has its locals merged. 151 IntegerPolyhedron commonDomainMatching = commonFunc.getDomain(); 152 for (unsigned row = 0, e = getNumOutputs(); row < e; ++row) 153 commonDomainMatching.addEquality( 154 subtract(commonFunc.output.getRow(row), other.output.getRow(row))); 155 156 // If the whole common domain is a subset of commonDomainMatching, then they 157 // are equal and the two functions match on the whole common domain. 158 return commonFunc.getDomain().isSubsetOf(commonDomainMatching); 159 } 160 161 /// Two PWMAFunctions are equal if they have the same dimensionalities, 162 /// the same domain, and take the same value at every point in the domain. 163 bool PWMAFunction::isEqual(const PWMAFunction &other) const { 164 if (!isSpaceCompatible(other)) 165 return false; 166 167 if (!this->getDomain().isEqual(other.getDomain())) 168 return false; 169 170 // Check if, whenever the domains of a piece of `this` and a piece of `other` 171 // overlap, they take the same output value. If `this` and `other` have the 172 // same domain (checked above), then this check passes iff the two functions 173 // have the same output at every point in the domain. 174 for (const MultiAffineFunction &aPiece : this->pieces) 175 for (const MultiAffineFunction &bPiece : other.pieces) 176 if (!aPiece.isEqualWhereDomainsOverlap(bPiece)) 177 return false; 178 return true; 179 } 180 181 void PWMAFunction::addPiece(const MultiAffineFunction &piece) { 182 assert(piece.isSpaceCompatible(*this) && 183 "Piece to be added is not compatible with this PWMAFunction!"); 184 assert(piece.isConsistent() && "Piece is internally inconsistent!"); 185 assert(this->getDomain() 186 .intersect(PresburgerSet(piece.getDomain())) 187 .isIntegerEmpty() && 188 "New piece's domain overlaps with that of existing pieces!"); 189 pieces.push_back(piece); 190 } 191 192 void PWMAFunction::addPiece(const IntegerPolyhedron &domain, 193 const Matrix &output) { 194 addPiece(MultiAffineFunction(domain, output)); 195 } 196 197 void PWMAFunction::print(raw_ostream &os) const { 198 os << pieces.size() << " pieces:\n"; 199 for (const MultiAffineFunction &piece : pieces) 200 piece.print(os); 201 } 202 203 void PWMAFunction::dump() const { print(llvm::errs()); } 204