blob: 8406a37d7938fe8a089051d07e700b2d2e569963 [file] [log] [blame]
Uday Bondhugulacf4f4c42018-09-12 10:21:23 -07001//===- LoopAnalysis.cpp - Misc loop analysis routines //-------------------===//
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// This file implements miscellaneous loop analysis routines.
19//
20//===----------------------------------------------------------------------===//
21
22#include "mlir/Analysis/LoopAnalysis.h"
23
24#include "mlir/Analysis/AffineAnalysis.h"
Nicolas Vasilachefd8d2562018-10-17 18:01:44 -070025#include "mlir/Analysis/AffineStructures.h"
26#include "mlir/Analysis/MLFunctionMatcher.h"
Nicolas Vasilache13b3bce2018-11-20 08:36:07 -080027#include "mlir/Analysis/VectorAnalysis.h"
Nicolas Vasilachefd8d2562018-10-17 18:01:44 -070028#include "mlir/IR/Builders.h"
29#include "mlir/IR/BuiltinOps.h"
Uday Bondhugulacf4f4c42018-09-12 10:21:23 -070030#include "mlir/IR/Statements.h"
Nicolas Vasilachefd8d2562018-10-17 18:01:44 -070031#include "mlir/StandardOps/StandardOps.h"
Nicolas Vasilache078d9b92018-10-30 07:54:23 -070032#include "mlir/Support/Functional.h"
Uday Bondhugula48e4c4b2018-10-03 10:07:54 -070033#include "mlir/Support/MathExtras.h"
Nicolas Vasilache6b197462018-11-14 04:04:10 -080034#include "llvm/ADT/SmallString.h"
Uday Bondhugulacf4f4c42018-09-12 10:21:23 -070035
Nicolas Vasilache5373b092018-10-03 15:39:12 -070036using namespace mlir;
Uday Bondhugulacf4f4c42018-09-12 10:21:23 -070037
38/// Returns the trip count of the loop as an affine expression if the latter is
39/// expressible as an affine expression, and nullptr otherwise. The trip count
40/// expression is simplified before returning.
Nicolas Vasilachefb11e0e2018-10-08 13:47:18 -070041AffineExpr mlir::getTripCountExpr(const ForStmt &forStmt) {
Nicolas Vasilacheff303282018-11-07 05:44:50 -080042 // upper_bound - lower_bound
Uday Bondhugulacf4f4c42018-09-12 10:21:23 -070043 int64_t loopSpan;
44
45 int64_t step = forStmt.getStep();
46 auto *context = forStmt.getContext();
47
48 if (forStmt.hasConstantBounds()) {
49 int64_t lb = forStmt.getConstantLowerBound();
50 int64_t ub = forStmt.getConstantUpperBound();
Nicolas Vasilacheff303282018-11-07 05:44:50 -080051 loopSpan = ub - lb;
Uday Bondhugulacf4f4c42018-09-12 10:21:23 -070052 } else {
Nicolas Vasilache75ed3372018-10-09 16:39:24 -070053 auto lbMap = forStmt.getLowerBoundMap();
54 auto ubMap = forStmt.getUpperBoundMap();
Uday Bondhugulacf4f4c42018-09-12 10:21:23 -070055 // TODO(bondhugula): handle max/min of multiple expressions.
Nicolas Vasilache75ed3372018-10-09 16:39:24 -070056 if (lbMap.getNumResults() != 1 || ubMap.getNumResults() != 1)
Uday Bondhugulacf4f4c42018-09-12 10:21:23 -070057 return nullptr;
Uday Bondhugulacf4f4c42018-09-12 10:21:23 -070058
59 // TODO(bondhugula): handle bounds with different operands.
Uday Bondhugulacf4f4c42018-09-12 10:21:23 -070060 // Bounds have different operands, unhandled for now.
Uday Bondhugula5912e872018-09-18 10:22:03 -070061 if (!forStmt.matchingBoundOperandList())
Uday Bondhugulacf4f4c42018-09-12 10:21:23 -070062 return nullptr;
63
Nicolas Vasilacheff303282018-11-07 05:44:50 -080064 // ub_expr - lb_expr
Nicolas Vasilache75ed3372018-10-09 16:39:24 -070065 AffineExpr lbExpr(lbMap.getResult(0));
66 AffineExpr ubExpr(ubMap.getResult(0));
Nicolas Vasilache5373b092018-10-03 15:39:12 -070067 auto loopSpanExpr = simplifyAffineExpr(
Nicolas Vasilacheff303282018-11-07 05:44:50 -080068 ubExpr - lbExpr, std::max(lbMap.getNumDims(), ubMap.getNumDims()),
Nicolas Vasilache75ed3372018-10-09 16:39:24 -070069 std::max(lbMap.getNumSymbols(), ubMap.getNumSymbols()));
Nicolas Vasilachefb11e0e2018-10-08 13:47:18 -070070 auto cExpr = loopSpanExpr.dyn_cast<AffineConstantExpr>();
Uday Bondhugulacf4f4c42018-09-12 10:21:23 -070071 if (!cExpr)
Nicolas Vasilachebc746092018-10-08 10:20:25 -070072 return loopSpanExpr.ceilDiv(step);
Nicolas Vasilacheb7717092018-10-09 10:59:27 -070073 loopSpan = cExpr.getValue();
Uday Bondhugulacf4f4c42018-09-12 10:21:23 -070074 }
75
76 // 0 iteration loops.
Uday Bondhugulaff5d6bd2018-09-27 18:03:27 -070077 if (loopSpan < 0)
Uday Bondhugulacf4f4c42018-09-12 10:21:23 -070078 return 0;
79
Nicolas Vasilachebc746092018-10-08 10:20:25 -070080 return getAffineConstantExpr(static_cast<uint64_t>(ceilDiv(loopSpan, step)),
81 context);
Uday Bondhugulacf4f4c42018-09-12 10:21:23 -070082}
83
84/// Returns the trip count of the loop if it's a constant, None otherwise. This
85/// method uses affine expression analysis (in turn using getTripCount) and is
86/// able to determine constant trip count in non-trivial cases.
87llvm::Optional<uint64_t> mlir::getConstantTripCount(const ForStmt &forStmt) {
Nicolas Vasilache5373b092018-10-03 15:39:12 -070088 auto tripCountExpr = getTripCountExpr(forStmt);
Uday Bondhugulacf4f4c42018-09-12 10:21:23 -070089
Nicolas Vasilache32402e52018-10-08 08:09:50 -070090 if (!tripCountExpr)
91 return None;
92
Nicolas Vasilachefb11e0e2018-10-08 13:47:18 -070093 if (auto constExpr = tripCountExpr.dyn_cast<AffineConstantExpr>())
Nicolas Vasilacheb7717092018-10-09 10:59:27 -070094 return constExpr.getValue();
Uday Bondhugulacf4f4c42018-09-12 10:21:23 -070095
96 return None;
97}
98
99/// Returns the greatest known integral divisor of the trip count. Affine
100/// expression analysis is used (indirectly through getTripCount), and
101/// this method is thus able to determine non-trivial divisors.
102uint64_t mlir::getLargestDivisorOfTripCount(const ForStmt &forStmt) {
Nicolas Vasilache5373b092018-10-03 15:39:12 -0700103 auto tripCountExpr = getTripCountExpr(forStmt);
Uday Bondhugulacf4f4c42018-09-12 10:21:23 -0700104
105 if (!tripCountExpr)
106 return 1;
107
Nicolas Vasilachefb11e0e2018-10-08 13:47:18 -0700108 if (auto constExpr = tripCountExpr.dyn_cast<AffineConstantExpr>()) {
Nicolas Vasilacheb7717092018-10-09 10:59:27 -0700109 uint64_t tripCount = constExpr.getValue();
Uday Bondhugulacf4f4c42018-09-12 10:21:23 -0700110
111 // 0 iteration loops (greatest divisor is 2^64 - 1).
112 if (tripCount == 0)
113 return ULONG_MAX;
114
115 // The greatest divisor is the trip count.
116 return tripCount;
117 }
118
119 // Trip count is not a known constant; return its largest known divisor.
Nicolas Vasilacheb7717092018-10-09 10:59:27 -0700120 return tripCountExpr.getLargestKnownDivisor();
Uday Bondhugulacf4f4c42018-09-12 10:21:23 -0700121}
Nicolas Vasilachefd8d2562018-10-17 18:01:44 -0700122
River Riddle666dfbe2018-10-30 14:59:22 -0700123bool mlir::isAccessInvariant(const MLValue &input, MemRefType memRefType,
Nicolas Vasilached64816a2018-11-01 07:14:14 -0700124 ArrayRef<const MLValue *> indices, unsigned dim) {
River Riddle666dfbe2018-10-30 14:59:22 -0700125 assert(indices.size() == memRefType.getRank());
Nicolas Vasilachefd8d2562018-10-17 18:01:44 -0700126 assert(dim < indices.size());
River Riddle666dfbe2018-10-30 14:59:22 -0700127 auto layoutMap = memRefType.getAffineMaps();
128 assert(memRefType.getAffineMaps().size() <= 1);
Nicolas Vasilached64816a2018-11-01 07:14:14 -0700129 // TODO(ntv): remove dependence on Builder once we support non-identity
Nicolas Vasilachefd8d2562018-10-17 18:01:44 -0700130 // layout map.
River Riddle666dfbe2018-10-30 14:59:22 -0700131 Builder b(memRefType.getContext());
Nicolas Vasilachefd8d2562018-10-17 18:01:44 -0700132 assert(layoutMap.empty() ||
133 layoutMap[0] == b.getMultiDimIdentityMap(indices.size()));
Uday Bondhugula861fe642018-10-18 11:14:26 -0700134 (void)layoutMap;
Nicolas Vasilachefd8d2562018-10-17 18:01:44 -0700135
136 SmallVector<OperationStmt *, 4> affineApplyOps;
Nicolas Vasilached64816a2018-11-01 07:14:14 -0700137 getReachableAffineApplyOps({const_cast<MLValue *>(indices[dim])},
138 affineApplyOps);
Nicolas Vasilachefd8d2562018-10-17 18:01:44 -0700139
140 if (affineApplyOps.empty()) {
141 // Pointer equality test because of MLValue pointer semantics.
Nicolas Vasilache078d9b92018-10-30 07:54:23 -0700142 return indices[dim] != &input;
Nicolas Vasilachefd8d2562018-10-17 18:01:44 -0700143 }
144
145 assert(affineApplyOps.size() == 1 &&
146 "CompositionAffineMapsPass must have "
147 "been run: there should be at most one AffineApplyOp");
Feng Liuec065d72018-10-19 09:07:58 -0700148 auto composeOp = affineApplyOps[0]->cast<AffineApplyOp>();
Nicolas Vasilache078d9b92018-10-30 07:54:23 -0700149 // We need yet another level of indirection because the `dim` index of the
150 // access may not correspond to the `dim` index of composeOp.
151 unsigned idx = std::numeric_limits<unsigned>::max();
152 unsigned numResults = composeOp->getNumResults();
153 for (unsigned i = 0; i < numResults; ++i) {
154 if (indices[dim] == composeOp->getResult(i)) {
155 idx = i;
156 break;
157 }
158 }
159 assert(idx < std::numeric_limits<unsigned>::max());
160 return !AffineValueMap(*composeOp)
161 .isFunctionOf(idx, &const_cast<MLValue &>(input));
Nicolas Vasilachefd8d2562018-10-17 18:01:44 -0700162}
163
164/// Determines whether a load or a store has a contiguous access along the
165/// value `input`. Contiguous is defined as either invariant or varying only
166/// along the fastest varying memory dimension.
167// TODO(ntv): allow more advanced notions of contiguity (non-fastest varying,
168// check strides, ...).
169template <typename LoadOrStoreOpPointer>
Nicolas Vasilache078d9b92018-10-30 07:54:23 -0700170static bool isContiguousAccess(const MLValue &input,
171 LoadOrStoreOpPointer memoryOp,
172 unsigned fastestVaryingDim) {
173 using namespace functional;
Nicolas Vasilached64816a2018-11-01 07:14:14 -0700174 auto indices = map([](const SSAValue *val) { return dyn_cast<MLValue>(val); },
Nicolas Vasilache078d9b92018-10-30 07:54:23 -0700175 memoryOp->getIndices());
River Riddle666dfbe2018-10-30 14:59:22 -0700176 auto memRefType = memoryOp->getMemRefType();
Nicolas Vasilache078d9b92018-10-30 07:54:23 -0700177 for (unsigned d = 0, numIndices = indices.size(); d < numIndices; ++d) {
178 if (fastestVaryingDim == (numIndices - 1) - d) {
179 continue;
180 }
Nicolas Vasilachefd8d2562018-10-17 18:01:44 -0700181 if (!isAccessInvariant(input, memRefType, indices, d)) {
182 return false;
183 }
184 }
185 return true;
186}
187
Nicolas Vasilache078d9b92018-10-30 07:54:23 -0700188template <typename LoadOrStoreOpPointer>
189static bool isVectorElement(LoadOrStoreOpPointer memoryOp) {
River Riddle666dfbe2018-10-30 14:59:22 -0700190 auto memRefType = memoryOp->getMemRefType();
191 return memRefType.getElementType().template isa<VectorType>();
Nicolas Vasilache078d9b92018-10-30 07:54:23 -0700192}
193
Nicolas Vasilache6b197462018-11-14 04:04:10 -0800194// TODO(ntv): make the following into MLIR instructions, then use isa<>.
195static bool isVectorTransferReadOrWrite(const Statement &stmt) {
196 const auto *opStmt = cast<OperationStmt>(&stmt);
Nicolas Vasilache13b3bce2018-11-20 08:36:07 -0800197 return isaVectorTransferRead(*opStmt) || isaVectorTransferWrite(*opStmt);
Nicolas Vasilache6b197462018-11-14 04:04:10 -0800198}
199
Nicolas Vasilached64816a2018-11-01 07:14:14 -0700200using VectorizableStmtFun =
201 std::function<bool(const ForStmt &, const OperationStmt &)>;
202
203static bool isVectorizableLoopWithCond(const ForStmt &loop,
204 VectorizableStmtFun isVectorizableStmt) {
Nicolas Vasilache078d9b92018-10-30 07:54:23 -0700205 if (!matcher::isParallelLoop(loop) && !matcher::isReductionLoop(loop)) {
206 return false;
207 }
208
209 // No vectorization across conditionals for now.
210 auto conditionals = matcher::If();
211 auto *forStmt = const_cast<ForStmt *>(&loop);
212 auto conditionalsMatched = conditionals.match(forStmt);
213 if (!conditionalsMatched.empty()) {
214 return false;
215 }
216
Nicolas Vasilache6b197462018-11-14 04:04:10 -0800217 auto vectorTransfers = matcher::Op(isVectorTransferReadOrWrite);
218 auto vectorTransfersMatched = vectorTransfers.match(forStmt);
219 if (!vectorTransfersMatched.empty()) {
220 return false;
221 }
222
Nicolas Vasilache078d9b92018-10-30 07:54:23 -0700223 auto loadAndStores = matcher::Op(matcher::isLoadOrStore);
224 auto loadAndStoresMatched = loadAndStores.match(forStmt);
225 for (auto ls : loadAndStoresMatched) {
Nicolas Vasilachefd8d2562018-10-17 18:01:44 -0700226 auto *op = cast<OperationStmt>(ls.first);
Feng Liuec065d72018-10-19 09:07:58 -0700227 auto load = op->dyn_cast<LoadOp>();
228 auto store = op->dyn_cast<StoreOp>();
Nicolas Vasilache078d9b92018-10-30 07:54:23 -0700229 // Only scalar types are considered vectorizable, all load/store must be
230 // vectorizable for a loop to qualify as vectorizable.
231 // TODO(ntv): ponder whether we want to be more general here.
232 bool vector = load ? isVectorElement(load) : isVectorElement(store);
233 if (vector) {
234 return false;
235 }
Nicolas Vasilached64816a2018-11-01 07:14:14 -0700236 if (!isVectorizableStmt(loop, *op)) {
Nicolas Vasilachefd8d2562018-10-17 18:01:44 -0700237 return false;
238 }
239 }
240 return true;
241}
Uday Bondhugula861fe642018-10-18 11:14:26 -0700242
Nicolas Vasilached64816a2018-11-01 07:14:14 -0700243bool mlir::isVectorizableLoopAlongFastestVaryingMemRefDim(
244 const ForStmt &loop, unsigned fastestVaryingDim) {
245 VectorizableStmtFun fun(
246 [fastestVaryingDim](const ForStmt &loop, const OperationStmt &op) {
247 auto load = op.dyn_cast<LoadOp>();
248 auto store = op.dyn_cast<StoreOp>();
249 return load ? isContiguousAccess(loop, load, fastestVaryingDim)
250 : isContiguousAccess(loop, store, fastestVaryingDim);
251 });
252 return isVectorizableLoopWithCond(loop, fun);
253}
254
255bool mlir::isVectorizableLoop(const ForStmt &loop) {
256 VectorizableStmtFun fun(
257 // TODO: implement me
258 [](const ForStmt &loop, const OperationStmt &op) { return true; });
259 return isVectorizableLoopWithCond(loop, fun);
260}
261
Uday Bondhugula861fe642018-10-18 11:14:26 -0700262/// Checks whether SSA dominance would be violated if a for stmt's body
263/// statements are shifted by the specified shifts. This method checks if a
264/// 'def' and all its uses have the same shift factor.
265// TODO(mlir-team): extend this to check for memory-based dependence
266// violation when we have the support.
267bool mlir::isStmtwiseShiftValid(const ForStmt &forStmt,
268 ArrayRef<uint64_t> shifts) {
269 assert(shifts.size() == forStmt.getStatements().size());
270 unsigned s = 0;
271 for (const auto &stmt : forStmt) {
272 // A for or if stmt does not produce any def/results (that are used
273 // outside).
274 if (const auto *opStmt = dyn_cast<OperationStmt>(&stmt)) {
275 for (unsigned i = 0, e = opStmt->getNumResults(); i < e; ++i) {
276 const MLValue *result = opStmt->getResult(i);
277 for (const StmtOperand &use : result->getUses()) {
278 // If an ancestor statement doesn't lie in the block of forStmt, there
279 // is no shift to check.
280 // This is a naive way. If performance becomes an issue, a map can
281 // be used to store 'shifts' - to look up the shift for a statement in
282 // constant time.
283 if (auto *ancStmt = forStmt.findAncestorStmtInBlock(*use.getOwner()))
284 if (shifts[s] != shifts[forStmt.findStmtPosInBlock(*ancStmt)])
285 return false;
286 }
287 }
288 }
289 s++;
290 }
291 return true;
292}