MLIR Team | f85a626 | 2018-06-27 11:03:08 -0700 | [diff] [blame] | 1 | //===- AffineExpr.cpp - MLIR Affine Expr 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/AffineExpr.h" |
Uday Bondhugula | 3934d4d | 2018-07-09 09:00:25 -0700 | [diff] [blame] | 19 | #include "mlir/Support/STLExtras.h" |
Uday Bondhugula | e082aad | 2018-07-11 21:19:31 -0700 | [diff] [blame] | 20 | #include "third_party/llvm/llvm/include/llvm/ADT/STLExtras.h" |
MLIR Team | f85a626 | 2018-06-27 11:03:08 -0700 | [diff] [blame] | 21 | |
| 22 | using namespace mlir; |
Uday Bondhugula | 3934d4d | 2018-07-09 09:00:25 -0700 | [diff] [blame] | 23 | |
Uday Bondhugula | e082aad | 2018-07-11 21:19:31 -0700 | [diff] [blame] | 24 | AffineBinaryOpExpr::AffineBinaryOpExpr(Kind kind, AffineExpr *lhs, |
| 25 | AffineExpr *rhs) |
| 26 | : AffineExpr(kind), lhs(lhs), rhs(rhs) { |
| 27 | // We verify affine op expr forms at construction time. |
| 28 | switch (kind) { |
| 29 | case Kind::Add: |
| 30 | assert(!isa<AffineConstantExpr>(lhs)); |
Uday Bondhugula | e082aad | 2018-07-11 21:19:31 -0700 | [diff] [blame] | 31 | break; |
| 32 | case Kind::Mul: |
| 33 | assert(!isa<AffineConstantExpr>(lhs)); |
Uday Bondhugula | cbe4cca | 2018-07-19 13:07:16 -0700 | [diff] [blame] | 34 | assert(rhs->isSymbolicOrConstant()); |
Uday Bondhugula | e082aad | 2018-07-11 21:19:31 -0700 | [diff] [blame] | 35 | break; |
| 36 | case Kind::FloorDiv: |
Uday Bondhugula | cbe4cca | 2018-07-19 13:07:16 -0700 | [diff] [blame] | 37 | assert(rhs->isSymbolicOrConstant()); |
Uday Bondhugula | e082aad | 2018-07-11 21:19:31 -0700 | [diff] [blame] | 38 | break; |
| 39 | case Kind::CeilDiv: |
Uday Bondhugula | cbe4cca | 2018-07-19 13:07:16 -0700 | [diff] [blame] | 40 | assert(rhs->isSymbolicOrConstant()); |
Uday Bondhugula | e082aad | 2018-07-11 21:19:31 -0700 | [diff] [blame] | 41 | break; |
| 42 | case Kind::Mod: |
Uday Bondhugula | cbe4cca | 2018-07-19 13:07:16 -0700 | [diff] [blame] | 43 | assert(rhs->isSymbolicOrConstant()); |
Uday Bondhugula | e082aad | 2018-07-11 21:19:31 -0700 | [diff] [blame] | 44 | break; |
| 45 | default: |
| 46 | llvm_unreachable("unexpected binary affine expr"); |
| 47 | } |
| 48 | } |
| 49 | |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 50 | /// Returns true if this expression is made out of only symbols and |
| 51 | /// constants (no dimensional identifiers). |
Uday Bondhugula | cbe4cca | 2018-07-19 13:07:16 -0700 | [diff] [blame] | 52 | bool AffineExpr::isSymbolicOrConstant() const { |
Uday Bondhugula | 3934d4d | 2018-07-09 09:00:25 -0700 | [diff] [blame] | 53 | switch (getKind()) { |
| 54 | case Kind::Constant: |
| 55 | return true; |
| 56 | case Kind::DimId: |
| 57 | return false; |
| 58 | case Kind::SymbolId: |
| 59 | return true; |
| 60 | |
| 61 | case Kind::Add: |
Uday Bondhugula | 3934d4d | 2018-07-09 09:00:25 -0700 | [diff] [blame] | 62 | case Kind::Mul: |
| 63 | case Kind::FloorDiv: |
| 64 | case Kind::CeilDiv: |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 65 | case Kind::Mod: { |
| 66 | auto expr = cast<AffineBinaryOpExpr>(this); |
Uday Bondhugula | cbe4cca | 2018-07-19 13:07:16 -0700 | [diff] [blame] | 67 | return expr->getLHS()->isSymbolicOrConstant() && |
| 68 | expr->getRHS()->isSymbolicOrConstant(); |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 69 | } |
Uday Bondhugula | 3934d4d | 2018-07-09 09:00:25 -0700 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 73 | /// Returns true if this is a pure affine expression, i.e., multiplication, |
| 74 | /// floordiv, ceildiv, and mod is only allowed w.r.t constants. |
Uday Bondhugula | 3934d4d | 2018-07-09 09:00:25 -0700 | [diff] [blame] | 75 | bool AffineExpr::isPureAffine() const { |
| 76 | switch (getKind()) { |
| 77 | case Kind::SymbolId: |
Uday Bondhugula | 3934d4d | 2018-07-09 09:00:25 -0700 | [diff] [blame] | 78 | case Kind::DimId: |
Uday Bondhugula | 3934d4d | 2018-07-09 09:00:25 -0700 | [diff] [blame] | 79 | case Kind::Constant: |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 80 | return true; |
Uday Bondhugula | c1faf66 | 2018-07-19 14:08:50 -0700 | [diff] [blame] | 81 | case Kind::Add: { |
| 82 | auto *op = cast<AffineBinaryOpExpr>(this); |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 83 | return op->getLHS()->isPureAffine() && op->getRHS()->isPureAffine(); |
Uday Bondhugula | 3934d4d | 2018-07-09 09:00:25 -0700 | [diff] [blame] | 84 | } |
Uday Bondhugula | 3934d4d | 2018-07-09 09:00:25 -0700 | [diff] [blame] | 85 | |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 86 | case Kind::Mul: { |
| 87 | // TODO: Canonicalize the constants in binary operators to the RHS when |
| 88 | // possible, allowing this to merge into the next case. |
Uday Bondhugula | c1faf66 | 2018-07-19 14:08:50 -0700 | [diff] [blame] | 89 | auto *op = cast<AffineBinaryOpExpr>(this); |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 90 | return op->getLHS()->isPureAffine() && op->getRHS()->isPureAffine() && |
| 91 | (isa<AffineConstantExpr>(op->getLHS()) || |
| 92 | isa<AffineConstantExpr>(op->getRHS())); |
| 93 | } |
| 94 | case Kind::FloorDiv: |
| 95 | case Kind::CeilDiv: |
| 96 | case Kind::Mod: { |
Uday Bondhugula | c1faf66 | 2018-07-19 14:08:50 -0700 | [diff] [blame] | 97 | auto *op = cast<AffineBinaryOpExpr>(this); |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame] | 98 | return op->getLHS()->isPureAffine() && |
| 99 | isa<AffineConstantExpr>(op->getRHS()); |
| 100 | } |
| 101 | } |
Uday Bondhugula | 3934d4d | 2018-07-09 09:00:25 -0700 | [diff] [blame] | 102 | } |