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 space.isCompatible(other.getSpace()) && 87 getDomain().isEqual(other.getDomain()) && 88 isEqualWhereDomainsOverlap(other); 89 } 90 91 unsigned MultiAffineFunction::insertId(IdKind kind, unsigned pos, 92 unsigned num) { 93 assert((kind != IdKind::Domain || num == 0) && 94 "Domain has to be zero in a set"); 95 unsigned absolutePos = getIdKindOffset(kind) + pos; 96 output.insertColumns(absolutePos, num); 97 return IntegerPolyhedron::insertId(kind, pos, num); 98 } 99 100 void MultiAffineFunction::swapId(unsigned posA, unsigned posB) { 101 output.swapColumns(posA, posB); 102 IntegerPolyhedron::swapId(posA, posB); 103 } 104 105 void MultiAffineFunction::removeIdRange(IdKind kind, unsigned idStart, 106 unsigned idLimit) { 107 output.removeColumns(idStart + getIdKindOffset(kind), idLimit - idStart); 108 IntegerPolyhedron::removeIdRange(kind, idStart, idLimit); 109 } 110 111 void MultiAffineFunction::eliminateRedundantLocalId(unsigned posA, 112 unsigned posB) { 113 unsigned localOffset = getIdKindOffset(IdKind::Local); 114 output.addToColumn(localOffset + posB, localOffset + posA, /*scale=*/1); 115 IntegerPolyhedron::eliminateRedundantLocalId(posA, posB); 116 } 117 118 void MultiAffineFunction::truncateOutput(unsigned count) { 119 assert(count <= output.getNumRows()); 120 output.resizeVertically(count); 121 } 122 123 void PWMAFunction::truncateOutput(unsigned count) { 124 assert(count <= numOutputs); 125 for (MultiAffineFunction &piece : pieces) 126 piece.truncateOutput(count); 127 numOutputs = count; 128 } 129 130 bool MultiAffineFunction::isEqualWhereDomainsOverlap( 131 MultiAffineFunction other) const { 132 if (!space.isCompatible(other.getSpace())) 133 return false; 134 135 // `commonFunc` has the same output as `this`. 136 MultiAffineFunction commonFunc = *this; 137 // After this merge, `commonFunc` and `other` have the same local ids; they 138 // are merged. 139 commonFunc.mergeLocalIds(other); 140 // After this, the domain of `commonFunc` will be the intersection of the 141 // domains of `this` and `other`. 142 commonFunc.IntegerPolyhedron::append(other); 143 144 // `commonDomainMatching` contains the subset of the common domain 145 // where the outputs of `this` and `other` match. 146 // 147 // We want to add constraints equating the outputs of `this` and `other`. 148 // However, `this` may have difference local ids from `other`, whereas we 149 // need both to have the same locals. Accordingly, we use `commonFunc.output` 150 // in place of `this->output`, since `commonFunc` has the same output but also 151 // has its locals merged. 152 IntegerPolyhedron commonDomainMatching = commonFunc.getDomain(); 153 for (unsigned row = 0, e = getNumOutputs(); row < e; ++row) 154 commonDomainMatching.addEquality( 155 subtract(commonFunc.output.getRow(row), other.output.getRow(row))); 156 157 // If the whole common domain is a subset of commonDomainMatching, then they 158 // are equal and the two functions match on the whole common domain. 159 return commonFunc.getDomain().isSubsetOf(commonDomainMatching); 160 } 161 162 /// Two PWMAFunctions are equal if they have the same dimensionalities, 163 /// the same domain, and take the same value at every point in the domain. 164 bool PWMAFunction::isEqual(const PWMAFunction &other) const { 165 if (!space.isCompatible(other.space)) 166 return false; 167 168 if (!this->getDomain().isEqual(other.getDomain())) 169 return false; 170 171 // Check if, whenever the domains of a piece of `this` and a piece of `other` 172 // overlap, they take the same output value. If `this` and `other` have the 173 // same domain (checked above), then this check passes iff the two functions 174 // have the same output at every point in the domain. 175 for (const MultiAffineFunction &aPiece : this->pieces) 176 for (const MultiAffineFunction &bPiece : other.pieces) 177 if (!aPiece.isEqualWhereDomainsOverlap(bPiece)) 178 return false; 179 return true; 180 } 181 182 void PWMAFunction::addPiece(const MultiAffineFunction &piece) { 183 assert(space.isCompatible(piece.getSpace()) && 184 "Piece to be added is not compatible with this PWMAFunction!"); 185 assert(piece.isConsistent() && "Piece is internally inconsistent!"); 186 assert(this->getDomain() 187 .intersect(PresburgerSet(piece.getDomain())) 188 .isIntegerEmpty() && 189 "New piece's domain overlaps with that of existing pieces!"); 190 pieces.push_back(piece); 191 } 192 193 void PWMAFunction::addPiece(const IntegerPolyhedron &domain, 194 const Matrix &output) { 195 addPiece(MultiAffineFunction(domain, output)); 196 } 197 198 void PWMAFunction::print(raw_ostream &os) const { 199 os << pieces.size() << " pieces:\n"; 200 for (const MultiAffineFunction &piece : pieces) 201 piece.print(os); 202 } 203 204 void PWMAFunction::dump() const { print(llvm::errs()); } 205