25#define DEBUG_TYPE "vector-broadcast-lowering"
39 LogicalResult matchAndRewrite(vector::BroadcastOp op,
41 auto loc = op.getLoc();
42 VectorType dstType = op.getResultVectorType();
43 VectorType srcType = dyn_cast<VectorType>(op.getSourceType());
44 Type eltType = dstType.getElementType();
49 op,
"broadcast from scalar already in lowered form");
52 int64_t srcRank = srcType.getRank();
53 int64_t dstRank = dstType.getRank();
56 if (srcType.getNumElements() == 1 && !srcType.isScalable()) {
58 Value ext = vector::ExtractOp::create(rewriter, loc, op.getSource(),
60 assert(!isa<VectorType>(ext.
getType()) &&
"expected scalar");
74 if (srcRank < dstRank) {
76 if (dstType.getScalableDims()[0])
78 op,
"Vector broadcasting over a scalable dimension is not "
79 "currently supported");
82 vector::BroadcastOp::create(rewriter, loc, resType, op.getSource());
83 Value result = ub::PoisonOp::create(rewriter, loc, dstType);
84 for (
int64_t d = 0, dim = dstType.getDimSize(0); d < dim; ++d)
85 result = vector::InsertOp::create(rewriter, loc, bcst,
result, d);
91 assert(srcRank == dstRank);
93 for (
int64_t r = 0; r < dstRank; r++)
94 if (srcType.getDimSize(r) != dstType.getDimSize(r)) {
121 VectorType::get(dstType.getShape().drop_front(), eltType,
122 dstType.getScalableDims().drop_front());
128 if (m != 0 && dstType.getScalableDims()[0]) {
133 Value result = ub::PoisonOp::create(rewriter, loc, dstType);
136 Value ext = vector::ExtractOp::create(rewriter, loc, op.getSource(), 0);
137 Value bcst = vector::BroadcastOp::create(rewriter, loc, resType, ext);
138 for (
int64_t d = 0, dim = dstType.getDimSize(0); d < dim; ++d)
139 result = vector::InsertOp::create(rewriter, loc, bcst,
result, d);
142 for (
int64_t d = 0, dim = dstType.getDimSize(0); d < dim; ++d) {
143 Value ext = vector::ExtractOp::create(rewriter, loc, op.getSource(), d);
144 Value bcst = vector::BroadcastOp::create(rewriter, loc, resType, ext);
145 result = vector::InsertOp::create(rewriter, loc, bcst,
result, d);
156 patterns.
add<BroadcastOpLowering>(patterns.
getContext(), benefit);
This class represents the benefit of a pattern match in a unitless scheme that ranges from 0 (very li...
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,...
OpTy replaceOpWithNewOp(Operation *op, Args &&...args)
Replace the results of the given (original) op with a new op that is created without verification (re...
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
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.
This is a builder type that keeps local references to arguments.
Builder & dropDim(unsigned pos)
Erase a dim from shape @pos.
void populateVectorBroadcastLoweringPatterns(RewritePatternSet &patterns, PatternBenefit benefit=1)
Populate the pattern set with the following patterns:
Include the generated interface declarations.
OpRewritePattern is a wrapper around RewritePattern that allows for matching and rewriting against an...