MLIR 23.0.0git
LoopAnalysis.cpp
Go to the documentation of this file.
1//===- LoopAnalysis.cpp - Misc loop analysis routines //-------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements miscellaneous loop analysis routines.
10//
11//===----------------------------------------------------------------------===//
12
14
21#include "llvm/Support/MathExtras.h"
22
23#include "llvm/Support/Debug.h"
24#include "llvm/Support/DebugLog.h"
25#include <numeric>
26#include <optional>
27
28#define DEBUG_TYPE "affine-loop-analysis"
29
30using namespace mlir;
31using namespace mlir::affine;
32
33namespace {
34
35/// A directed graph to model relationships between MLIR Operations.
36class DirectedOpGraph {
37public:
38 /// Add a node to the graph.
39 void addNode(Operation *op) {
40 assert(!hasNode(op) && "node already added");
41 nodes.emplace_back(op);
42 edges[op] = {};
43 }
44
45 /// Add an edge from `src` to `dest`.
46 void addEdge(Operation *src, Operation *dest) {
47 // This is a multi-graph.
48 assert(hasNode(src) && "src node does not exist in graph");
49 assert(hasNode(dest) && "dest node does not exist in graph");
50 edges[src].push_back(getNode(dest));
51 }
52
53 /// Returns true if there is a (directed) cycle in the graph.
54 bool hasCycle() { return dfs(/*cycleCheck=*/true); }
55
56 void printEdges() {
57 for (auto &en : edges) {
58 llvm::dbgs() << *en.first << " (" << en.first << ")"
59 << " has " << en.second.size() << " edges:\n";
60 for (auto *node : en.second) {
61 llvm::dbgs() << '\t' << *node->op << '\n';
62 }
63 }
64 }
65
66private:
67 /// A node of a directed graph between MLIR Operations to model various
68 /// relationships. This is meant to be used internally.
69 struct DGNode {
70 DGNode(Operation *op) : op(op) {};
71 Operation *op;
72
73 // Start and finish visit numbers are standard in DFS to implement things
74 // like finding strongly connected components. These numbers are modified
75 // during analyses on the graph and so seemingly const API methods will be
76 // non-const.
77
78 /// Start visit number.
79 int vn = -1;
80
81 /// Finish visit number.
82 int fn = -1;
83 };
84
85 /// Get internal node corresponding to `op`.
86 DGNode *getNode(Operation *op) {
87 auto *value =
88 llvm::find_if(nodes, [&](const DGNode &node) { return node.op == op; });
89 assert(value != nodes.end() && "node doesn't exist in graph");
90 return &*value;
91 }
92
93 /// Returns true if `key` is in the graph.
94 bool hasNode(Operation *key) const {
95 return llvm::find_if(nodes, [&](const DGNode &node) {
96 return node.op == key;
97 }) != nodes.end();
98 }
99
100 /// Perform a depth-first traversal of the graph setting visited and finished
101 /// numbers. If `cycleCheck` is set, detects cycles and returns true as soon
102 /// as the first cycle is detected, and false if there are no cycles. If
103 /// `cycleCheck` is not set, completes the DFS and the `return` value doesn't
104 /// have a meaning.
105 bool dfs(bool cycleCheck = false) {
106 for (DGNode &node : nodes) {
107 node.vn = 0;
108 node.fn = -1;
109 }
110
111 unsigned time = 0;
112 for (DGNode &node : nodes) {
113 if (node.vn == 0) {
114 bool ret = dfsNode(node, cycleCheck, time);
115 // Check if a cycle was already found.
116 if (cycleCheck && ret)
117 return true;
118 } else if (cycleCheck && node.fn == -1) {
119 // We have encountered a node whose visit has started but it's not
120 // finished. So we have a cycle.
121 return true;
122 }
123 }
124 return false;
125 }
126
127 /// Perform depth-first traversal starting at `node`. Return true
128 /// as soon as a cycle is found if `cycleCheck` was set. Update `time`.
129 bool dfsNode(DGNode &node, bool cycleCheck, unsigned &time) const {
130 auto nodeEdges = edges.find(node.op);
131 assert(nodeEdges != edges.end() && "missing node in graph");
132 node.vn = ++time;
133
134 for (auto &neighbour : nodeEdges->second) {
135 if (neighbour->vn == 0) {
136 bool ret = dfsNode(*neighbour, cycleCheck, time);
137 if (cycleCheck && ret)
138 return true;
139 } else if (cycleCheck && neighbour->fn == -1) {
140 // We have encountered a node whose visit has started but it's not
141 // finished. So we have a cycle.
142 return true;
143 }
144 }
145
146 // Update finish time.
147 node.fn = ++time;
148
149 return false;
150 }
151
152 // The list of nodes. The storage is owned by this class.
154
155 // Edges as an adjacency list.
157};
158
159} // namespace
160
161/// Returns the trip count of the loop as an affine expression if the latter is
162/// expressible as an affine expression, and nullptr otherwise. The trip count
163/// expression is simplified before returning. This method only utilizes map
164/// composition to construct lower and upper bounds before computing the trip
165/// count expressions.
167 AffineForOp forOp, AffineMap *tripCountMap,
168 SmallVectorImpl<Value> *tripCountOperands) {
169 MLIRContext *context = forOp.getContext();
170 int64_t step = forOp.getStepAsInt();
171 int64_t loopSpan;
172 if (forOp.hasConstantBounds()) {
173 int64_t lb = forOp.getConstantLowerBound();
174 int64_t ub = forOp.getConstantUpperBound();
175 loopSpan = ub - lb;
176 if (loopSpan < 0)
177 loopSpan = 0;
178 *tripCountMap = AffineMap::getConstantMap(
179 llvm::divideCeilSigned(loopSpan, step), context);
180 tripCountOperands->clear();
181 return;
182 }
183 auto lbMap = forOp.getLowerBoundMap();
184 auto ubMap = forOp.getUpperBoundMap();
185 if (lbMap.getNumResults() != 1) {
186 *tripCountMap = AffineMap();
187 return;
188 }
189
190 // Difference of each upper bound expression from the single lower bound
191 // expression (divided by the step) provides the expressions for the trip
192 // count map.
193 AffineValueMap ubValueMap(ubMap, forOp.getUpperBoundOperands());
194
195 SmallVector<AffineExpr, 4> lbSplatExpr(ubValueMap.getNumResults(),
196 lbMap.getResult(0));
197 auto lbMapSplat = AffineMap::get(lbMap.getNumDims(), lbMap.getNumSymbols(),
198 lbSplatExpr, context);
199 AffineValueMap lbSplatValueMap(lbMapSplat, forOp.getLowerBoundOperands());
200
201 AffineValueMap tripCountValueMap;
202 AffineValueMap::difference(ubValueMap, lbSplatValueMap, &tripCountValueMap);
203 for (unsigned i = 0, e = tripCountValueMap.getNumResults(); i < e; ++i)
204 tripCountValueMap.setResult(i,
205 tripCountValueMap.getResult(i).ceilDiv(step));
206
207 *tripCountMap = tripCountValueMap.getAffineMap();
208 tripCountOperands->assign(tripCountValueMap.getOperands().begin(),
209 tripCountValueMap.getOperands().end());
210}
211
212/// Returns the trip count of the loop if it's a constant, std::nullopt
213/// otherwise. This method uses affine expression analysis (in turn using
214/// getTripCount) and is able to determine constant trip count in non-trivial
215/// cases.
216std::optional<uint64_t> mlir::affine::getConstantTripCount(AffineForOp forOp) {
217 if (std::optional<APInt> tripCount = forOp.getStaticTripCount())
218 return tripCount->getZExtValue();
219 return std::nullopt;
220}
221
222/// Returns the greatest known integral divisor of the trip count. Affine
223/// expression analysis is used (indirectly through getTripCount), and
224/// this method is thus able to determine non-trivial divisors.
226 SmallVector<Value, 4> operands;
227 AffineMap map;
228 getTripCountMapAndOperands(forOp, &map, &operands);
229
230 if (!map)
231 return 1;
232
233 // The largest divisor of the trip count is the GCD of the individual largest
234 // divisors.
235 assert(map.getNumResults() >= 1 && "expected one or more results");
236 std::optional<uint64_t> gcd;
237 for (auto resultExpr : map.getResults()) {
238 uint64_t thisGcd;
239 if (auto constExpr = dyn_cast<AffineConstantExpr>(resultExpr)) {
240 uint64_t tripCount = constExpr.getValue();
241 // 0 iteration loops (greatest divisor is 2^64 - 1).
242 if (tripCount == 0)
243 thisGcd = std::numeric_limits<uint64_t>::max();
244 else
245 // The greatest divisor is the trip count.
246 thisGcd = tripCount;
247 } else {
248 // Trip count is not a known constant; return its largest known divisor.
249 thisGcd = resultExpr.getLargestKnownDivisor();
250 }
251 if (gcd.has_value())
252 gcd = std::gcd(*gcd, thisGcd);
253 else
254 gcd = thisGcd;
255 }
256 assert(gcd.has_value() && "value expected per above logic");
257 return *gcd;
258}
259
260/// Given an affine.for `iv` and an access `index` of type index, returns `true`
261/// if `index` is independent of `iv` and false otherwise.
262///
263/// Prerequisites: `iv` and `index` of the proper type;
265 assert(isAffineForInductionVar(iv) && "iv must be an affine.for iv");
266 assert(isa<IndexType>(index.getType()) && "index must be of 'index' type");
267 auto map = AffineMap::getMultiDimIdentityMap(/*numDims=*/1, iv.getContext());
268 SmallVector<Value> operands = {index};
269 AffineValueMap avm(map, operands);
271 return !avm.isFunctionOf(0, iv);
272}
273
274// Pre-requisite: Loop bounds should be in canonical form.
275template <typename LoadOrStoreOp>
276bool mlir::affine::isInvariantAccess(LoadOrStoreOp memOp, AffineForOp forOp) {
277 AffineValueMap avm(memOp.getAffineMap(), memOp.getMapOperands());
279 return !llvm::is_contained(avm.getOperands(), forOp.getInductionVar());
280}
281
282// Explicitly instantiate the template so that the compiler knows we need them.
283template bool mlir::affine::isInvariantAccess(AffineReadOpInterface,
284 AffineForOp);
285template bool mlir::affine::isInvariantAccess(AffineWriteOpInterface,
286 AffineForOp);
287template bool mlir::affine::isInvariantAccess(AffineLoadOp, AffineForOp);
288template bool mlir::affine::isInvariantAccess(AffineStoreOp, AffineForOp);
289
292 DenseSet<Value> res;
293 for (Value index : indices) {
295 res.insert(index);
296 }
297 return res;
298}
299
300// TODO: check access stride.
301template <typename LoadOrStoreOp>
302bool mlir::affine::isContiguousAccess(Value iv, LoadOrStoreOp memoryOp,
303 int *memRefDim) {
304 static_assert(llvm::is_one_of<LoadOrStoreOp, AffineReadOpInterface,
305 AffineWriteOpInterface>::value,
306 "Must be called on either an affine read or write op");
307 assert(memRefDim && "memRefDim == nullptr");
308 auto memRefType = memoryOp.getMemRefType();
309
310 if (!memRefType.getLayout().isIdentity())
311 return memoryOp.emitError("NYI: non-trivial layout map"), false;
312
313 int uniqueVaryingIndexAlongIv = -1;
314 auto accessMap = memoryOp.getAffineMap();
315 SmallVector<Value, 4> mapOperands(memoryOp.getMapOperands());
316 unsigned numDims = accessMap.getNumDims();
317 for (unsigned i = 0, e = memRefType.getRank(); i < e; ++i) {
318 // Gather map operands used in result expr 'i' in 'exprOperands'.
319 SmallVector<Value, 4> exprOperands;
320 auto resultExpr = accessMap.getResult(i);
321 resultExpr.walk([&](AffineExpr expr) {
322 if (auto dimExpr = dyn_cast<AffineDimExpr>(expr))
323 exprOperands.push_back(mapOperands[dimExpr.getPosition()]);
324 else if (auto symExpr = dyn_cast<AffineSymbolExpr>(expr))
325 exprOperands.push_back(mapOperands[numDims + symExpr.getPosition()]);
326 });
327 // Check access invariance of each operand in 'exprOperands'.
328 for (Value exprOperand : exprOperands) {
329 if (!isAccessIndexInvariant(iv, exprOperand)) {
330 if (uniqueVaryingIndexAlongIv != -1) {
331 // 2+ varying indices -> do not vectorize along iv.
332 return false;
333 }
334 uniqueVaryingIndexAlongIv = i;
335 }
336 }
337 }
338
339 if (uniqueVaryingIndexAlongIv == -1)
340 *memRefDim = -1;
341 else
342 *memRefDim = memRefType.getRank() - (uniqueVaryingIndexAlongIv + 1);
343 return true;
344}
345
347 AffineReadOpInterface loadOp,
348 int *memRefDim);
350 AffineWriteOpInterface loadOp,
351 int *memRefDim);
352
353template <typename LoadOrStoreOp>
354static bool isVectorElement(LoadOrStoreOp memoryOp) {
355 auto memRefType = memoryOp.getMemRefType();
356 return isa<VectorType>(memRefType.getElementType());
357}
358
359using VectorizableOpFun = std::function<bool(AffineForOp, Operation &)>;
360
361static bool
363 const VectorizableOpFun &isVectorizableOp,
364 NestedPattern &vectorTransferMatcher) {
365 auto *forOp = loop.getOperation();
366
367 // No vectorization across conditionals for now.
368 auto conditionals = matcher::If();
369 SmallVector<NestedMatch, 8> conditionalsMatched;
370 conditionals.match(forOp, &conditionalsMatched);
371 if (!conditionalsMatched.empty()) {
372 return false;
373 }
374
375 // No vectorization for ops with operand or result types that are not
376 // vectorizable.
377 auto types = matcher::Op([](Operation &op) -> bool {
378 if (llvm::any_of(op.getOperandTypes(), [](Type type) {
379 if (MemRefType t = dyn_cast<MemRefType>(type))
380 return !VectorType::isValidElementType(t.getElementType());
381 return !VectorType::isValidElementType(type);
382 }))
383 return true;
384 return !llvm::all_of(op.getResultTypes(), VectorType::isValidElementType);
385 });
387 types.match(forOp, &opsMatched);
388 if (!opsMatched.empty()) {
389 return false;
390 }
391
392 // No vectorization across unknown regions.
393 auto regions = matcher::Op([](Operation &op) -> bool {
394 return op.getNumRegions() != 0 && !isa<AffineIfOp, AffineForOp>(op);
395 });
396 SmallVector<NestedMatch, 8> regionsMatched;
397 regions.match(forOp, &regionsMatched);
398 if (!regionsMatched.empty()) {
399 return false;
400 }
401
402 SmallVector<NestedMatch, 8> vectorTransfersMatched;
403 vectorTransferMatcher.match(forOp, &vectorTransfersMatched);
404 if (!vectorTransfersMatched.empty()) {
405 return false;
406 }
407
408 auto loadAndStores = matcher::Op(matcher::isLoadOrStore);
409 SmallVector<NestedMatch, 8> loadAndStoresMatched;
410 loadAndStores.match(forOp, &loadAndStoresMatched);
411 for (auto ls : loadAndStoresMatched) {
412 auto *op = ls.getMatchedOperation();
413 auto load = dyn_cast<AffineLoadOp>(op);
414 auto store = dyn_cast<AffineStoreOp>(op);
415 // Only scalar types are considered vectorizable, all load/store must be
416 // vectorizable for a loop to qualify as vectorizable.
417 // TODO: ponder whether we want to be more general here.
419 if (vector) {
420 return false;
421 }
422 if (isVectorizableOp && !isVectorizableOp(loop, *op)) {
423 return false;
424 }
425 }
426 return true;
427}
428
430 AffineForOp loop, int *memRefDim, NestedPattern &vectorTransferMatcher) {
431 *memRefDim = -1;
432 VectorizableOpFun fun([memRefDim](AffineForOp loop, Operation &op) {
433 auto load = dyn_cast<AffineLoadOp>(op);
434 auto store = dyn_cast<AffineStoreOp>(op);
435 int thisOpMemRefDim = -1;
436 bool isContiguous =
437 load ? isContiguousAccess(loop.getInductionVar(),
438 cast<AffineReadOpInterface>(*load),
439 &thisOpMemRefDim)
440 : isContiguousAccess(loop.getInductionVar(),
441 cast<AffineWriteOpInterface>(*store),
442 &thisOpMemRefDim);
443 if (thisOpMemRefDim != -1) {
444 // If memory accesses vary across different dimensions then the loop is
445 // not vectorizable.
446 if (*memRefDim != -1 && *memRefDim != thisOpMemRefDim)
447 return false;
448 *memRefDim = thisOpMemRefDim;
449 }
450 return isContiguous;
451 });
452 return isVectorizableLoopBodyWithOpCond(loop, fun, vectorTransferMatcher);
453}
454
456 AffineForOp loop, NestedPattern &vectorTransferMatcher) {
457 return isVectorizableLoopBodyWithOpCond(loop, nullptr, vectorTransferMatcher);
458}
459
460/// Checks whether SSA dominance would be violated if a for op's body
461/// operations are shifted by the specified shifts. This method checks if a
462/// 'def' and all its uses have the same shift factor.
463// TODO: extend this to check for memory-based dependence violation when we have
464// the support.
465bool mlir::affine::isOpwiseShiftValid(AffineForOp forOp,
466 ArrayRef<uint64_t> shifts) {
467 auto *forBody = forOp.getBody();
468 assert(shifts.size() == forBody->getOperations().size());
469
470 // Work backwards over the body of the block so that the shift of a use's
471 // ancestor operation in the block gets recorded before it's looked up.
473 for (const auto &it :
474 llvm::enumerate(llvm::reverse(forBody->getOperations()))) {
475 auto &op = it.value();
476
477 // Get the index of the current operation, note that we are iterating in
478 // reverse so we need to fix it up.
479 size_t index = shifts.size() - it.index() - 1;
480
481 // Remember the shift of this operation.
482 uint64_t shift = shifts[index];
483 forBodyShift.try_emplace(&op, shift);
484
485 // Validate the results of this operation if it were to be shifted.
486 for (unsigned i = 0, e = op.getNumResults(); i < e; ++i) {
487 Value result = op.getResult(i);
488 for (auto *user : result.getUsers()) {
489 // If an ancestor operation doesn't lie in the block of forOp,
490 // there is no shift to check.
491 if (auto *ancOp = forBody->findAncestorOpInBlock(*user)) {
492 assert(forBodyShift.count(ancOp) > 0 && "ancestor expected in map");
493 if (shift != forBodyShift[ancOp])
494 return false;
495 }
496 }
497 }
498 }
499 return true;
500}
501
503 assert(!loops.empty() && "no original loops provided");
504
505 // We first find out all dependences we intend to check.
506 SmallVector<Operation *, 8> loadAndStoreOps;
507 loops[0]->walk([&](Operation *op) {
508 if (isa<AffineReadOpInterface, AffineWriteOpInterface>(op))
509 loadAndStoreOps.push_back(op);
510 });
511
512 unsigned numOps = loadAndStoreOps.size();
513 unsigned numLoops = loops.size();
514 for (unsigned d = 1; d <= numLoops + 1; ++d) {
515 for (unsigned i = 0; i < numOps; ++i) {
516 Operation *srcOp = loadAndStoreOps[i];
517 MemRefAccess srcAccess(srcOp);
518 for (unsigned j = 0; j < numOps; ++j) {
519 Operation *dstOp = loadAndStoreOps[j];
520 MemRefAccess dstAccess(dstOp);
521
524 srcAccess, dstAccess, d, /*dependenceConstraints=*/nullptr,
525 &depComps);
526
527 // Skip if there is no dependence in this case.
528 if (!hasDependence(result))
529 continue;
530
531 // Check whether there is any negative direction vector in the
532 // dependence components found above, which means that dependence is
533 // violated by the default hyper-rect tiling method.
534 LDBG() << "Checking whether tiling legality violated "
535 << "for dependence at depth: " << Twine(d) << " between:"
536 << OpWithFlags(srcAccess.opInst, OpPrintingFlags().skipRegions())
537 << "\nand:\n"
538 << OpWithFlags(dstAccess.opInst,
539 OpPrintingFlags().skipRegions());
540 for (const DependenceComponent &depComp : depComps) {
541 if (depComp.lb.has_value() && depComp.ub.has_value() &&
542 *depComp.lb < *depComp.ub && *depComp.ub < 0) {
543 LDBG() << "Dependence component lb = " << Twine(*depComp.lb)
544 << " ub = " << Twine(*depComp.ub)
545 << " is negative at depth: " << Twine(d)
546 << " and thus violates the legality rule.";
547 return false;
548 }
549 }
550 }
551 }
552 }
553
554 return true;
555}
556
557bool mlir::affine::hasCyclicDependence(AffineForOp root) {
558 // Collect all the memory accesses in the source nest grouped by their
559 // immediate parent block.
560 DirectedOpGraph graph;
562 root->walk([&](Operation *op) {
563 if (isa<AffineReadOpInterface, AffineWriteOpInterface>(op)) {
564 accesses.emplace_back(op);
565 graph.addNode(op);
566 }
567 });
568
569 // Construct the dependence graph for all the collected acccesses.
570 unsigned rootDepth = getNestingDepth(root);
571 for (const auto &accA : accesses) {
572 for (const auto &accB : accesses) {
573 if (accA.memref != accB.memref)
574 continue;
575 // Perform the dependence on all surrounding loops + the body.
576 unsigned numCommonLoops =
577 getNumCommonSurroundingLoops(*accA.opInst, *accB.opInst);
578 for (unsigned d = rootDepth + 1; d <= numCommonLoops + 1; ++d) {
579 if (!noDependence(checkMemrefAccessDependence(accA, accB, d)))
580 graph.addEdge(accA.opInst, accB.opInst);
581 }
582 }
583 }
584 return graph.hasCycle();
585}
static bool isVectorizableLoopBodyWithOpCond(AffineForOp loop, const VectorizableOpFun &isVectorizableOp, NestedPattern &vectorTransferMatcher)
std::function< bool(AffineForOp, Operation &)> VectorizableOpFun
static bool isAccessIndexInvariant(Value iv, Value index)
Given an affine.for iv and an access index of type index, returns true if index is independent of iv ...
static bool isVectorElement(LoadOrStoreOp memoryOp)
auto load
Base type for affine expression.
Definition AffineExpr.h:68
AffineExpr ceilDiv(uint64_t v) const
A multi-dimensional affine map Affine map's are immutable like Type's, and they are uniqued.
Definition AffineMap.h:46
static AffineMap getMultiDimIdentityMap(unsigned numDims, MLIRContext *context)
Returns an AffineMap with 'numDims' identity result dim exprs.
static AffineMap get(MLIRContext *context)
Returns a zero result affine map with no dimensions or symbols: () -> ().
ArrayRef< AffineExpr > getResults() const
unsigned getNumResults() const
static AffineMap getConstantMap(int64_t val, MLIRContext *context)
Returns a single constant result affine map.
MLIRContext is the top-level object for a collection of MLIR operations.
Definition MLIRContext.h:63
Set of flags used to control the behavior of the various IR print methods (e.g.
A wrapper class that allows for printing an operation with a set of flags, useful to act as a "stream...
Definition Operation.h:1142
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
unsigned getNumRegions()
Returns the number of regions held by this operation.
Definition Operation.h:699
operand_type_range getOperandTypes()
Definition Operation.h:422
result_type_range getResultTypes()
Definition Operation.h:453
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition Types.h:74
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition Value.h:96
MLIRContext * getContext() const
Utility to get the associated MLIRContext that this value is defined in.
Definition Value.h:108
An AffineValueMap is an affine map plus its ML value operands and results for analysis purposes.
void composeSimplifyAndCanonicalize()
Composes all incoming affine.apply ops and then simplifies and canonicalizes the map and operands.
ArrayRef< Value > getOperands() const
AffineExpr getResult(unsigned i)
bool isFunctionOf(unsigned idx, Value value) const
Return true if the idx^th result depends on 'value', false otherwise.
void setResult(unsigned i, AffineExpr e)
static void difference(const AffineValueMap &a, const AffineValueMap &b, AffineValueMap *res)
Return the value map that is the difference of value maps 'a' and 'b', represented as an affine map a...
void match(Operation *op, SmallVectorImpl< NestedMatch > *matches)
Returns all the top-level matches in op.
NestedPattern If(const NestedPattern &child)
bool isLoadOrStore(Operation &op)
NestedPattern Op(FilterFunctionType filter=defaultFilterFunction)
std::optional< uint64_t > getConstantTripCount(AffineForOp forOp)
Returns the trip count of the loop if it's a constant, std::nullopt otherwise.
bool isTilingValid(ArrayRef< AffineForOp > loops)
Checks whether hyper-rectangular loop tiling of the nest represented by loops is valid.
bool isVectorizableLoopBody(AffineForOp loop, NestedPattern &vectorTransferMatcher)
Checks whether the loop is structurally vectorizable; i.e.:
unsigned getNumCommonSurroundingLoops(Operation &a, Operation &b)
Returns the number of surrounding loops common to both A and B.
Definition Utils.cpp:2105
DenseSet< Value, DenseMapInfo< Value > > getInvariantAccesses(Value iv, ArrayRef< Value > indices)
Given an induction variable iv of type AffineForOp and indices of type IndexType, returns the set of ...
void getTripCountMapAndOperands(AffineForOp forOp, AffineMap *map, SmallVectorImpl< Value > *operands)
Returns the trip count of the loop as an affine map with its corresponding operands if the latter is ...
bool isInvariantAccess(LoadOrStoreOp memOp, AffineForOp forOp)
Checks if an affine read or write operation depends on forOp's IV, i.e., if the memory access is inva...
DependenceResult checkMemrefAccessDependence(const MemRefAccess &srcAccess, const MemRefAccess &dstAccess, unsigned loopDepth, FlatAffineValueConstraints *dependenceConstraints=nullptr, SmallVector< DependenceComponent, 2 > *dependenceComponents=nullptr, bool allowRAR=false)
bool isAffineForInductionVar(Value val)
Returns true if the provided value is the induction variable of an AffineForOp.
uint64_t getLargestDivisorOfTripCount(AffineForOp forOp)
Returns the greatest known integral divisor of the trip count.
bool isContiguousAccess(Value iv, LoadOrStoreOp memoryOp, int *memRefDim)
Given:
bool hasDependence(DependenceResult result)
Utility function that returns true if the provided DependenceResult corresponds to a dependence resul...
unsigned getNestingDepth(Operation *op)
Returns the nesting depth of this operation, i.e., the number of loops surrounding this operation.
Definition Utils.cpp:2060
bool isOpwiseShiftValid(AffineForOp forOp, ArrayRef< uint64_t > shifts)
Checks where SSA dominance would be violated if a for op's body operations are shifted by the specifi...
bool hasCyclicDependence(AffineForOp root)
Returns true if the affine nest rooted at root has a cyclic dependence among its affine memory access...
bool noDependence(DependenceResult result)
Returns true if the provided DependenceResult corresponds to the absence of a dependence.
Include the generated interface declarations.
llvm::DenseSet< ValueT, ValueInfoT > DenseSet
Definition LLVM.h:122
llvm::DenseMap< KeyT, ValueT, KeyInfoT, BucketT > DenseMap
Definition LLVM.h:120
Checks whether two accesses to the same memref access the same element.
Encapsulates a memref load or store access information.
Eliminates variable at the specified position using Fourier-Motzkin variable elimination.