MLIR Team | f85a626 | 2018-06-27 11:03:08 -0700 | [diff] [blame] | 1 | //===- AffineMap.cpp - MLIR Affine Map Classes ----------------------------===// |
| 2 | // |
| 3 | // Copyright 2019 The MLIR Authors. |
| 4 | // |
| 5 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | // you may not use this file except in compliance with the License. |
| 7 | // You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, software |
| 12 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | // See the License for the specific language governing permissions and |
| 15 | // limitations under the License. |
| 16 | // ============================================================================= |
| 17 | |
| 18 | #include "mlir/IR/AffineMap.h" |
Uday Bondhugula | 3934d4d | 2018-07-09 09:00:25 -0700 | [diff] [blame] | 19 | #include "mlir/IR/AffineExpr.h" |
MLIR Team | f85a626 | 2018-06-27 11:03:08 -0700 | [diff] [blame] | 20 | #include "llvm/ADT/StringRef.h" |
Uday Bondhugula | e082aad | 2018-07-11 21:19:31 -0700 | [diff] [blame] | 21 | #include "llvm/Support/MathExtras.h" |
MLIR Team | f85a626 | 2018-06-27 11:03:08 -0700 | [diff] [blame] | 22 | |
| 23 | using namespace mlir; |
| 24 | |
Uday Bondhugula | 015cbb1 | 2018-07-03 20:16:08 -0700 | [diff] [blame] | 25 | AffineMap::AffineMap(unsigned numDims, unsigned numSymbols, unsigned numResults, |
Uday Bondhugula | 0115dbb | 2018-07-11 21:31:07 -0700 | [diff] [blame] | 26 | AffineExpr *const *results, AffineExpr *const *rangeSizes) |
Uday Bondhugula | 015cbb1 | 2018-07-03 20:16:08 -0700 | [diff] [blame] | 27 | : numDims(numDims), numSymbols(numSymbols), numResults(numResults), |
Uday Bondhugula | 0115dbb | 2018-07-11 21:31:07 -0700 | [diff] [blame] | 28 | results(results), rangeSizes(rangeSizes) {} |
Uday Bondhugula | 3934d4d | 2018-07-09 09:00:25 -0700 | [diff] [blame] | 29 | |
MLIR Team | cfeca5f | 2018-08-14 12:43:51 -0700 | [diff] [blame] | 30 | bool AffineMap::isIdentity() const { |
| 31 | if (getNumDims() != getNumResults()) |
| 32 | return false; |
| 33 | ArrayRef<AffineExpr *> results = getResults(); |
MLIR Team | 04914bc | 2018-08-15 15:14:45 -0700 | [diff] [blame] | 34 | for (unsigned i = 0, numDims = getNumDims(); i < numDims; ++i) { |
| 35 | auto *expr = dyn_cast<AffineDimExpr>(results[i]); |
| 36 | if (!expr || expr->getPosition() != i) |
MLIR Team | cfeca5f | 2018-08-14 12:43:51 -0700 | [diff] [blame] | 37 | return false; |
| 38 | } |
| 39 | return true; |
| 40 | } |
| 41 | |
Tatiana Shpeisman | de8829f | 2018-08-24 23:38:14 -0700 | [diff] [blame] | 42 | bool AffineMap::isSingleConstant() const { |
| 43 | return getNumResults() == 1 && isa<AffineConstantExpr>(getResult(0)); |
| 44 | } |
| 45 | |
Uday Bondhugula | d300400 | 2018-09-11 16:29:24 -0700 | [diff] [blame] | 46 | int64_t AffineMap::getSingleConstantResult() const { |
Tatiana Shpeisman | de8829f | 2018-08-24 23:38:14 -0700 | [diff] [blame] | 47 | assert(isSingleConstant() && "map must have a single constant result"); |
Uday Bondhugula | d300400 | 2018-09-11 16:29:24 -0700 | [diff] [blame] | 48 | return cast<AffineConstantExpr>(getResult(0))->getValue(); |
Tatiana Shpeisman | de8829f | 2018-08-24 23:38:14 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Uday Bondhugula | 970f5b8 | 2018-08-01 22:02:00 -0700 | [diff] [blame] | 51 | /// Simplify add expression. Return nullptr if it can't be simplified. |
Uday Bondhugula | e082aad | 2018-07-11 21:19:31 -0700 | [diff] [blame] | 52 | AffineExpr *AffineBinaryOpExpr::simplifyAdd(AffineExpr *lhs, AffineExpr *rhs, |
| 53 | MLIRContext *context) { |
Uday Bondhugula | 970f5b8 | 2018-08-01 22:02:00 -0700 | [diff] [blame] | 54 | auto *lhsConst = dyn_cast<AffineConstantExpr>(lhs); |
| 55 | auto *rhsConst = dyn_cast<AffineConstantExpr>(rhs); |
Uday Bondhugula | e082aad | 2018-07-11 21:19:31 -0700 | [diff] [blame] | 56 | |
Uday Bondhugula | 970f5b8 | 2018-08-01 22:02:00 -0700 | [diff] [blame] | 57 | // Fold if both LHS, RHS are a constant. |
| 58 | if (lhsConst && rhsConst) |
| 59 | return AffineConstantExpr::get(lhsConst->getValue() + rhsConst->getValue(), |
| 60 | context); |
| 61 | |
| 62 | // Canonicalize so that only the RHS is a constant. (4 + d0 becomes d0 + 4). |
| 63 | // If only one of them is a symbolic expressions, make it the RHS. |
Uday Bondhugula | cbe4cca | 2018-07-19 13:07:16 -0700 | [diff] [blame] | 64 | if (isa<AffineConstantExpr>(lhs) || |
Uday Bondhugula | 970f5b8 | 2018-08-01 22:02:00 -0700 | [diff] [blame] | 65 | (lhs->isSymbolicOrConstant() && !rhs->isSymbolicOrConstant())) { |
Uday Bondhugula | cf4f4c4 | 2018-09-12 10:21:23 -0700 | [diff] [blame^] | 66 | return AffineBinaryOpExpr::getAdd(rhs, lhs, context); |
Uday Bondhugula | 970f5b8 | 2018-08-01 22:02:00 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | // At this point, if there was a constant, it would be on the right. |
| 70 | |
| 71 | // Addition with a zero is a noop, return the other input. |
| 72 | if (rhsConst) { |
| 73 | if (rhsConst->getValue() == 0) |
| 74 | return lhs; |
| 75 | } |
| 76 | // Fold successive additions like (d0 + 2) + 3 into d0 + 5. |
| 77 | auto *lBin = dyn_cast<AffineBinaryOpExpr>(lhs); |
| 78 | if (lBin && rhsConst && lBin->getKind() == Kind::Add) { |
| 79 | if (auto *lrhs = dyn_cast<AffineConstantExpr>(lBin->getRHS())) |
Uday Bondhugula | cf4f4c4 | 2018-09-12 10:21:23 -0700 | [diff] [blame^] | 80 | return AffineBinaryOpExpr::getAdd( |
| 81 | lBin->getLHS(), |
Uday Bondhugula | 970f5b8 | 2018-08-01 22:02:00 -0700 | [diff] [blame] | 82 | AffineConstantExpr::get(lrhs->getValue() + rhsConst->getValue(), |
| 83 | context), |
| 84 | context); |
| 85 | } |
| 86 | |
| 87 | // When doing successive additions, bring constant to the right: turn (d0 + 2) |
| 88 | // + d1 into (d0 + d1) + 2. |
| 89 | if (lBin && lBin->getKind() == Kind::Add) { |
| 90 | if (auto *lrhs = dyn_cast<AffineConstantExpr>(lBin->getRHS())) { |
Uday Bondhugula | cf4f4c4 | 2018-09-12 10:21:23 -0700 | [diff] [blame^] | 91 | return AffineBinaryOpExpr::getAdd( |
| 92 | AffineBinaryOpExpr::getAdd(lBin->getLHS(), rhs, context), lrhs, |
| 93 | context); |
Uday Bondhugula | 970f5b8 | 2018-08-01 22:02:00 -0700 | [diff] [blame] | 94 | } |
| 95 | } |
Uday Bondhugula | e082aad | 2018-07-11 21:19:31 -0700 | [diff] [blame] | 96 | |
Uday Bondhugula | 3934d4d | 2018-07-09 09:00:25 -0700 | [diff] [blame] | 97 | return nullptr; |
Uday Bondhugula | 3934d4d | 2018-07-09 09:00:25 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Uday Bondhugula | 970f5b8 | 2018-08-01 22:02:00 -0700 | [diff] [blame] | 100 | /// Simplify a multiply expression. Return nullptr if it can't be simplified. |
Uday Bondhugula | e082aad | 2018-07-11 21:19:31 -0700 | [diff] [blame] | 101 | AffineExpr *AffineBinaryOpExpr::simplifyMul(AffineExpr *lhs, AffineExpr *rhs, |
| 102 | MLIRContext *context) { |
Uday Bondhugula | 970f5b8 | 2018-08-01 22:02:00 -0700 | [diff] [blame] | 103 | auto *lhsConst = dyn_cast<AffineConstantExpr>(lhs); |
| 104 | auto *rhsConst = dyn_cast<AffineConstantExpr>(rhs); |
| 105 | |
| 106 | if (lhsConst && rhsConst) |
| 107 | return AffineConstantExpr::get(lhsConst->getValue() * rhsConst->getValue(), |
| 108 | context); |
Uday Bondhugula | e082aad | 2018-07-11 21:19:31 -0700 | [diff] [blame] | 109 | |
Uday Bondhugula | cbe4cca | 2018-07-19 13:07:16 -0700 | [diff] [blame] | 110 | assert(lhs->isSymbolicOrConstant() || rhs->isSymbolicOrConstant()); |
Uday Bondhugula | e082aad | 2018-07-11 21:19:31 -0700 | [diff] [blame] | 111 | |
| 112 | // Canonicalize the mul expression so that the constant/symbolic term is the |
| 113 | // RHS. If both the lhs and rhs are symbolic, swap them if the lhs is a |
| 114 | // constant. (Note that a constant is trivially symbolic). |
Uday Bondhugula | cbe4cca | 2018-07-19 13:07:16 -0700 | [diff] [blame] | 115 | if (!rhs->isSymbolicOrConstant() || isa<AffineConstantExpr>(lhs)) { |
Uday Bondhugula | e082aad | 2018-07-11 21:19:31 -0700 | [diff] [blame] | 116 | // At least one of them has to be symbolic. |
Uday Bondhugula | cf4f4c4 | 2018-09-12 10:21:23 -0700 | [diff] [blame^] | 117 | return AffineBinaryOpExpr::getMul(rhs, lhs, context); |
Uday Bondhugula | e082aad | 2018-07-11 21:19:31 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Uday Bondhugula | 970f5b8 | 2018-08-01 22:02:00 -0700 | [diff] [blame] | 120 | // At this point, if there was a constant, it would be on the right. |
| 121 | |
| 122 | // Multiplication with a one is a noop, return the other input. |
| 123 | if (rhsConst) { |
| 124 | if (rhsConst->getValue() == 1) |
| 125 | return lhs; |
| 126 | // Multiplication with zero. |
| 127 | if (rhsConst->getValue() == 0) |
| 128 | return rhsConst; |
| 129 | } |
| 130 | |
| 131 | // Fold successive multiplications: eg: (d0 * 2) * 3 into d0 * 6. |
| 132 | auto *lBin = dyn_cast<AffineBinaryOpExpr>(lhs); |
| 133 | if (lBin && rhsConst && lBin->getKind() == Kind::Mul) { |
| 134 | if (auto *lrhs = dyn_cast<AffineConstantExpr>(lBin->getRHS())) |
Uday Bondhugula | cf4f4c4 | 2018-09-12 10:21:23 -0700 | [diff] [blame^] | 135 | return AffineBinaryOpExpr::getMul( |
| 136 | lBin->getLHS(), |
Uday Bondhugula | 970f5b8 | 2018-08-01 22:02:00 -0700 | [diff] [blame] | 137 | AffineConstantExpr::get(lrhs->getValue() * rhsConst->getValue(), |
| 138 | context), |
| 139 | context); |
| 140 | } |
| 141 | |
| 142 | // When doing successive multiplication, bring constant to the right: turn (d0 |
| 143 | // * 2) * d1 into (d0 * d1) * 2. |
| 144 | if (lBin && lBin->getKind() == Kind::Mul) { |
| 145 | if (auto *lrhs = dyn_cast<AffineConstantExpr>(lBin->getRHS())) { |
Uday Bondhugula | cf4f4c4 | 2018-09-12 10:21:23 -0700 | [diff] [blame^] | 146 | return AffineBinaryOpExpr::getMul( |
| 147 | AffineBinaryOpExpr::getMul(lBin->getLHS(), rhs, context), lrhs, |
| 148 | context); |
Uday Bondhugula | 970f5b8 | 2018-08-01 22:02:00 -0700 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
Uday Bondhugula | e082aad | 2018-07-11 21:19:31 -0700 | [diff] [blame] | 152 | return nullptr; |
Uday Bondhugula | e082aad | 2018-07-11 21:19:31 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | AffineExpr *AffineBinaryOpExpr::simplifyFloorDiv(AffineExpr *lhs, |
| 156 | AffineExpr *rhs, |
| 157 | MLIRContext *context) { |
Uday Bondhugula | 970f5b8 | 2018-08-01 22:02:00 -0700 | [diff] [blame] | 158 | auto *lhsConst = dyn_cast<AffineConstantExpr>(lhs); |
| 159 | auto *rhsConst = dyn_cast<AffineConstantExpr>(rhs); |
| 160 | |
| 161 | if (lhsConst && rhsConst) |
| 162 | return AffineConstantExpr::get(lhsConst->getValue() / rhsConst->getValue(), |
| 163 | context); |
| 164 | |
| 165 | // Fold floordiv of a multiply with a constant that is a multiple of the |
| 166 | // divisor. Eg: (i * 128) floordiv 64 = i * 2. |
| 167 | if (rhsConst) { |
Uday Bondhugula | cf4f4c4 | 2018-09-12 10:21:23 -0700 | [diff] [blame^] | 168 | if (rhsConst->getValue() == 1) |
| 169 | return lhs; |
| 170 | |
Uday Bondhugula | 970f5b8 | 2018-08-01 22:02:00 -0700 | [diff] [blame] | 171 | auto *lBin = dyn_cast<AffineBinaryOpExpr>(lhs); |
| 172 | if (lBin && lBin->getKind() == Kind::Mul) { |
| 173 | if (auto *lrhs = dyn_cast<AffineConstantExpr>(lBin->getRHS())) { |
| 174 | // rhsConst is known to be positive if a constant. |
| 175 | if (lrhs->getValue() % rhsConst->getValue() == 0) |
Uday Bondhugula | cf4f4c4 | 2018-09-12 10:21:23 -0700 | [diff] [blame^] | 176 | return AffineBinaryOpExpr::getMul( |
| 177 | lBin->getLHS(), |
Uday Bondhugula | 970f5b8 | 2018-08-01 22:02:00 -0700 | [diff] [blame] | 178 | AffineConstantExpr::get(lrhs->getValue() / rhsConst->getValue(), |
| 179 | context), |
| 180 | context); |
| 181 | } |
| 182 | } |
| 183 | } |
Uday Bondhugula | e082aad | 2018-07-11 21:19:31 -0700 | [diff] [blame] | 184 | |
| 185 | return nullptr; |
Uday Bondhugula | e082aad | 2018-07-11 21:19:31 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | AffineExpr *AffineBinaryOpExpr::simplifyCeilDiv(AffineExpr *lhs, |
| 189 | AffineExpr *rhs, |
| 190 | MLIRContext *context) { |
Uday Bondhugula | 970f5b8 | 2018-08-01 22:02:00 -0700 | [diff] [blame] | 191 | auto *lhsConst = dyn_cast<AffineConstantExpr>(lhs); |
| 192 | auto *rhsConst = dyn_cast<AffineConstantExpr>(rhs); |
| 193 | |
| 194 | if (lhsConst && rhsConst) |
| 195 | return AffineConstantExpr::get( |
| 196 | (int64_t)llvm::divideCeil((uint64_t)lhsConst->getValue(), |
| 197 | (uint64_t)rhsConst->getValue()), |
| 198 | context); |
| 199 | |
| 200 | // Fold ceildiv of a multiply with a constant that is a multiple of the |
| 201 | // divisor. Eg: (i * 128) ceildiv 64 = i * 2. |
| 202 | if (rhsConst) { |
Uday Bondhugula | cf4f4c4 | 2018-09-12 10:21:23 -0700 | [diff] [blame^] | 203 | if (rhsConst->getValue() == 1) |
| 204 | return lhs; |
| 205 | |
Uday Bondhugula | 970f5b8 | 2018-08-01 22:02:00 -0700 | [diff] [blame] | 206 | auto *lBin = dyn_cast<AffineBinaryOpExpr>(lhs); |
| 207 | if (lBin && lBin->getKind() == Kind::Mul) { |
| 208 | if (auto *lrhs = dyn_cast<AffineConstantExpr>(lBin->getRHS())) { |
| 209 | // rhsConst is known to be positive if a constant. |
| 210 | if (lrhs->getValue() % rhsConst->getValue() == 0) |
Uday Bondhugula | cf4f4c4 | 2018-09-12 10:21:23 -0700 | [diff] [blame^] | 211 | return AffineBinaryOpExpr::getMul( |
| 212 | lBin->getLHS(), |
Uday Bondhugula | 970f5b8 | 2018-08-01 22:02:00 -0700 | [diff] [blame] | 213 | AffineConstantExpr::get(lrhs->getValue() / rhsConst->getValue(), |
| 214 | context), |
| 215 | context); |
| 216 | } |
| 217 | } |
| 218 | } |
Uday Bondhugula | e082aad | 2018-07-11 21:19:31 -0700 | [diff] [blame] | 219 | |
| 220 | return nullptr; |
Uday Bondhugula | e082aad | 2018-07-11 21:19:31 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | AffineExpr *AffineBinaryOpExpr::simplifyMod(AffineExpr *lhs, AffineExpr *rhs, |
| 224 | MLIRContext *context) { |
Uday Bondhugula | 257339b | 2018-08-21 10:32:24 -0700 | [diff] [blame] | 225 | auto *lhsConst = dyn_cast<AffineConstantExpr>(lhs); |
| 226 | auto *rhsConst = dyn_cast<AffineConstantExpr>(rhs); |
| 227 | |
| 228 | if (lhsConst && rhsConst) |
| 229 | return AffineConstantExpr::get(lhsConst->getValue() % rhsConst->getValue(), |
| 230 | context); |
| 231 | |
Uday Bondhugula | 83a41c9 | 2018-08-30 17:35:15 -0700 | [diff] [blame] | 232 | // Fold modulo of an expression that is known to be a multiple of a constant |
| 233 | // to zero if that constant is a multiple of the modulo factor. Eg: (i * 128) |
| 234 | // mod 64 is folded to 0, and less trivially, (i*(j*4*(k*32))) mod 128 = 0. |
Uday Bondhugula | 257339b | 2018-08-21 10:32:24 -0700 | [diff] [blame] | 235 | if (rhsConst) { |
Uday Bondhugula | 83a41c9 | 2018-08-30 17:35:15 -0700 | [diff] [blame] | 236 | // rhsConst is known to be positive if a constant. |
Uday Bondhugula | cf4f4c4 | 2018-09-12 10:21:23 -0700 | [diff] [blame^] | 237 | if (lhs->getLargestKnownDivisor() % rhsConst->getValue() == 0) |
Uday Bondhugula | 83a41c9 | 2018-08-30 17:35:15 -0700 | [diff] [blame] | 238 | return AffineConstantExpr::get(0, context); |
Uday Bondhugula | 257339b | 2018-08-21 10:32:24 -0700 | [diff] [blame] | 239 | } |
Uday Bondhugula | e082aad | 2018-07-11 21:19:31 -0700 | [diff] [blame] | 240 | |
| 241 | return nullptr; |
Uday Bondhugula | 257339b | 2018-08-21 10:32:24 -0700 | [diff] [blame] | 242 | // TODO(bondhugula): In general, this can be simplified more by using the GCD |
| 243 | // test, or in general using quantifier elimination (add two new variables q |
| 244 | // and r, and eliminate all variables from the linear system other than r. All |
| 245 | // of this can be done through mlir/Analysis/'s FlatAffineConstraints. |
Uday Bondhugula | e082aad | 2018-07-11 21:19:31 -0700 | [diff] [blame] | 246 | } |