1//===- Fusion.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 the linalg dialect Fusion pass.
10//
11//===----------------------------------------------------------------------===//
12
13#include "mlir/Dialect/Affine/IR/AffineOps.h"
14#include "mlir/Dialect/Arith/IR/Arith.h"
15#include "mlir/Dialect/Linalg/IR/Linalg.h"
16#include "mlir/Dialect/Linalg/Passes.h"
17#include "mlir/Dialect/Linalg/Transforms/Transforms.h"
18#include "mlir/Dialect/Linalg/Utils/Utils.h"
19#include "mlir/Dialect/MemRef/IR/MemRef.h"
20#include "mlir/Dialect/Tensor/IR/Tensor.h"
21#include "mlir/IR/AffineExpr.h"
22#include "mlir/IR/AffineMap.h"
23#include "mlir/IR/Dominance.h"
24#include "mlir/Support/LLVM.h"
25#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
26#include "mlir/Transforms/RegionUtils.h"
27#include "llvm/ADT/MapVector.h"
28#include "llvm/ADT/ScopeExit.h"
29#include "llvm/Support/CommandLine.h"
30#include "llvm/Support/Debug.h"
31
32#include <optional>
33#include <set>
34
35#define DEBUG_TYPE "linalg-fusion"
36
37using namespace mlir;
38using namespace mlir::linalg;
39
40/// Implements a simple high-level fusion pass on linalg structured operations.
41///
42/// In each block, linalg ops are processed in reverse textual order.
43/// Given a linalg op `O`, fusion occurs by:
44/// 1. inspecting the linalg ops that write into the views read by `O`. There
45/// are 2 cases:
46/// a) buffer case: use the SSA value of the views and a simple alias
47/// analysis on subview ops to determine producer-consumer dependences;
48/// b) tensor case: use SSA use-def chains on extract_slice ops;
49/// 2. greedily fuse the linalg ops that produce the subview/extract_slice.
50/// 3. inspect the fused ops and determine whether they have other remaining
51/// LinalgOp uses. If not, then erase the original producing linalg op.
52///
53/// More advanced use cases, analyses as well as profitability heuristics are
54/// left for future work.
55
56struct ShapeDimension {
57 Value shape;
58 unsigned dimension;
59};
60
61// Given an `op`, returns the first (`shape`, `dimension`) pair that identifies
62// the loop range at `loopDepth`. The semantics of the loopToOperandRangesMaps
63// guarantees at least one such dimension is found. If multiple candidates exist
64// they must agree by construction (i.e. have the same size) and we just return
65// the first one.
66static ShapeDimension
67getShapeDefiningLoopRange(LinalgOp op, unsigned loopDepth,
68 bool fromSubViewOpOnly = false) {
69 // Iterate over the inputs and outputs in order.
70 // Extract the subranges from the linearized ranges.
71 for (OpOperand &opOperand : op->getOpOperands()) {
72 // The method `getRangeFromOperandShape` requires using SubViewOp or
73 // ExtractSliceOps. If the value isn't defined from there continue.
74 // todo: The method should be adapted to get the values from
75 // `ViewInterface`. The interface needs a `getOrCreateRanges` method which
76 // currently returns a `linalg.range`. The fix here is to move this op to
77 // `std` dialect and add the method to `ViewInterface`.
78 if (fromSubViewOpOnly &&
79 !isa_and_nonnull<memref::SubViewOp, tensor::ExtractSliceOp>(
80 opOperand.get().getDefiningOp()))
81 continue;
82
83 AffineMap map = op.getMatchingIndexingMap(&opOperand);
84 LLVM_DEBUG(llvm::dbgs() << "getShapeDefiningLoopRange I/O idx: "
85 << opOperand.getOperandNumber() << "\n");
86 LLVM_DEBUG(llvm::dbgs()
87 << "getShapeDefiningLoopRange map: " << map << "\n");
88 SmallVector<Value, 8> shapeRanges(map.getNumResults(), nullptr);
89 for (const auto &en : llvm::enumerate(map.getResults())) {
90 auto dimExpr = dyn_cast<AffineDimExpr>(en.value());
91 if (!dimExpr)
92 continue;
93 if (loopDepth == cast<AffineDimExpr>(en.value()).getPosition()) {
94 LLVM_DEBUG(llvm::dbgs() << "getShapeDefiningLoopRange loopDepth: "
95 << loopDepth << "\n");
96 LLVM_DEBUG(llvm::dbgs() << "getShapeDefiningLoopRange shape: "
97 << opOperand.get() << "\n");
98 return ShapeDimension{opOperand.get(),
99 static_cast<unsigned>(en.index())};
100 }
101 }
102 }
103 llvm_unreachable("Expect to be able to extract a shape defining loop range");
104}
105
106static SmallVector<Value> getTiledOperands(LinalgOp producer) {
107 return producer->getOperands();
108}
109
110/// Fuses the producer by cloning the `producer`. The `fusedLoopsAndRanges`
111/// provides the loop range information for the fused loops. The rest are
112/// obtained from the producer itself, since they are not tiled + fused.
113static LinalgOp fuse(OpBuilder &b, LinalgOp producer,
114 const DenseMap<unsigned, Range> &fusedLoopsAndRanges) {
115 SmallVector<OpFoldResult> ivs, tileSizes, sizeBounds;
116 SmallVector<Range> loopRanges;
117 Location loc = producer.getLoc();
118
119 for (unsigned i = 0, e = producer.getNumLoops(); i < e; ++i) {
120 auto shapeDim = getShapeDefiningLoopRange(producer, i);
121 OpFoldResult dim =
122 createFoldedDimOp(b, loc, shapeDim.shape, shapeDim.dimension);
123 sizeBounds.push_back(Elt: dim);
124 auto it = fusedLoopsAndRanges.find(Val: i);
125 if (it != fusedLoopsAndRanges.end()) {
126 ivs.push_back(Elt: it->second.offset);
127 tileSizes.push_back(Elt: it->second.size);
128 loopRanges.push_back(Elt: it->second);
129 LLVM_DEBUG(llvm::dbgs() << "tiled loop#" << i << " with LoopRange "
130 << loopRanges.back() << "\n");
131 } else {
132 tileSizes.push_back(b.getIndexAttr(0));
133 loopRanges.push_back(Elt: Range{b.getIndexAttr(0), .size: dim, b.getIndexAttr(1)});
134 LLVM_DEBUG(llvm::dbgs() << "full loop#" << i << " with LoopRange "
135 << loopRanges.back() << "\n");
136 }
137 }
138
139 SmallVector<Value, 8> clonedShapes;
140 clonedShapes.reserve(N: producer->getNumOperands());
141
142 // Compute subranges for all tensor input/output operands.
143 clonedShapes.append(makeTiledShapes(
144 b, loc, producer, getTiledOperands(producer), ivs, tileSizes, sizeBounds,
145 /**omitPartialTileCheck=*/false));
146
147 // Take result types from the tiled init operands.
148 MutableOperandRange producerDpsInits = producer.getDpsInitsMutable();
149 SmallVector<Type, 4> resultTypes;
150 resultTypes.reserve(N: producer->getNumResults());
151 int64_t firstInitOperandIdx =
152 producerDpsInits.getAsOperandRange().getBeginOperandIndex();
153 for (int64_t i = 0, e = producer->getNumResults(); i < e; ++i) {
154 resultTypes.push_back(Elt: clonedShapes[firstInitOperandIdx + i].getType());
155 }
156
157 // Clone the producer with new operands and result types.
158 LinalgOp clonedOp = clone(b, producer, resultTypes, clonedShapes);
159
160 // Shift all IndexOp results by the tile offset.
161 SmallVector<OpFoldResult> allIvs = llvm::to_vector(
162 Range: llvm::map_range(C&: loopRanges, F: [&](Range range) { return range.offset; }));
163 offsetIndices(b, clonedOp, allIvs);
164
165 return clonedOp;
166}
167
168/// Get the loop range for a dimension `dim` based on the `shapedOperand`. It is
169/// expected to be defined by a subview op or an extract_slice op.
170static Range getRangeFromOperandShape(OpBuilder &b, Location loc,
171 Value shapedOperand, unsigned dim) {
172 Operation *shapeProducingOp = shapedOperand.getDefiningOp();
173 if (auto subViewOp = dyn_cast<memref::SubViewOp>(shapeProducingOp))
174 return subViewOp.getOrCreateRanges(b, loc)[dim];
175 if (auto sliceOp = dyn_cast<tensor::ExtractSliceOp>(shapeProducingOp))
176 return sliceOp.getOrCreateRanges(b, loc)[dim];
177 llvm_unreachable("SubviewOp or ExtractSliceOp expected");
178}
179
180/// Fuses the producer into the loop immediately enclosing the consumer.
181/// This is achieved by "recomputing" the producer at the time it
182/// is needed just before the consumer.
183static LinalgOp fuse(OpBuilder &b, LinalgOp producerOp, AffineMap producerMap,
184 OpOperand &consumerOpOperand) {
185 LLVM_DEBUG(llvm::dbgs() << "Producer map: " << producerMap << "\n");
186 DenseMap<unsigned, Range> fusedLoopsAndRanges;
187 Value shapedOperand = consumerOpOperand.get();
188 for (const auto &en : llvm::enumerate(First: producerMap.getResults())) {
189 unsigned posInProducerLoop = cast<AffineDimExpr>(Val: en.value()).getPosition();
190 fusedLoopsAndRanges[posInProducerLoop] = getRangeFromOperandShape(
191 b, loc: consumerOpOperand.getOwner()->getLoc(), shapedOperand, dim: en.index());
192 }
193 return fuse(b, producerOp, fusedLoopsAndRanges);
194}
195
196/// Walk back use-def chain through scf::For yields.
197/// Sets `producer` and `outputIndex` if it finds a producer LinalgOp
198
199// TODO(ravishankarm, ntv): This can be moved into the dependence graphs
200// dependence tracking since the dependence tracking is similar to what is done
201// w.r.t to buffers.
202static void getProducerOfTensor(Value tensor, OpResult &opResult) {
203 if (!isa<RankedTensorType>(Val: tensor.getType()))
204 return;
205
206 while (true) {
207 LLVM_DEBUG(llvm::dbgs() << "\ngetProducerOfTensor: " << tensor);
208 if (auto linalgOp = tensor.getDefiningOp<LinalgOp>()) {
209 opResult = cast<OpResult>(Val&: tensor);
210 return;
211 }
212 if (auto sliceOp = tensor.getDefiningOp<tensor::ExtractSliceOp>()) {
213 tensor = sliceOp.getSource();
214 continue;
215 }
216 if (auto blockArg = dyn_cast<BlockArgument>(Val&: tensor)) {
217 if (auto forOp = blockArg.getDefiningOp<scf::ForOp>()) {
218 tensor = forOp.getInitArgs()[blockArg.getArgNumber()];
219 continue;
220 }
221 }
222 return;
223 }
224}
225
226FailureOr<FusionInfo>
227mlir::linalg::fuseProducerOfTensor(OpBuilder &b, OpOperand &consumerOpOperand) {
228 Value inputTensor = consumerOpOperand.get();
229 OpResult producerOpResult;
230 getProducerOfTensor(tensor: inputTensor, opResult&: producerOpResult);
231 if (!producerOpResult) {
232 LLVM_DEBUG(llvm::dbgs() << "\nUnable to find producer");
233 return failure();
234 }
235 return fuseProducerOfTensor(b, producerOpResult, consumerOpOperand);
236}
237
238FailureOr<FusionInfo>
239mlir::linalg::fuseProducerOfTensor(OpBuilder &b, OpResult producerOpResult,
240 OpOperand &consumerOpOperand) {
241 auto producerOp = dyn_cast<LinalgOp>(producerOpResult.getOwner());
242 if (!producerOp)
243 return failure();
244
245 LinalgOp consumerOp = dyn_cast<LinalgOp>(consumerOpOperand.getOwner());
246 if (!consumerOp)
247 return failure();
248
249 Value inputTensor = consumerOpOperand.get();
250
251 // Must be an extract_slice op to guarantee there are loops we can fuse into.
252 auto sliceOp = inputTensor.getDefiningOp<tensor::ExtractSliceOp>();
253 if (!sliceOp) {
254 LLVM_DEBUG(llvm::dbgs()
255 << "\nNot fusable, not an extract_slice op: " << inputTensor);
256 return failure();
257 }
258
259 // If producer is already in the same block as consumer, we are done.
260 if (consumerOpOperand.get().getParentBlock() ==
261 producerOpResult.getParentBlock())
262 return failure();
263
264 // Insert fused `producer` just before `consumer`.
265 OpBuilder::InsertionGuard g(b);
266 b.setInsertionPoint(consumerOp);
267 LLVM_DEBUG(llvm::dbgs() << "Fuse into consumer: " << *consumerOp << "\n");
268 OpOperand *opOperand =
269 producerOp.getDpsInitOperand(producerOpResult.getResultNumber());
270 LinalgOp fusedProducer =
271 fuse(b, producerOp, producerOp.getMatchingIndexingMap(opOperand),
272 consumerOpOperand);
273
274 // Replace use.
275 // Canonicalizations are not guaranteed to have happened before constructing
276 // `fusedProducer`. In the tensor case this can result in temporary type
277 // mismatches. Insert a `tensor.cast` op to propagate the transformation
278 // invariant that types are compatible.
279 Value def = fusedProducer->getResult(producerOpResult.getResultNumber());
280 Type consumerType = consumerOpOperand.get().getType();
281 if (consumerType != def.getType())
282 def = b.create<tensor::CastOp>(fusedProducer.getLoc(), consumerType, def);
283 consumerOpOperand.set(def);
284 return FusionInfo{cast<LinalgOp>(producerOpResult.getOwner()), fusedProducer};
285}
286

source code of mlir/lib/Dialect/Linalg/Transforms/Fusion.cpp