Uday Bondhugula | 257339b | 2018-08-21 10:32:24 -0700 | [diff] [blame] | 1 | //===- AffineStructures.cpp - MLIR Affine Structures Class-------*- C++ -*-===// |
| 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 | // Structures for affine/polyhedral analysis of MLIR functions. |
| 19 | // |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | |
| 22 | #include "mlir/Analysis/AffineStructures.h" |
Uday Bondhugula | cf4f4c4 | 2018-09-12 10:21:23 -0700 | [diff] [blame^] | 23 | #include "mlir/Analysis/AffineAnalysis.h" |
Uday Bondhugula | 257339b | 2018-08-21 10:32:24 -0700 | [diff] [blame] | 24 | #include "mlir/IR/AffineMap.h" |
| 25 | #include "mlir/IR/IntegerSet.h" |
Uday Bondhugula | 257339b | 2018-08-21 10:32:24 -0700 | [diff] [blame] | 26 | #include "mlir/IR/StandardOps.h" |
| 27 | |
Uday Bondhugula | 128c7aa | 2018-09-04 15:55:38 -0700 | [diff] [blame] | 28 | using namespace mlir; |
| 29 | |
Uday Bondhugula | 83a41c9 | 2018-08-30 17:35:15 -0700 | [diff] [blame] | 30 | MutableAffineMap::MutableAffineMap(AffineMap *map, MLIRContext *context) |
| 31 | : numDims(map->getNumDims()), numSymbols(map->getNumSymbols()), |
| 32 | context(context) { |
Uday Bondhugula | 257339b | 2018-08-21 10:32:24 -0700 | [diff] [blame] | 33 | for (auto *result : map->getResults()) |
| 34 | results.push_back(result); |
| 35 | for (auto *rangeSize : map->getRangeSizes()) |
| 36 | results.push_back(rangeSize); |
| 37 | } |
| 38 | |
Uday Bondhugula | 83a41c9 | 2018-08-30 17:35:15 -0700 | [diff] [blame] | 39 | bool MutableAffineMap::isMultipleOf(unsigned idx, int64_t factor) const { |
| 40 | if (results[idx]->isMultipleOf(factor)) |
| 41 | return true; |
Uday Bondhugula | 257339b | 2018-08-21 10:32:24 -0700 | [diff] [blame] | 42 | |
Uday Bondhugula | 128c7aa | 2018-09-04 15:55:38 -0700 | [diff] [blame] | 43 | // TODO(bondhugula): use simplifyAffineExpr and FlatAffineConstraints to |
| 44 | // complete this (for a more powerful analysis). |
Uday Bondhugula | b553adb | 2018-08-25 17:17:56 -0700 | [diff] [blame] | 45 | return false; |
Uday Bondhugula | 257339b | 2018-08-21 10:32:24 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Uday Bondhugula | 128c7aa | 2018-09-04 15:55:38 -0700 | [diff] [blame] | 48 | // Simplifies the result affine expressions of this map. The expressions have to |
| 49 | // be pure for the simplification implemented. |
| 50 | void MutableAffineMap::simplify() { |
| 51 | // Simplify each of the results if possible. |
| 52 | for (unsigned i = 0, e = getNumResults(); i < e; i++) { |
| 53 | AffineExpr *sExpr = |
| 54 | simplifyAffineExpr(getResult(i), numDims, numSymbols, context); |
| 55 | if (sExpr) |
| 56 | results[i] = sExpr; |
| 57 | } |
| 58 | } |
| 59 | |
Uday Bondhugula | 83a41c9 | 2018-08-30 17:35:15 -0700 | [diff] [blame] | 60 | MutableIntegerSet::MutableIntegerSet(IntegerSet *set, MLIRContext *context) |
| 61 | : numDims(set->getNumDims()), numSymbols(set->getNumSymbols()), |
| 62 | context(context) { |
| 63 | // TODO(bondhugula) |
| 64 | } |
| 65 | |
| 66 | // Universal set. |
| 67 | MutableIntegerSet::MutableIntegerSet(unsigned numDims, unsigned numSymbols, |
| 68 | MLIRContext *context) |
| 69 | : numDims(numDims), numSymbols(numSymbols), context(context) {} |
| 70 | |
| 71 | AffineValueMap::AffineValueMap(const AffineApplyOp &op, MLIRContext *context) |
| 72 | : map(op.getAffineMap(), context) { |
| 73 | // TODO: pull operands and results in. |
| 74 | } |
| 75 | |
| 76 | inline bool AffineValueMap::isMultipleOf(unsigned idx, int64_t factor) const { |
| 77 | return map.isMultipleOf(idx, factor); |
| 78 | } |
| 79 | |
Uday Bondhugula | 257339b | 2018-08-21 10:32:24 -0700 | [diff] [blame] | 80 | AffineValueMap::~AffineValueMap() {} |
| 81 | |
Uday Bondhugula | 83a41c9 | 2018-08-30 17:35:15 -0700 | [diff] [blame] | 82 | void FlatAffineConstraints::addEquality(ArrayRef<int64_t> eq) { |
| 83 | assert(eq.size() == getNumCols()); |
| 84 | unsigned offset = equalities.size(); |
| 85 | equalities.resize(equalities.size() + eq.size()); |
| 86 | for (unsigned i = 0, e = eq.size(); i < e; i++) { |
| 87 | equalities[offset + i] = eq[i]; |
| 88 | } |
| 89 | } |