blob: d8e09b5b22967d3cfe9a5fe6c59b03dc6a498a27 [file] [log] [blame]
MLIR Teamf85a6262018-06-27 11:03:08 -07001//===- 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 Bondhugula3934d4d2018-07-09 09:00:25 -070019#include "mlir/IR/AffineExpr.h"
MLIR Teamf85a6262018-06-27 11:03:08 -070020#include "llvm/ADT/StringRef.h"
Uday Bondhugulae082aad2018-07-11 21:19:31 -070021#include "llvm/Support/MathExtras.h"
MLIR Teamf85a6262018-06-27 11:03:08 -070022
23using namespace mlir;
24
Uday Bondhugula015cbb12018-07-03 20:16:08 -070025AffineMap::AffineMap(unsigned numDims, unsigned numSymbols, unsigned numResults,
Uday Bondhugula0115dbb2018-07-11 21:31:07 -070026 AffineExpr *const *results, AffineExpr *const *rangeSizes)
Uday Bondhugula015cbb12018-07-03 20:16:08 -070027 : numDims(numDims), numSymbols(numSymbols), numResults(numResults),
Uday Bondhugula0115dbb2018-07-11 21:31:07 -070028 results(results), rangeSizes(rangeSizes) {}
Uday Bondhugula3934d4d2018-07-09 09:00:25 -070029
Uday Bondhugulae082aad2018-07-11 21:19:31 -070030/// 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.
33AffineExpr *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
Uday Bondhugulacbe4cca2018-07-19 13:07:16 -070039 if (isa<AffineConstantExpr>(lhs) ||
40 (lhs->isSymbolicOrConstant() && !rhs->isSymbolicOrConstant()))
Uday Bondhugulae082aad2018-07-11 21:19:31 -070041 return AffineAddExpr::get(rhs, lhs, context);
42
Uday Bondhugula3934d4d2018-07-09 09:00:25 -070043 return nullptr;
Uday Bondhugulae082aad2018-07-11 21:19:31 -070044 // TODO(someone): implement more simplification like x + 0 -> x; (x + 2) + 4
45 // -> (x + 6). Do this in a systematic way in conjunction with other
46 // simplifications as opposed to incremental hacks.
Uday Bondhugula3934d4d2018-07-09 09:00:25 -070047}
48
Uday Bondhugulae082aad2018-07-11 21:19:31 -070049AffineExpr *AffineBinaryOpExpr::simplifySub(AffineExpr *lhs, AffineExpr *rhs,
50 MLIRContext *context) {
51 if (auto *l = dyn_cast<AffineConstantExpr>(lhs))
52 if (auto *r = dyn_cast<AffineConstantExpr>(rhs))
53 return AffineConstantExpr::get(l->getValue() - r->getValue(), context);
54
55 return nullptr;
56 // TODO(someone): implement more simplification like mentioned for add.
57}
58
59/// Simplify a multiply expression. Fold it to a constant when possible, and
60/// make the symbolic/constant operand the RHS.
61AffineExpr *AffineBinaryOpExpr::simplifyMul(AffineExpr *lhs, AffineExpr *rhs,
62 MLIRContext *context) {
63 if (auto *l = dyn_cast<AffineConstantExpr>(lhs))
64 if (auto *r = dyn_cast<AffineConstantExpr>(rhs))
65 return AffineConstantExpr::get(l->getValue() * r->getValue(), context);
66
Uday Bondhugulacbe4cca2018-07-19 13:07:16 -070067 assert(lhs->isSymbolicOrConstant() || rhs->isSymbolicOrConstant());
Uday Bondhugulae082aad2018-07-11 21:19:31 -070068
69 // Canonicalize the mul expression so that the constant/symbolic term is the
70 // RHS. If both the lhs and rhs are symbolic, swap them if the lhs is a
71 // constant. (Note that a constant is trivially symbolic).
Uday Bondhugulacbe4cca2018-07-19 13:07:16 -070072 if (!rhs->isSymbolicOrConstant() || isa<AffineConstantExpr>(lhs)) {
Uday Bondhugulae082aad2018-07-11 21:19:31 -070073 // At least one of them has to be symbolic.
74 return AffineMulExpr::get(rhs, lhs, context);
75 }
76
77 return nullptr;
78 // TODO(someone): implement some more simplification/canonicalization such as
79 // 1*x is same as x, and in general, move it in the form d_i*expr where d_i is
80 // a dimensional identifier. So, 2*(d0 + 4) + s0*d0 becomes (2 + s0)*d0 + 8.
81}
82
83AffineExpr *AffineBinaryOpExpr::simplifyFloorDiv(AffineExpr *lhs,
84 AffineExpr *rhs,
85 MLIRContext *context) {
86 if (auto *l = dyn_cast<AffineConstantExpr>(lhs))
87 if (auto *r = dyn_cast<AffineConstantExpr>(rhs))
88 return AffineConstantExpr::get(l->getValue() / r->getValue(), context);
89
90 return nullptr;
91 // TODO(someone): implement more simplification along the lines described in
92 // simplifyMod TODO. For eg: 128*N floordiv 128 is N.
93}
94
95AffineExpr *AffineBinaryOpExpr::simplifyCeilDiv(AffineExpr *lhs,
96 AffineExpr *rhs,
97 MLIRContext *context) {
98 if (auto *l = dyn_cast<AffineConstantExpr>(lhs))
99 if (auto *r = dyn_cast<AffineConstantExpr>(rhs))
100 return AffineConstantExpr::get(
101 (int64_t)llvm::divideCeil((uint64_t)l->getValue(),
102 (uint64_t)r->getValue()),
103 context);
104
105 return nullptr;
106 // TODO(someone): implement more simplification along the lines described in
107 // simplifyMod TODO. For eg: 128*N ceildiv 128 is N.
108}
109
110AffineExpr *AffineBinaryOpExpr::simplifyMod(AffineExpr *lhs, AffineExpr *rhs,
111 MLIRContext *context) {
112 if (auto *l = dyn_cast<AffineConstantExpr>(lhs))
113 if (auto *r = dyn_cast<AffineConstantExpr>(rhs))
114 return AffineConstantExpr::get(l->getValue() % r->getValue(), context);
115
116 return nullptr;
117 // TODO(someone): implement more simplification; for eg: 2*x mod 2 is 0; (2*x
118 // + 1) mod 2 is 1. In general, this can be simplified by using the GCD test
119 // iteratively if the RHS of the mod is a small number, or in general using
120 // quantifier elimination (add two new variables q and r, and eliminate all
121 // variables from the linear system other than r.
122}