315 VectorType lhsTy = contractOp.getLhsType();
316 Type elemTy = lhsTy.getElementType();
319 contractOp,
"Only BF16/F16 lowering is supported.");
321 if (fallback && elemTy.
isF16())
323 contractOp,
"The BF16 fallback does not support F16.");
326 contractOp.getIndexingMapsArray(),
329 VectorType accTy = dyn_cast<VectorType>(contractOp.getAccType());
333 if (!accTy.getElementType().isF32())
335 contractOp,
"Only F32 acumulation supported for BF16/F16 type.");
339 llvm::copy_if(accShape, std::back_inserter(nonUnitDimAcc),
340 [](
int64_t dim) {
return dim != 1; });
341 if (nonUnitDimAcc.size() != 1)
343 contractOp,
"A or B should be a non-unit dim in acc.");
347 llvm::copy_if(lhsShape, std::back_inserter(nonUnitDimLhs),
348 [](
int64_t dim) {
return dim != 1; });
350 VectorType rhsTy = contractOp.getRhsType();
353 llvm::copy_if(rhsShape, std::back_inserter(nonUnitDimRhs),
354 [](
int64_t dim) {
return dim != 1; });
356 if (isVnni && (nonUnitDimLhs.size() - 1) > 0 &&
357 (nonUnitDimRhs.size() - 1) > 0)
359 "Excepts unit dimensions for either "
360 "LHS or RHS shape other than VNNI.");
362 if (isVnni && (nonUnitDimLhs.size() - 1) != 1 &&
363 (nonUnitDimRhs.size() - 1) != 1)
366 "Excepts a one non-unit A/B dimension for either LHS or RHS shape.");
368 if (!isVnni && nonUnitDimLhs.size() > 0 && nonUnitDimRhs.size() > 0)
370 "Excepts unit dimensions for either "
371 "LHS or RHS shape.");
373 if (!isVnni && nonUnitDimLhs.size() != 1 && nonUnitDimRhs.size() != 1)
376 "Excepts a one non-unit A/B dimension for either LHS or RHS shape.");
379 unsigned int nonUnitDim = nonUnitDimAcc.front();
381 if (fallback && nonUnitDim != 4 && nonUnitDim != 8 && nonUnitDim != 16)
383 contractOp,
"BF16 fallback expects non-unit (LHS or RHS) dim and "
384 "acc dim of size 4/8/16.");
386 if (!fallback && nonUnitDim != 4 && nonUnitDim != 8)
388 contractOp,
"BF16 packed load operation expects non-unit (LHR or "
389 "RHS) dim and acc dim of size 4/8.");
394 contractOp,
"The LHS or RHS is in an invalid format. Either it has "
395 "false in-bounds, a mask, "
396 "a non-identity permutation map, a non-zero VNNI offset, "
398 "source, or a non-unit VNNI stride");
402 auto loc = contractOp.getLoc();
411 bool rhsHasMultipleNonUnitDims = nonUnitDimRhs.size() > 0;
413 rhsHasMultipleNonUnitDims = (nonUnitDimRhs.size() - 1) > 0;
418 rhsHasMultipleNonUnitDims ? contractOp.getLhs() : contractOp.getRhs();
420 rhsHasMultipleNonUnitDims ? contractOp.getRhs() : contractOp.getLhs();
423 rhsHasMultipleNonUnitDims ? rhsShape : lhsShape;
437 vector::ContractionOp pairContractOp;
440 while ((nextOp = nextOp->getNextNode())) {
441 auto contOp = dyn_cast<vector::ContractionOp>(nextOp);
447 rhsHasMultipleNonUnitDims,
448 nonUnitDimAcc.front())) {
449 pairContractOp = contOp;
461 contractOp,
"The LHS or RHS of the pair contract is in an invalid "
476 if (!accReadOp0 || !accReadOp1)
479 "Operand doesn't have load or transfer_read as its parent op");
481 if (!resultWriteOp0 || !resultWriteOp1)
484 "The use of contract operations are neither vector.store "
485 "or transfer_write or has multiple users");
487 if (contractOp->getBlock() == accReadOp1->
getBlock() &&
488 contractOp->isBeforeInBlock(accReadOp1))
490 contractOp,
"The load/read operation of pair contract operation is "
491 "after the contractOp");
493 if (pairContractOp->getBlock() == resultWriteOp0->
getBlock() &&
496 contractOp,
"The store/write operation of contract operation is "
497 "before the pair contract operation");
503 loc, rewriter, unitSrc, nonUnitDimShape,
true, isVnni);
506 loc, rewriter, nonUnitSrc, nonUnitDimShape,
false, isVnni);
508 auto castAcc = vector::ShapeCastOp::create(
510 VectorType::get(nonUnitDimAcc.front(), accTy.getElementType()),
511 contractOp.getAcc());
513 VectorType::get(nonUnitDimAcc.front(), rewriter.
getF32Type());
523 if (!isa<arith::ConstantOp>(accReadOp0)) {
525 LogicalResult readShuffle =
527 pairContractOp, nonUnitDim, accTy);
529 if (failed(readShuffle))
531 contractOp,
"Accumulator read is not by transfer_read or load");
536 rewriter, contractOp.getResult(), pairContractOp.getResult(),
539 if (failed(writeShuffle))
542 "Write to accumulator is not by transfer_write or store");
545 castAcc = vector::ShapeCastOp::create(
547 VectorType::get(nonUnitDimAcc.front(), accTy.getElementType()),
548 contractOp.getAcc());
550 auto loadBcstBF16ElementToF32 = x86::avx::BcstToPackedF32Op::create(
551 rewriter, loc, dstType, unitDimSubview[0]);
552 auto loadEvenIdxElementF32 =
553 x86::avx::CvtPackedEvenIndexedToF32Op::create(rewriter, loc, dstType,
554 nonUnitDimSubview[0]);
556 vector::FMAOp::create(rewriter, loc, loadBcstBF16ElementToF32,
557 loadEvenIdxElementF32, castAcc);
559 vector::ShapeCastOp::create(rewriter, loc, accTy, evenIdxFMA);
560 rewriter.
replaceOp(contractOp, castEvenFma);
563 auto pairContOpLoc = pairContractOp.getLoc();
564 VectorType accTyPairCont =
565 dyn_cast<VectorType>(pairContractOp.getAccType());
566 auto castAccPairCont = vector::ShapeCastOp::create(
567 rewriter, pairContOpLoc,
568 VectorType::get(nonUnitDimAcc.front(),
569 accTyPairCont.getElementType()),
570 pairContractOp.getAcc());
572 auto loadOddIdxElementF32 = x86::avx::CvtPackedOddIndexedToF32Op::create(
573 rewriter, pairContOpLoc, dstType, nonUnitDimSubview[0]);
574 auto oddIdxFMA = vector::FMAOp::create(
575 rewriter, pairContOpLoc, loadBcstBF16ElementToF32,
576 loadOddIdxElementF32, castAccPairCont);
577 auto castOddFma = vector::ShapeCastOp::create(rewriter, pairContOpLoc,
578 accTyPairCont, oddIdxFMA);
579 rewriter.
replaceOp(pairContractOp, castOddFma);
585 auto loadBcstOddIdxElementToF32 = x86::avx::BcstToPackedF32Op::create(
586 rewriter, loc, dstType, unitDimSubview[0]);
587 auto loadOddIdxElementF32 = x86::avx::CvtPackedOddIndexedToF32Op::create(
588 rewriter, loc, dstType, nonUnitDimSubview[0]);
590 vector::FMAOp::create(rewriter, loc, loadBcstOddIdxElementToF32,
591 loadOddIdxElementF32, castAcc);
594 auto loadBcstEvenIdxElementToF32 = x86::avx::BcstToPackedF32Op::create(
595 rewriter, loc, dstType, unitDimSubview[1]);
596 auto loadEvenIdxElementF32 = x86::avx::CvtPackedEvenIndexedToF32Op::create(
597 rewriter, loc, dstType, nonUnitDimSubview[0]);
599 vector::FMAOp::create(rewriter, loc, loadBcstEvenIdxElementToF32,
600 loadEvenIdxElementF32, oddIdxFMA);
602 auto castFma = vector::ShapeCastOp::create(rewriter, loc, accTy, fma);