MLIR 24.0.0git
VectorContractToFMA.cpp
Go to the documentation of this file.
1//===- VectorContractToFMA.cpp --------------------------------------------===//
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
13
15#include "mlir/IR/Dominance.h"
17
18#include "mlir/Pass/Pass.h"
20
21using namespace mlir;
22using namespace mlir::vector;
23using namespace mlir::x86;
24
25namespace {
26
27// Implements outer product contraction as a sequence of broadcast and
28// FMA operations.
29//
30// For example - for F32 type:
31// ```
32// vector.contract <1x1xf32>, <1x16xf32> into <1x16xf32>
33// ```
34// to
35// ```
36// vector.broadcast %lhs to <16xf32>
37// vector.fma vector<16xf32>
38// ```
39struct VectorContractToFMA : public AddContractionOpRewritePattern {
40 using Base::Base;
41
42 LogicalResult
43 matchAndRewriteAddContractOp(vector::ContractionOp contractOp,
44 PatternRewriter &rewriter) const override {
45
46 VectorType lhsTy = contractOp.getLhsType();
47 if (!lhsTy.getElementType().isF32())
48 return rewriter.notifyMatchFailure(contractOp,
49 "Only F32 lowering is supported.");
50
51 ArrayRef<int64_t> lhsShape = lhsTy.getShape();
52 llvm::SmallVector<int64_t> nonUnitDimLhs;
53 llvm::copy_if(lhsShape, std::back_inserter(nonUnitDimLhs),
54 [](int64_t dim) { return dim != 1; });
55
56 VectorType rhsTy = contractOp.getRhsType();
57 ArrayRef<int64_t> rhsShape = rhsTy.getShape();
58 llvm::SmallVector<int64_t> nonUnitDimRhs;
59 llvm::copy_if(rhsShape, std::back_inserter(nonUnitDimRhs),
60 [](int64_t dim) { return dim != 1; });
61
62 if (nonUnitDimLhs.size() > 0 && nonUnitDimRhs.size() > 0)
63 return rewriter.notifyMatchFailure(
64 contractOp, "Excepts unit dimensions for either LHS or RHS shape.");
65
66 if (nonUnitDimLhs.size() != 1 && nonUnitDimRhs.size() != 1)
67 return rewriter.notifyMatchFailure(
68 contractOp,
69 "Excepts a one non-unit A/B dimension for either LHS or RHS shape.");
70
71 VectorType accTy = dyn_cast<VectorType>(contractOp.getAccType());
72 if (!accTy)
73 return rewriter.notifyMatchFailure(contractOp,
74 "Accmulator is not a vector type");
75
76 if (!accTy.getElementType().isF32())
77 return rewriter.notifyMatchFailure(contractOp,
78 "Accmulator should be F32 type.");
79
80 ArrayRef<int64_t> accShape = accTy.getShape();
81 llvm::SmallVector<int64_t> nonUnitDimAcc;
82 llvm::copy_if(accShape, std::back_inserter(nonUnitDimAcc),
83 [](int64_t dim) { return dim != 1; });
84 if (nonUnitDimAcc.size() != 1)
85 return rewriter.notifyMatchFailure(
86 contractOp, "A or B dimension should be non-unit.");
87
88 // Lowers vector.contract into a broadcast+FMA sequence.
89 auto loc = contractOp.getLoc();
90 auto castAcc = vector::ShapeCastOp::create(
91 rewriter, loc,
92 VectorType::get(nonUnitDimAcc.front(), accTy.getElementType()),
93 contractOp.getAcc());
94
95 vector::FMAOp fma;
96
97 // Broadcast the unit-dimension LHS or RHS to match the vector length of the
98 // corresponding non-unit dimension on the other operand. For example,
99 // if LHS has type vector<1x1xf32> and RHS has type vector<1x16xf32>, we
100 // broadcast the LHS to vector<1x16xf32>. In the opposite case (non-unit
101 // dimension on the LHS), we broadcast the RHS instead.
102 if (nonUnitDimRhs.size() > 0) {
103 auto castLhs = vector::ShapeCastOp::create(
104 rewriter, loc, VectorType::get(1, lhsTy.getElementType()),
105 contractOp.getLhs());
106 auto castRhs = vector::ShapeCastOp::create(
107 rewriter, loc,
108 VectorType::get(nonUnitDimRhs.front(), rhsTy.getElementType()),
109 contractOp.getRhs());
110 auto broadcastLhs = vector::BroadcastOp::create(
111 rewriter, loc, castRhs.getResult().getType(), castLhs);
112 fma =
113 vector::FMAOp::create(rewriter, loc, broadcastLhs, castRhs, castAcc);
114 } else {
115 auto castLhs = vector::ShapeCastOp::create(
116 rewriter, loc,
117 VectorType::get(nonUnitDimLhs.front(), lhsTy.getElementType()),
118 contractOp.getLhs());
119 auto castRhs = vector::ShapeCastOp::create(
120 rewriter, loc, VectorType::get(1, rhsTy.getElementType()),
121 contractOp.getRhs());
122 auto broadcastRhs = vector::BroadcastOp::create(
123 rewriter, loc, castLhs.getResult().getType(), castRhs);
124 fma =
125 vector::FMAOp::create(rewriter, loc, castLhs, broadcastRhs, castAcc);
126 }
127
128 auto castFma = vector::ShapeCastOp::create(rewriter, loc, accTy, fma);
129 rewriter.replaceOp(contractOp, castFma);
130
131 return success();
132 }
133};
134} // namespace
135
137 patterns.add<VectorContractToFMA>(patterns.getContext());
138}
return success()
MLIRContext * getContext() const
RewritePatternSet & add(ConstructorArg &&arg, ConstructorArgs &&...args)
Add an instance of each of the pattern types 'Ts' to the pattern list with the given arguments.
virtual void replaceOp(Operation *op, ValueRange newValues)
Replace the results of the given (original) operation with the specified list of values (replacements...
std::enable_if_t<!std::is_convertible< CallbackT, Twine >::value, LogicalResult > notifyMatchFailure(Location loc, CallbackT &&reasonCallback)
Used to notify the listener that the IR failed to be rewritten because of a match failure,...
void populateVectorContractToFMAPatterns(RewritePatternSet &patterns)
Include the generated interface declarations.
Convenience wrapper for patterns that are only intended for default contractions, i....