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" |
MLIR Team | f85a626 | 2018-06-27 11:03:08 -0700 | [diff] [blame] | 20 | |
| 21 | using namespace mlir; |
Uday Bondhugula | 3934d4d | 2018-07-09 09:00:25 -0700 | [diff] [blame] | 22 | |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame^] | 23 | /// Returns true if this expression is made out of only symbols and |
| 24 | /// constants (no dimensional identifiers). |
Uday Bondhugula | 3934d4d | 2018-07-09 09:00:25 -0700 | [diff] [blame] | 25 | bool AffineExpr::isSymbolic() const { |
| 26 | switch (getKind()) { |
| 27 | case Kind::Constant: |
| 28 | return true; |
| 29 | case Kind::DimId: |
| 30 | return false; |
| 31 | case Kind::SymbolId: |
| 32 | return true; |
| 33 | |
| 34 | case Kind::Add: |
| 35 | case Kind::Sub: |
| 36 | case Kind::Mul: |
| 37 | case Kind::FloorDiv: |
| 38 | case Kind::CeilDiv: |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame^] | 39 | case Kind::Mod: { |
| 40 | auto expr = cast<AffineBinaryOpExpr>(this); |
| 41 | return expr->getLHS()->isSymbolic() && expr->getRHS()->isSymbolic(); |
| 42 | } |
Uday Bondhugula | 3934d4d | 2018-07-09 09:00:25 -0700 | [diff] [blame] | 43 | } |
| 44 | } |
| 45 | |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame^] | 46 | /// Returns true if this is a pure affine expression, i.e., multiplication, |
| 47 | /// floordiv, ceildiv, and mod is only allowed w.r.t constants. |
Uday Bondhugula | 3934d4d | 2018-07-09 09:00:25 -0700 | [diff] [blame] | 48 | bool AffineExpr::isPureAffine() const { |
| 49 | switch (getKind()) { |
| 50 | case Kind::SymbolId: |
Uday Bondhugula | 3934d4d | 2018-07-09 09:00:25 -0700 | [diff] [blame] | 51 | case Kind::DimId: |
Uday Bondhugula | 3934d4d | 2018-07-09 09:00:25 -0700 | [diff] [blame] | 52 | case Kind::Constant: |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame^] | 53 | return true; |
Uday Bondhugula | 3934d4d | 2018-07-09 09:00:25 -0700 | [diff] [blame] | 54 | case Kind::Add: |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame^] | 55 | case Kind::Sub: { |
| 56 | auto op = cast<AffineBinaryOpExpr>(this); |
| 57 | return op->getLHS()->isPureAffine() && op->getRHS()->isPureAffine(); |
Uday Bondhugula | 3934d4d | 2018-07-09 09:00:25 -0700 | [diff] [blame] | 58 | } |
Uday Bondhugula | 3934d4d | 2018-07-09 09:00:25 -0700 | [diff] [blame] | 59 | |
Chris Lattner | 1ac20cb | 2018-07-10 10:59:53 -0700 | [diff] [blame^] | 60 | case Kind::Mul: { |
| 61 | // TODO: Canonicalize the constants in binary operators to the RHS when |
| 62 | // possible, allowing this to merge into the next case. |
| 63 | auto op = cast<AffineBinaryOpExpr>(this); |
| 64 | return op->getLHS()->isPureAffine() && op->getRHS()->isPureAffine() && |
| 65 | (isa<AffineConstantExpr>(op->getLHS()) || |
| 66 | isa<AffineConstantExpr>(op->getRHS())); |
| 67 | } |
| 68 | case Kind::FloorDiv: |
| 69 | case Kind::CeilDiv: |
| 70 | case Kind::Mod: { |
| 71 | auto op = cast<AffineBinaryOpExpr>(this); |
| 72 | return op->getLHS()->isPureAffine() && |
| 73 | isa<AffineConstantExpr>(op->getRHS()); |
| 74 | } |
| 75 | } |
Uday Bondhugula | 3934d4d | 2018-07-09 09:00:25 -0700 | [diff] [blame] | 76 | } |