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 | |
Uday Bondhugula | e082aad | 2018-07-11 21:19:31 -0700 | [diff] [blame] | 30 | /// Fold to a constant when possible. Canonicalize so that only the RHS is a |
| 31 | /// constant. (4 + d0 becomes d0 + 4). If only one of them is a symbolic |
| 32 | /// expressions, make it the RHS. Return nullptr if it can't be simplified. |
| 33 | AffineExpr *AffineBinaryOpExpr::simplifyAdd(AffineExpr *lhs, AffineExpr *rhs, |
| 34 | MLIRContext *context) { |
| 35 | if (auto *l = dyn_cast<AffineConstantExpr>(lhs)) |
| 36 | if (auto *r = dyn_cast<AffineConstantExpr>(rhs)) |
| 37 | return AffineConstantExpr::get(l->getValue() + r->getValue(), context); |
| 38 | |
| 39 | if (isa<AffineConstantExpr>(lhs) || (lhs->isSymbolic() && !rhs->isSymbolic())) |
| 40 | return AffineAddExpr::get(rhs, lhs, context); |
| 41 | |
Uday Bondhugula | 3934d4d | 2018-07-09 09:00:25 -0700 | [diff] [blame] | 42 | return nullptr; |
Uday Bondhugula | e082aad | 2018-07-11 21:19:31 -0700 | [diff] [blame] | 43 | // TODO(someone): implement more simplification like x + 0 -> x; (x + 2) + 4 |
| 44 | // -> (x + 6). Do this in a systematic way in conjunction with other |
| 45 | // simplifications as opposed to incremental hacks. |
Uday Bondhugula | 3934d4d | 2018-07-09 09:00:25 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Uday Bondhugula | e082aad | 2018-07-11 21:19:31 -0700 | [diff] [blame] | 48 | AffineExpr *AffineBinaryOpExpr::simplifySub(AffineExpr *lhs, AffineExpr *rhs, |
| 49 | MLIRContext *context) { |
| 50 | if (auto *l = dyn_cast<AffineConstantExpr>(lhs)) |
| 51 | if (auto *r = dyn_cast<AffineConstantExpr>(rhs)) |
| 52 | return AffineConstantExpr::get(l->getValue() - r->getValue(), context); |
| 53 | |
| 54 | return nullptr; |
| 55 | // TODO(someone): implement more simplification like mentioned for add. |
| 56 | } |
| 57 | |
| 58 | /// Simplify a multiply expression. Fold it to a constant when possible, and |
| 59 | /// make the symbolic/constant operand the RHS. |
| 60 | AffineExpr *AffineBinaryOpExpr::simplifyMul(AffineExpr *lhs, AffineExpr *rhs, |
| 61 | MLIRContext *context) { |
| 62 | if (auto *l = dyn_cast<AffineConstantExpr>(lhs)) |
| 63 | if (auto *r = dyn_cast<AffineConstantExpr>(rhs)) |
| 64 | return AffineConstantExpr::get(l->getValue() * r->getValue(), context); |
| 65 | |
| 66 | assert(lhs->isSymbolic() || rhs->isSymbolic()); |
| 67 | |
| 68 | // Canonicalize the mul expression so that the constant/symbolic term is the |
| 69 | // RHS. If both the lhs and rhs are symbolic, swap them if the lhs is a |
| 70 | // constant. (Note that a constant is trivially symbolic). |
| 71 | if (!rhs->isSymbolic() || isa<AffineConstantExpr>(lhs)) { |
| 72 | // At least one of them has to be symbolic. |
| 73 | return AffineMulExpr::get(rhs, lhs, context); |
| 74 | } |
| 75 | |
| 76 | return nullptr; |
| 77 | // TODO(someone): implement some more simplification/canonicalization such as |
| 78 | // 1*x is same as x, and in general, move it in the form d_i*expr where d_i is |
| 79 | // a dimensional identifier. So, 2*(d0 + 4) + s0*d0 becomes (2 + s0)*d0 + 8. |
| 80 | } |
| 81 | |
| 82 | AffineExpr *AffineBinaryOpExpr::simplifyFloorDiv(AffineExpr *lhs, |
| 83 | AffineExpr *rhs, |
| 84 | MLIRContext *context) { |
| 85 | if (auto *l = dyn_cast<AffineConstantExpr>(lhs)) |
| 86 | if (auto *r = dyn_cast<AffineConstantExpr>(rhs)) |
| 87 | return AffineConstantExpr::get(l->getValue() / r->getValue(), context); |
| 88 | |
| 89 | return nullptr; |
| 90 | // TODO(someone): implement more simplification along the lines described in |
| 91 | // simplifyMod TODO. For eg: 128*N floordiv 128 is N. |
| 92 | } |
| 93 | |
| 94 | AffineExpr *AffineBinaryOpExpr::simplifyCeilDiv(AffineExpr *lhs, |
| 95 | AffineExpr *rhs, |
| 96 | MLIRContext *context) { |
| 97 | if (auto *l = dyn_cast<AffineConstantExpr>(lhs)) |
| 98 | if (auto *r = dyn_cast<AffineConstantExpr>(rhs)) |
| 99 | return AffineConstantExpr::get( |
| 100 | (int64_t)llvm::divideCeil((uint64_t)l->getValue(), |
| 101 | (uint64_t)r->getValue()), |
| 102 | context); |
| 103 | |
| 104 | return nullptr; |
| 105 | // TODO(someone): implement more simplification along the lines described in |
| 106 | // simplifyMod TODO. For eg: 128*N ceildiv 128 is N. |
| 107 | } |
| 108 | |
| 109 | AffineExpr *AffineBinaryOpExpr::simplifyMod(AffineExpr *lhs, AffineExpr *rhs, |
| 110 | MLIRContext *context) { |
| 111 | if (auto *l = dyn_cast<AffineConstantExpr>(lhs)) |
| 112 | if (auto *r = dyn_cast<AffineConstantExpr>(rhs)) |
| 113 | return AffineConstantExpr::get(l->getValue() % r->getValue(), context); |
| 114 | |
| 115 | return nullptr; |
| 116 | // TODO(someone): implement more simplification; for eg: 2*x mod 2 is 0; (2*x |
| 117 | // + 1) mod 2 is 1. In general, this can be simplified by using the GCD test |
| 118 | // iteratively if the RHS of the mod is a small number, or in general using |
| 119 | // quantifier elimination (add two new variables q and r, and eliminate all |
| 120 | // variables from the linear system other than r. |
| 121 | } |