1//===- Generalization.cpp - linalg named ops to generic ops --------------===//
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 generalization pass. It converts named
10// Linalg ops to linalg.generic ops.
11//
12//===----------------------------------------------------------------------===//
13
14#include "mlir/Dialect/Linalg/Passes.h"
15
16#include "mlir/Dialect/Linalg/IR/Linalg.h"
17#include "mlir/Dialect/Linalg/Transforms/Transforms.h"
18#include "mlir/IR/AffineMap.h"
19#include "mlir/IR/Builders.h"
20#include "mlir/IR/PatternMatch.h"
21#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
22
23namespace mlir {
24#define GEN_PASS_DEF_LINALGGENERALIZENAMEDOPSPASS
25#include "mlir/Dialect/Linalg/Passes.h.inc"
26} // namespace mlir
27
28#define DEBUG_TYPE "linalg-generalization"
29
30using namespace mlir;
31using namespace mlir::linalg;
32
33static LogicalResult generalizeNamedOpPrecondition(LinalgOp linalgOp) {
34 // Bailout if `linalgOp` is already a generic.
35 if (isa<GenericOp>(Val: linalgOp))
36 return failure();
37 // Check if the operation has exactly one region.
38 if (linalgOp->getNumRegions() != 1) {
39 assert(linalgOp->getNumRegions() == 0 && "op with multiple regions");
40 // TOD: Otherwise it needs to be built explicitly from the region builder.
41 return failure();
42 }
43 return success();
44}
45
46// Converts a named matmul-like op (`matmul`, `batch_matmul`, or
47// `batch_reduce_matmul`) into a `linalg.contract` category op, preserving the
48// operand indexing maps and cast semantics. Returns failure for other ops.
49static FailureOr<LinalgOp> generalizeToContractOp(RewriterBase &rewriter,
50 LinalgOp namedOp) {
51 // These are the ODS-defined matmul-like operations.
52 // For OpDSL declared contractions, please move them to ODS first,
53 // then add them to the isa<> check below + tests.
54 if (!isa<MatmulOp, BatchMatmulOp, BatchReduceMatmulOp>(
55 Val: namedOp.getOperation()))
56 return failure();
57
58 SmallVector<NamedAttribute> attributes;
59
60 // Preserve operand indexing semantics (transposition, batch/reduction dims)
61 // via the named op's indexing maps.
62 SmallVector<Attribute> indexingMaps = llvm::map_to_vector(
63 C: namedOp.getIndexingMapsArray(),
64 F: [](AffineMap map) -> Attribute { return AffineMapAttr::get(value: map); });
65 attributes.push_back(Elt: rewriter.getNamedAttr(
66 name: "indexing_maps", val: rewriter.getArrayAttr(value: indexingMaps)));
67
68 // Only the unsigned cast needs to be explicit; signed is the default.
69 if (auto castAttr = namedOp->getAttrOfType<TypeFnAttr>(name: "cast");
70 castAttr && castAttr.getValue() == TypeFn::cast_unsigned)
71 attributes.push_back(Elt: rewriter.getNamedAttr(name: "cast", val: castAttr));
72
73 // Capture the operands and discardable attributes before `namedOp` is erased
74 // by the replacement below.
75 Value lhs = namedOp.getDpsInputs()[0];
76 Value rhs = namedOp.getDpsInputs()[1];
77 Value init = namedOp.getDpsInits()[0];
78 DictionaryAttr discardableAttrs = namedOp->getDiscardableAttrDictionary();
79
80 LinalgOp contractOp = rewriter.replaceOpWithNewOp<ContractOp>(
81 op: namedOp, args: ValueRange{lhs, rhs}, args: ValueRange{init}, args&: attributes);
82
83 // Discardable attributes carry user-defined metadata (e.g., annotations for
84 // downstream passes). Generalization is a semantics-preserving
85 // transformation, so dropping this metadata would be unexpected. This is safe
86 // because discardable attributes are by definition independent of op
87 // semantics.
88 contractOp->setDiscardableAttrs(discardableAttrs);
89
90 return contractOp;
91}
92
93FailureOr<LinalgOp> mlir::linalg::generalizeNamedOp(RewriterBase &rewriter,
94 LinalgOp linalgOp,
95 bool emitCategoryOps) {
96 if (failed(Result: generalizeNamedOpPrecondition(linalgOp)))
97 return rewriter.notifyMatchFailure(arg&: linalgOp, msg: "preconditions not met");
98
99 // Emit the `linalg.contract` category op for matmul-like named ops.
100 if (emitCategoryOps) {
101 FailureOr<LinalgOp> contractOp = generalizeToContractOp(rewriter, namedOp: linalgOp);
102 if (succeeded(Result: contractOp))
103 return contractOp;
104 return rewriter.notifyMatchFailure(arg&: linalgOp,
105 msg: "failed to categorize to named op");
106 }
107
108 SmallVector<Value> inputs = linalgOp.getDpsInputs();
109 ValueRange outputs = linalgOp.getDpsInits();
110 SmallVector<AffineMap> indexingMaps = linalgOp.getIndexingMapsArray();
111 SmallVector<utils::IteratorType> iterators = linalgOp.getIteratorTypesArray();
112 SmallVector<Type> resultTypes = linalgOp.hasPureTensorSemantics()
113 ? TypeRange(ValueRange(outputs))
114 : TypeRange{};
115
116 // All named ops have a region attached that can be inlined.
117 assert(linalgOp->getNumRegions() == 1 &&
118 "expect named op to have one region attached");
119 GenericOp genericOp =
120 GenericOp::create(builder&: rewriter, location: linalgOp.getLoc(), resultTensorTypes: resultTypes, inputs,
121 outputs, indexingMaps, iteratorTypes: iterators);
122 rewriter.inlineRegionBefore(region&: linalgOp->getRegion(index: 0), parent&: genericOp.getRegion(),
123 before: genericOp.getRegion().begin());
124
125 // Discardable attributes carry user-defined metadata (e.g., annotations for
126 // downstream passes). Generalization is a semantics-preserving
127 // transformation, so dropping this metadata would be unexpected. This is safe
128 // because discardable attributes are by definition independent of op
129 // semantics.
130 genericOp->setDiscardableAttrs(linalgOp->getDiscardableAttrDictionary());
131
132 rewriter.replaceOp(op: linalgOp, newValues: genericOp->getResults());
133 return cast<LinalgOp>(Val: genericOp.getOperation());
134}
135
136namespace {
137
138struct LinalgGeneralizeNamedOpsPass
139 : public impl::LinalgGeneralizeNamedOpsPassBase<
140 LinalgGeneralizeNamedOpsPass> {
141 using impl::LinalgGeneralizeNamedOpsPassBase<
142 LinalgGeneralizeNamedOpsPass>::LinalgGeneralizeNamedOpsPassBase;
143 void runOnOperation() override;
144};
145
146} // namespace
147
148void LinalgGeneralizeNamedOpsPass::runOnOperation() {
149 RewritePatternSet patterns(&getContext());
150 populateLinalgNamedOpsGeneralizationPatterns(patterns);
151 (void)applyPatternsGreedily(op: getOperation(), patterns: std::move(patterns));
152}
153
154void mlir::linalg::populateLinalgNamedOpsGeneralizationPatterns(
155 RewritePatternSet &patterns, bool emitCategoryOps) {
156 patterns.add<LinalgGeneralizationPattern>(arg: patterns.getContext(),
157 args&: emitCategoryOps);
158}
159