xref: /llvm-project/mlir/lib/Dialect/Affine/Transforms/AffineDataCopyGeneration.cpp (revision a70aa7bb0d9a6066831b339e0a09a2c1bc74fe2b)
1 //===- AffineDataCopyGeneration.cpp - Explicit memref copying pass ------*-===//
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 a pass to automatically promote accessed memref regions
10 // to buffers in a faster memory space that is explicitly managed, with the
11 // necessary data movement operations performed through either regular
12 // point-wise load/store's or DMAs. Such explicit copying (also referred to as
13 // array packing/unpacking in the literature), when done on arrays that exhibit
14 // reuse, results in near elimination of conflict misses, TLB misses, reduced
15 // use of hardware prefetch streams, and reduced false sharing. It is also
16 // necessary for hardware that explicitly managed levels in the memory
17 // hierarchy, and where DMAs may have to be used. This optimization is often
18 // performed on already tiled code.
19 //
20 //===----------------------------------------------------------------------===//
21 
22 #include "PassDetail.h"
23 #include "mlir/Dialect/Affine/Analysis/Utils.h"
24 #include "mlir/Dialect/Affine/IR/AffineOps.h"
25 #include "mlir/Dialect/Affine/LoopUtils.h"
26 #include "mlir/Dialect/Affine/Passes.h"
27 #include "mlir/Dialect/Arithmetic/IR/Arithmetic.h"
28 #include "mlir/Dialect/MemRef/IR/MemRef.h"
29 #include "mlir/Dialect/StandardOps/IR/Ops.h"
30 #include "mlir/Transforms/GreedyPatternRewriteDriver.h"
31 #include "llvm/ADT/MapVector.h"
32 #include "llvm/Support/CommandLine.h"
33 #include "llvm/Support/Debug.h"
34 #include <algorithm>
35 
36 #define DEBUG_TYPE "affine-data-copy-generate"
37 
38 using namespace mlir;
39 
40 namespace {
41 
42 /// Replaces all loads and stores on memref's living in 'slowMemorySpace' by
43 /// introducing copy operations to transfer data into `fastMemorySpace` and
44 /// rewriting the original load's/store's to instead load/store from the
45 /// allocated fast memory buffers. Additional options specify the identifier
46 /// corresponding to the fast memory space and the amount of fast memory space
47 /// available. The pass traverses through the nesting structure, recursing to
48 /// inner levels if necessary to determine at what depth copies need to be
49 /// placed so that the allocated buffers fit within the memory capacity
50 /// provided.
51 // TODO: We currently can't generate copies correctly when stores
52 // are strided. Check for strided stores.
53 struct AffineDataCopyGeneration
54     : public AffineDataCopyGenerationBase<AffineDataCopyGeneration> {
55   AffineDataCopyGeneration() = default;
56   explicit AffineDataCopyGeneration(unsigned slowMemorySpace,
57                                     unsigned fastMemorySpace,
58                                     unsigned tagMemorySpace,
59                                     int minDmaTransferSize,
60                                     uint64_t fastMemCapacityBytes) {
61     this->slowMemorySpace = slowMemorySpace;
62     this->fastMemorySpace = fastMemorySpace;
63     this->tagMemorySpace = tagMemorySpace;
64     this->minDmaTransferSize = minDmaTransferSize;
65     this->fastMemoryCapacity = fastMemCapacityBytes / 1024;
66   }
67 
68   void runOnOperation() override;
69   void runOnBlock(Block *block, DenseSet<Operation *> &copyNests);
70 
71   // Constant zero index to avoid too many duplicates.
72   Value zeroIndex = nullptr;
73 };
74 
75 } // namespace
76 
77 /// Generates copies for memref's living in 'slowMemorySpace' into newly created
78 /// buffers in 'fastMemorySpace', and replaces memory operations to the former
79 /// by the latter. Only load op's handled for now.
80 /// TODO: extend this to store op's.
81 std::unique_ptr<OperationPass<FuncOp>> mlir::createAffineDataCopyGenerationPass(
82     unsigned slowMemorySpace, unsigned fastMemorySpace, unsigned tagMemorySpace,
83     int minDmaTransferSize, uint64_t fastMemCapacityBytes) {
84   return std::make_unique<AffineDataCopyGeneration>(
85       slowMemorySpace, fastMemorySpace, tagMemorySpace, minDmaTransferSize,
86       fastMemCapacityBytes);
87 }
88 std::unique_ptr<OperationPass<FuncOp>>
89 mlir::createAffineDataCopyGenerationPass() {
90   return std::make_unique<AffineDataCopyGeneration>();
91 }
92 
93 /// Generate copies for this block. The block is partitioned into separate
94 /// ranges: each range is either a sequence of one or more operations starting
95 /// and ending with an affine load or store op, or just an affine.forop (which
96 /// could have other affine for op's nested within).
97 void AffineDataCopyGeneration::runOnBlock(Block *block,
98                                           DenseSet<Operation *> &copyNests) {
99   if (block->empty())
100     return;
101 
102   uint64_t fastMemCapacityBytes =
103       fastMemoryCapacity != std::numeric_limits<uint64_t>::max()
104           ? fastMemoryCapacity * 1024
105           : fastMemoryCapacity;
106   AffineCopyOptions copyOptions = {generateDma, slowMemorySpace,
107                                    fastMemorySpace, tagMemorySpace,
108                                    fastMemCapacityBytes};
109 
110   // Every affine.for op in the block starts and ends a block range for copying;
111   // in addition, a contiguous sequence of operations starting with a
112   // load/store op but not including any copy nests themselves is also
113   // identified as a copy block range. Straightline code (a contiguous chunk of
114   // operations excluding AffineForOp's) are always assumed to not exhaust
115   // memory. As a result, this approach is conservative in some cases at the
116   // moment; we do a check later and report an error with location info.
117   // TODO: An 'affine.if' operation is being treated similar to an
118   // operation. 'affine.if''s could have 'affine.for's in them;
119   // treat them separately.
120 
121   // Get to the first load, store, or for op (that is not a copy nest itself).
122   auto curBegin =
123       std::find_if(block->begin(), block->end(), [&](Operation &op) {
124         return isa<AffineLoadOp, AffineStoreOp, AffineForOp>(op) &&
125                copyNests.count(&op) == 0;
126       });
127 
128   // Create [begin, end) ranges.
129   auto it = curBegin;
130   while (it != block->end()) {
131     AffineForOp forOp;
132     // If you hit a non-copy for loop, we will split there.
133     if ((forOp = dyn_cast<AffineForOp>(&*it)) && copyNests.count(forOp) == 0) {
134       // Perform the copying up unti this 'for' op first.
135       (void)affineDataCopyGenerate(/*begin=*/curBegin, /*end=*/it, copyOptions,
136                                    /*filterMemRef=*/llvm::None, copyNests);
137 
138       // Returns true if the footprint is known to exceed capacity.
139       auto exceedsCapacity = [&](AffineForOp forOp) {
140         Optional<int64_t> footprint =
141             getMemoryFootprintBytes(forOp,
142                                     /*memorySpace=*/0);
143         return (footprint.hasValue() &&
144                 static_cast<uint64_t>(footprint.getValue()) >
145                     fastMemCapacityBytes);
146       };
147 
148       // If the memory footprint of the 'affine.for' loop is higher than fast
149       // memory capacity (when provided), we recurse to copy at an inner level
150       // until we find a depth at which footprint fits in fast mem capacity. If
151       // the footprint can't be calculated, we assume for now it fits. Recurse
152       // inside if footprint for 'forOp' exceeds capacity, or when
153       // skipNonUnitStrideLoops is set and the step size is not one.
154       bool recurseInner = skipNonUnitStrideLoops ? forOp.getStep() != 1
155                                                  : exceedsCapacity(forOp);
156       if (recurseInner) {
157         // We'll recurse and do the copies at an inner level for 'forInst'.
158         // Recurse onto the body of this loop.
159         runOnBlock(forOp.getBody(), copyNests);
160       } else {
161         // We have enough capacity, i.e., copies will be computed for the
162         // portion of the block until 'it', and for 'it', which is 'forOp'. Note
163         // that for the latter, the copies are placed just before this loop (for
164         // incoming copies) and right after (for outgoing ones).
165 
166         // Inner loop copies have their own scope - we don't thus update
167         // consumed capacity. The footprint check above guarantees this inner
168         // loop's footprint fits.
169         (void)affineDataCopyGenerate(/*begin=*/it, /*end=*/std::next(it),
170                                      copyOptions,
171                                      /*filterMemRef=*/llvm::None, copyNests);
172       }
173       // Get to the next load or store op after 'forOp'.
174       curBegin = std::find_if(std::next(it), block->end(), [&](Operation &op) {
175         return isa<AffineLoadOp, AffineStoreOp, AffineForOp>(op) &&
176                copyNests.count(&op) == 0;
177       });
178       it = curBegin;
179     } else {
180       assert(copyNests.count(&*it) == 0 &&
181              "all copy nests generated should have been skipped above");
182       // We simply include this op in the current range and continue for more.
183       ++it;
184     }
185   }
186 
187   // Generate the copy for the final block range.
188   if (curBegin != block->end()) {
189     // Can't be a terminator because it would have been skipped above.
190     assert(!curBegin->hasTrait<OpTrait::IsTerminator>() &&
191            "can't be a terminator");
192     // Exclude the affine.yield - hence, the std::prev.
193     (void)affineDataCopyGenerate(/*begin=*/curBegin,
194                                  /*end=*/std::prev(block->end()), copyOptions,
195                                  /*filterMemRef=*/llvm::None, copyNests);
196   }
197 }
198 
199 void AffineDataCopyGeneration::runOnOperation() {
200   FuncOp f = getOperation();
201   OpBuilder topBuilder(f.getBody());
202   zeroIndex = topBuilder.create<arith::ConstantIndexOp>(f.getLoc(), 0);
203 
204   // Nests that are copy-in's or copy-out's; the root AffineForOps of those
205   // nests are stored herein.
206   DenseSet<Operation *> copyNests;
207 
208   // Clear recorded copy nests.
209   copyNests.clear();
210 
211   for (auto &block : f)
212     runOnBlock(&block, copyNests);
213 
214   // Promote any single iteration loops in the copy nests and collect
215   // load/stores to simplify.
216   SmallVector<Operation *, 4> copyOps;
217   for (Operation *nest : copyNests)
218     // With a post order walk, the erasure of loops does not affect
219     // continuation of the walk or the collection of load/store ops.
220     nest->walk([&](Operation *op) {
221       if (auto forOp = dyn_cast<AffineForOp>(op))
222         (void)promoteIfSingleIteration(forOp);
223       else if (isa<AffineLoadOp, AffineStoreOp>(op))
224         copyOps.push_back(op);
225     });
226 
227   // Promoting single iteration loops could lead to simplification of
228   // contained load's/store's, and the latter could anyway also be
229   // canonicalized.
230   RewritePatternSet patterns(&getContext());
231   AffineLoadOp::getCanonicalizationPatterns(patterns, &getContext());
232   AffineStoreOp::getCanonicalizationPatterns(patterns, &getContext());
233   FrozenRewritePatternSet frozenPatterns(std::move(patterns));
234   (void)applyOpPatternsAndFold(copyOps, frozenPatterns, /*strict=*/true);
235 }
236