blob: 8972510af19217466988552d6275c93647f226cb [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 Bondhugulac1faf662018-07-19 14:08:50 -070041 return AffineBinaryOpExpr::get(Kind::Add, rhs, lhs, context);
Uday Bondhugulae082aad2018-07-11 21:19:31 -070042
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 -070049/// Simplify a multiply expression. Fold it to a constant when possible, and
50/// make the symbolic/constant operand the RHS.
51AffineExpr *AffineBinaryOpExpr::simplifyMul(AffineExpr *lhs, AffineExpr *rhs,
52 MLIRContext *context) {
53 if (auto *l = dyn_cast<AffineConstantExpr>(lhs))
54 if (auto *r = dyn_cast<AffineConstantExpr>(rhs))
55 return AffineConstantExpr::get(l->getValue() * r->getValue(), context);
56
Uday Bondhugulacbe4cca2018-07-19 13:07:16 -070057 assert(lhs->isSymbolicOrConstant() || rhs->isSymbolicOrConstant());
Uday Bondhugulae082aad2018-07-11 21:19:31 -070058
59 // Canonicalize the mul expression so that the constant/symbolic term is the
60 // RHS. If both the lhs and rhs are symbolic, swap them if the lhs is a
61 // constant. (Note that a constant is trivially symbolic).
Uday Bondhugulacbe4cca2018-07-19 13:07:16 -070062 if (!rhs->isSymbolicOrConstant() || isa<AffineConstantExpr>(lhs)) {
Uday Bondhugulae082aad2018-07-11 21:19:31 -070063 // At least one of them has to be symbolic.
Uday Bondhugulac1faf662018-07-19 14:08:50 -070064 return AffineBinaryOpExpr::get(Kind::Mul, rhs, lhs, context);
Uday Bondhugulae082aad2018-07-11 21:19:31 -070065 }
66
67 return nullptr;
68 // TODO(someone): implement some more simplification/canonicalization such as
69 // 1*x is same as x, and in general, move it in the form d_i*expr where d_i is
70 // a dimensional identifier. So, 2*(d0 + 4) + s0*d0 becomes (2 + s0)*d0 + 8.
71}
72
73AffineExpr *AffineBinaryOpExpr::simplifyFloorDiv(AffineExpr *lhs,
74 AffineExpr *rhs,
75 MLIRContext *context) {
76 if (auto *l = dyn_cast<AffineConstantExpr>(lhs))
77 if (auto *r = dyn_cast<AffineConstantExpr>(rhs))
78 return AffineConstantExpr::get(l->getValue() / r->getValue(), context);
79
80 return nullptr;
81 // TODO(someone): implement more simplification along the lines described in
82 // simplifyMod TODO. For eg: 128*N floordiv 128 is N.
83}
84
85AffineExpr *AffineBinaryOpExpr::simplifyCeilDiv(AffineExpr *lhs,
86 AffineExpr *rhs,
87 MLIRContext *context) {
88 if (auto *l = dyn_cast<AffineConstantExpr>(lhs))
89 if (auto *r = dyn_cast<AffineConstantExpr>(rhs))
90 return AffineConstantExpr::get(
91 (int64_t)llvm::divideCeil((uint64_t)l->getValue(),
92 (uint64_t)r->getValue()),
93 context);
94
95 return nullptr;
96 // TODO(someone): implement more simplification along the lines described in
97 // simplifyMod TODO. For eg: 128*N ceildiv 128 is N.
98}
99
100AffineExpr *AffineBinaryOpExpr::simplifyMod(AffineExpr *lhs, AffineExpr *rhs,
101 MLIRContext *context) {
102 if (auto *l = dyn_cast<AffineConstantExpr>(lhs))
103 if (auto *r = dyn_cast<AffineConstantExpr>(rhs))
104 return AffineConstantExpr::get(l->getValue() % r->getValue(), context);
105
106 return nullptr;
107 // TODO(someone): implement more simplification; for eg: 2*x mod 2 is 0; (2*x
108 // + 1) mod 2 is 1. In general, this can be simplified by using the GCD test
109 // iteratively if the RHS of the mod is a small number, or in general using
110 // quantifier elimination (add two new variables q and r, and eliminate all
111 // variables from the linear system other than r.
112}