1 //===- Hoisting.cpp - Linalg hoisting transformations ---------------------===//
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 functions concerned with hoisting invariant operations
10 // in the context of Linalg transformations.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "mlir/Dialect/Linalg/Transforms/Hoisting.h"
15 #include "mlir/Analysis/AffineStructures.h"
16 #include "mlir/Analysis/SliceAnalysis.h"
17 #include "mlir/Dialect/Affine/IR/AffineValueMap.h"
18 #include "mlir/Dialect/Affine/Utils.h"
19 #include "mlir/Dialect/Linalg/IR/LinalgOps.h"
20 #include "mlir/Dialect/Linalg/Transforms/Transforms.h"
21 #include "mlir/Dialect/SCF/SCF.h"
22 #include "mlir/Dialect/SCF/Utils.h"
23 #include "mlir/Dialect/StandardOps/IR/Ops.h"
24 #include "mlir/Dialect/Tensor/IR/Tensor.h"
25 #include "mlir/Dialect/Vector/VectorOps.h"
26 #include "mlir/Dialect/Vector/VectorUtils.h"
27 #include "mlir/IR/BuiltinOps.h"
28 #include "mlir/IR/Dominance.h"
29 #include "mlir/Transforms/GreedyPatternRewriteDriver.h"
30 #include "mlir/Transforms/LoopUtils.h"
31 #include "llvm/ADT/StringRef.h"
32 #include "llvm/Support/Debug.h"
33 
34 using llvm::dbgs;
35 
36 #define DEBUG_TYPE "linalg-hoisting"
37 
38 #define DBGS() (dbgs() << '[' << DEBUG_TYPE << "] ")
39 
40 using namespace mlir;
41 using namespace mlir::linalg;
42 
43 namespace {
44 /// Represents a unit of hoistable TransferWriteOp. This may comprise other
45 /// instructions that need to be hoisted too.
46 struct HoistableWrite {
47   vector::TransferWriteOp transferWriteOp;
48   tensor::InsertSliceOp insertSliceOp;
49 };
50 /// Represents a unit of hoistable TransferReadOp. This may comprise other
51 /// instructions that need to be hoisted too.
52 struct HoistableRead {
53   vector::TransferReadOp transferReadOp;
54   tensor::ExtractSliceOp extractSliceOp;
55 };
56 } // namespace
57 
58 /// Return true if op1 and op2 are the same constant or the same SSA value.
59 static bool isEqualOffsetSizeOrStride(OpFoldResult op1, OpFoldResult op2) {
60   auto getConstantIntValue = [](OpFoldResult ofr) -> llvm::Optional<int64_t> {
61     Attribute attr = ofr.dyn_cast<Attribute>();
62     // Note: isa+cast-like pattern allows writing the condition below as 1 line.
63     if (!attr && ofr.get<Value>().getDefiningOp<ConstantOp>())
64       attr = ofr.get<Value>().getDefiningOp<ConstantOp>().getValue();
65     if (auto intAttr = attr.dyn_cast_or_null<IntegerAttr>())
66       return intAttr.getValue().getSExtValue();
67     return llvm::None;
68   };
69   auto cst1 = getConstantIntValue(op1), cst2 = getConstantIntValue(op2);
70   if (cst1 && cst2 && *cst1 == *cst2)
71     return true;
72   auto v1 = op1.dyn_cast<Value>(), v2 = op2.dyn_cast<Value>();
73   return v1 && v2 && v1 == v2;
74 }
75 
76 /// Return true is all offsets, sizes and strides are equal.
77 static bool sameOffsetsSizesAndStrides(tensor::ExtractSliceOp s,
78                                        tensor::InsertSliceOp si) {
79   if (s.static_offsets().size() != si.static_offsets().size())
80     return false;
81   if (s.static_sizes().size() != si.static_sizes().size())
82     return false;
83   if (s.static_strides().size() != si.static_strides().size())
84     return false;
85   for (auto it : llvm::zip(s.getMixedOffsets(), si.getMixedOffsets()))
86     if (!isEqualOffsetSizeOrStride(std::get<0>(it), std::get<1>(it)))
87       return false;
88   for (auto it : llvm::zip(s.getMixedSizes(), si.getMixedSizes()))
89     if (!isEqualOffsetSizeOrStride(std::get<0>(it), std::get<1>(it)))
90       return false;
91   for (auto it : llvm::zip(s.getMixedStrides(), si.getMixedStrides()))
92     if (!isEqualOffsetSizeOrStride(std::get<0>(it), std::get<1>(it)))
93       return false;
94   return true;
95 }
96 
97 /// Look for a HoistableRead, in the given tensor uses, accessing the same
98 /// offset as the HoistableWrite.
99 static HoistableRead findMatchingTransferRead(HoistableWrite write,
100                                               Value srcTensor) {
101   assert(write.transferWriteOp &&
102          "expected hoistable write to have a .transfer_write");
103 
104   LLVM_DEBUG(DBGS() << "findMatchingTransferRead for: "
105                     << *write.transferWriteOp.getOperation() << "\n");
106   if (write.insertSliceOp)
107     LLVM_DEBUG(DBGS() << "findMatchingTransferRead inserSliceOp: "
108                       << *write.insertSliceOp.getOperation() << "\n");
109 
110   for (Operation *user : srcTensor.getUsers()) {
111     LLVM_DEBUG(DBGS() << "findMatchingTransferRead inspect user: " << *user
112                       << "\n");
113 
114     // If HoistableWrite involves a InsertSliceOp, we need to find a
115     // matching ExtractSliceOp.
116     tensor::ExtractSliceOp sliceOp;
117     Operation *maybeTransferReadUser = user;
118     if (write.insertSliceOp) {
119       sliceOp = dyn_cast<tensor::ExtractSliceOp>(user);
120       if (!sliceOp || sliceOp.getResult().getType() !=
121                           write.insertSliceOp.source().getType())
122         continue;
123 
124       LLVM_DEBUG(DBGS() << "check whether sameOffsetsSizesAndStrides: "
125                         << *sliceOp << " vs " << *write.insertSliceOp << "\n");
126       if (!sameOffsetsSizesAndStrides(sliceOp, write.insertSliceOp))
127         continue;
128 
129       LLVM_DEBUG(DBGS() << "sameOffsetsSizesAndStrides: SUCCESS\n");
130       // If we got here, sliceOp is hoistable iff it has exactly 2 uses:
131       //   1. the transfer_write we want to hoist.
132       //   2. a matching transfer_read.
133       // Anything else, we skip.
134       bool skip = false;
135       Operation *otherUser = nullptr;
136       for (Operation *u : sliceOp->getUsers()) {
137         if (u == write.transferWriteOp)
138           continue;
139         if (otherUser) {
140           skip = true;
141           break;
142         }
143         otherUser = u;
144       }
145       if (skip || !otherUser)
146         continue;
147       maybeTransferReadUser = otherUser;
148     }
149 
150     LLVM_DEBUG(DBGS() << "maybeTransferReadUser: " << *maybeTransferReadUser
151                       << "\n");
152     auto read = dyn_cast<vector::TransferReadOp>(maybeTransferReadUser);
153     if (read && read.indices() == write.transferWriteOp.indices() &&
154         read.getVectorType() == write.transferWriteOp.getVectorType())
155       return HoistableRead{read, sliceOp};
156   }
157   return HoistableRead();
158 }
159 
160 /// Check if the chunk of data inserted by the HoistableWrite are read by any
161 /// other op than the HoistableRead candidate.
162 static bool tensorChunkAccessedByUnknownOp(HoistableWrite write,
163                                            HoistableRead candidateRead,
164                                            BlockArgument tensorArg) {
165   // Make sure none of the other uses read the part of the tensor modified
166   // by the transfer_write.
167   llvm::SmallVector<Value::use_range, 1> uses;
168   uses.push_back(tensorArg.getUses());
169   while (!uses.empty()) {
170     for (OpOperand &use : uses.pop_back_val()) {
171       Operation *user = use.getOwner();
172       // Skip the candidate use, only inspect the "other" uses.
173       if (user == candidateRead.transferReadOp ||
174           user == candidateRead.extractSliceOp ||
175           user == write.transferWriteOp || user == write.insertSliceOp)
176         continue;
177       // Consider all transitive uses through a extract_slice / insert_slice.
178       // TODO: atm we just bail because a stronger analysis is needed for these
179       // cases.
180       if (isa<tensor::ExtractSliceOp, tensor::InsertSliceOp>(user))
181         return true;
182       // Consider all transitive uses through a vector.transfer_write.
183       if (auto writeUser = dyn_cast<vector::TransferWriteOp>(user)) {
184         uses.push_back(writeUser->getResult(0).getUses());
185         continue;
186       }
187       // Consider all nested uses through an scf::ForOp. We may have
188       // pass-through tensor arguments left from previous level of
189       // hoisting.
190       if (auto forUser = dyn_cast<scf::ForOp>(user)) {
191         Value arg = forUser.getLoopBody().getArgument(
192             use.getOperandNumber() - forUser.getNumControlOperands() +
193             /*iv value*/ 1);
194         uses.push_back(arg.getUses());
195         continue;
196       }
197       // Follow the use yield as long as it doesn't escape the original
198       // region.
199       scf::YieldOp yieldUser = dyn_cast<scf::YieldOp>(user);
200       if (yieldUser && write.transferWriteOp->getParentOp()->isAncestor(
201                            yieldUser->getParentOp())) {
202         Value ret = yieldUser->getParentOp()->getResult(use.getOperandNumber());
203         uses.push_back(ret.getUses());
204         continue;
205       }
206       auto read = dyn_cast<vector::TransferReadOp>(user);
207       if (!read || !isDisjointTransferIndices(
208                        cast<VectorTransferOpInterface>(read.getOperation()),
209                        cast<VectorTransferOpInterface>(
210                            write.transferWriteOp.getOperation()))) {
211         return true;
212       }
213     }
214   }
215   return false;
216 }
217 
218 /// Return the `forOp`-invariant HoistableWrite that produces `yieldOperand`.
219 /// Return the null HoistableWrite() if it is not comprised of a
220 /// vector.transfer_write + optional insert_slice or if any of the indexings
221 /// is `forOp`-dependent.
222 static HoistableWrite
223 getLoopInvariantTransferWriteOpDefining(scf::ForOp forOp,
224                                         OpOperand &yieldOperand) {
225   Value v = yieldOperand.get();
226   if (auto write = v.getDefiningOp<vector::TransferWriteOp>()) {
227     // Indexing must not depend on `forOp`.
228     for (Value operand : write.indices())
229       if (!forOp.isDefinedOutsideOfLoop(operand))
230         return HoistableWrite();
231 
232     return HoistableWrite{write, nullptr};
233   }
234 
235   if (auto insertSliceOp = v.getDefiningOp<tensor::InsertSliceOp>()) {
236     // Inserted slice must come from vector.transfer_write.
237     auto write =
238         insertSliceOp.source().getDefiningOp<vector::TransferWriteOp>();
239     if (!write)
240       return HoistableWrite();
241 
242     // Tensor inserted into must be a BBArg at position matching yieldOperand's.
243     auto bbArg = insertSliceOp.dest().dyn_cast<BlockArgument>();
244     if (!bbArg || bbArg.getOwner()->getParentOp() != forOp ||
245         bbArg.getArgNumber() != /*num iv=*/1 + yieldOperand.getOperandNumber())
246       return HoistableWrite();
247 
248     // Indexing inserted into must not depend on `forOp`.
249     for (Value operand : insertSliceOp->getOperands().drop_front(
250              tensor::InsertSliceOp::getOffsetSizeAndStrideStartOperandIndex()))
251       if (!forOp.isDefinedOutsideOfLoop(operand))
252         return HoistableWrite();
253 
254     return HoistableWrite{write, insertSliceOp};
255   }
256 
257   return HoistableWrite();
258 }
259 
260 /// Mechanical hoisting of a matching HoistableRead / HoistableWrite pair.
261 static void hoistReadWrite(HoistableRead read, HoistableWrite write,
262                            BlockArgument tensorBBArg) {
263   scf::ForOp forOp = cast<scf::ForOp>(tensorBBArg.getOwner()->getParentOp());
264   assert(read.transferReadOp && write.transferWriteOp &&
265          "expected transfer_read and transfer_write ops to be set");
266   assert(((read.extractSliceOp && write.insertSliceOp) ||
267           (!read.extractSliceOp && !write.insertSliceOp)) &&
268          "expected matching extract_slice / insert_slice");
269   LLVM_DEBUG(DBGS() << "In forOp:\n"
270                     << *forOp.getOperation()
271                     << "\nHoist: " << *read.transferReadOp.getOperation()
272                     << "\nHoist: " << *write.transferWriteOp.getOperation()
273                     << "\nInvolving: " << tensorBBArg << "\n");
274 
275   // If a read slice is present, hoist it.
276   if (read.extractSliceOp && failed(forOp.moveOutOfLoop({read.extractSliceOp})))
277     llvm_unreachable("Unexpected failure moving extract_slice out of loop");
278 
279   // Hoist the transfer_read op.
280   if (failed(forOp.moveOutOfLoop({read.transferReadOp})))
281     llvm_unreachable("Unexpected failure moving transfer read out of loop");
282 
283   // TODO: don't hardcode /*numIvs=*/1.
284   assert(tensorBBArg.getArgNumber() >= /*numIvs=*/1);
285   unsigned initArgNumber = tensorBBArg.getArgNumber() - /*numIvs=*/1;
286 
287   // Update the source tensor.
288   if (read.extractSliceOp)
289     read.extractSliceOp.sourceMutable().assign(forOp.initArgs()[initArgNumber]);
290   else
291     read.transferReadOp.sourceMutable().assign(forOp.initArgs()[initArgNumber]);
292 
293   // Hoist write after.
294   if (write.insertSliceOp)
295     write.insertSliceOp->moveAfter(forOp);
296   write.transferWriteOp->moveAfter(forOp);
297 
298   // Update the yield.
299   auto yieldOp = cast<scf::YieldOp>(forOp.region().front().getTerminator());
300   if (write.insertSliceOp)
301     yieldOp->setOperand(initArgNumber, write.insertSliceOp.dest());
302   else
303     yieldOp->setOperand(initArgNumber, write.transferWriteOp.source());
304 
305   // Rewrite `loop` with additional new yields.
306   OpBuilder b(read.transferReadOp);
307   auto newForOp = cloneWithNewYields(b, forOp, read.transferReadOp.vector(),
308                                      write.transferWriteOp.vector());
309   // Transfer write has been hoisted, need to update the vector and tensor
310   // source. Replace the result of the loop to use the new tensor created
311   // outside the loop.
312   // Depending on whether a insert_slice is present or not, it carries the
313   // update on the tensor operands.
314   if (write.insertSliceOp) {
315     newForOp.getResult(initArgNumber)
316         .replaceAllUsesWith(write.insertSliceOp.getResult());
317     write.transferWriteOp.sourceMutable().assign(read.extractSliceOp.result());
318     write.insertSliceOp.destMutable().assign(read.extractSliceOp.source());
319   } else {
320     newForOp.getResult(initArgNumber)
321         .replaceAllUsesWith(write.transferWriteOp.getResult(0));
322     write.transferWriteOp.sourceMutable().assign(
323         newForOp.getResult(initArgNumber));
324   }
325 
326   // Always update with the newly yield tensor and vector.
327   write.transferWriteOp.vectorMutable().assign(newForOp.getResults().back());
328 }
329 
330 // To hoist transfer op on tensor the logic can be significantly simplified
331 // compared to the case on buffer. The transformation follows this logic:
332 // 1. Look for transfer_write with a single use from ForOp yield
333 // 2. Check the uses of the matching block argument and look for a transfer_read
334 // with the same indices.
335 // 3. Check that all the other uses of the tensor argument are either disjoint
336 // tensor_read or transfer_write. For transfer_write uses recurse to make sure
337 // the new tensor has the same restrictions on its uses.
338 // 4. Hoist the tensor_read/tensor_write and update the tensor SSA links.
339 // After this transformation the scf.forOp may have unused arguments that can be
340 // remove by the canonicalization pass.
341 void mlir::linalg::hoistRedundantVectorTransfersOnTensor(FuncOp func) {
342   bool changed = true;
343   while (changed) {
344     changed = false;
345     func.walk([&](scf::ForOp forOp) {
346       Operation *yield = forOp.getBody()->getTerminator();
347       for (auto it : llvm::enumerate(forOp.getRegionIterArgs())) {
348         OpOperand &ret = yield->getOpOperand(it.index());
349         HoistableWrite write =
350             getLoopInvariantTransferWriteOpDefining(forOp, ret);
351         if (!write.transferWriteOp || !write.transferWriteOp->hasOneUse())
352           continue;
353         LLVM_DEBUG(dbgs() << "\n";
354                    DBGS() << "Candidate write for hoisting: "
355                           << *write.transferWriteOp.getOperation() << "\n");
356         if (write.insertSliceOp)
357           LLVM_DEBUG(DBGS() << "Candidate insert_slice for hoisting: "
358                             << *write.insertSliceOp.getOperation() << "\n");
359         if (llvm::any_of(write.transferWriteOp.indices(),
360                          [&forOp](Value index) {
361                            return !forOp.isDefinedOutsideOfLoop(index);
362                          }))
363           continue;
364         // Find a read with the same type and indices.
365         HoistableRead matchingRead =
366             findMatchingTransferRead(write, it.value());
367         // Make sure none of the other uses read the part of the tensor modified
368         // by the transfer_write.
369         if (!matchingRead.transferReadOp ||
370             tensorChunkAccessedByUnknownOp(write, matchingRead, it.value()))
371           continue;
372 
373         LLVM_DEBUG(DBGS() << "Start hoisting\n");
374         hoistReadWrite(matchingRead, write, it.value());
375         changed = true;
376         forOp.erase();
377 
378         // Need to interrupt and restart: erasing the loop messes up the walk.
379         return WalkResult::interrupt();
380       }
381       return WalkResult::advance();
382     });
383     // Apply canonicalization so the newForOp + yield folds immediately, thus
384     // cleaning up the IR and potentially enabling more hoisting.
385     if (changed) {
386       RewritePatternSet patterns(func->getContext());
387       scf::ForOp::getCanonicalizationPatterns(patterns, func->getContext());
388       (void)applyPatternsAndFoldGreedily(func, std::move(patterns));
389     }
390   }
391 }
392 
393 void mlir::linalg::hoistRedundantVectorTransfers(FuncOp func) {
394   bool changed = true;
395   while (changed) {
396     changed = false;
397 
398     func.walk([&](vector::TransferReadOp transferRead) {
399       if (!transferRead.getShapedType().isa<MemRefType>())
400         return WalkResult::advance();
401 
402       LLVM_DEBUG(DBGS() << "Candidate for hoisting: "
403                         << *transferRead.getOperation() << "\n");
404       auto loop = dyn_cast<scf::ForOp>(transferRead->getParentOp());
405       LLVM_DEBUG(DBGS() << "Parent op: " << *transferRead->getParentOp()
406                         << "\n");
407       if (!loop)
408         return WalkResult::advance();
409 
410       if (failed(moveLoopInvariantCode(
411               cast<LoopLikeOpInterface>(loop.getOperation()))))
412         llvm_unreachable(
413             "Unexpected failure to move invariant code out of loop");
414 
415       LLVM_DEBUG(DBGS() << "Candidate read: " << *transferRead.getOperation()
416                         << "\n");
417 
418       SetVector<Operation *> forwardSlice;
419       getForwardSlice(transferRead.getOperation(), &forwardSlice);
420 
421       // Look for the last TransferWriteOp in the forwardSlice of
422       // `transferRead` that operates on the same memref.
423       vector::TransferWriteOp transferWrite;
424       for (auto *sliceOp : llvm::reverse(forwardSlice)) {
425         auto candidateWrite = dyn_cast<vector::TransferWriteOp>(sliceOp);
426         if (!candidateWrite || candidateWrite.source() != transferRead.source())
427           continue;
428         transferWrite = candidateWrite;
429       }
430 
431       // All operands of the TransferRead must be defined outside of the loop.
432       for (auto operand : transferRead.getOperands())
433         if (!loop.isDefinedOutsideOfLoop(operand))
434           return WalkResult::advance();
435 
436       // Only hoist transfer_read / transfer_write pairs for now.
437       if (!transferWrite)
438         return WalkResult::advance();
439 
440       LLVM_DEBUG(DBGS() << "Candidate: " << *transferWrite.getOperation()
441                         << "\n");
442 
443       // Approximate aliasing by checking that:
444       //   1. indices are the same,
445       //   2. no other operations in the loop access the same memref except
446       //      for transfer_read/transfer_write accessing statically disjoint
447       //      slices.
448       if (transferRead.indices() != transferWrite.indices() &&
449           transferRead.getVectorType() == transferWrite.getVectorType())
450         return WalkResult::advance();
451 
452       // TODO: may want to memoize this information for performance but it
453       // likely gets invalidated often.
454       DominanceInfo dom(loop);
455       if (!dom.properlyDominates(transferRead.getOperation(), transferWrite))
456         return WalkResult::advance();
457       for (auto &use : transferRead.source().getUses()) {
458         if (!dom.properlyDominates(loop, use.getOwner()))
459           continue;
460         if (use.getOwner() == transferRead.getOperation() ||
461             use.getOwner() == transferWrite.getOperation())
462           continue;
463         if (auto transferWriteUse =
464                 dyn_cast<vector::TransferWriteOp>(use.getOwner())) {
465           if (!isDisjointTransferSet(
466                   cast<VectorTransferOpInterface>(transferWrite.getOperation()),
467                   cast<VectorTransferOpInterface>(
468                       transferWriteUse.getOperation())))
469             return WalkResult::advance();
470         } else if (auto transferReadUse =
471                        dyn_cast<vector::TransferReadOp>(use.getOwner())) {
472           if (!isDisjointTransferSet(
473                   cast<VectorTransferOpInterface>(transferWrite.getOperation()),
474                   cast<VectorTransferOpInterface>(
475                       transferReadUse.getOperation())))
476             return WalkResult::advance();
477         } else {
478           // Unknown use, we cannot prove that it doesn't alias with the
479           // transferRead/transferWrite operations.
480           return WalkResult::advance();
481         }
482       }
483 
484       // Hoist read before.
485       if (failed(loop.moveOutOfLoop({transferRead})))
486         llvm_unreachable(
487             "Unexpected failure to move transfer read out of loop");
488 
489       // Hoist write after.
490       transferWrite->moveAfter(loop);
491 
492       // Rewrite `loop` with new yields by cloning and erase the original loop.
493       OpBuilder b(transferRead);
494       auto newForOp = cloneWithNewYields(b, loop, transferRead.vector(),
495                                          transferWrite.vector());
496 
497       // Transfer write has been hoisted, need to update the written value to
498       // the value yielded by the newForOp.
499       transferWrite.vector().replaceAllUsesWith(
500           newForOp.getResults().take_back()[0]);
501 
502       changed = true;
503       loop.erase();
504       // Need to interrupt and restart because erasing the loop messes up the
505       // walk.
506       return WalkResult::interrupt();
507     });
508   }
509 }
510 
511 /// Return success if `v` is a value that is only transitively defined by ops of
512 /// type in `OpTypeList`.
513 template <typename... OpTypeList>
514 static bool backwardsSliceOnlyHasOpsOfType(scf::ForOp outerLimit, Value v) {
515   // Compute a backward slice up to, but not including, `outerLimit`.
516   SetVector<Operation *> backwardSlice;
517   getBackwardSlice(v, &backwardSlice, [&](Operation *op) {
518     return outerLimit->isProperAncestor(op);
519   });
520   // Traverse the backward slice and ensure we can perform the computation to
521   // hoist.
522   for (Operation *op : backwardSlice) {
523     if (isa<OpTypeList...>(op))
524       continue;
525     LLVM_DEBUG(DBGS() << "Abort: unadmissible op in slice " << *op << "\n");
526     return false;
527   }
528   return true;
529 }
530 
531 bool isDefinedOutsideOrConstant(scf::ForOp outer, Value v) {
532   return outer.isDefinedOutsideOfLoop(v) || v.getDefiningOp<ConstantOp>();
533 }
534 
535 /// Return the current iteration number in the loop (iv - lb).ceilDiv(step).
536 /// The returned Value is guaranteed not to depend on any loop comprised in
537 /// [`outer`, `forOp`].
538 /// Return null if such a loop-independent quantity cannot be computed.
539 static Value buildLoopIterationCount(OpBuilder &b, scf::ForOp outer,
540                                      scf::ForOp forOp) {
541   MLIRContext *ctx = forOp->getContext();
542   AffineExpr iv, lb, step;
543   bindDims(ctx, iv, lb);
544   bindSymbols(ctx, step);
545   if (!isDefinedOutsideOrConstant(outer, forOp.lowerBound()) ||
546       !isDefinedOutsideOrConstant(outer, forOp.step()))
547     return Value();
548   Value ivVal = forOp.getInductionVar(), lbVal = forOp.lowerBound(),
549         stepVal = forOp.step();
550   auto loc = forOp->getLoc();
551   return b.createOrFold<AffineApplyOp>(loc, (iv - lb).ceilDiv(step),
552                                        ValueRange{ivVal, lbVal, stepVal});
553 }
554 
555 /// Given a set of loops, assumed to be scf::ForOp, create a constraint set
556 /// containing the inequalities `iv - lb >= 0` and `-iv + ub - 1 >= 0` for each
557 /// loop.
558 static FlatAffineValueConstraints
559 initLoopIvsAndBounds(ArrayRef<Operation *> loops) {
560   FlatAffineValueConstraints constraints;
561   for (Operation *op : loops)
562     constraints.addDimId(constraints.getNumDimIds(),
563                          cast<scf::ForOp>(op).getInductionVar());
564   for (Operation *op : loops)
565     constraints.addDimId(constraints.getNumDimIds(),
566                          cast<scf::ForOp>(op).lowerBound());
567   for (Operation *op : loops)
568     constraints.addDimId(constraints.getNumDimIds(),
569                          cast<scf::ForOp>(op).upperBound());
570   unsigned numLoops = loops.size();
571   for (unsigned ivIdx = 0, e = numLoops; ivIdx < e; ++ivIdx) {
572     // iv - lb >= 0
573     SmallVector<int64_t, 8> ineqLb(constraints.getNumCols(), 0);
574     ineqLb[ivIdx] = 1;
575     ineqLb[ivIdx + numLoops] = -1;
576     // -iv + ub >= 0
577     SmallVector<int64_t, 8> ineqUb(constraints.getNumCols(), 0);
578     ineqUb[ivIdx] = -1;
579     ineqUb[ivIdx + 2 * numLoops] = 1;
580     ineqUb[constraints.getNumCols() - 1] = -1;
581     constraints.addInequality(ineqLb);
582     constraints.addInequality(ineqUb);
583   }
584   return constraints;
585 }
586 
587 /// For each loop in `loops`, determine the ops involved in the construction of
588 /// its upper bound---up to the outerLimit loop--- and fold them as new
589 /// inequalities in the constraint set.
590 /// This is achieved by computing the backwardSlice of the loop's upper bound
591 /// and iteratively folding each op in reverse topological order to guarantee
592 /// use-def ordering.
593 /// As operations are folded in, their result is projected out of the
594 /// constraints set.
595 /// The following operations are supported:
596 ///   - scf::ForOp are simply skipped.
597 ///   - AffineApplyOp are composed to replace the result by an equality.
598 ///   - AffineMinOp are composed by adding each entry as an upper bound.
599 /// If any other operation is met, return failure.
600 // TODO: extend on a per-need basis.
601 static LogicalResult
602 foldUpperBoundsIntoConstraintsSet(FlatAffineValueConstraints &constraints,
603                                   scf::ForOp outerLimit,
604                                   ArrayRef<Operation *> loops) {
605   SetVector<Value> toProjectOut;
606   for (Operation *loop : loops) {
607     auto ub = cast<scf::ForOp>(loop).upperBound();
608     if (isDefinedOutsideOrConstant(outerLimit, ub))
609       continue;
610 
611     // Compute a backward slice up to, but not including, `outerLimit`.
612     SetVector<Operation *> backwardSlice;
613     getBackwardSlice(ub, &backwardSlice, [&](Operation *op) {
614       return outerLimit->isProperAncestor(op);
615     });
616     backwardSlice.insert(ub.getDefiningOp());
617 
618     // Iterate over all ops in the slice and compose them in the constraints.
619     for (Operation *op : llvm::reverse(backwardSlice)) {
620       if (!isa<scf::ForOp, AffineApplyOp, AffineMinOp>(op))
621         return failure();
622       if (isa<scf::ForOp>(op))
623         continue;
624       // Ensure there is a
625       auto ensureIdFailed = [&](Value v) {
626         if (constraints.containsId(v)) {
627           unsigned pos;
628           constraints.findId(v, &pos);
629           return pos >= constraints.getNumDimIds();
630         }
631         constraints.addDimId(constraints.getNumDimIds(), v);
632         return false;
633       };
634 
635       // Ensure all ids exist and add results for later projection.
636       if (llvm::any_of(op->getResults(), ensureIdFailed) ||
637           llvm::any_of(op->getOperands(), ensureIdFailed))
638         return failure();
639 
640       // All supported ops have 1 result.
641       // TODO: extend when needed.
642       toProjectOut.insert(op->getResult(0));
643 
644       // Compose supported ops.
645       if (auto affineApplyOp = dyn_cast<AffineApplyOp>(op)) {
646         AffineValueMap avm(affineApplyOp.getAffineMap(),
647                            affineApplyOp.getOperands(),
648                            affineApplyOp.getResult());
649         if (failed(constraints.composeMap(&avm)))
650           return failure();
651         continue;
652       }
653       auto affineMinOp = cast<AffineMinOp>(op);
654       unsigned pos;
655       bool foundMinOp = constraints.findId(affineMinOp.getResult(), &pos);
656       (void)foundMinOp;
657       assert(foundMinOp);
658       AffineMap alignedMap = constraints.computeAlignedMap(
659           affineMinOp.getAffineMap(), affineMinOp.getOperands());
660       if (failed(
661               constraints.addBound(FlatAffineConstraints::UB, pos, alignedMap)))
662         return failure();
663     }
664   }
665   for (Value v : toProjectOut)
666     constraints.projectOut(v);
667   return success();
668 }
669 
670 /// Ensure prerequisites that guarantee pad op hoisting can occur.
671 /// Return failure in the cases when we cannot perform hoisting; i.e. if either:
672 ///   1. There exists a use of `padTensorOp` that is not a linalg input operand.
673 ///   2. There isn't an enclosing `outermostEnclosingForOp` loop.
674 ///   3. There exists an op with a region that is dominated by
675 ///   `outermostEnclosingForOp` and that isn't a LoopLikeInterface or a
676 ///    LinalgOp.
677 ///   4. There exists an op with side effects that is dominated by
678 ///   `outermostEnclosingForOp` and that isn't a LoopLikeInterface.
679 ///   5. The lower bound, upper bound and step of all the loops involved in the
680 ///   hoisting can be
681 ///
682 /// While ensuring prerequisites:
683 ///   1. Fill the `backwardSlice` to contain the topologically sorted ops
684 ///   dominated by `outermostEnclosingForOp`.
685 ///   2. Fill the `packingLoops` to contain only the enclosing loops of
686 ///   `backwardSlice` whose IV is actually used in computing padding. Loops that
687 ///   remain in `backwardSlice` but that are not in `packingLoops` are
688 ///   dimensions of reuse.
689 static LogicalResult
690 hoistPaddingOnTensorsPrerequisites(linalg::PadTensorOp padTensorOp, int nLevels,
691                                    SetVector<Operation *> &backwardSlice,
692                                    SetVector<Operation *> &packingLoops,
693                                    SmallVector<Value> &dynamicTensorSizes) {
694   // Bail on any use that isn't an input of a Linalg op.
695   // Hoisting of inplace updates happens after vectorization.
696   for (OpOperand &use : padTensorOp.result().getUses()) {
697     auto linalgUser = dyn_cast<linalg::LinalgOp>(use.getOwner());
698     if (!linalgUser || !linalgUser.isInputTensor(&use))
699       return failure();
700   }
701 
702   // Get at most nLevels of enclosing loops.
703   SmallVector<LoopLikeOpInterface> reverseEnclosingLoops;
704   Operation *outermostEnclosingForOp = nullptr,
705             *nextEnclosingForOp =
706                 padTensorOp->getParentOfType<LoopLikeOpInterface>();
707   while (nLevels-- > 0 && nextEnclosingForOp) {
708     outermostEnclosingForOp = nextEnclosingForOp;
709     reverseEnclosingLoops.push_back(outermostEnclosingForOp);
710     nextEnclosingForOp =
711         nextEnclosingForOp->getParentOfType<LoopLikeOpInterface>();
712   }
713   if (!outermostEnclosingForOp)
714     return failure();
715 
716   // Get the backwards slice from `padTensorOp` that is dominated by the
717   // outermost enclosing loop.
718   DominanceInfo domInfo(outermostEnclosingForOp);
719   getBackwardSlice(padTensorOp.getOperation(), &backwardSlice,
720                    [&](Operation *op) {
721                      return domInfo.dominates(outermostEnclosingForOp, op);
722                    });
723 
724   // Bail on any op with a region that is not a LoopLikeInterface or a LinalgOp.
725   if (llvm::any_of(backwardSlice, [](Operation *op) {
726         return op->getNumRegions() > 0 && !isa<LoopLikeOpInterface>(op) &&
727                !isa<LinalgOp>(op);
728       }))
729     return failure();
730 
731   // Filter out the loops whose induction variable is not used to compute the
732   // padded result. As a first approximation, just look for IVs that have no use
733   // in the backwardSlice.
734   // These are the dimensions of reuse that we can exploit to reduce the amount
735   // of work / memory.
736   // TODO: would this optimization compose better as a canonicalization?
737   for (LoopLikeOpInterface loop : llvm::reverse(reverseEnclosingLoops)) {
738     auto forOp = dyn_cast<scf::ForOp>(loop.getOperation());
739     if (!forOp)
740       continue;
741     for (Operation *user : forOp.getInductionVar().getUsers()) {
742       if (backwardSlice.contains(user)) {
743         packingLoops.insert(forOp);
744         break;
745       }
746     }
747   }
748 
749   // Backward slice is a topologically sorted list of ops starting at
750   // `outermostEnclosingForOp`.
751   assert(outermostEnclosingForOp == backwardSlice.front());
752 
753   scf::ForOp outer = cast<scf::ForOp>(outermostEnclosingForOp);
754 
755   FlatAffineValueConstraints constraints =
756       initLoopIvsAndBounds(packingLoops.getArrayRef());
757   if (failed(foldUpperBoundsIntoConstraintsSet(constraints, outer,
758                                                packingLoops.getArrayRef())))
759     return failure();
760 
761   unsigned numLoops = packingLoops.size();
762   SmallVector<AffineMap> lbs(numLoops), ubs(numLoops);
763   // Compute the bounds of the first positions, assuming the others are fixed.
764   constraints.getSliceBounds(/*pos=*/0, /*num=*/packingLoops.size(),
765                              outer->getContext(), &lbs, &ubs);
766 
767   SmallVector<Value> allValues;
768   constraints.getAllValues(&allValues);
769   SmallVector<Value> allNonLoopValues(allValues.begin() + numLoops,
770                                       allValues.end());
771 
772   // For each packingLoop, create the extent by (ub - lb).ceilDiv(step).
773   // IP just before the outermost loop considered that we hoist above.
774   ImplicitLocOpBuilder b(outer->getLoc(), outer);
775   assert(packingLoops.size() == lbs.size() && "expected matching lb sizes");
776   assert(packingLoops.size() == ubs.size() && "expected matching ub sizes");
777   for (auto it : llvm::zip(packingLoops, lbs, ubs)) {
778     scf::ForOp loop = cast<scf::ForOp>(std::get<0>(it));
779     AffineMap lbMap = std::get<1>(it);
780     AffineMap ubMap = std::get<2>(it);
781     SmallVector<Value> lbOperands(allNonLoopValues);
782     canonicalizeMapAndOperands(&lbMap, &lbOperands);
783     Value lbVal = b.createOrFold<AffineMaxOp>(lbMap, lbOperands);
784 
785     SmallVector<Value> ubOperands(allNonLoopValues);
786     canonicalizeMapAndOperands(&ubMap, &ubOperands);
787     Value ubVal = b.createOrFold<AffineMinOp>(ubMap, ubOperands);
788 
789     AffineExpr lb, ub, step;
790     bindDims(b.getContext(), lb, ub);
791     bindSymbols(b.getContext(), step);
792     Value res = b.createOrFold<AffineApplyOp>(
793         (ub - lb).ceilDiv(step),
794         ValueRange{lbVal, ubVal, cast<scf::ForOp>(loop).step()});
795 
796     dynamicTensorSizes.push_back(res);
797   }
798 
799   return success();
800 }
801 
802 LogicalResult mlir::linalg::hoistPaddingOnTensors(PadTensorOp &padTensorOp,
803                                                   unsigned nLoops) {
804   SmallVector<Value> dynamicTensorSizes;
805   SetVector<Operation *> backwardSlice, packingLoops;
806   if (failed(hoistPaddingOnTensorsPrerequisites(padTensorOp, nLoops,
807                                                 backwardSlice, packingLoops,
808                                                 dynamicTensorSizes)))
809     return failure();
810 
811   // Update actual number of loops, which may be smaller.
812   nLoops = packingLoops.size();
813 
814   Location loc = padTensorOp->getLoc();
815   RankedTensorType paddedTensorType = padTensorOp.getResultType();
816   unsigned paddedRank = paddedTensorType.getRank();
817 
818   // Backward slice is a topologically sorted list of ops starting at
819   // `outermostEnclosingForOp`.
820   Operation *outermostEnclosingForOp = backwardSlice.front();
821   // IP just before the outermost loop considered that we hoist above.
822   OpBuilder b(outermostEnclosingForOp);
823 
824   // Create the packed tensor<?x?x..?xpadded_shape> into which we amortize
825   // padding.
826   SmallVector<int64_t> packedShape(nLoops, ShapedType::kDynamicSize);
827   // TODO: go grab dims when necessary, for now PadTensorOp returns a static
828   // tensor.
829   llvm::append_range(packedShape, paddedTensorType.getShape());
830   auto packedTensorType =
831       RankedTensorType::get(packedShape, paddedTensorType.getElementType());
832   Value packedTensor = b.create<linalg::InitTensorOp>(
833       loc, dynamicTensorSizes, packedTensorType.getShape(),
834       packedTensorType.getElementType());
835 
836   // Clone the operations involved in the backward slice, iteratively stepping
837   // into the loops that we encounter.
838   // The implementation proceeds in a stack-like fashion:
839   //   1. Iteratively clone and step into the loops, pushing the `packedTensor`
840   //      deeper in the stack.
841   //   2. Create a InsertSliceOp at the top of the stack.
842   //   3. Iteratively pop and yield the result of the InsertSliceOp across
843   //     the cloned loops.
844   SmallVector<Value> clonedLoopIvs, leadingPackedTensorIndexings;
845   clonedLoopIvs.reserve(nLoops);
846   leadingPackedTensorIndexings.reserve(nLoops);
847   BlockAndValueMapping bvm;
848   // Insert `padTensorOp` into the backwardSlice so we clone it too.
849   backwardSlice.insert(padTensorOp);
850   // Stack step 1. iteratively clone loops and push `packedTensor`.
851   for (Operation *op : backwardSlice) {
852     // Specifically sit out in the extract_slice(packedTensor) case: this is the
853     // piece we seek to replace.
854     if (auto sliceOp = dyn_cast<tensor::ExtractSliceOp>(op))
855       if (bvm.lookupOrDefault(sliceOp.source()) == packedTensor)
856         continue;
857     auto effects = dyn_cast<MemoryEffectOpInterface>(op);
858     bool hasNoEffects = !effects || effects.hasNoEffect();
859     if (hasNoEffects &&
860         (op->getNumRegions() == 0 || isa<linalg::PadTensorOp>(op))) {
861       b.clone(*op, bvm);
862       continue;
863     }
864     // TODO: support more cases as they appear.
865     auto forOp = dyn_cast<scf::ForOp>(op);
866     assert(forOp && "Expected scf::ForOp when hoisting pad ops");
867     // Unused loop, just skip it.
868     if (!packingLoops.contains(forOp))
869       continue;
870 
871     auto clonedForOp =
872         b.create<scf::ForOp>(loc, bvm.lookupOrDefault(forOp.lowerBound()),
873                              bvm.lookupOrDefault(forOp.upperBound()),
874                              bvm.lookupOrDefault(forOp.step()), packedTensor);
875     // Map the induction var, region args and results to the `clonedForOp`.
876     bvm.map(forOp.getInductionVar(), clonedForOp.getInductionVar());
877     bvm.map(forOp.getRegionIterArgs(), clonedForOp.getRegionIterArgs());
878     bvm.map(forOp.getResults(), clonedForOp.getResults());
879     assert(clonedForOp->getNumRegions() == 1);
880     clonedLoopIvs.push_back(clonedForOp.getInductionVar());
881 
882     b.setInsertionPointToStart(&clonedForOp->getRegion(0).front());
883     Value loopIndependentIterationCount = buildLoopIterationCount(
884         b, cast<scf::ForOp>(outermostEnclosingForOp), clonedForOp);
885     // Assert the loop-independent iteration count can be computed.
886     if (!loopIndependentIterationCount)
887       llvm_unreachable("loop independence prerequisite not met");
888     leadingPackedTensorIndexings.push_back(loopIndependentIterationCount);
889     packedTensor = clonedForOp.getRegionIterArgs().front();
890   }
891 
892   // Stack step 2. create InsertSliceOp at the top of the stack.
893   // offsets = [clonedLoopIvs, 0 .. 0].
894   SmallVector<OpFoldResult> offsets(leadingPackedTensorIndexings.begin(),
895                                     leadingPackedTensorIndexings.end());
896   offsets.append(paddedRank, b.getIndexAttr(0));
897   // sizes = [1 .. 1, paddedShape].
898   SmallVector<OpFoldResult> sizes(nLoops, b.getIndexAttr(1));
899   for (int64_t sz : paddedTensorType.getShape()) {
900     // TODO: go grab dims when necessary, for now PadTensorOp returns a static
901     // tensor.
902     assert(!ShapedType::isDynamic(sz) && "padded tensor needs static sizes");
903     sizes.push_back(b.getIndexAttr(sz));
904   }
905   // strides = [1 .. 1].
906   SmallVector<OpFoldResult> strides(nLoops + paddedRank, b.getIndexAttr(1));
907 
908   Value inserted =
909       b.create<tensor::InsertSliceOp>(loc, bvm.lookup(padTensorOp.result()),
910                                       packedTensor, offsets, sizes, strides);
911 
912   // Stack step 3. iteratively pop the stack and propagate the yield.
913   Value valueToYield = inserted;
914   for (Value iv : llvm::reverse(clonedLoopIvs)) {
915     auto forOp = scf::getForInductionVarOwner(iv);
916     b.setInsertionPointToEnd(&forOp.getRegion().front());
917     b.create<scf::YieldOp>(loc, valueToYield);
918     valueToYield = forOp.getResult(0);
919   }
920 
921   // Now the packed tensor is ready, replace the original padding op by a
922   // 1x..x1 slice [originalLoopIvs, 0 .. 0][1 .. 1, paddedShape][1 .. 1].
923   b.setInsertionPoint(padTensorOp);
924   SmallVector<Value> loopIterationCounts =
925       llvm::to_vector<4>(llvm::map_range(packingLoops, [&](Operation *loop) {
926         return buildLoopIterationCount(
927             b, cast<scf::ForOp>(outermostEnclosingForOp),
928             cast<scf::ForOp>(loop));
929       }));
930   // Assert all loop iteration counts can be computed.
931   if (llvm::any_of(loopIterationCounts, [](Value v) { return !v; }))
932     llvm_unreachable("loop independence prerequisite not met");
933   // offsets = [originalLoopIvs, 0 .. 0].
934   offsets.assign(loopIterationCounts.begin(), loopIterationCounts.end());
935   offsets.append(paddedRank, b.getIndexAttr(0));
936   // sizes = [1 .. 1, paddedShape] (definedabove).
937   // strides = [1 .. 1] (defined above)
938   packedTensor =
939       scf::getForInductionVarOwner(clonedLoopIvs.front())->getResult(0);
940   padTensorOp.replaceAllUsesWith(
941       b.create<tensor::ExtractSliceOp>(loc, padTensorOp.getResultType(),
942                                        packedTensor, offsets, sizes, strides)
943           ->getResult(0));
944 
945   Operation *toErase = padTensorOp;
946 
947   // Make the newly cloned `padTensorOp` available to the caller.
948   padTensorOp =
949       cast<PadTensorOp>(bvm.lookup(padTensorOp.result()).getDefiningOp());
950 
951   toErase->erase();
952 
953   return success();
954 }
955