1 //===- FusionOnTensors.cpp - Implementation of linalg Fusion --------------===//
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 linalg fusion on tensors
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "PassDetail.h"
14 #include "mlir/Analysis/SliceAnalysis.h"
15 #include "mlir/Dialect/Affine/IR/AffineOps.h"
16 #include "mlir/Dialect/Linalg/IR/Linalg.h"
17 #include "mlir/Dialect/Linalg/Passes.h"
18 #include "mlir/Dialect/Linalg/Transforms/Transforms.h"
19 #include "mlir/Dialect/Linalg/Utils/Utils.h"
20 #include "mlir/Dialect/Tensor/IR/Tensor.h"
21 #include "mlir/Dialect/Utils/IndexingUtils.h"
22 #include "mlir/IR/AffineExpr.h"
23 #include "mlir/IR/AffineMap.h"
24 #include "mlir/Support/LLVM.h"
25 
26 using namespace mlir;
27 using namespace linalg;
28 
29 //===----------------------------------------------------------------------===//
30 // StructuredOp specific helpers.
31 //===----------------------------------------------------------------------===//
32 
33 /// Returns the tiled slice dimensions given the tiled consumer loop dimensions.
34 /// The slice defines a hyper rectangular iteration space and fusing the
35 /// producer is always possible. However, depending on the consumer indexing
36 /// map, not all slice elements may be consumed and the tiles may overlap. In
37 /// these cases, fusion introduces redundant computation.
38 static SmallVector<int64_t> getTiledSliceDims(OpOperand *consumerOperand,
39                                               ArrayRef<int64_t> tiledLoopDims) {
40   // Get the consumer operand indexing map.
41   LinalgOp consumerOp = consumerOperand->getOwner();
42   AffineMap indexingMap = consumerOp.getTiedIndexingMap(consumerOperand);
43 
44   // Search the slice dimensions tiled by a tile loop dimension.
45   DenseSet<int64_t> tiledSliceDimIndices;
46   for (const auto &en : enumerate(indexingMap.getResults())) {
47     for (auto tiledLoopDim : tiledLoopDims) {
48       if (en.value().isFunctionOfDim(tiledLoopDim))
49         tiledSliceDimIndices.insert(en.index());
50     }
51   }
52   return {tiledSliceDimIndices.begin(), tiledSliceDimIndices.end()};
53 }
54 
55 /// Given a vector of `tiledSliceDimIndices` that represent the tiled dimensions
56 /// of the producer result slice returns the tiled producer loop dimensions.
57 /// Example:
58 /// ```
59 /// %res = linalg.fill(%cst, %input)
60 /// scf.for %i
61 ///   scf.for %j
62 ///     %slice = tensor.extract_slice %res[%i, %j]
63 /// ```
64 /// getTiledProducerLoops(%res, [0, 1]) returns the loop indices [0, 1].
65 static SmallVector<int64_t>
66 getTiledProducerLoops(OpResult producerResult,
67                       ArrayRef<int64_t> tiledSliceDimIndices) {
68   LinalgOp producerOp = producerResult.getOwner();
69 
70   // Get the indexing map of the `producerOp` output operand that matches
71   // ´producerResult´.
72   AffineMap producerIndexingMap = producerOp.getTiedIndexingMap(
73       producerOp.getOutputOperand(producerResult.getResultNumber()));
74 
75   // Keep only the tiled result slice dimensions of `producerIndexingMap`.
76   AffineMap tiledProducerIndexingSubMap =
77       producerIndexingMap.getSubMap(SmallVector<unsigned>(
78           tiledSliceDimIndices.begin(), tiledSliceDimIndices.end()));
79 
80   // Compute the producer loop indices mapped to the tiled result slice
81   // dimensions. As the output indexing map of structured operations are
82   // projected permutations, `tiledProducerIndexingSubMap` has to be a
83   // projected permutation as well. We can thus obtain the producer loop indices
84   // by getting the positions of the result dimensions.
85   // Example:
86   // (d0, d1, d2) -> (d0, d2) has the result positions [0, 2].
87   assert(tiledProducerIndexingSubMap.isProjectedPermutation() &&
88          "expect slice and producer loop dimensions map one-to-one");
89   SmallVector<int64_t> tiledProducerLoopIndices;
90   transform(llvm::seq<unsigned>(0, tiledProducerIndexingSubMap.getNumResults()),
91             std::back_inserter(tiledProducerLoopIndices), [&](unsigned idx) {
92               return tiledProducerIndexingSubMap.getDimPosition(idx);
93             });
94 
95   return tiledProducerLoopIndices;
96 }
97 
98 /// Returns the producer fused in place of `sliceOp`. Tile the producer operands
99 /// along the `tiledSliceDimIndices` and clone the producer. Consider the case
100 /// of fusion of an output tensor:
101 /// ```
102 /// %1 = producer ins(...) outs(%0)
103 /// %2 = consumer ins(...) outs(%1)
104 /// ```
105 /// When consumer is tiled, %1 appears in the loop iter_args:
106 /// ```
107 /// %1 = producer ins(...) outs(%0)
108 /// %2 = scf.for ... iter_args(%1) .. (%bbarg) {
109 ///   %t1 = tensor.extract_slice %bbarg[..]
110 ///   %t2 = consumer ins(...) outs(%t1)
111 ///   %r = tensor.insert_slice %t2, %bbarg[...]
112 /// }
113 /// ```
114 /// Fusing %1 into the loop requires updating iter_args(%1) to iter_args(%0):
115 /// ```
116 /// %2 = scf.for ... iter_args(%0) .. (%bbarg) {
117 ///   %t0 = tensor.extract_slice %bbarg[..]
118 ///   %t1 = producer ins(...) outs(%t0)
119 ///   %t2 = consumer ins(...) outs(%t1)
120 ///   %r = tensor.insert_slice %t2, %bbarg[...]
121 /// }
122 /// ```
123 /// This transformation is only valid if %bbarg is exclusively used by the
124 /// output ExtractSliceOp / InsertSliceOp pair, which is checked by the
125 /// `fuseProducer` method.
126 /// TODO: instead of check and failure, insert new iter_args each time a
127 /// producer is fused into a consumer and fold away unused iter_args.
128 static LinalgOp getTiledProducer(OpBuilder &b, OpResult producerResult,
129                                  tensor::ExtractSliceOp sliceOp,
130                                  ArrayRef<int64_t> tiledSliceDimIndices,
131                                  ArrayRef<int64_t> tiledProducerLoopIndices,
132                                  OpOperand *iterArg) {
133   // Clone the producer after `sliceOp` since the slice may be reused to pass in
134   // the producer result.
135   OpBuilder::InsertionGuard guard(b);
136   b.setInsertionPointAfter(sliceOp);
137 
138   // Get the producer.
139   LinalgOp producerOp = producerResult.getOwner();
140   Location loc = producerOp.getLoc();
141 
142   // Obtain the `producerOp` loop bounds and the `sliceOp` ranges.
143   SmallVector<Value> producerLoopBounds;
144   transform(producerOp.createLoopRanges(b, loc),
145             std::back_inserter(producerLoopBounds),
146             [](Range range) { return range.size; });
147   SmallVector<Range> sliceOpRanges = sliceOp.getOrCreateRanges(b, loc);
148 
149   // Tile the producer operands given the `sliceOp` ranges. Iterate the
150   // `tiledSliceDimIndices` and store the tile offset and size for the tiled
151   // slice dimension.
152   auto zero = b.create<arith::ConstantIndexOp>(loc, 0);
153   SmallVector<Value> tileIvs(producerOp.getNumLoops(), nullptr);
154   SmallVector<Value> tileSizes(producerOp.getNumLoops(), zero);
155   SmallVector<Value> allIvs(producerOp.getNumLoops(), nullptr);
156   for (auto it : zip(tiledSliceDimIndices, tiledProducerLoopIndices)) {
157     int64_t tiledSliceDim = std::get<0>(it);
158     int64_t tiledProducerLoop = std::get<1>(it);
159     tileIvs[tiledProducerLoop] = sliceOpRanges[tiledSliceDim].offset;
160     tileSizes[tiledProducerLoop] = sliceOpRanges[tiledSliceDim].size;
161     allIvs[tiledProducerLoop] = tileIvs[tiledProducerLoop];
162   }
163   erase_value(tileIvs, nullptr);
164   SmallVector<Value> tiledOperands = producerOp.getInputAndOutputOperands();
165   tiledOperands = makeTiledShapes(b, loc, producerOp, tiledOperands, tileIvs,
166                                   tileSizes, producerLoopBounds,
167                                   /**omitPartialTileCheck=*/false);
168 
169   // Output fusion has to update the iteration arguments of the tile loop nest.
170   // In particular, the iteration argument of the outermost tile loop needs to
171   // be set to the producer output instead of the producer result and `clonedOp`
172   // shall use the existing `sliceOp` result instead of the tiled producer
173   // output operand.
174   if (iterArg) {
175     OpOperand *outputOperand =
176         producerOp.getOutputOperand(producerResult.getResultNumber());
177     iterArg->set(outputOperand->get());
178     tiledOperands[outputOperand->getOperandNumber()] = sliceOp.getResult();
179   }
180 
181   // Clone the producer using the tiled producer operands.
182   TypeRange resultTypes = ValueRange(tiledOperands)
183                               .take_back(producerOp.getNumOutputs())
184                               .getTypes();
185   LinalgOp clonedOp = producerOp.clone(b, loc, resultTypes, tiledOperands);
186 
187   // Shift all IndexOp results by the tile offset.
188   addTileLoopIvsToIndexOpResults(b, clonedOp, allIvs);
189 
190   return clonedOp;
191 }
192 
193 //===----------------------------------------------------------------------===//
194 // TileLoopNest specific helpers.
195 //===----------------------------------------------------------------------===//
196 
197 bool TileLoopNest::isEmpty() { return tileLoopOps.empty(); }
198 
199 bool TileLoopNest::isValid() {
200   // Check if `rootOp` has been tiled at least once.
201   if (isEmpty() || tiledRootAndFusedOpsLoops.count(rootOp) == 0)
202     return false;
203 
204   // Check if the number of loop operations and dimensions match.
205   if (tileLoopOps.size() != tiledRootAndFusedOpsLoops[rootOp].size())
206     return false;
207 
208   // Check if the innermost tile loop is the parent of `tiledOp`.
209   if (rootOp->getParentOp() != tileLoopOps.back())
210     return false;
211 
212   // Check if the tile loops are directly nested.
213   return std::adjacent_find(tileLoopOps.begin(), tileLoopOps.end(),
214                             [](Operation *op1, Operation *op2) {
215                               return op1 != op2->getParentOp();
216                             }) == tileLoopOps.end();
217 }
218 
219 SmallVector<BlockArgument> TileLoopNest::getTiedBBArgs(BlockArgument bbArg) {
220   assert(bbArg && "expect the block argument to be non-zero");
221   SmallVector<BlockArgument> bbArgs;
222 
223   // Search all tile loop block arguments from inner to outer.
224   for (auto tileLoop : reverse(tileLoopOps)) {
225     if (bbArg.getOwner()->getParentOp() != tileLoop)
226       return {};
227     bbArgs.push_back(bbArg);
228     OpOperand *iterArg = &tileLoop.getOpOperandForRegionIterArg(bbArg);
229     bbArg = iterArg->get().dyn_cast<BlockArgument>();
230   }
231 
232   // Reverse the block arguments to order them from outer to inner.
233   return {bbArgs.rbegin(), bbArgs.rend()};
234 }
235 
236 OpOperand *TileLoopNest::getTiedIterArg(BlockArgument bbArg) {
237   // Search all block arguments and return the matching iteration argument.
238   SmallVector<BlockArgument> bbArgs = getTiedBBArgs(bbArg);
239   if (bbArgs.size() != tileLoopOps.size())
240     return nullptr;
241   return &tileLoopOps.front().getOpOperandForRegionIterArg(bbArgs.front());
242 }
243 
244 bool TileLoopNest::hasOtherUses(BlockArgument bbArg,
245                                 tensor::ExtractSliceOp sliceOp) {
246   // Check the innermost block argument is either used by the ExtractSliceOp
247   // `sliceOp`, the matching InsertSliceOp, or by a DimOp. Handle other uses
248   // conservatively.
249   for (Operation *op : bbArg.getUsers()) {
250     if (!isa<tensor::DimOp, tensor::InsertSliceOp, tensor::ExtractSliceOp>(op))
251       return false;
252     if (auto extractSliceOp = dyn_cast<tensor::ExtractSliceOp>(op)) {
253       if (extractSliceOp != sliceOp)
254         return false;
255     }
256     if (auto insertSliceOp = dyn_cast<tensor::InsertSliceOp>(op)) {
257       SetVector<Operation *> backwardSlice;
258       getBackwardSlice(insertSliceOp.source(), &backwardSlice,
259                        [](Operation *op) {
260                          return isa<LinalgOp, tensor::InsertSliceOp>(op);
261                        });
262       if (backwardSlice.empty() || backwardSlice.front() != sliceOp)
263         return false;
264     }
265   }
266 
267   // Check the block arguments, except for the innermost one, have one use.
268   SmallVector<BlockArgument> bbArgs = getTiedBBArgs(bbArg);
269   return !all_of(bbArgs, [&](BlockArgument bbArg) {
270     return bbArg.hasOneUse() || bbArg == bbArgs.back();
271   });
272 }
273 
274 LogicalResult TileLoopNest::tileRootOp(
275     OpBuilder &b, ArrayRef<int64_t> tileSizes,
276     ArrayRef<int64_t> tileInterchange,
277     Optional<LinalgLoopDistributionOptions> tileDistribution) {
278   // Exit if all tile sizes are zero.
279   if (tileSizes.size() == static_cast<size_t>(count(tileSizes, 0)))
280     return success();
281 
282   // Tile the root operation.
283   LinalgTilingOptions tilingOptions;
284   tilingOptions = tilingOptions
285                       .setInterchange(SmallVector<unsigned>(
286                           tileInterchange.begin(), tileInterchange.end()))
287                       .setTileSizes(tileSizes)
288                       .setLoopType(LinalgTilingLoopType::Loops);
289   if (tileDistribution)
290     tilingOptions =
291         tilingOptions.setDistributionOptions(tileDistribution.getValue());
292 
293   // TODO: Propagate RewriterBase everywhere.
294   IRRewriter rewriter(b);
295   FailureOr<TiledLinalgOp> tiledRootOp =
296       tileLinalgOp(rewriter, rootOp, tilingOptions);
297 
298   // Exit if tiling the root operation fails.
299   if (failed(tiledRootOp))
300     return failure();
301 
302   // Replace all uses of the root operation if it has been tiled before. All
303   // uses of the original untiled root operation are updated by the calling pass
304   // or pattern.
305   if (!isEmpty())
306     rootOp->replaceAllUsesWith(tiledRootOp->tensorResults);
307 
308   // Transfer the stored `rootOp` loop dimensions if it has been tiled before.
309   if (tiledRootAndFusedOpsLoops.count(rootOp) != 0) {
310     tiledRootAndFusedOpsLoops[tiledRootOp->op] =
311         tiledRootAndFusedOpsLoops[rootOp];
312   }
313 
314   // Update the root operation and append the loops and tile loop dimensions.
315   rootOp = tiledRootOp->op;
316   tileLoopOps.append(tiledRootOp->loops.begin(), tiledRootOp->loops.end());
317   for (const auto &en : enumerate(tileSizes)) {
318     // Copy only the tiled loop dimensions with non-zero tile size.
319     if (en.value() == 0)
320       continue;
321     tiledRootAndFusedOpsLoops[rootOp].push_back(tileInterchange[en.index()]);
322   }
323   assert(isValid() && "expect tile loop nest to be valid after tiling");
324   return success();
325 }
326 
327 FailureOr<LinalgOp> TileLoopNest::fuseProducer(OpBuilder &b,
328                                                OpOperand *consumerOpOperand) {
329   // Check if the consumer has been tiled before. For example, it may not have
330   // been tiled if the outermost tile loop is a reduction loop.
331   if (tiledRootAndFusedOpsLoops.count(consumerOpOperand->getOwner()) == 0)
332     return failure();
333 
334   assert(this->isValid() &&
335          "expect the tile loop nest to satisfy all invariants");
336 
337   // Check the tile loop nest is non-empty.
338   if (isEmpty())
339     return failure();
340 
341   // Check `consumerOpOperand` is defined by an ExtractSliceOp.
342   auto sliceOp =
343       consumerOpOperand->get().getDefiningOp<tensor::ExtractSliceOp>();
344   if (!sliceOp)
345     return failure();
346 
347   // Check `sliceOp` and `consumerOp` are in the same block.
348   LinalgOp consumerOp = consumerOpOperand->getOwner();
349   if (sliceOp->getBlock() != rootOp->getBlock() ||
350       consumerOp->getBlock() != rootOp->getBlock())
351     return failure();
352 
353   // Check `consumerOpOperand` is not shape-only to avoid fusion if the data is
354   // not used by the `consumerOp` computation.
355   BlockArgument bbArg = consumerOp.getTiedBlockArgument(consumerOpOperand);
356   if (bbArg.getUses().empty())
357     return failure();
358 
359   // Check if the producer is a LinalgOp possibly passed by iteration argument.
360   OpOperand *iterArg = nullptr;
361   auto producerResult = sliceOp.source().dyn_cast<OpResult>();
362   if (auto bbArg = sliceOp.source().dyn_cast<BlockArgument>()) {
363     iterArg = getTiedIterArg(bbArg);
364     // Check the iteration argument may be used to pass in the producer output.
365     if (!iterArg || hasOtherUses(bbArg, sliceOp))
366       return failure();
367     producerResult = iterArg->get().dyn_cast<OpResult>();
368   }
369   if (!producerResult || !isa<LinalgOp>(producerResult.getOwner()))
370     return failure();
371 
372   // Compute the tiled producer slice dimensions given the tiled consumer loops.
373   SmallVector<int64_t> tiledSliceDimIndices = getTiledSliceDims(
374       consumerOpOperand, tiledRootAndFusedOpsLoops[consumerOp]);
375   if (tiledSliceDimIndices.empty())
376     return failure();
377 
378   // Compute the tiled producer loop indices.
379   SmallVector<int64_t> tiledProducerLoopIndices =
380       getTiledProducerLoops(producerResult, tiledSliceDimIndices);
381 
382   // Tile the producer operands and clone the producer in place of `sliceOp`.
383   LinalgOp clonedOp =
384       getTiledProducer(b, producerResult, sliceOp, tiledSliceDimIndices,
385                        tiledProducerLoopIndices, iterArg);
386   tiledRootAndFusedOpsLoops[clonedOp] = tiledProducerLoopIndices;
387 
388   // Cast the `clonedOp` result to gap type mismatches before canonicalization.
389   Type consumerOperandType = consumerOpOperand->get().getType();
390   Value newResult = clonedOp->getResult(producerResult.getResultNumber());
391   if (newResult.getType() != consumerOperandType) {
392     OpBuilder::InsertionGuard guard(b);
393     b.setInsertionPointAfter(clonedOp);
394     newResult = b.create<tensor::CastOp>(producerResult.getLoc(),
395                                          consumerOperandType, newResult);
396   }
397 
398   // Replace the `sliceOp` uses except for the `clonedOp` output uses.
399   sliceOp.getResult().replaceAllUsesExcept(newResult, clonedOp);
400   return clonedOp;
401 }
402 
403 ValueRange TileLoopNest::getRootOpReplacementResults() {
404   assert(!isEmpty() && "expect tile loop nest to be non-empty");
405   return tileLoopOps.front()->getOpResults();
406 }
407 
408 SmallVector<LinalgOp> TileLoopNest::getAllTiledAndFusedOps() {
409   SmallVector<LinalgOp> result;
410   for (const auto &kvp : tiledRootAndFusedOpsLoops) {
411     auto linalgOp = dyn_cast<LinalgOp>(kvp.getFirst());
412     assert(linalgOp &&
413            "expect all tiled and fused operations are linalg operations");
414     result.push_back(linalgOp);
415   }
416   return result;
417 }
418 
419 //===----------------------------------------------------------------------===//
420 // Tile and fuse entry-points.
421 //===----------------------------------------------------------------------===//
422 
423 FailureOr<TileLoopNest> mlir::linalg::tileConsumerAndFuseProducers(
424     OpBuilder &b, LinalgOp consumerOp, ArrayRef<int64_t> tileSizes,
425     ArrayRef<int64_t> tileInterchange,
426     const Optional<LinalgLoopDistributionOptions> &tileDistribution) {
427   assert(tileSizes.size() == tileInterchange.size() &&
428          "expect the number of tile sizes and interchange dims to match");
429   assert(isPermutation(tileInterchange) &&
430          "expect tile interchange is a permutation");
431 
432   // Create an empty tile loop nest.
433   TileLoopNest tileLoopNest(consumerOp);
434 
435   // Search the number of outer parallel loops to separate them from possible
436   // inner reduction dimensions.
437   SmallVector<StringAttr> iterTypes =
438       llvm::to_vector<6>(consumerOp.iterator_types().getAsRange<StringAttr>());
439   applyPermutationToVector(iterTypes, tileInterchange);
440   auto *it = find_if(iterTypes, [&](StringAttr iterType) {
441     return !isParallelIterator(iterType);
442   });
443   int64_t split = std::distance(iterTypes.begin(), it);
444 
445   // Helper to fuse the producers greedily using a queue of fusion candidates.
446   auto fuseProducersGreedily = [&](ArrayRef<OpOperand *> operands) {
447     SmallVector<OpOperand *> candidates(operands.begin(), operands.end());
448     while (!candidates.empty()) {
449       FailureOr<LinalgOp> fusedProducer =
450           tileLoopNest.fuseProducer(b, candidates.pop_back_val());
451       if (failed(fusedProducer))
452         continue;
453       candidates.append(fusedProducer->getInputAndOutputOperands());
454     }
455   };
456 
457   // Tile the outer parallel loops and fuse the output operands.
458   SmallVector<int64_t> outerTileSizes;
459   outerTileSizes.append(tileSizes.begin(), tileSizes.begin() + split);
460   outerTileSizes.append(tileSizes.size() - split, 0);
461   if (failed(tileLoopNest.tileRootOp(b, outerTileSizes, tileInterchange,
462                                      tileDistribution)))
463     return failure();
464   fuseProducersGreedily(tileLoopNest.getRootOp().getOutputOperands());
465 
466   // Tile the remaining loops and fuse the input operands.
467   SmallVector<int64_t> innerTileSizes;
468   innerTileSizes.append(split, 0);
469   innerTileSizes.append(tileSizes.begin() + split, tileSizes.end());
470   if (failed(tileLoopNest.tileRootOp(b, innerTileSizes, tileInterchange,
471                                      tileDistribution)))
472     return failure();
473   fuseProducersGreedily(tileLoopNest.getRootOp().getInputOperands());
474 
475   // Exit if the tile loop nest is empty since all tile sizes are zero.
476   if (tileLoopNest.isEmpty())
477     return failure();
478 
479   return tileLoopNest;
480 }
481