43 matchAndRewriteAddContractOp(vector::ContractionOp contractOp,
44 PatternRewriter &rewriter)
const override {
46 VectorType lhsTy = contractOp.getLhsType();
47 if (!lhsTy.getElementType().isF32())
49 "Only F32 lowering is supported.");
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; });
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; });
62 if (nonUnitDimLhs.size() > 0 && nonUnitDimRhs.size() > 0)
64 contractOp,
"Excepts unit dimensions for either LHS or RHS shape.");
66 if (nonUnitDimLhs.size() != 1 && nonUnitDimRhs.size() != 1)
69 "Excepts a one non-unit A/B dimension for either LHS or RHS shape.");
71 VectorType accTy = dyn_cast<VectorType>(contractOp.getAccType());
74 "Accmulator is not a vector type");
76 if (!accTy.getElementType().isF32())
78 "Accmulator should be F32 type.");
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)
86 contractOp,
"A or B dimension should be non-unit.");
89 auto loc = contractOp.getLoc();
90 auto castAcc = vector::ShapeCastOp::create(
92 VectorType::get(nonUnitDimAcc.front(), accTy.getElementType()),
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(
108 VectorType::get(nonUnitDimRhs.front(), rhsTy.getElementType()),
109 contractOp.getRhs());
110 auto broadcastLhs = vector::BroadcastOp::create(
111 rewriter, loc, castRhs.getResult().getType(), castLhs);
113 vector::FMAOp::create(rewriter, loc, broadcastLhs, castRhs, castAcc);
115 auto castLhs = vector::ShapeCastOp::create(
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);
125 vector::FMAOp::create(rewriter, loc, castLhs, broadcastRhs, castAcc);
128 auto castFma = vector::ShapeCastOp::create(rewriter, loc, accTy, fma);
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....