My Project
LoopUtils.h
Go to the documentation of this file.
1 //===- LoopUtils.h - Loop transformation utilities --------------*- C++ -*-===//
2 //
3 // Part of the MLIR 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 header file defines prototypes for various loop transformation utility
10 // methods: these are not passes by themselves but are used either by passes,
11 // optimization sequences, or in turn by other transformation utilities.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef MLIR_TRANSFORMS_LOOP_UTILS_H
16 #define MLIR_TRANSFORMS_LOOP_UTILS_H
17 
18 #include "mlir/IR/Block.h"
19 #include "mlir/Support/LLVM.h"
21 
22 namespace mlir {
23 class AffineForOp;
24 class FuncOp;
25 class OpBuilder;
26 class Value;
27 
28 namespace loop {
29 class ForOp;
30 } // end namespace loop
31 
34 LogicalResult loopUnrollFull(AffineForOp forOp);
35 
39 LogicalResult loopUnrollByFactor(AffineForOp forOp, uint64_t unrollFactor);
40 
43 LogicalResult loopUnrollUpToFactor(AffineForOp forOp, uint64_t unrollFactor);
44 
49 void getPerfectlyNestedLoops(SmallVectorImpl<AffineForOp> &nestedLoops,
50  AffineForOp root);
51 void getPerfectlyNestedLoops(SmallVectorImpl<loop::ForOp> &nestedLoops,
52  loop::ForOp root);
53 
56 LogicalResult loopUnrollJamByFactor(AffineForOp forOp,
57  uint64_t unrollJamFactor);
58 
61 LogicalResult loopUnrollJamUpToFactor(AffineForOp forOp,
62  uint64_t unrollJamFactor);
63 
66 LogicalResult promoteIfSingleIteration(AffineForOp forOp);
67 
70 void promoteSingleIterationLoops(FuncOp f);
71 
77 void getCleanupLoopLowerBound(AffineForOp forOp, unsigned unrollFactor,
78  AffineMap *map, SmallVectorImpl<Value> *operands,
79  OpBuilder &builder);
80 
85 LLVM_NODISCARD
86 LogicalResult instBodySkew(AffineForOp forOp, ArrayRef<uint64_t> shifts,
87  bool unrollPrologueEpilogue = false);
88 
91 LLVM_NODISCARD
92 LogicalResult tileCodeGen(MutableArrayRef<AffineForOp> band,
93  ArrayRef<unsigned> tileSizes);
94 
97 void interchangeLoops(AffineForOp forOpA, AffineForOp forOpB);
98 
102 bool isValidLoopInterchangePermutation(ArrayRef<AffineForOp> loops,
103  ArrayRef<unsigned> loopPermMap);
104 
108 unsigned interchangeLoops(ArrayRef<AffineForOp> loops,
109  ArrayRef<unsigned> loopPermMap);
110 
111 // Sinks all sequential loops to the innermost levels (while preserving
112 // relative order among them) and moves all parallel loops to the
113 // outermost (while again preserving relative order among them).
114 // Returns AffineForOp of the root of the new loop nest after loop interchanges.
115 AffineForOp sinkSequentialLoops(AffineForOp forOp);
116 
120 void sinkLoop(AffineForOp forOp, unsigned loopDepth);
121 
128 using TileLoops = std::pair<Loops, Loops>;
130  ArrayRef<uint64_t> sizes,
131  ArrayRef<AffineForOp> targets);
133  ArrayRef<loop::ForOp> targets);
134 
140  ArrayRef<uint64_t> sizes, AffineForOp target);
142  loop::ForOp target);
143 
149 Loops tilePerfectlyNested(loop::ForOp rootForOp, ArrayRef<Value> sizes);
150 
153  // True if DMAs should be generated instead of point-wise copies.
155  // The slower memory space from which data is to be moved.
156  unsigned slowMemorySpace;
157  // Memory space of the faster one (typically a scratchpad).
158  unsigned fastMemorySpace;
159  // Memory space to place tags in: only meaningful for DMAs.
160  unsigned tagMemorySpace;
161  // Capacity of the fast memory space in bytes.
163 };
164 
176  const AffineCopyOptions &copyOptions,
177  DenseSet<Operation *> &copyNests);
178 
182 TileLoops extractFixedOuterLoops(loop::ForOp rootFOrOp,
183  ArrayRef<int64_t> sizes);
184 
189 
221 void mapLoopToProcessorIds(loop::ForOp forOp, ArrayRef<Value> processorId,
222  ArrayRef<Value> numProcessors);
223 } // end namespace mlir
224 
225 #endif // MLIR_TRANSFORMS_LOOP_UTILS_H
Definition: InferTypeOpInterface.cpp:20
std::pair< Loops, Loops > TileLoops
Definition: LoopUtils.h:128
LLVM_NODISCARD LogicalResult tileCodeGen(MutableArrayRef< AffineForOp > band, ArrayRef< unsigned > tileSizes)
Definition: LoopTiling.cpp:177
Definition: LLVM.h:46
Definition: Attributes.h:129
void mapLoopToProcessorIds(loop::ForOp forOp, ArrayRef< Value > processorId, ArrayRef< Value > numProcessors)
Definition: LoopUtils.cpp:1097
void getCleanupLoopLowerBound(AffineForOp forOp, unsigned unrollFactor, AffineMap *map, SmallVectorImpl< Value > *operands, OpBuilder &builder)
Definition: LoopUtils.cpp:44
void sinkLoop(AffineForOp forOp, unsigned loopDepth)
Definition: LoopUtils.cpp:647
LogicalResult promoteIfSingleIteration(AffineForOp forOp)
Definition: LoopUtils.cpp:111
bool generateDma
Definition: LoopUtils.h:154
void coalesceLoops(MutableArrayRef< loop::ForOp > loops)
Definition: LoopUtils.cpp:1042
LogicalResult loopUnrollJamUpToFactor(AffineForOp forOp, uint64_t unrollJamFactor)
Definition: LoopUnrollAndJam.cpp:105
uint64_t affineDataCopyGenerate(Block::iterator begin, Block::iterator end, const AffineCopyOptions &copyOptions, DenseSet< Operation *> &copyNests)
Definition: LoopUtils.cpp:1596
Loops tilePerfectlyNested(loop::ForOp rootForOp, ArrayRef< Value > sizes)
Definition: LoopUtils.cpp:820
LogicalResult loopUnrollFull(AffineForOp forOp)
Unrolls this loop completely.
Definition: LoopUtils.cpp:385
Definition: LLVM.h:37
OpListType::iterator iterator
Definition: Block.h:107
LLVM_NODISCARD LogicalResult instBodySkew(AffineForOp forOp, ArrayRef< uint64_t > shifts, bool unrollPrologueEpilogue=false)
Definition: LoopUtils.cpp:230
unsigned tagMemorySpace
Definition: LoopUtils.h:160
SmallVector< SmallVector< AffineForOp, 8 >, 8 > tile(ArrayRef< AffineForOp > forOps, ArrayRef< uint64_t > sizes, ArrayRef< AffineForOp > targets)
Definition: LoopUtils.cpp:787
auto map(Fn fun, IterType begin, IterType end) -> SmallVector< typename std::result_of< Fn(decltype(*begin))>::type, 8 >
Map with iterators.
Definition: Functional.h:28
Definition: LLVM.h:38
void getPerfectlyNestedLoops(SmallVectorImpl< AffineForOp > &nestedLoops, AffineForOp root)
Definition: LoopUtils.cpp:374
AffineForOp sinkSequentialLoops(AffineForOp forOp)
Definition: LoopUtils.cpp:593
LogicalResult loopUnrollUpToFactor(AffineForOp forOp, uint64_t unrollFactor)
Definition: LoopUtils.cpp:399
uint64_t fastMemCapacityBytes
Definition: LoopUtils.h:162
TileLoops extractFixedOuterLoops(loop::ForOp rootFOrOp, ArrayRef< int64_t > sizes)
Definition: LoopUtils.cpp:924
LogicalResult loopUnrollByFactor(AffineForOp forOp, uint64_t unrollFactor)
Definition: LoopUtils.cpp:411
Explicit copy / DMA generation options for mlir::affineDataCopyGenerate.
Definition: LoopUtils.h:152
Definition: LLVM.h:35
void interchangeLoops(AffineForOp forOpA, AffineForOp forOpB)
Definition: LoopUtils.cpp:500
LogicalResult loopUnrollJamByFactor(AffineForOp forOp, uint64_t unrollJamFactor)
Unrolls and jams this loop by the specified factor.
Definition: LoopUnrollAndJam.cpp:116
SmallVector< loop::ForOp, 8 > Loops
Definition: LoopUtils.h:127
void promoteSingleIterationLoops(FuncOp f)
Definition: LoopUtils.cpp:155
bool isValidLoopInterchangePermutation(ArrayRef< AffineForOp > loops, ArrayRef< unsigned > loopPermMap)
Definition: LoopUtils.cpp:560
unsigned slowMemorySpace
Definition: LoopUtils.h:156
unsigned fastMemorySpace
Definition: LoopUtils.h:158