33static bool isNonUnitDimOperandShuffled(
Value nonUnitDimOperand) {
35 if (isa<vector::ShuffleOp>(defOp))
38 if (isa<vector::ShapeCastOp>(defOp)) {
40 if (isa<vector::ShuffleOp>(defOpShpCst))
53 if (mlir::isa<mlir::vector::ContractionOp>(user) ||
54 mlir::isa<mlir::scf::ForOp>(user)) {
65 mlir::vector::ContractionOp contractA,
66 mlir::vector::ContractionOp contractB,
68 mlir::VectorType Ty) {
82 auto elemTy = Ty.getElementType();
83 auto flatTy = mlir::VectorType::get(nonUnitDimAcc, elemTy);
85 if (elemTy.isSignlessInteger(8))
86 flatTy = mlir::VectorType::get({2, nonUnitDimAcc / 2}, elemTy);
93 srcBuff = readOp.getOperand(0);
95 auto indices = readOp.getIndices();
96 indexVals.reserve(
indices.size());
104 int64_t srcRank = (dyn_cast<ShapedType>(srcBuff.getType())).getRank();
105 Value padding = ub::PoisonOp::create(rewriter, loc, elemTy);
110 Value vec1 = vector::TransferReadOp::create(
111 rewriter, loc, flatTy, srcBuff, indexVals, padding, map, inBounds);
113 if (elemTy.isSignlessInteger(8))
114 vec1 = vector::ShapeCastOp::create(
115 rewriter, loc, VectorType::get(nonUnitDimAcc, elemTy), vec1);
117 unsigned int offset = 1;
118 if (elemTy.isSignlessInteger(8))
123 arith::AddIOp::create(rewriter, loc, rewriter.
getIndexType(), cOffset,
124 indexVals[indexVals.size() - 2]);
125 indexVals[indexVals.size() - 2] = nextIndx;
127 Value vec2 = vector::TransferReadOp::create(
128 rewriter, loc, flatTy, srcBuff, indexVals, padding, map, inBounds);
130 if (elemTy.isSignlessInteger(8))
131 vec2 = vector::ShapeCastOp::create(
132 rewriter, loc, VectorType::get(nonUnitDimAcc, elemTy), vec2);
134 flatTy = mlir::VectorType::get(nonUnitDimAcc, elemTy);
136 static constexpr int64_t maskLo_bf16[] = {
137 0, 32, 1, 33, 2, 34, 3, 35, 8, 40, 9, 41, 10, 42, 11, 43,
138 16, 48, 17, 49, 18, 50, 19, 51, 24, 56, 25, 57, 26, 58, 27, 59};
139 static constexpr int64_t maskHi_bf16[] = {
140 4, 36, 5, 37, 6, 38, 7, 39, 12, 44, 13, 45, 14, 46, 15, 47,
141 20, 52, 21, 53, 22, 54, 23, 55, 28, 60, 29, 61, 30, 62, 31, 63};
143 static constexpr int64_t maskLo_int8_avx2[] = {
144 0, 16, 32, 48, 1, 17, 33, 49, 2, 18, 34, 50, 3, 19, 35, 51,
145 8, 24, 40, 56, 9, 25, 41, 57, 10, 26, 42, 58, 11, 27, 43, 59};
146 static constexpr int64_t maskHi_int8_avx2[] = {
147 4, 20, 36, 52, 5, 21, 37, 53, 6, 22, 38, 54, 7, 23, 39, 55,
148 12, 28, 44, 60, 13, 29, 45, 61, 14, 30, 46, 62, 15, 31, 47, 63};
150 static constexpr int64_t maskLo_int8_avx10[] = {
151 0, 32, 64, 96, 1, 33, 65, 97, 2, 34, 66, 98, 3, 35, 67, 99,
152 8, 40, 72, 104, 9, 41, 73, 105, 10, 42, 74, 106, 11, 43, 75, 107,
153 16, 48, 80, 112, 17, 49, 81, 113, 18, 50, 82, 114, 19, 51, 83, 115,
154 24, 56, 88, 120, 25, 57, 89, 121, 26, 58, 90, 122, 27, 59, 91, 123};
155 static constexpr int64_t maskHi_int8_avx10[] = {
156 4, 36, 68, 100, 5, 37, 69, 101, 6, 38, 70, 102, 7, 39, 71, 103,
157 12, 44, 76, 108, 13, 45, 77, 109, 14, 46, 78, 110, 15, 47, 79, 111,
158 20, 52, 84, 116, 21, 53, 85, 117, 22, 54, 86, 118, 23, 55, 87, 119,
159 28, 60, 92, 124, 29, 61, 93, 125, 30, 62, 94, 126, 31, 63, 95, 127};
164 if (elemTy.isSignlessInteger(8)) {
168 if (nonUnitDimAcc == 32) {
174 auto shuffleLo = mlir::vector::ShuffleOp::create(rewriter, loc, flatTy, vec1,
176 auto shuffleHi = mlir::vector::ShuffleOp::create(rewriter, loc, flatTy, vec1,
179 auto newA = mlir::vector::ShapeCastOp::create(rewriter, loc, Ty, shuffleLo);
180 auto newB = mlir::vector::ShapeCastOp::create(rewriter, loc, Ty, shuffleHi);
182 rewriteUses(opA->
getResult(0), newA.getResult(), contractA, rewriter);
183 rewriteUses(opB->
getResult(0), newB.getResult(), contractB, rewriter);
217struct VectorContractToPackedTypeDotProduct
222 matchAndRewriteAddContractOp(vector::ContractionOp contractOp,
223 PatternRewriter &rewriter)
const override {
225 VectorType lhsTy = contractOp.getLhsType();
226 if (!lhsTy.getElementType().isBF16() &&
227 !lhsTy.getElementType().isSignlessInteger(8))
229 contractOp,
"Only BF16/Int8 lowering is supported.");
231 unsigned int blockingFactor = lhsTy.getElementType().isBF16() ? 2 : 4;
234 contractOp.getIndexingMapsArray(), blockingFactor);
236 VectorType accTy = dyn_cast<VectorType>(contractOp.getAccType());
240 ArrayRef<int64_t> accShape = accTy.getShape();
241 llvm::SmallVector<int64_t> nonUnitDimAcc;
242 llvm::copy_if(accShape, std::back_inserter(nonUnitDimAcc),
243 [](int64_t dim) {
return dim != 1; });
244 if (nonUnitDimAcc.size() != 1)
246 contractOp,
"A or B should be a non-unit dim in acc.");
248 int64_t nonUnitDimValue = nonUnitDimAcc.front();
251 if (lhsTy.getElementType().isBF16() && nonUnitDimValue != 4 &&
252 nonUnitDimValue != 8 && nonUnitDimValue != 16)
254 contractOp,
"BF16 dot-product operation expects non-unit (LHR or "
255 "RHS) dim and acc dim of size 4/8/16.");
257 if (lhsTy.getElementType().isSignlessInteger(8) && nonUnitDimValue != 4 &&
258 nonUnitDimValue != 8 && nonUnitDimValue != 16 &&
259 nonUnitDimAcc.front() == nonUnitDimValue)
261 contractOp,
"Int8 dot-product operation expects non-unit (LHR or "
262 "RHS) dim and acc dim of size 4/8/16.");
264 ArrayRef<int64_t> lhsShape = lhsTy.getShape();
265 llvm::SmallVector<int64_t> nonUnitDimLhs;
266 llvm::copy_if(lhsShape, std::back_inserter(nonUnitDimLhs),
267 [](int64_t dim) {
return dim != 1; });
269 VectorType rhsTy = contractOp.getRhsType();
270 ArrayRef<int64_t> rhsShape = rhsTy.getShape();
271 llvm::SmallVector<int64_t> nonUnitDimRhs;
272 llvm::copy_if(rhsShape, std::back_inserter(nonUnitDimRhs),
273 [](int64_t dim) {
return dim != 1; });
275 if ((nonUnitDimLhs.size() - 1) > 0 && (nonUnitDimRhs.size() - 1) > 0)
277 "Excepts unit dimensions for either "
278 "LHS or RHS shape.");
280 if ((nonUnitDimLhs.size() - 1) != 1 && (nonUnitDimRhs.size() - 1) != 1)
283 "Excepts a one non-unit A/B dimension for either LHS or RHS shape.");
285 bool rhsHasMultipleNonUnitDims = (nonUnitDimRhs.size() - 1) > 0;
286 int64_t extraFlatDim = rhsHasMultipleNonUnitDims ? nonUnitDimLhs.front()
287 : nonUnitDimRhs.front();
289 if (!isVnni && (extraFlatDim != blockingFactor))
291 contractOp,
"The K or reduction dim for flat layout should be 2/4.");
293 if ((lhsTy.getElementType().isBF16() && !accTy.getElementType().isF32()) ||
294 (lhsTy.getElementType().isSignlessInteger(8) &&
295 !accTy.getElementType().isSignlessInteger(32)))
297 "Only F32 for BF16 or Int32 for Int8 "
298 "accumulation type is supported.");
300 Value unitDimOperand =
301 rhsHasMultipleNonUnitDims ? contractOp.getLhs() : contractOp.getRhs();
302 Value nonUnitDimOperand =
303 rhsHasMultipleNonUnitDims ? contractOp.getRhs() : contractOp.getLhs();
308 vector::ContractionOp pairContractOp;
309 Operation *nextOp = contractOp;
310 while ((nextOp = nextOp->getNextNode())) {
311 auto contOp = dyn_cast<vector::ContractionOp>(nextOp);
317 rhsHasMultipleNonUnitDims,
319 pairContractOp = contOp;
326 Operation *accWrite =
329 if (!pairContractOp &&
330 (!isNonUnitDimOperandShuffled(nonUnitDimOperand) || accWrite))
332 "Could not find a contract pair");
338 Operation *accReadOp0 =
340 Operation *accReadOp1 =
345 Operation *resultWriteOp0 =
347 Operation *resultWriteOp1 =
350 if (!accReadOp0 || !accReadOp1)
352 contractOp,
"Operands doesn't have load or transfer_read or "
353 "dense constant attribute as its parent op");
355 if (!resultWriteOp0 || !resultWriteOp1)
358 "The use of contract operations are neither vector.store "
359 "or transfer_write or has multiple users.");
361 if (contractOp->getBlock() == accReadOp1->
getBlock() &&
362 contractOp->isBeforeInBlock(accReadOp1))
365 "The load/read operation of pair contract operation is "
366 "after the contractOp");
368 if (pairContractOp->getBlock() == resultWriteOp0->
getBlock() &&
371 contractOp,
"The store/write operation of contract operation is "
372 "before the pair contract operation");
374 if (!isa<arith::ConstantOp>(accReadOp0)) {
377 rewriter, accReadOp0, accReadOp1, contractOp, pairContractOp,
378 nonUnitDimValue, accTy);
382 contractOp,
"Accumulator read is not by transfer_read or load");
387 rewriter, contractOp.getResult(), pairContractOp.getResult(),
388 nonUnitDimValue, accTy);
393 "Write to accumulator is not by transfer_write or store");
396 if (!isNonUnitDimOperandShuffled(nonUnitDimOperand)) {
397 Value nonUnitDimOperandPairContract = rhsHasMultipleNonUnitDims
398 ? pairContractOp.getRhs()
399 : pairContractOp.getLhs();
402 Operation *nonUnitDimReadOp =
404 Operation *nonUnitDimReadOpPairContract =
407 if (!nonUnitDimReadOp || !nonUnitDimReadOpPairContract)
409 contractOp,
"Could not find a valid contract pair");
411 VectorType nonUnitDimTy = rhsHasMultipleNonUnitDims
412 ? contractOp.getRhsType()
413 : contractOp.getLhsType();
415 packNonUnitDimOperandToVNNI(
416 rewriter, nonUnitDimReadOp, nonUnitDimReadOpPairContract,
417 contractOp, pairContractOp, blockingFactor * nonUnitDimValue,
420 nonUnitDimOperand = rhsHasMultipleNonUnitDims ? contractOp.getRhs()
421 : contractOp.getLhs();
426 auto loc = contractOp.getLoc();
427 auto castAcc = vector::ShapeCastOp::create(
429 VectorType::get(nonUnitDimAcc.front(), accTy.getElementType()),
430 contractOp.getAcc());
432 VectorType nonUnitDimTy = rhsHasMultipleNonUnitDims
433 ? contractOp.getRhsType()
434 : contractOp.getLhsType();
435 VectorType unitDimTy = rhsHasMultipleNonUnitDims ? contractOp.getLhsType()
436 : contractOp.getRhsType();
440 auto castNonUnitDim = vector::ShapeCastOp::create(
442 VectorType::get(blockingFactor * nonUnitDimValue,
443 nonUnitDimTy.getElementType()),
446 auto castUnitDim = vector::ShapeCastOp::create(
448 VectorType::get(blockingFactor, unitDimTy.getElementType()),
450 auto bitcastUnitDim = vector::BitCastOp::create(
453 auto broadcastUnitDim = vector::BroadcastOp::create(
457 auto bitcastUnitDimPkType = vector::BitCastOp::create(
458 rewriter, loc, castNonUnitDim.getResult().getType(), broadcastUnitDim);
460 if (lhsTy.getElementType().isBF16()) {
461 dp = x86::avx512::DotBF16Op::create(
463 VectorType::get(nonUnitDimValue, rewriter.
getF32Type()), castAcc,
464 bitcastUnitDimPkType, castNonUnitDim);
467 if (lhsTy.getElementType().isSignlessInteger(8)) {
468 if (nonUnitDimAcc.front() == 16) {
469 dp = x86::avx10::AVX10DotInt8Op::create(
472 castAcc, bitcastUnitDimPkType, castNonUnitDim);
474 dp = x86::avx::DotInt8Op::create(
477 castAcc, bitcastUnitDimPkType, castNonUnitDim);
484 auto castDp = vector::ShapeCastOp::create(rewriter, loc, accTy, dp);
493 patterns.
add<VectorContractToPackedTypeDotProduct>(patterns.
getContext());
static AffineMap getMinorIdentityMap(unsigned dims, unsigned results, MLIRContext *context)
Returns an identity affine map (d0, ..., dn) -> (dp, ..., dn) on the most minor dimensions.
DenseI64ArrayAttr getDenseI64ArrayAttr(ArrayRef< int64_t > values)
IntegerType getIntegerType(unsigned width)
MLIRContext * getContext() const
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
void setInsertionPoint(Block *block, Block::iterator insertPoint)
Set the insertion point to the specified location.
void setInsertionPointAfter(Operation *op)
Sets the insertion point to the node after the specified operation, which will cause subsequent inser...
This class represents a single result from folding an operation.
This class represents an operand of an operation.
Operation is the basic unit of execution within MLIR.
Value getOperand(unsigned idx)
bool isBeforeInBlock(Operation *other)
Given an operation 'other' that is within the same parent block, return whether the current operation...
Block * getBlock()
Returns the operation block that contains this operation.
OpResult getResult(unsigned idx)
Get the 'idx'th result of this operation.
Location getLoc()
The source location the operation was defined or derived from.
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,...
void modifyOpInPlace(Operation *root, CallableT &&callable)
This method is a utility wrapper around an in-place modification of an operation.
void moveOpAfter(Operation *op, Operation *existingOp)
Unlink this operation from its current block and insert it right after existingOp which may be in the...
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
use_range getUses() const
Returns a range of all uses, which is useful for iterating over all uses.
Operation * getDefiningOp() const
If this value is the result of an operation, return the operation that defines it.
static ConstantIndexOp create(OpBuilder &builder, Location location, int64_t value)
LogicalResult shuffleBeforeWriteLikeOp(PatternRewriter &rewriter, Value contractARes, Value contractBRes, int64_t nonUnitDimAcc, VectorType accTy)
Operation * traceToVectorWriteLikeUserOperation(Value v)
void populateVectorContractToPackedTypeDotProductPatterns(RewritePatternSet &patterns)
bool isInVnniLayout(Operation *op, llvm::ArrayRef< AffineMap > indexingMaps, std::optional< unsigned > blockingFactor=std::nullopt)
Operation * traceToVectorReadLikeParentOperation(Value v)
LogicalResult shuffleAfterReadLikeOp(PatternRewriter &rewriter, Operation *opA, Operation *opB, vector::ContractionOp contractA, vector::ContractionOp contractB, int64_t nonUnitDimAcc, VectorType accTy)
bool validatePairVectorContract(vector::ContractionOp contractOp, vector::ContractionOp pairContOp, bool rhsHasMultipleNonUnitDims, int64_t nonUnitDimValue)
Include the generated interface declarations.
detail::DenseArrayAttrImpl< int64_t > DenseI64ArrayAttr
Value getValueOrCreateConstantIndexOp(OpBuilder &b, Location loc, OpFoldResult ofr)
Converts an OpFoldResult to a Value.
Convenience wrapper for patterns that are only intended for default contractions, i....