237 VectorType lhsTy = contractOp.getLhsType();
238 if (!lhsTy.getElementType().isBF16())
240 "Only BF16 lowering is supported.");
243 contractOp.getIndexingMapsArray(),
246 VectorType accTy = dyn_cast<VectorType>(contractOp.getAccType());
250 if (!accTy.getElementType().isF32())
252 contractOp,
"Only F32 acumulation supported for BF16 type.");
256 llvm::copy_if(accShape, std::back_inserter(nonUnitDimAcc),
257 [](
int64_t dim) {
return dim != 1; });
258 if (nonUnitDimAcc.size() != 1)
260 contractOp,
"A or B should be a non-unit dim in acc.");
264 llvm::copy_if(lhsShape, std::back_inserter(nonUnitDimLhs),
265 [](
int64_t dim) {
return dim != 1; });
267 VectorType rhsTy = contractOp.getRhsType();
270 llvm::copy_if(rhsShape, std::back_inserter(nonUnitDimRhs),
271 [](
int64_t dim) {
return dim != 1; });
273 if (isVnni && (nonUnitDimLhs.size() - 1) > 0 &&
274 (nonUnitDimRhs.size() - 1) > 0)
276 "Excepts unit dimensions for either "
277 "LHS or RHS shape other than VNNI.");
279 if (isVnni && (nonUnitDimLhs.size() - 1) != 1 &&
280 (nonUnitDimRhs.size() - 1) != 1)
283 "Excepts a one non-unit A/B dimension for either LHS or RHS shape.");
285 if (!isVnni && nonUnitDimLhs.size() > 0 && nonUnitDimRhs.size() > 0)
287 "Excepts unit dimensions for either "
288 "LHS or RHS shape.");
290 if (!isVnni && nonUnitDimLhs.size() != 1 && nonUnitDimRhs.size() != 1)
293 "Excepts a one non-unit A/B dimension for either LHS or RHS shape.");
296 unsigned int nonUnitDim = nonUnitDimAcc.front();
297 if (nonUnitDim != 4 && nonUnitDim != 8)
299 contractOp,
"BF16 packed load operation expects non-unit (LHR or "
300 "RHS) dim and acc dim of size 4/8.");
305 contractOp,
"The LHS or RHS is in an invalid format. Either it has "
306 "false in-bounds, a mask, "
307 "a non-identity permutation map, a non-zero VNNI offset, "
309 "source, or a non-unit VNNI stride");
313 auto loc = contractOp.getLoc();
322 bool rhsHasMultipleNonUnitDims = nonUnitDimRhs.size() > 0;
324 rhsHasMultipleNonUnitDims = (nonUnitDimRhs.size() - 1) > 0;
329 rhsHasMultipleNonUnitDims ? contractOp.getLhs() : contractOp.getRhs();
331 rhsHasMultipleNonUnitDims ? contractOp.getRhs() : contractOp.getLhs();
334 rhsHasMultipleNonUnitDims ? rhsShape : lhsShape;
341 vector::ContractionOp pairContractOp;
344 while ((nextOp = nextOp->getNextNode())) {
345 auto contOp = dyn_cast<vector::ContractionOp>(nextOp);
351 rhsHasMultipleNonUnitDims,
352 nonUnitDimAcc.front())) {
353 pairContractOp = contOp;
365 contractOp,
"The LHS or RHS of the pair contract is in an invalid "
380 if (!accReadOp0 || !accReadOp1)
383 "Operand doesn't have load or transfer_read as its parent op");
385 if (!resultWriteOp0 || !resultWriteOp1)
388 "The use of contract operations are neither vector.store "
389 "or transfer_write or has multiple users");
391 if (contractOp->getBlock() == accReadOp1->
getBlock() &&
392 contractOp->isBeforeInBlock(accReadOp1))
394 contractOp,
"The load/read operation of pair contract operation is "
395 "after the contractOp");
397 if (pairContractOp->getBlock() == resultWriteOp0->
getBlock() &&
400 contractOp,
"The store/write operation of contract operation is "
401 "before the pair contract operation");
407 loc, rewriter, unitSrc, nonUnitDimShape,
true, isVnni);
410 loc, rewriter, nonUnitSrc, nonUnitDimShape,
false, isVnni);
412 auto castAcc = vector::ShapeCastOp::create(
414 VectorType::get(nonUnitDimAcc.front(), accTy.getElementType()),
415 contractOp.getAcc());
417 VectorType::get(nonUnitDimAcc.front(), rewriter.
getF32Type());
427 if (!isa<arith::ConstantOp>(accReadOp0)) {
429 LogicalResult readShuffle =
431 pairContractOp, nonUnitDim, accTy);
433 if (failed(readShuffle))
435 contractOp,
"Accumulator read is not by transfer_read or load");
440 rewriter, contractOp.getResult(), pairContractOp.getResult(),
443 if (failed(writeShuffle))
446 "Write to accumulator is not by transfer_write or store");
449 castAcc = vector::ShapeCastOp::create(
451 VectorType::get(nonUnitDimAcc.front(), accTy.getElementType()),
452 contractOp.getAcc());
454 auto loadBcstBF16ElementToF32 = x86::avx::BcstToPackedF32Op::create(
455 rewriter, loc, dstType, unitDimSubview[0]);
456 auto loadEvenIdxElementF32 =
457 x86::avx::CvtPackedEvenIndexedToF32Op::create(rewriter, loc, dstType,
458 nonUnitDimSubview[0]);
460 vector::FMAOp::create(rewriter, loc, loadBcstBF16ElementToF32,
461 loadEvenIdxElementF32, castAcc);
463 vector::ShapeCastOp::create(rewriter, loc, accTy, evenIdxFMA);
464 rewriter.
replaceOp(contractOp, castEvenFma);
467 auto pairContOpLoc = pairContractOp.getLoc();
468 VectorType accTyPairCont =
469 dyn_cast<VectorType>(pairContractOp.getAccType());
470 auto castAccPairCont = vector::ShapeCastOp::create(
471 rewriter, pairContOpLoc,
472 VectorType::get(nonUnitDimAcc.front(),
473 accTyPairCont.getElementType()),
474 pairContractOp.getAcc());
476 auto loadOddIdxElementF32 = x86::avx::CvtPackedOddIndexedToF32Op::create(
477 rewriter, pairContOpLoc, dstType, nonUnitDimSubview[0]);
478 auto oddIdxFMA = vector::FMAOp::create(
479 rewriter, pairContOpLoc, loadBcstBF16ElementToF32,
480 loadOddIdxElementF32, castAccPairCont);
481 auto castOddFma = vector::ShapeCastOp::create(rewriter, pairContOpLoc,
482 accTyPairCont, oddIdxFMA);
483 rewriter.
replaceOp(pairContractOp, castOddFma);
489 auto loadBcstOddIdxElementToF32 = x86::avx::BcstToPackedF32Op::create(
490 rewriter, loc, dstType, unitDimSubview[0]);
491 auto loadOddIdxElementF32 = x86::avx::CvtPackedOddIndexedToF32Op::create(
492 rewriter, loc, dstType, nonUnitDimSubview[0]);
494 vector::FMAOp::create(rewriter, loc, loadBcstOddIdxElementToF32,
495 loadOddIdxElementF32, castAcc);
498 auto loadBcstEvenIdxElementToF32 = x86::avx::BcstToPackedF32Op::create(
499 rewriter, loc, dstType, unitDimSubview[1]);
500 auto loadEvenIdxElementF32 = x86::avx::CvtPackedEvenIndexedToF32Op::create(
501 rewriter, loc, dstType, nonUnitDimSubview[0]);
503 vector::FMAOp::create(rewriter, loc, loadBcstEvenIdxElementToF32,
504 loadEvenIdxElementF32, oddIdxFMA);
506 auto castFma = vector::ShapeCastOp::create(rewriter, loc, accTy, fma);