1 //===- Interchange.cpp - Linalg interchange transformation ----------------===//
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 interchange transformation.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "mlir/Dialect/Linalg/Analysis/DependenceAnalysis.h"
14 #include "mlir/Dialect/Linalg/IR/Linalg.h"
15 #include "mlir/Dialect/Linalg/Transforms/Transforms.h"
16 #include "mlir/Dialect/Linalg/Utils/Utils.h"
17 #include "mlir/Dialect/Utils/StructuredOpsUtils.h"
18 #include "mlir/Dialect/Vector/VectorOps.h"
19 #include "mlir/IR/AffineExpr.h"
20 #include "mlir/IR/Matchers.h"
21 #include "mlir/IR/PatternMatch.h"
22 #include "mlir/Pass/Pass.h"
23 #include "mlir/Support/LLVM.h"
24 #include "llvm/ADT/ScopeExit.h"
25 #include "llvm/Support/Debug.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include <type_traits>
28 
29 #define DEBUG_TYPE "linalg-interchange"
30 
31 using namespace mlir;
32 using namespace mlir::linalg;
33 
34 static LogicalResult
35 interchangeGenericOpPrecondition(GenericOp genericOp,
36                                  ArrayRef<unsigned> interchangeVector) {
37   // Interchange vector must be non-empty and match the number of loops.
38   if (interchangeVector.empty() ||
39       genericOp.getNumLoops() != interchangeVector.size())
40     return failure();
41   // Permutation map must be invertible.
42   if (!inversePermutation(AffineMap::getPermutationMap(interchangeVector,
43                                                        genericOp.getContext())))
44     return failure();
45   return success();
46 }
47 
48 FailureOr<GenericOp>
49 mlir::linalg::interchangeGenericOp(RewriterBase &rewriter, GenericOp genericOp,
50                                    ArrayRef<unsigned> interchangeVector) {
51   if (failed(interchangeGenericOpPrecondition(genericOp, interchangeVector)))
52     return rewriter.notifyMatchFailure(genericOp, "preconditions not met");
53 
54   // 1. Compute the inverse permutation map, it must be non-null since the
55   // preconditions are satisfied.
56   MLIRContext *context = genericOp.getContext();
57   AffineMap permutationMap = inversePermutation(
58       AffineMap::getPermutationMap(interchangeVector, context));
59   assert(permutationMap && "unexpected null map");
60 
61   // Start a guarded inplace update.
62   rewriter.startRootUpdate(genericOp);
63   auto guard =
64       llvm::make_scope_exit([&]() { rewriter.finalizeRootUpdate(genericOp); });
65 
66   // 2. Compute the interchanged indexing maps.
67   SmallVector<AffineMap> newIndexingMaps;
68   for (OpOperand *opOperand : genericOp.getInputAndOutputOperands()) {
69     AffineMap m = genericOp.getTiedIndexingMap(opOperand);
70     if (!permutationMap.isEmpty())
71       m = m.compose(permutationMap);
72     newIndexingMaps.push_back(m);
73   }
74   genericOp->setAttr(getIndexingMapsAttrName(),
75                      rewriter.getAffineMapArrayAttr(newIndexingMaps));
76 
77   // 3. Compute the interchanged iterator types.
78   ArrayRef<Attribute> itTypes = genericOp.iterator_types().getValue();
79   SmallVector<Attribute> itTypesVector;
80   llvm::append_range(itTypesVector, itTypes);
81   SmallVector<int64_t> permutation(interchangeVector.begin(),
82                                    interchangeVector.end());
83   applyPermutationToVector(itTypesVector, permutation);
84   genericOp->setAttr(getIteratorTypesAttrName(),
85                      ArrayAttr::get(context, itTypesVector));
86 
87   // 4. Transform the index operations by applying the permutation map.
88   if (genericOp.hasIndexSemantics()) {
89     OpBuilder::InsertionGuard guard(rewriter);
90     for (IndexOp indexOp :
91          llvm::make_early_inc_range(genericOp.getBody()->getOps<IndexOp>())) {
92       rewriter.setInsertionPoint(indexOp);
93       SmallVector<Value> allIndices;
94       allIndices.reserve(genericOp.getNumLoops());
95       llvm::transform(llvm::seq<uint64_t>(0, genericOp.getNumLoops()),
96                       std::back_inserter(allIndices), [&](uint64_t dim) {
97                         return rewriter.create<IndexOp>(indexOp->getLoc(), dim);
98                       });
99       rewriter.replaceOpWithNewOp<AffineApplyOp>(
100           indexOp, permutationMap.getSubMap(indexOp.dim()), allIndices);
101     }
102   }
103 
104   return genericOp;
105 }
106