33#define DEBUG_TYPE "lower-contract-to-arm-sve-i8mm"
49std::optional<Value> getExtOperand(
Value v) {
51 static_assert(llvm::is_one_of<Op, arith::ExtSIOp, arith::ExtUIOp>::value,
52 "Must be instantiated with either sign- or zero- extension op");
58 if constexpr (std::is_same<Op, arith::ExtSIOp>::value) {
59 auto vTy = cast<VectorType>(v.
getType());
60 if (!vTy.getElementType().isSignlessInteger(8))
69 auto inOp = extOp.getIn();
70 auto inTy = dyn_cast<VectorType>(inOp.getType());
71 if (!inTy || !inTy.getElementType().isSignlessInteger(8))
74 auto outTy = dyn_cast<VectorType>(extOp.getType());
75 if (!outTy || !outTy.getElementType().isSignlessInteger(32))
161class VectorContractRewriter {
174 MMLA mmlaOp = MMLA::Nop;
179 bool swapOperands =
false;
200 LogicalResult match(vector::ContractionOp op,
PatternRewriter &rewriter);
203 VectorContractRewriter() =
default;
219 case MMLA::SignedInt:
220 return arm_sve::SmmlaOp::create(rewriter, loc, resTy,
acc, lhs, rhs);
221 case MMLA::UnsignedInt:
222 return arm_sve::UmmlaOp::create(rewriter, loc, resTy,
acc, lhs, rhs);
224 return arm_sve::UsmmlaOp::create(rewriter, loc, resTy,
acc, lhs, rhs);
226 return arm_sve::BfmmlaOp::create(rewriter, loc, resTy,
acc, lhs, rhs);
228 llvm_unreachable(
"Uninitialized operation kind");
232LogicalResult VectorContractRewriter::match(vector::ContractionOp op,
235 auto itTypes = op.getIteratorTypesArray();
236 if (itTypes.size() != 3 || itTypes[0] != vector::IteratorType::parallel ||
237 itTypes[1] != vector::IteratorType::parallel ||
238 itTypes[2] != vector::IteratorType::reduction)
240 op,
"iterator types do not correspond to matrix multiplication");
247 if (op.getIndexingMapsArray()[0] !=
250 op.getIndexingMapsArray()[1] !=
254 3,
ArrayRef{0u, 1u}, op.getContext()))
258 if (op.getKind() != vector::CombiningKind::ADD)
264Value VectorContractRewriter::lower(vector::ContractionOp op,
268 Type operandEltType = cast<VectorType>(lhs.getType()).getElementType();
269 Type resultEltType = cast<VectorType>(op.getResultType()).getElementType();
271 const int64_t numOperandSubTileElts =
275 "Only implemented for i32 or f32 output");
276 const int64_t numResultSubTileElts = 4;
281 VectorType::get(numOperandSubTileElts, operandEltType,
284 VectorType::get(numOperandSubTileElts, operandEltType,
287 VectorType::get(numResultSubTileElts, resultEltType,
292 auto flatRhsTileType = VectorType::get(k * n, operandEltType,
297 auto accRowTy = VectorType::get(n, resultEltType,
302 auto accRowX2Ty = VectorType::get(2 * n, resultEltType,
307 auto accRow64Ty = VectorType::get(n / 2, rewriter.
getI64Type(),
312 auto accRowX264Ty = VectorType::get(n, rewriter.
getI64Type(),
319 for (
int64_t i = 0; i < m; i += 2) {
327 std::iota(shuffleIdx.begin(), shuffleIdx.end(), 0);
328 auto t = vector::ShuffleOp::create(rewriter, loc, r0, r1, shuffleIdx);
330 auto s = vector::ScalableInsertOp::create(
331 rewriter, loc, t, ub::PoisonOp::create(rewriter, loc, flatLhsType), 0);
333 auto r = arm_sve::DupQLaneOp::create(rewriter, loc, s, 0);
334 lhsTile.push_back(r);
338 auto rhs = vector::ShapeCastOp::create(rewriter, this->rhs.
getLoc(),
339 flatRhsTileType,
this->rhs);
344 rhsTile.push_back(vector::ScalableExtractOp::create(
345 rewriter, loc, flatRhsType, rhs,
j * k));
349 for (
int64_t i = 0; i < m; i += 2) {
351 auto r0 = vector::ExtractOp::create(rewriter, loc, op.getAcc(),
353 auto r1 = vector::ExtractOp::create(rewriter, loc, op.getAcc(),
360 accTileVec = vector::InterleaveOp::create(rewriter, loc, r0, r1);
364 auto r0I64 = vector::BitCastOp::create(rewriter, loc, accRow64Ty, r0);
365 auto r1I64 = vector::BitCastOp::create(rewriter, loc, accRow64Ty, r1);
369 auto intrI64 = vector::InterleaveOp::create(rewriter, loc, r0I64, r1I64);
373 vector::BitCastOp::create(rewriter, loc, accRowX2Ty, intrI64);
377 accTile.push_back(vector::ScalableExtractOp::create(
378 rewriter, loc, flatAccType, accTileVec,
j * 2));
383 for (
int64_t i = 0; i < m / 2; ++i)
385 Value mmla = createMMLA(rewriter, loc, accTile[i * n / 2 +
j], lhsTile[i],
387 outTile.push_back(mmla);
391 Value result = ub::PoisonOp::create(rewriter, loc, op.getResultType());
392 for (
int64_t i = 0; i < m / 2; ++i) {
394 Value row = ub::PoisonOp::create(rewriter, loc, accRowX2Ty);
396 row = vector::ScalableInsertOp::create(
397 rewriter, loc, outTile[i * n / 2 +
j], row,
j * 4);
405 auto tmp = vector::DeinterleaveOp::create(rewriter, loc, row);
406 out0 = tmp.getResultEven();
407 out1 = tmp.getResultOdd();
410 auto row64 = vector::BitCastOp::create(rewriter, loc, accRowX264Ty, row);
411 auto deintr64 = vector::DeinterleaveOp::create(rewriter, loc, row64);
414 out0 = vector::BitCastOp::create(rewriter, loc, accRowTy,
415 deintr64.getResultEven());
416 out1 = vector::BitCastOp::create(rewriter, loc, accRowTy,
417 deintr64.getResultOdd());
419 result = vector::InsertOp::create(rewriter, loc, out0,
result, i * 2);
420 result = vector::InsertOp::create(rewriter, loc, out1,
result, i * 2 + 1);
426class VectorContractRewriterI8MM :
public VectorContractRewriter {
430 LogicalResult matchAndInit(vector::ContractionOp op,
432 if (failed(match(op, rewriter)))
435 VectorType lhsType = op.getLhsType();
436 VectorType rhsType = op.getRhsType();
438 m = lhsType.getDimSize(0);
439 n = rhsType.getDimSize(0);
440 k = rhsType.getDimSize(1);
447 if (lhsType.isScalable() || !rhsType.getScalableDims()[0] ||
448 rhsType.getScalableDims()[1] || lhsType.getDimSize(1) != k || k != 8 ||
449 m < 2 || m % 2 != 0 || n < 2 || n % 2 != 0)
453 auto outTy = dyn_cast<VectorType>(op.getResultType());
454 if (!outTy || outTy.getElementType() != rewriter.
getI32Type())
456 "output type is not a vector of i32");
462 mmlaOp = MMLA::SignedInt;
463 swapOperands =
false;
464 auto maybeLhs = getExtOperand<arith::ExtSIOp>(op.getLhs());
466 mmlaOp = MMLA::UnsignedInt;
467 maybeLhs = getExtOperand<arith::ExtUIOp>(op.getLhs());
471 op,
"LHS is not a sign- or zero- extended i8");
473 auto maybeRhs = getExtOperand<arith::ExtSIOp>(op.getRhs());
475 if (mmlaOp == MMLA::UnsignedInt)
476 mmlaOp = MMLA::MixedInt;
478 if (mmlaOp == MMLA::SignedInt) {
479 mmlaOp = MMLA::MixedInt;
482 maybeRhs = getExtOperand<arith::ExtUIOp>(op.getRhs());
486 op,
"RHS is not a sign- or zero- extended i8");
497class VectorContractRewriterBfloat :
public VectorContractRewriter {
501 LogicalResult matchAndInit(vector::ContractionOp op,
503 if (failed(match(op, rewriter)))
506 VectorType lhsType = op.getLhsType();
507 VectorType rhsType = op.getRhsType();
509 m = lhsType.getDimSize(0);
510 n = rhsType.getDimSize(0);
511 k = rhsType.getDimSize(1);
518 if (lhsType.isScalable() || !rhsType.getScalableDims()[0] ||
519 rhsType.getScalableDims()[1] || lhsType.getDimSize(1) != k || k != 4 ||
520 m < 2 || m % 2 != 0 || n < 2 || n % 2 != 0)
524 auto outTy = dyn_cast<VectorType>(op.getResultType());
525 if (!outTy || outTy.getElementType() != rewriter.
getF32Type())
527 "output type is not a vector of f32");
530 if (lhsType.getElementType() != rewriter.
getBF16Type())
532 "input type is not a vector of bf16");
535 mmlaOp = MMLA::Bfloat;
536 swapOperands =
false;
545class LowerContractionToSVEI8MMPattern
549 LogicalResult matchAndRewrite(vector::ContractionOp op,
553 VectorContractRewriterI8MM vcr;
554 if (failed(vcr.matchAndInit(op, rewriter)))
564class LowerContractionToSVEBFMMLAPattern
568 LogicalResult matchAndRewrite(vector::ContractionOp op,
572 VectorContractRewriterBfloat vcr;
573 if (failed(vcr.matchAndInit(op, rewriter)))
586static LogicalResult matchIndexed(vector::ContractionOp op,
590 auto itTypes = op.getIteratorTypesArray();
591 if (itTypes.size() != 3 || itTypes[0] != vector::IteratorType::parallel ||
592 itTypes[1] != vector::IteratorType::parallel ||
593 itTypes[2] != vector::IteratorType::reduction)
595 op,
"iterator types do not correspond to matrix multiplication");
602 if (op.getIndexingMapsArray()[0] !=
605 op.getIndexingMapsArray()[1] !=
609 3,
ArrayRef{0u, 1u}, op.getContext()))
614 if (op.getKind() != vector::CombiningKind::ADD)
621 VectorType lhsType = op.getLhsType();
622 VectorType rhsType = op.getRhsType();
623 auto m = lhsType.getDimSize(0);
624 auto n = rhsType.getDimSize(1);
625 auto k = rhsType.getDimSize(0);
627 if (rhsType.getScalableDims()[0] || !rhsType.getScalableDims()[1] ||
628 lhsType.isScalable() || lhsType.getDimSize(1) != k || k != 4 || m != 1 ||
633 auto outTy = dyn_cast<VectorType>(op.getResultType());
634 if (!outTy || outTy.getElementType() != rewriter.
getF32Type())
636 "output type is not a vector of f32");
639 if (lhsType.getElementType() != rewriter.
getF32Type())
641 "input type is not a vector of bf16");
668class LowerContractionToSVEIndexedFMLAPatterns
673 LogicalResult matchAndRewrite(vector::ContractionOp op,
676 if (failed(matchIndexed(op, rewriter)))
679 auto loc = op.getLoc();
680 auto acc = op.getAcc();
684 mlir::VectorType legalType =
685 VectorType::get({4}, rewriter.
getF32Type(), {
true});
689 auto lhs1D = vector::ExtractOp::create(rewriter, loc, op.getLhs(), 0);
691 VectorType::get({4}, rewriter.
getF32Type(), {
true});
693 LLVM::vector_repeat::create(rewriter, loc, repeatedTy, lhs1D)
698 auto accRow = vector::ExtractOp::create(rewriter, loc,
acc, 0);
700 vector::ScalableExtractOp::create(rewriter, loc, legalType, accRow, 0)
703 vector::ScalableExtractOp::create(rewriter, loc, legalType, accRow, 4)
707 for (
size_t laneIdx = 0; laneIdx < 4; ++laneIdx) {
713 vector::ExtractOp::create(rewriter, loc, op.getRhs(), laneIdx);
714 auto rhs0 = vector::ScalableExtractOp::create(rewriter, loc, legalType,
716 auto rhs1 = vector::ScalableExtractOp::create(rewriter, loc, legalType,
722 acc0 = arm_sve::FmlaOp::create(rewriter, loc, legalType, acc0, rhs0,
725 acc1 = arm_sve::FmlaOp::create(rewriter, loc, legalType, acc1, rhs1,
731 acc = vector::ScalableInsertOp::create(rewriter, loc, acc0, accRow, 0);
732 acc = vector::ScalableInsertOp::create(rewriter, loc, acc1,
acc, 4);
734 rewriter.
replaceOp(op, vector::ShapeCastOp::create(
735 rewriter, loc, op.getResultType(),
acc));
745 patterns.
add<LowerContractionToSVEI8MMPattern>(context, 2);
751 patterns.
add<LowerContractionToSVEBFMMLAPattern>(context, 2);
757 patterns.
add<LowerContractionToSVEIndexedFMLAPatterns>(context,
return MutableOperandRange * this
static AffineMap getMultiDimMapWithTargets(unsigned numDims, ArrayRef< unsigned > targets, MLIRContext *context)
Returns an affine map with numDims input dimensions and results specified by targets.
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
MLIRContext is the top-level object for a collection of MLIR operations.
This provides public APIs that all operations should have.
A special type of RewriterBase that coordinates the application of a rewrite pattern on the current I...
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,...
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
unsigned getIntOrFloatBitWidth() const
Return the bit width of an integer or a float type, assert failure on other types.
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Type getType() const
Return the type of this value.
Location getLoc() const
Return the location of this value.
Operation * getDefiningOp() const
If this value is the result of an operation, return the operation that defines it.
static ConstantIntOp create(OpBuilder &builder, Location location, int64_t value, unsigned width)
void populateLowerContractionToSVEBFMMLAPatterns(RewritePatternSet &patterns)
void populateLowerContractionToSVEIndexedFMLAPatterns(RewritePatternSet &patterns)
void populateLowerContractionToSVEI8MMPatterns(RewritePatternSet &patterns)
Include the generated interface declarations.
OpRewritePattern is a wrapper around RewritePattern that allows for matching and rewriting against an...
Eliminates variable at the specified position using Fourier-Motzkin variable elimination.