MLIR  21.0.0git
MultiBuffer.cpp
Go to the documentation of this file.
1 //===----------- MultiBuffering.cpp ---------------------------------------===//
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 multi buffering transformation.
10 //
11 //===----------------------------------------------------------------------===//
12 
18 #include "mlir/IR/AffineExpr.h"
20 #include "mlir/IR/Dominance.h"
21 #include "mlir/IR/PatternMatch.h"
22 #include "mlir/IR/ValueRange.h"
24 #include "llvm/ADT/STLExtras.h"
25 #include "llvm/Support/Debug.h"
26 
27 using namespace mlir;
28 
29 #define DEBUG_TYPE "memref-transforms"
30 #define DBGS() (llvm::dbgs() << "[" DEBUG_TYPE "]: ")
31 #define DBGSNL() (llvm::dbgs() << "\n")
32 
33 /// Return true if the op fully overwrite the given `buffer` value.
34 static bool overrideBuffer(Operation *op, Value buffer) {
35  auto copyOp = dyn_cast<memref::CopyOp>(op);
36  if (!copyOp)
37  return false;
38  return copyOp.getTarget() == buffer;
39 }
40 
41 /// Replace the uses of `oldOp` with the given `val` and for subview uses
42 /// propagate the type change. Changing the memref type may require propagating
43 /// it through subview ops so we cannot just do a replaceAllUse but need to
44 /// propagate the type change and erase old subview ops.
46  Operation *oldOp, Value val) {
47  SmallVector<Operation *> opsToDelete;
48  SmallVector<OpOperand *> operandsToReplace;
49 
50  // Save the operand to replace / delete later (avoid iterator invalidation).
51  // TODO: can we use an early_inc iterator?
52  for (OpOperand &use : oldOp->getUses()) {
53  // Non-subview ops will be replaced by `val`.
54  auto subviewUse = dyn_cast<memref::SubViewOp>(use.getOwner());
55  if (!subviewUse) {
56  operandsToReplace.push_back(&use);
57  continue;
58  }
59 
60  // `subview(old_op)` is replaced by a new `subview(val)`.
61  OpBuilder::InsertionGuard g(rewriter);
62  rewriter.setInsertionPoint(subviewUse);
63  MemRefType newType = memref::SubViewOp::inferRankReducedResultType(
64  subviewUse.getType().getShape(), cast<MemRefType>(val.getType()),
65  subviewUse.getStaticOffsets(), subviewUse.getStaticSizes(),
66  subviewUse.getStaticStrides());
67  Value newSubview = rewriter.create<memref::SubViewOp>(
68  subviewUse->getLoc(), newType, val, subviewUse.getMixedOffsets(),
69  subviewUse.getMixedSizes(), subviewUse.getMixedStrides());
70 
71  // Ouch recursion ... is this really necessary?
72  replaceUsesAndPropagateType(rewriter, subviewUse, newSubview);
73 
74  opsToDelete.push_back(use.getOwner());
75  }
76 
77  // Perform late replacement.
78  // TODO: can we use an early_inc iterator?
79  for (OpOperand *operand : operandsToReplace) {
80  Operation *op = operand->getOwner();
81  rewriter.startOpModification(op);
82  operand->set(val);
83  rewriter.finalizeOpModification(op);
84  }
85 
86  // Perform late op erasure.
87  // TODO: can we use an early_inc iterator?
88  for (Operation *op : opsToDelete)
89  rewriter.eraseOp(op);
90 }
91 
92 // Transformation to do multi-buffering/array expansion to remove dependencies
93 // on the temporary allocation between consecutive loop iterations.
94 // Returns success if the transformation happened and failure otherwise.
95 // This is not a pattern as it requires propagating the new memref type to its
96 // uses and requires updating subview ops.
97 FailureOr<memref::AllocOp>
98 mlir::memref::multiBuffer(RewriterBase &rewriter, memref::AllocOp allocOp,
99  unsigned multiBufferingFactor,
100  bool skipOverrideAnalysis) {
101  LLVM_DEBUG(DBGS() << "Start multibuffering: " << allocOp << "\n");
102  DominanceInfo dom(allocOp->getParentOp());
103  LoopLikeOpInterface candidateLoop;
104  for (Operation *user : allocOp->getUsers()) {
105  auto parentLoop = user->getParentOfType<LoopLikeOpInterface>();
106  if (!parentLoop) {
107  if (isa<memref::DeallocOp>(user)) {
108  // Allow dealloc outside of any loop.
109  // TODO: The whole precondition function here is very brittle and will
110  // need to rethought an isolated into a cleaner analysis.
111  continue;
112  }
113  LLVM_DEBUG(DBGS() << "--no parent loop -> fail\n");
114  LLVM_DEBUG(DBGS() << "----due to user: " << *user << "\n");
115  return failure();
116  }
117  if (!skipOverrideAnalysis) {
118  /// Make sure there is no loop-carried dependency on the allocation.
119  if (!overrideBuffer(user, allocOp.getResult())) {
120  LLVM_DEBUG(DBGS() << "--Skip user: found loop-carried dependence\n");
121  continue;
122  }
123  // If this user doesn't dominate all the other users keep looking.
124  if (llvm::any_of(allocOp->getUsers(), [&](Operation *otherUser) {
125  return !dom.dominates(user, otherUser);
126  })) {
127  LLVM_DEBUG(
128  DBGS() << "--Skip user: does not dominate all other users\n");
129  continue;
130  }
131  } else {
132  if (llvm::any_of(allocOp->getUsers(), [&](Operation *otherUser) {
133  return !isa<memref::DeallocOp>(otherUser) &&
134  !parentLoop->isProperAncestor(otherUser);
135  })) {
136  LLVM_DEBUG(
137  DBGS()
138  << "--Skip user: not all other users are in the parent loop\n");
139  continue;
140  }
141  }
142  candidateLoop = parentLoop;
143  break;
144  }
145 
146  if (!candidateLoop) {
147  LLVM_DEBUG(DBGS() << "Skip alloc: no candidate loop\n");
148  return failure();
149  }
150 
151  std::optional<Value> inductionVar = candidateLoop.getSingleInductionVar();
152  std::optional<OpFoldResult> lowerBound = candidateLoop.getSingleLowerBound();
153  std::optional<OpFoldResult> singleStep = candidateLoop.getSingleStep();
154  if (!inductionVar || !lowerBound || !singleStep ||
155  !llvm::hasSingleElement(candidateLoop.getLoopRegions())) {
156  LLVM_DEBUG(DBGS() << "Skip alloc: no single iv, lb, step or region\n");
157  return failure();
158  }
159 
160  if (!dom.dominates(allocOp.getOperation(), candidateLoop)) {
161  LLVM_DEBUG(DBGS() << "Skip alloc: does not dominate candidate loop\n");
162  return failure();
163  }
164 
165  LLVM_DEBUG(DBGS() << "Start multibuffering loop: " << candidateLoop << "\n");
166 
167  // 1. Construct the multi-buffered memref type.
168  ArrayRef<int64_t> originalShape = allocOp.getType().getShape();
169  SmallVector<int64_t, 4> multiBufferedShape{multiBufferingFactor};
170  llvm::append_range(multiBufferedShape, originalShape);
171  LLVM_DEBUG(DBGS() << "--original type: " << allocOp.getType() << "\n");
172  MemRefType mbMemRefType = MemRefType::Builder(allocOp.getType())
173  .setShape(multiBufferedShape)
174  .setLayout(MemRefLayoutAttrInterface());
175  LLVM_DEBUG(DBGS() << "--multi-buffered type: " << mbMemRefType << "\n");
176 
177  // 2. Create the multi-buffered alloc.
178  Location loc = allocOp->getLoc();
179  OpBuilder::InsertionGuard g(rewriter);
180  rewriter.setInsertionPoint(allocOp);
181  auto mbAlloc = rewriter.create<memref::AllocOp>(
182  loc, mbMemRefType, ValueRange{}, allocOp->getAttrs());
183  LLVM_DEBUG(DBGS() << "--multi-buffered alloc: " << mbAlloc << "\n");
184 
185  // 3. Within the loop, build the modular leading index (i.e. each loop
186  // iteration %iv accesses slice ((%iv - %lb) / %step) % %mb_factor).
187  rewriter.setInsertionPointToStart(
188  &candidateLoop.getLoopRegions().front()->front());
189  Value ivVal = *inductionVar;
190  Value lbVal = getValueOrCreateConstantIndexOp(rewriter, loc, *lowerBound);
191  Value stepVal = getValueOrCreateConstantIndexOp(rewriter, loc, *singleStep);
192  AffineExpr iv, lb, step;
193  bindDims(rewriter.getContext(), iv, lb, step);
195  rewriter, loc, ((iv - lb).floorDiv(step)) % multiBufferingFactor,
196  {ivVal, lbVal, stepVal});
197  LLVM_DEBUG(DBGS() << "--multi-buffered indexing: " << bufferIndex << "\n");
198 
199  // 4. Build the subview accessing the particular slice, taking modular
200  // rotation into account.
201  int64_t mbMemRefTypeRank = mbMemRefType.getRank();
202  IntegerAttr zero = rewriter.getIndexAttr(0);
203  IntegerAttr one = rewriter.getIndexAttr(1);
204  SmallVector<OpFoldResult> offsets(mbMemRefTypeRank, zero);
205  SmallVector<OpFoldResult> sizes(mbMemRefTypeRank, one);
206  SmallVector<OpFoldResult> strides(mbMemRefTypeRank, one);
207  // Offset is [bufferIndex, 0 ... 0 ].
208  offsets.front() = bufferIndex;
209  // Sizes is [1, original_size_0 ... original_size_n ].
210  for (int64_t i = 0, e = originalShape.size(); i != e; ++i)
211  sizes[1 + i] = rewriter.getIndexAttr(originalShape[i]);
212  // Strides is [1, 1 ... 1 ].
213  MemRefType dstMemref = memref::SubViewOp::inferRankReducedResultType(
214  originalShape, mbMemRefType, offsets, sizes, strides);
215  Value subview = rewriter.create<memref::SubViewOp>(loc, dstMemref, mbAlloc,
216  offsets, sizes, strides);
217  LLVM_DEBUG(DBGS() << "--multi-buffered slice: " << subview << "\n");
218 
219  // 5. Due to the recursive nature of replaceUsesAndPropagateType , we need to
220  // handle dealloc uses separately..
221  for (OpOperand &use : llvm::make_early_inc_range(allocOp->getUses())) {
222  auto deallocOp = dyn_cast<memref::DeallocOp>(use.getOwner());
223  if (!deallocOp)
224  continue;
225  OpBuilder::InsertionGuard g(rewriter);
226  rewriter.setInsertionPoint(deallocOp);
227  auto newDeallocOp =
228  rewriter.create<memref::DeallocOp>(deallocOp->getLoc(), mbAlloc);
229  (void)newDeallocOp;
230  LLVM_DEBUG(DBGS() << "----Created dealloc: " << newDeallocOp << "\n");
231  rewriter.eraseOp(deallocOp);
232  }
233 
234  // 6. RAUW with the particular slice, taking modular rotation into account.
235  replaceUsesAndPropagateType(rewriter, allocOp, subview);
236 
237  // 7. Finally, erase the old allocOp.
238  rewriter.eraseOp(allocOp);
239 
240  return mbAlloc;
241 }
242 
243 FailureOr<memref::AllocOp>
244 mlir::memref::multiBuffer(memref::AllocOp allocOp,
245  unsigned multiBufferingFactor,
246  bool skipOverrideAnalysis) {
247  IRRewriter rewriter(allocOp->getContext());
248  return multiBuffer(rewriter, allocOp, multiBufferingFactor,
249  skipOverrideAnalysis);
250 }
static bool overrideBuffer(Operation *op, Value buffer)
Return true if the op fully overwrite the given buffer value.
Definition: MultiBuffer.cpp:34
static void replaceUsesAndPropagateType(RewriterBase &rewriter, Operation *oldOp, Value val)
Replace the uses of oldOp with the given val and for subview uses propagate the type change.
Definition: MultiBuffer.cpp:45
#define DBGS()
Definition: MultiBuffer.cpp:30
Base type for affine expression.
Definition: AffineExpr.h:68
IntegerAttr getIndexAttr(int64_t value)
Definition: Builders.cpp:104
MLIRContext * getContext() const
Definition: Builders.h:56
A class for computing basic dominance information.
Definition: Dominance.h:140
bool dominates(Operation *a, Operation *b) const
Return true if operation A dominates operation B, i.e.
Definition: Dominance.h:158
This class coordinates rewriting a piece of IR outside of a pattern rewrite, providing a way to keep ...
Definition: PatternMatch.h:730
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:66
This is a builder type that keeps local references to arguments.
Definition: BuiltinTypes.h:166
Builder & setShape(ArrayRef< int64_t > newShape)
Definition: BuiltinTypes.h:177
RAII guard to reset the insertion point of the builder when destroyed.
Definition: Builders.h:346
void setInsertionPointToStart(Block *block)
Sets the insertion point to the start of the specified block.
Definition: Builders.h:429
void setInsertionPoint(Block *block, Block::iterator insertPoint)
Set the insertion point to the specified location.
Definition: Builders.h:396
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
Definition: Builders.cpp:453
This class represents an operand of an operation.
Definition: Value.h:243
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
user_range getUsers()
Returns a range of all users.
Definition: Operation.h:874
use_range getUses()
Returns a range of all uses, which is useful for iterating over all uses.
Definition: Operation.h:847
This class coordinates the application of a rewrite on a set of IR, providing a way for clients to tr...
Definition: PatternMatch.h:358
virtual void finalizeOpModification(Operation *op)
This method is used to signal the end of an in-place modification of the given operation.
virtual void eraseOp(Operation *op)
This method erases an operation that is known to have no uses.
virtual void startOpModification(Operation *op)
This method is used to notify the rewriter that an in-place operation modification is about to happen...
Definition: PatternMatch.h:578
This class provides an abstraction over the different types of ranges over Values.
Definition: ValueRange.h:387
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition: Value.h:96
Type getType() const
Return the type of this value.
Definition: Value.h:105
AffineApplyOp makeComposedAffineApply(OpBuilder &b, Location loc, AffineMap map, ArrayRef< OpFoldResult > operands)
Returns a composed AffineApplyOp by composing map and operands with other AffineApplyOps supplying th...
Definition: AffineOps.cpp:1167
FailureOr< memref::AllocOp > multiBuffer(RewriterBase &rewriter, memref::AllocOp allocOp, unsigned multiplier, bool skipOverrideAnalysis=false)
Transformation to do multi-buffering/array expansion to remove dependencies on the temporary allocati...
Definition: MultiBuffer.cpp:98
Include the generated interface declarations.
void bindDims(MLIRContext *ctx, AffineExprTy &...exprs)
Bind a list of AffineExpr references to DimExpr at positions: [0 .
Definition: AffineExpr.h:311
Value getValueOrCreateConstantIndexOp(OpBuilder &b, Location loc, OpFoldResult ofr)
Converts an OpFoldResult to a Value.
Definition: Utils.cpp:112