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/LinalgOps.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/Support/Debug.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include <type_traits>
27 
28 #define DEBUG_TYPE "linalg-interchange"
29 
30 using namespace mlir;
31 using namespace mlir::linalg;
32 
33 LogicalResult mlir::linalg::interchangeGenericOpPrecondition(
34     GenericOp genericOp, ArrayRef<unsigned> interchangeVector) {
35   // Interchange vector must be non-empty and match the number of loops.
36   if (interchangeVector.empty() ||
37       genericOp.getNumLoops() != interchangeVector.size())
38     return failure();
39   // Permutation map must be invertible.
40   if (!inversePermutation(AffineMap::getPermutationMap(interchangeVector,
41                                                        genericOp.getContext())))
42     return failure();
43   return success();
44 }
45 
46 void mlir::linalg::interchangeGenericOp(PatternRewriter &rewriter,
47                                         GenericOp genericOp,
48                                         ArrayRef<unsigned> interchangeVector) {
49   // 1. Compute the inverse permutation map.
50   MLIRContext *context = genericOp.getContext();
51   AffineMap permutationMap = inversePermutation(
52       AffineMap::getPermutationMap(interchangeVector, context));
53   assert(permutationMap && "expected permutation to be invertible");
54   assert(interchangeVector.size() == genericOp.getNumLoops() &&
55          "expected interchange vector to have entry for every loop");
56 
57   // 2. Compute the interchanged indexing maps.
58   SmallVector<Attribute, 4> newIndexingMaps;
59   ArrayRef<Attribute> indexingMaps = genericOp.indexing_maps().getValue();
60   for (unsigned i = 0, e = genericOp.getNumShapedOperands(); i != e; ++i) {
61     AffineMap m = indexingMaps[i].cast<AffineMapAttr>().getValue();
62     if (!permutationMap.isEmpty())
63       m = m.compose(permutationMap);
64     newIndexingMaps.push_back(AffineMapAttr::get(m));
65   }
66   genericOp->setAttr(getIndexingMapsAttrName(),
67                      ArrayAttr::get(context, newIndexingMaps));
68 
69   // 3. Compute the interchanged iterator types.
70   ArrayRef<Attribute> itTypes = genericOp.iterator_types().getValue();
71   SmallVector<Attribute, 4> itTypesVector;
72   llvm::append_range(itTypesVector, itTypes);
73   applyPermutationToVector(itTypesVector, interchangeVector);
74   genericOp->setAttr(getIteratorTypesAttrName(),
75                      ArrayAttr::get(context, itTypesVector));
76 
77   // 4. Transform the index operations by applying the permutation map.
78   if (genericOp.hasIndexSemantics()) {
79     // TODO: Remove the assertion and add a getBody() method to LinalgOp
80     // interface once every LinalgOp has a body.
81     assert(genericOp->getNumRegions() == 1 &&
82            genericOp->getRegion(0).getBlocks().size() == 1 &&
83            "expected generic operation to have one block.");
84     Block &block = genericOp->getRegion(0).front();
85     OpBuilder::InsertionGuard guard(rewriter);
86     for (IndexOp indexOp :
87          llvm::make_early_inc_range(block.getOps<IndexOp>())) {
88       rewriter.setInsertionPoint(indexOp);
89       SmallVector<Value> allIndices;
90       allIndices.reserve(genericOp.getNumLoops());
91       llvm::transform(llvm::seq<uint64_t>(0, genericOp.getNumLoops()),
92                       std::back_inserter(allIndices), [&](uint64_t dim) {
93                         return rewriter.create<IndexOp>(indexOp->getLoc(), dim);
94                       });
95       rewriter.replaceOpWithNewOp<AffineApplyOp>(
96           indexOp, permutationMap.getSubMap(indexOp.dim()), allIndices);
97     }
98   }
99 }
100