1 //===- LinalgTransforms.cpp - Linalg transformations as patterns ----------===//
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 logic and helpers to expose Linalg transforms as rewrite
10 // patterns.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "mlir/Dialect/Linalg/Transforms/Transforms.h"
15 #include "mlir/Dialect/Affine/Utils.h"
16 #include "mlir/Dialect/Linalg/Analysis/DependenceAnalysis.h"
17 #include "mlir/Dialect/Linalg/IR/LinalgOps.h"
18 #include "mlir/Dialect/Linalg/Utils/Utils.h"
19 #include "mlir/Dialect/StandardOps/EDSC/Intrinsics.h"
20 #include "mlir/Dialect/Utils/StructuredOpsUtils.h"
21 #include "mlir/Dialect/Vector/EDSC/Intrinsics.h"
22 #include "mlir/Dialect/Vector/VectorOps.h"
23 #include "mlir/IR/AffineExpr.h"
24 #include "mlir/IR/Matchers.h"
25 #include "mlir/Pass/Pass.h"
26 #include "mlir/Support/LLVM.h"
27 #include "mlir/Transforms/GreedyPatternRewriteDriver.h"
28 #include "llvm/Support/Debug.h"
29 #include "llvm/Support/raw_ostream.h"
30 #include <type_traits>
31 
32 #define DEBUG_TYPE "linalg-transforms"
33 
34 using namespace mlir;
35 using namespace mlir::edsc;
36 using namespace mlir::edsc::intrinsics;
37 using namespace mlir::linalg;
38 
39 #define DBGS() (llvm::dbgs() << "[" DEBUG_TYPE << "]: ")
40 
41 //===----------------------------------------------------------------------===//
42 // Transformations exposed as rewrite patterns.
43 //===----------------------------------------------------------------------===//
44 // Marker used as attribute name in generated Linalg rewriting transformations.
45 const StringLiteral mlir::linalg::LinalgTransforms::kLinalgTransformMarker =
46     "__internal_linalg_transform__";
47 
48 mlir::linalg::LinalgMarker::LinalgMarker(ArrayRef<Identifier> matchDisjunction,
49                                          Optional<Identifier> replacement)
50     : matchDisjunction(matchDisjunction.begin(), matchDisjunction.end()),
51       replacement(replacement) {}
52 
53 LogicalResult
54 mlir::linalg::LinalgMarker::checkAndNotify(PatternRewriter &rewriter,
55                                            Operation *op) const {
56   auto attr = op->template getAttrOfType<StringAttr>(
57       LinalgTransforms::kLinalgTransformMarker);
58 
59   if (!attr) {
60     // 1. Has no marker case and matchDisjunction is empty.
61     if (matchDisjunction.empty())
62       return success();
63 
64     // 2. Has no marker but was expecting a marker.
65     return rewriter.notifyMatchFailure(op, [&](Diagnostic &diag) {
66       diag << " does not have any marker from list: ";
67       interleaveComma(matchDisjunction, diag);
68     });
69   }
70 
71   // 4. Match explicit marker.
72   for (auto marker : matchDisjunction)
73     if (attr.getValue() == marker)
74       return success();
75 
76   // 5. Fail to match.
77   return rewriter.notifyMatchFailure(op, [&](Diagnostic &diag) {
78     diag << " does not have any marker from list: ";
79     interleaveComma(matchDisjunction, diag);
80   });
81 }
82 
83 void mlir::linalg::LinalgMarker::replaceLinalgMarker(PatternRewriter &rewriter,
84                                                      Operation *op) const {
85   if (replacement.hasValue())
86     op->setAttr(LinalgTransforms::kLinalgTransformMarker,
87                 rewriter.getStringAttr(replacement.getValue()));
88   else
89     op->removeAttr(Identifier::get(LinalgTransforms::kLinalgTransformMarker,
90                                    rewriter.getContext()));
91 }
92 
93 LinalgTilingOptions &
94 mlir::linalg::LinalgTilingOptions::setTileSizes(ArrayRef<int64_t> ts) {
95   SmallVector<int64_t, 4> tileSizes(ts.begin(), ts.end());
96   tileSizeComputationFunction = [tileSizes](OpBuilder &b, Operation *op) {
97     OpBuilder::InsertionGuard guard(b);
98     b.setInsertionPointToStart(
99         &op->getParentOfType<FuncOp>().getBody().front());
100     return llvm::to_vector<4>(map_range(tileSizes, [&](int64_t s) {
101       Value v = b.create<ConstantIndexOp>(op->getLoc(), s);
102       return v;
103     }));
104   };
105   return *this;
106 }
107 
108 /// Linalg base tiling pattern.
109 mlir::linalg::LinalgBaseTilingPattern::LinalgBaseTilingPattern(
110     StringRef opName, MLIRContext *context, LinalgTilingOptions options,
111     LinalgMarker marker, PatternBenefit benefit)
112     : RewritePattern(opName, {}, benefit, context), marker(marker),
113       options(options) {}
114 
115 mlir::linalg::LinalgBaseTilingPattern::LinalgBaseTilingPattern(
116     LinalgTilingOptions options, LinalgMarker marker, PatternBenefit benefit)
117     : RewritePattern(benefit, MatchAnyOpTypeTag()), marker(marker),
118       options(options) {}
119 
120 LogicalResult mlir::linalg::LinalgBaseTilingPattern::matchAndRewriteBase(
121     Operation *op, PatternRewriter &rewriter, TiledLinalgOp &result) const {
122   LinalgOp linalgOp = dyn_cast<LinalgOp>(op);
123   if (!linalgOp)
124     return failure();
125   if (failed(marker.checkAndNotify(rewriter, linalgOp)))
126     return failure();
127 
128   Optional<TiledLinalgOp> res = tileLinalgOp(rewriter, linalgOp, options);
129 
130   if (!res)
131     return failure();
132 
133   // Return relevant information to derived pattern.
134   result = *res;
135 
136   // New marker if specified.
137   marker.replaceLinalgMarker(rewriter, res->op.getOperation());
138   return success();
139 }
140 
141 mlir::linalg::LinalgBaseTileAndFusePattern::LinalgBaseTileAndFusePattern(
142     StringRef opName, MLIRContext *context,
143     const LinalgDependenceGraph &dependenceGraph,
144     LinalgTilingOptions tilingOptions, LinalgFusionOptions fusionOptions,
145     LinalgMarker marker, LinalgMarker fusedOpMarker,
146     LinalgMarker originalOpMarker, PatternBenefit benefit)
147     : RewritePattern(opName, {}, benefit, context),
148       dependenceGraph(dependenceGraph), tilingOptions(tilingOptions),
149       fusionOptions(fusionOptions), marker(marker),
150       fusedOpMarker(fusedOpMarker), originalOpMarker(originalOpMarker) {}
151 
152 LogicalResult mlir::linalg::LinalgBaseTileAndFusePattern::matchAndRewrite(
153     Operation *op, PatternRewriter &rewriter) const {
154   LinalgOp linalgOp = dyn_cast<LinalgOp>(op);
155   if (!linalgOp)
156     return failure();
157   if (failed(marker.checkAndNotify(rewriter, linalgOp)))
158     return failure();
159   if (!linalgOp.hasBufferSemantics())
160     return failure();
161 
162   DenseSet<Operation *> producers;
163   producers.insert(linalgOp);
164   for (auto dependence : dependenceGraph.getDependentOperations(linalgOp)) {
165     if (!fusionOptions.indicesToFuse.count(
166             dependence.indexingOpView->getOperandNumber()))
167       continue;
168     if (isa<LinalgOp>(dependence.dependentOpView->getOwner()))
169       producers.insert(dependence.dependentOpView->getOwner());
170   }
171 
172   SmallVector<LinalgOp, 1> fusionOps;
173   for (auto it = op->getBlock()->begin(), ie = Block::iterator(op); it != ie;
174        ++it) {
175     auto producerLinalgOp = dyn_cast<LinalgOp>(&(*it));
176     if (producerLinalgOp && producers.count(producerLinalgOp))
177       fusionOps.push_back(producerLinalgOp);
178   }
179   fusionOps.push_back(linalgOp);
180 
181   SmallVector<Value, 4> tileSizes =
182       tilingOptions.tileSizeComputationFunction(rewriter, op);
183   LinalgTilingOptions instanceTilingOptions = tilingOptions;
184   instanceTilingOptions.setTileSizes(tileSizes);
185   Optional<TiledAndFusedLinalgOps> tiledAndFusedOps = tileAndFuseLinalgOps(
186       rewriter, fusionOps, dependenceGraph, instanceTilingOptions);
187   if (!tiledAndFusedOps)
188     return failure();
189 
190   // Tile the unfused loops;
191   SmallVector<Value, 4> unfusedLoopTileSizes;
192   Value zero = rewriter.create<ConstantIndexOp>(op->getLoc(), 0);
193   for (auto tileSize : enumerate(tileSizes)) {
194     if (tiledAndFusedOps->fusedLoopDims.count(tileSize.index()))
195       unfusedLoopTileSizes.push_back(zero);
196     else
197       unfusedLoopTileSizes.push_back(tileSize.value());
198   }
199   // Tile the loop only if there is a non-zero tile size.
200   if (unfusedLoopTileSizes.size() > linalgOp.getNumLoops())
201     unfusedLoopTileSizes.resize(linalgOp.getNumLoops());
202   if (llvm::any_of(unfusedLoopTileSizes, [](Value val) {
203         if (auto cst = val.getDefiningOp<ConstantIndexOp>())
204           return cst.getValue() != 0;
205         return true;
206       })) {
207     LinalgTilingOptions unfusedTilingOptions = tilingOptions;
208     unfusedTilingOptions.setTileSizes(unfusedLoopTileSizes);
209     Optional<TiledLinalgOp> unfusedTiledOp =
210         tileLinalgOp(rewriter, tiledAndFusedOps->op, unfusedTilingOptions);
211     if (!unfusedTiledOp)
212       return failure();
213     rewriter.eraseOp(tiledAndFusedOps->op);
214     tiledAndFusedOps->op = unfusedTiledOp->op;
215   }
216 
217   marker.replaceLinalgMarker(rewriter, tiledAndFusedOps->op.getOperation());
218   for (auto fusedOp : tiledAndFusedOps->fusedProducers) {
219     fusedOpMarker.replaceLinalgMarker(rewriter, fusedOp.getOperation());
220   }
221   for (auto origProducerOp : ArrayRef<LinalgOp>(fusionOps).drop_back()) {
222     originalOpMarker.replaceLinalgMarker(rewriter,
223                                          origProducerOp.getOperation());
224   }
225   rewriter.updateRootInPlace(
226       op, [&]() { originalOpMarker.replaceLinalgMarker(rewriter, op); });
227   return success();
228 }
229 
230 /// Linalg base interchange pattern.
231 mlir::linalg::LinalgBaseInterchangePattern::LinalgBaseInterchangePattern(
232     StringRef opName, MLIRContext *context,
233     ArrayRef<unsigned> interchangeVector, LinalgMarker marker,
234     PatternBenefit benefit)
235     : RewritePattern(opName, {}, benefit, context), marker(marker),
236       interchangeVector(interchangeVector.begin(), interchangeVector.end()) {}
237 
238 LogicalResult mlir::linalg::LinalgBaseInterchangePattern::matchAndRewrite(
239     Operation *op, PatternRewriter &rewriter) const {
240   LinalgOp linalgOp = dyn_cast<LinalgOp>(op);
241   if (!linalgOp)
242     return failure();
243   if (failed(marker.checkAndNotify(rewriter, linalgOp)))
244     return failure();
245   if (failed(interchangeGenericLinalgOpPrecondition(op, interchangeVector)))
246     return failure();
247 
248   // TODO: figure out how this interplays with named ops. In particular this
249   // should break the named op property.
250   rewriter.updateRootInPlace(op, [&]() {
251     interchange(linalgOp, interchangeVector);
252     // New marker if specified.
253     marker.replaceLinalgMarker(rewriter, op);
254   });
255   return success();
256 }
257 
258 mlir::linalg::LinalgBasePromotionPattern::LinalgBasePromotionPattern(
259     StringRef opName, MLIRContext *context, LinalgPromotionOptions options,
260     LinalgMarker marker, PatternBenefit benefit)
261     : RewritePattern(opName, {}, benefit, context), marker(marker),
262       options(options) {}
263 
264 LogicalResult mlir::linalg::LinalgBasePromotionPattern::matchAndRewrite(
265     Operation *op, PatternRewriter &rewriter) const {
266   if (failed(marker.checkAndNotify(rewriter, op)))
267     return failure();
268   if (failed(promoteSubviewsPrecondition(op, options)))
269     return failure();
270 
271   // TODO: We cannot use root update here. This pattern is creating other ops,
272   // so if the promotion fails, those need to be cleaned up, which doesnt seem
273   // to be happening here. So to fail properly, we should be cloning the op and
274   // deleting the previous op. This needs more investigation.
275   rewriter.startRootUpdate(op);
276   Optional<LinalgOp> promotedOp = promoteSubViews(rewriter, op, options);
277   if (!promotedOp) {
278     rewriter.cancelRootUpdate(op);
279     return op->emitError("subview promotion failed");
280   }
281   rewriter.finalizeRootUpdate(op);
282   marker.replaceLinalgMarker(rewriter, op);
283   return success();
284 }
285 
286 mlir::linalg::LinalgBaseVectorizationPattern::LinalgBaseVectorizationPattern(
287     StringRef opName, MLIRContext *context, LinalgMarker marker,
288     PatternBenefit benefit)
289     : RewritePattern(opName, {}, benefit, context), marker(marker) {}
290 
291 LogicalResult mlir::linalg::LinalgBaseVectorizationPattern::matchAndRewrite(
292     Operation *op, PatternRewriter &rewriter) const {
293   LinalgOp linalgOp = dyn_cast<LinalgOp>(op);
294   if (!linalgOp)
295     return failure();
296   if (failed(marker.checkAndNotify(rewriter, linalgOp)))
297     return failure();
298   if (failed(vectorizeLinalgOpPrecondition(op)))
299     return failure();
300   vectorizeLinalgOp(rewriter, op);
301   rewriter.eraseOp(op);
302   return success();
303 }
304 
305 LogicalResult mlir::linalg::applyStagedPatterns(
306     Operation *op, ArrayRef<FrozenRewritePatternList> stage1Patterns,
307     const FrozenRewritePatternList &stage2Patterns,
308     function_ref<LogicalResult(Operation *)> stage3Lambda) {
309   unsigned iteration = 0;
310   (void)iteration;
311   for (const auto &patterns : stage1Patterns) {
312     LLVM_DEBUG(DBGS() << "Before 1st stage, iter: " << ++iteration << "\n"
313                       << *op);
314     if (failed(applyPatternsAndFoldGreedily(op, patterns))) {
315       LLVM_DEBUG(DBGS() << "Underlying first stage rewrite did not converge");
316       return failure();
317     }
318     LLVM_DEBUG(DBGS() << "After 1st stage, iter: " << ++iteration << "\n"
319                       << *op);
320     if (failed(applyPatternsAndFoldGreedily(op, stage2Patterns))) {
321       LLVM_DEBUG(DBGS() << "Underlying 2nd stage rewrite did not converge");
322       return failure();
323     }
324     LLVM_DEBUG(DBGS() << "After 2nd stage, iter : " << iteration << "\n"
325                       << *op);
326     if (stage3Lambda) {
327       if (failed(stage3Lambda(op)))
328         return failure();
329       LLVM_DEBUG(DBGS() << "After 3rd stage, iter : " << iteration << "\n"
330                         << *op);
331     }
332   }
333   return success();
334 }
335 
336 /// Given the `lbVal`, `ubVal` and `stepVal` of a loop, append `lbVal` and
337 /// `ubVal` to `dims` and `stepVal` to `symbols`.
338 /// Create new AffineDimExpr (`%lb` and `%ub`) and AffineSymbolExpr (`%step`)
339 /// with positions matching the newly appended values. Substitute occurrences of
340 /// `dimExpr` by either the min expression (i.e. `%lb`) or the max expression
341 /// (i.e. `%lb + %step * floordiv(%ub -1 - %lb, %step)`), depending on whether
342 /// the induction variable is used with a positive or negative  coefficient.
343 static AffineExpr substituteLoopInExpr(AffineExpr expr, AffineExpr dimExpr,
344                                        Value lbVal, Value ubVal, Value stepVal,
345                                        SmallVectorImpl<Value> &dims,
346                                        SmallVectorImpl<Value> &symbols) {
347   MLIRContext *ctx = lbVal.getContext();
348   AffineExpr lb = getAffineDimExpr(dims.size(), ctx);
349   dims.push_back(lbVal);
350   AffineExpr ub = getAffineDimExpr(dims.size(), ctx);
351   dims.push_back(ubVal);
352   AffineExpr step = getAffineSymbolExpr(symbols.size(), ctx);
353   symbols.push_back(stepVal);
354   LLVM_DEBUG(DBGS() << "Before: " << expr << "\n");
355   AffineExpr ee = substWithMin(expr, dimExpr, lb,
356                                lb + step * ((ub - 1) - lb).floorDiv(step));
357   LLVM_DEBUG(DBGS() << "After: " << expr << "\n");
358   return ee;
359 }
360 
361 /// Traverse the `dims` and substitute known min or max expressions in place of
362 /// induction variables in `exprs`.
363 static AffineMap substitute(AffineMap map, SmallVectorImpl<Value> &dims,
364                             SmallVectorImpl<Value> &symbols) {
365   auto exprs = llvm::to_vector<4>(map.getResults());
366   for (AffineExpr &expr : exprs) {
367     bool substituted = true;
368     while (substituted) {
369       substituted = false;
370       for (unsigned dimIdx = 0; dimIdx < dims.size(); ++dimIdx) {
371         Value dim = dims[dimIdx];
372         AffineExpr dimExpr = getAffineDimExpr(dimIdx, expr.getContext());
373         LLVM_DEBUG(DBGS() << "Subst: " << dim << " @ " << dimExpr << "\n");
374         AffineExpr substitutedExpr;
375         if (auto forOp = scf::getForInductionVarOwner(dim))
376           substitutedExpr = substituteLoopInExpr(
377               expr, dimExpr, forOp.lowerBound(), forOp.upperBound(),
378               forOp.step(), dims, symbols);
379 
380         if (auto parallelForOp = scf::getParallelForInductionVarOwner(dim))
381           for (unsigned idx = 0, e = parallelForOp.getNumLoops(); idx < e;
382                ++idx)
383             substitutedExpr = substituteLoopInExpr(
384                 expr, dimExpr, parallelForOp.lowerBound()[idx],
385                 parallelForOp.upperBound()[idx], parallelForOp.step()[idx],
386                 dims, symbols);
387 
388         if (!substitutedExpr)
389           continue;
390 
391         substituted = (substitutedExpr != expr);
392         expr = substitutedExpr;
393       }
394     }
395 
396     // Cleanup and simplify the results.
397     // This needs to happen outside of the loop iterating on dims.size() since
398     // it modifies dims.
399     SmallVector<Value, 4> operands(dims.begin(), dims.end());
400     operands.append(symbols.begin(), symbols.end());
401     auto map = AffineMap::get(dims.size(), symbols.size(), exprs,
402                               exprs.front().getContext());
403 
404     LLVM_DEBUG(DBGS() << "Map to simplify: " << map << "\n");
405 
406     // Pull in affine.apply operations and compose them fully into the
407     // result.
408     fullyComposeAffineMapAndOperands(&map, &operands);
409     canonicalizeMapAndOperands(&map, &operands);
410     map = simplifyAffineMap(map);
411     // Assign the results.
412     exprs.assign(map.getResults().begin(), map.getResults().end());
413     dims.assign(operands.begin(), operands.begin() + map.getNumDims());
414     symbols.assign(operands.begin() + map.getNumDims(), operands.end());
415 
416     LLVM_DEBUG(DBGS() << "Map simplified: " << map << "\n");
417   }
418 
419   assert(!exprs.empty() && "Unexpected empty exprs");
420   return AffineMap::get(dims.size(), symbols.size(), exprs, map.getContext());
421 }
422 
423 LogicalResult AffineMinSCFCanonicalizationPattern::matchAndRewrite(
424     AffineMinOp minOp, PatternRewriter &rewriter) const {
425   LLVM_DEBUG(DBGS() << "Canonicalize AffineMinSCF: " << *minOp.getOperation()
426                     << "\n");
427 
428   SmallVector<Value, 4> dims(minOp.getDimOperands()),
429       symbols(minOp.getSymbolOperands());
430   AffineMap map = substitute(minOp.getAffineMap(), dims, symbols);
431 
432   LLVM_DEBUG(DBGS() << "Resulting map: " << map << "\n");
433 
434   // Check whether any of the expressions, when subtracted from all other
435   // expressions, produces only >= 0 constants. If so, it is the min.
436   for (auto e : minOp.getAffineMap().getResults()) {
437     LLVM_DEBUG(DBGS() << "Candidate min: " << e << "\n");
438     if (!e.isSymbolicOrConstant())
439       continue;
440 
441     auto isNonPositive = [](AffineExpr e) {
442       if (auto cst = e.dyn_cast<AffineConstantExpr>())
443         return cst.getValue() < 0;
444       return true;
445     };
446 
447     // Build the subMap and check everything is statically known to be
448     // positive.
449     SmallVector<AffineExpr, 4> subExprs;
450     subExprs.reserve(map.getNumResults());
451     for (auto ee : map.getResults())
452       subExprs.push_back(ee - e);
453     MLIRContext *ctx = minOp.getContext();
454     AffineMap subMap = simplifyAffineMap(
455         AffineMap::get(map.getNumDims(), map.getNumSymbols(), subExprs, ctx));
456     LLVM_DEBUG(DBGS() << "simplified subMap: " << subMap << "\n");
457     if (llvm::any_of(subMap.getResults(), isNonPositive))
458       continue;
459 
460     // Static min found.
461     if (auto cst = e.dyn_cast<AffineConstantExpr>()) {
462       rewriter.replaceOpWithNewOp<ConstantIndexOp>(minOp, cst.getValue());
463     } else {
464       auto resultMap = AffineMap::get(0, map.getNumSymbols(), {e}, ctx);
465       SmallVector<Value, 4> resultOperands = dims;
466       resultOperands.append(symbols.begin(), symbols.end());
467       canonicalizeMapAndOperands(&resultMap, &resultOperands);
468       resultMap = simplifyAffineMap(resultMap);
469       rewriter.replaceOpWithNewOp<AffineApplyOp>(minOp, resultMap,
470                                                  resultOperands);
471     }
472     return success();
473   }
474 
475   return failure();
476 }
477