blob: 696513760d389c5d9a2a5c784b7fb5955d16923b [file] [log] [blame]
Uday Bondhugula257339b2018-08-21 10:32:24 -07001//===- 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 Bondhugulacf4f4c42018-09-12 10:21:23 -070023#include "mlir/Analysis/AffineAnalysis.h"
Uday Bondhugula257339b2018-08-21 10:32:24 -070024#include "mlir/IR/AffineMap.h"
25#include "mlir/IR/IntegerSet.h"
Uday Bondhugula257339b2018-08-21 10:32:24 -070026#include "mlir/IR/StandardOps.h"
27
Uday Bondhugula128c7aa2018-09-04 15:55:38 -070028using namespace mlir;
29
Uday Bondhugula83a41c92018-08-30 17:35:15 -070030MutableAffineMap::MutableAffineMap(AffineMap *map, MLIRContext *context)
31 : numDims(map->getNumDims()), numSymbols(map->getNumSymbols()),
32 context(context) {
Uday Bondhugula257339b2018-08-21 10:32:24 -070033 for (auto *result : map->getResults())
34 results.push_back(result);
35 for (auto *rangeSize : map->getRangeSizes())
36 results.push_back(rangeSize);
37}
38
Uday Bondhugula83a41c92018-08-30 17:35:15 -070039bool MutableAffineMap::isMultipleOf(unsigned idx, int64_t factor) const {
40 if (results[idx]->isMultipleOf(factor))
41 return true;
Uday Bondhugula257339b2018-08-21 10:32:24 -070042
Uday Bondhugula128c7aa2018-09-04 15:55:38 -070043 // TODO(bondhugula): use simplifyAffineExpr and FlatAffineConstraints to
44 // complete this (for a more powerful analysis).
Uday Bondhugulab553adb2018-08-25 17:17:56 -070045 return false;
Uday Bondhugula257339b2018-08-21 10:32:24 -070046}
47
Uday Bondhugula128c7aa2018-09-04 15:55:38 -070048// Simplifies the result affine expressions of this map. The expressions have to
49// be pure for the simplification implemented.
50void 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 Bondhugula83a41c92018-08-30 17:35:15 -070060MutableIntegerSet::MutableIntegerSet(IntegerSet *set, MLIRContext *context)
61 : numDims(set->getNumDims()), numSymbols(set->getNumSymbols()),
62 context(context) {
63 // TODO(bondhugula)
64}
65
66// Universal set.
67MutableIntegerSet::MutableIntegerSet(unsigned numDims, unsigned numSymbols,
68 MLIRContext *context)
69 : numDims(numDims), numSymbols(numSymbols), context(context) {}
70
71AffineValueMap::AffineValueMap(const AffineApplyOp &op, MLIRContext *context)
72 : map(op.getAffineMap(), context) {
73 // TODO: pull operands and results in.
74}
75
76inline bool AffineValueMap::isMultipleOf(unsigned idx, int64_t factor) const {
77 return map.isMultipleOf(idx, factor);
78}
79
Uday Bondhugula257339b2018-08-21 10:32:24 -070080AffineValueMap::~AffineValueMap() {}
81
Uday Bondhugula83a41c92018-08-30 17:35:15 -070082void 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}