1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
|
//===- Specialize.cpp - linalg generic ops to named ops ------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file implements a method to specialize generic operations to named
// operations. Conceptually it is the opposite of generalize.cpp.
//
//===----------------------------------------------------------------------===//
#include "mlir/Dialect/Complex/IR/Complex.h"
#include "mlir/Dialect/Linalg/IR/Linalg.h"
#include "mlir/Dialect/Linalg/IR/LinalgInterfaces.h"
#include "mlir/Dialect/Linalg/Passes.h"
#include "mlir/Dialect/Linalg/Transforms/Transforms.h"
#include "mlir/Dialect/Math/IR/Math.h"
#include "mlir/IR/PatternMatch.h"
#include "mlir/Support/TypeID.h"
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
#include "llvm/Support/Debug.h"
namespace mlir {
#define GEN_PASS_DEF_LINALGSPECIALIZEGENERICOPSPASS
#include "mlir/Dialect/Linalg/Passes.h.inc"
} // namespace mlir
#define DEBUG_TYPE "linalg-specialization"
#define REPLACE_BINARY_OP(NEWOP, OPERANDS_SWAP) \
(rewriter.replaceOpWithNewOp<NEWOP>( \
genericOp, \
ValueRange{genericOp.getDpsInputs()[(OPERANDS_SWAP) ? 1 : 0], \
genericOp.getDpsInputs()[(OPERANDS_SWAP) ? 0 : 1]}, \
ValueRange{genericOp.getDpsInits()[0]}))
#define REPLACE_UNARY_OP(NEWOP) \
(rewriter.replaceOpWithNewOp<NEWOP>(genericOp, \
ValueRange{genericOp.getDpsInputs()[0]}, \
ValueRange{genericOp.getDpsInits()[0]}))
using namespace mlir;
using namespace mlir::linalg;
// Given a elementwise single binary linalg generic op, checks whether the
// binary op accesses operands as swapped. e.g.
// this differentiates between a linalg-generic body that contains:
// ^bb0(%a: f32, %b: f32, %c : f32):
// %0 = arith.subf %a, %b : f32
// linalg.yield %0: f32
// against:
// ^bb0(%a: f32, %b: f32, %c : f32):
// %0 = arith.subf %b, %a : f32
// linalg.yield %0: f32
// Former is linalg.sub(a,b), latter is linalg.sub(b,a).
static bool areBinOpsSwapped(GenericOp genericOp) {
Block *body = genericOp.getBody();
Operation *op = &body->front();
bool swapped = false;
if (op->getOpOperand(0).get() != body->getArgument(0)) {
swapped = true;
assert(op->getOpOperand(0).get() == body->getArgument(1) &&
op->getOpOperand(1).get() == body->getArgument(0) &&
"binary op uses just one block arg");
}
return swapped;
}
//===----------------------------------------------------------------------===//
// Specialize linalg generic to matmul variants.
//===----------------------------------------------------------------------===//
/// Identifies linalg.generic that is essentially named op of the form:
// ` linalg.{batch_}?matmul{_transpose_a | _transpose_b}? `
//
// It is possible that a linalg.generic may be implementing a matmul but not
// in a straight-forward way e.g. below is matrix multiply over some slice
// ```
// %0 = linalg.generic {
// indexing_maps = [affine_map<(d0, d1, d2) -> (3, d1, d0)>,
// affine_map<(d0, d1, d2) -> (d0, 5, d2)>,
// affine_map<(d0, d1, d2) -> (d2, d1, 13)>],
// iterator_types = ["parallel", "parallel", "parallel"]}
// ins(%A, %B : tensor<20x20x20xf32>, tensor<20x20x20xf32>)
// outs(%C : tensor<20x20x20xf32>) {
// ^bb0(%a: f32, %b: f32, %c : f32):
// %mul = arith.mulf %a, %b : f32
// %add = arith.addf %mul, %c : f32
// linalg.yield %add : f32
// } -> tensor<20x20x20xf32>
// ```
// It is not possible to represent above as named op.
// e.g. linalg.batch_matmul(%A, %B : tensor<20x20x20xf32>, ...) is
// not the same as linalg.generic above.
namespace {
enum class IndexMatchResult {
Match = 0, // identity map.
Transposed, // transposed map.
Mismatch // none of the above.
};
// Checks whether the input Affine `map` contains two consecutive dims that
// can be interpreted as accessing a 2D matrix. It is assumed that the row
// column dimension are adjacent axis (in this order) and start at
// `rowDimIdx` in the input map.
//
// e.g. consider A matrix in `C[M,N] = A[M,K] * B[K,N]`. We will check
// whether the map of A is identity (match), transposed, or something
// completely different (mis-match). Similar for B and C.
static IndexMatchResult matchOperandMap(AffineMap map, unsigned rowDimIdx,
unsigned expectedPosOfRowDim,
unsigned expectedPosOfColDim) {
// Get the matrix multiply indices. They are past the batch indices.
auto exprOfRowDim = map.getResults()[rowDimIdx];
auto exprOfColDim = map.getResults()[rowDimIdx + 1];
// They should be pure dimension ids.
if (exprOfRowDim.getKind() != AffineExprKind::DimId ||
exprOfColDim.getKind() != AffineExprKind::DimId)
return IndexMatchResult::Mismatch;
auto posRowDim = cast<AffineDimExpr>(exprOfRowDim).getPosition();
auto posColDim = cast<AffineDimExpr>(exprOfColDim).getPosition();
if (expectedPosOfRowDim == posRowDim && expectedPosOfColDim == posColDim)
return IndexMatchResult::Match;
if (expectedPosOfRowDim == posColDim && expectedPosOfColDim == posRowDim)
return IndexMatchResult::Transposed;
return IndexMatchResult::Mismatch;
}
// Replaces genericOp with `NamedOpTy` op, supplied as a template arg.
// All the variants expressed as pseudo regular expression:
// `linalg.{batch_}?matmul{_transpose_a | _transpose_b}?`
// have same number of ins/out, so its easy to stamp different versions.
template <typename NamedOpTy>
static LinalgOp replaceWithMatmulVariant(RewriterBase &rewriter, GenericOp op) {
LinalgOp namedOp = rewriter.replaceOpWithNewOp<NamedOpTy>(
op, ValueRange{op.getDpsInputs()[0], op.getDpsInputs()[1]},
ValueRange{op.getDpsInits()[0]});
return namedOp;
}
// Converts linalg.generic to named linalg.*matmul* where possible.
static FailureOr<LinalgOp> specializeLinalgContractions(RewriterBase &rewriter,
GenericOp genericOp) {
if (genericOp.getNumDpsInputs() != 2 || genericOp.getNumDpsInits() != 1)
return failure();
// Early exit if not projected permutations.
auto mapRange = genericOp.getIndexingMapsArray();
if (llvm::any_of(mapRange,
[](AffineMap m) { return !m.isProjectedPermutation(); }))
return failure();
// Linalg generic contraction can be across multiple axis e.g.
// ```
// linalg.generic
// {indexing_maps = [affine_map<(m, n, k1, k2) -> (m, k1, k2)>,
// affine_map<(m, n, k1, k2) -> (k2, k1, n)>,
// affine_map<(m, n, k1, k2) -> (m, n)>],
// iterator_types = ["parallel", "parallel",
// "reduction", "reduction"]}
// ins(%A, %B : tensor<10x20x30xf32>, tensor<30x20x40xf32>)
// outs(%C : tensor<10x40xf32>) {
// ^bb0(%a: f32, %b: f32, %c: f32):
// %1 = arith.mulf %a, %b : f32
// %2 = arith.addf %c, %1 : f32
// linalg.yield %2 : f32
// } -> tensor<10x40xf32>
// ```
// In above contraction, there are two reduction dimensions {k1, k2}
// and although a valid linalg contraction, it is not a named-op
// matrix multiply kind. Therefore, reject multi-dim reduction.
auto res = inferContractionDims(genericOp);
if (!succeeded(res))
return failure();
auto dims = *res;
if (dims.m.size() != 1 || dims.n.size() != 1 || dims.k.size() != 1)
return failure();
if (!mlir::linalg::detail::isContractionBody(
*genericOp.getBlock(), [](Operation *first, Operation *second) {
if ((isa<arith::MulFOp>(first) && isa<arith::AddFOp>(second)) ||
(isa<arith::MulIOp>(first) && isa<arith::AddIOp>(second)) ||
(isa<complex::MulOp>(first) && isa<complex::AddOp>(second)))
return true;
return false;
}))
return failure();
// Check rank of operands
auto indexingMaps = genericOp.getIndexingMapsArray();
if (llvm::any_of(indexingMaps, [&dims](AffineMap m) {
return m.getResults().size() !=
dims.batch.size() + 2 /* any two of {m,n,k} */;
}))
return failure();
auto numOfBatchDims = dims.batch.size();
if (indexingMaps[0].getNumDims() != numOfBatchDims + 3)
return failure();
if (numOfBatchDims) {
// Each operand in a linalg generic contraction could express different
// permutations for its batch dimension. But for named op it must be
// identity since separate maps are not specified.
if (llvm::any_of(indexingMaps, [numOfBatchDims](AffineMap m) {
for (unsigned i = 0; i < numOfBatchDims; ++i) {
auto expr = m.getResults()[i];
if (expr.getKind() != AffineExprKind::DimId ||
cast<AffineDimExpr>(expr).getPosition() != i)
return true;
}
return false;
}))
return failure();
}
auto a =
matchOperandMap(indexingMaps[0], numOfBatchDims, dims.m[0], dims.k[0]);
auto b =
matchOperandMap(indexingMaps[1], numOfBatchDims, dims.k[0], dims.n[0]);
auto c =
matchOperandMap(indexingMaps[2], numOfBatchDims, dims.m[0], dims.n[0]);
if (llvm::any_of(ArrayRef<IndexMatchResult>{a, b, c}, [](IndexMatchResult r) {
return r == IndexMatchResult::Mismatch;
}))
return failure();
if (c != IndexMatchResult::Match ||
(a == IndexMatchResult::Transposed && b == IndexMatchResult::Transposed))
return failure();
/// Codegen the different matmul variants.
if (numOfBatchDims) {
if (a == IndexMatchResult::Transposed)
return replaceWithMatmulVariant<BatchMatmulTransposeAOp>(rewriter,
genericOp);
if (b == IndexMatchResult::Transposed)
return replaceWithMatmulVariant<BatchMatmulTransposeBOp>(rewriter,
genericOp);
return replaceWithMatmulVariant<BatchMatmulOp>(rewriter, genericOp);
}
if (a == IndexMatchResult::Transposed)
return replaceWithMatmulVariant<MatmulTransposeAOp>(rewriter, genericOp);
if (b == IndexMatchResult::Transposed)
return replaceWithMatmulVariant<MatmulTransposeBOp>(rewriter, genericOp);
return replaceWithMatmulVariant<MatmulOp>(rewriter, genericOp);
}
} // namespace
//===----------------------------------------------------------------------===//
// Categorize linalg generic to named op where possible.
//===----------------------------------------------------------------------===//
FailureOr<LinalgOp> mlir::linalg::specializeGenericOp(RewriterBase &rewriter,
GenericOp genericOp) {
if (isaCopyOpInterface(genericOp)) {
LinalgOp namedOp = rewriter.replaceOpWithNewOp<CopyOp>(
genericOp, genericOp.getDpsInputs()[0], genericOp.getDpsInits()[0]);
return namedOp;
}
if (isaFillOpInterface(genericOp)) {
LinalgOp namedOp = rewriter.replaceOpWithNewOp<FillOp>(
genericOp, genericOp.getDpsInputs()[0], genericOp.getDpsInits()[0]);
return namedOp;
}
if (isaElemwiseSingleUnaryOpInterface(genericOp)) {
Operation *op = &genericOp.getBody()->front();
if (isa<math::ExpOp>(op)) {
LinalgOp namedOp = REPLACE_UNARY_OP(ExpOp);
return namedOp;
}
}
if (isaElemwiseSingleBinaryOpInterface(genericOp)) {
bool swap = areBinOpsSwapped(genericOp);
Operation *op = &genericOp.getBody()->front();
if (isa<arith::AddFOp>(op)) {
LinalgOp namedOp = REPLACE_BINARY_OP(AddOp, swap);
return namedOp;
}
if (isa<arith::SubFOp>(op)) {
LinalgOp namedOp = REPLACE_BINARY_OP(SubOp, swap);
return namedOp;
}
if (isa<arith::MulFOp>(op)) {
LinalgOp namedOp = REPLACE_BINARY_OP(MulOp, swap);
return namedOp;
}
if (isa<arith::DivFOp>(op)) {
LinalgOp namedOp = REPLACE_BINARY_OP(DivOp, swap);
return namedOp;
}
}
if (isaContractionOpInterface(genericOp)) {
return specializeLinalgContractions(rewriter, genericOp);
}
return failure();
}
namespace {
struct LinalgSpecializeGenericOpsPass
: public impl::LinalgSpecializeGenericOpsPassBase<
LinalgSpecializeGenericOpsPass> {
using impl::LinalgSpecializeGenericOpsPassBase<
LinalgSpecializeGenericOpsPass>::LinalgSpecializeGenericOpsPassBase;
void runOnOperation() override;
};
} // namespace
void LinalgSpecializeGenericOpsPass::runOnOperation() {
RewritePatternSet patterns(&getContext());
populateLinalgGenericOpsSpecializationPatterns(patterns);
if (failed(applyPatternsAndFoldGreedily(getOperation(), std::move(patterns))))
signalPassFailure();
}
void mlir::linalg::populateLinalgGenericOpsSpecializationPatterns(
RewritePatternSet &patterns) {
patterns.add<LinalgSpecializationPattern>(patterns.getContext());
}
|