25#define DEBUG_TYPE "vector-broadcast-lowering"
32static bool isValidKind(
bool isInt, vector::CombiningKind kind) {
33 using vector::CombiningKind;
34 enum class KindType { FLOAT, INT, INVALID };
35 KindType type{KindType::INVALID};
37 case CombiningKind::MINNUMF:
38 case CombiningKind::MINIMUMF:
39 case CombiningKind::MAXNUMF:
40 case CombiningKind::MAXIMUMF:
41 case CombiningKind::MAXIMUMNUMF:
42 case CombiningKind::MINIMUMNUMF:
43 type = KindType::FLOAT;
45 case CombiningKind::MINUI:
46 case CombiningKind::MINSI:
47 case CombiningKind::MAXUI:
48 case CombiningKind::MAXSI:
49 case CombiningKind::AND:
50 case CombiningKind::OR:
51 case CombiningKind::XOR:
54 case CombiningKind::ADD:
55 case CombiningKind::MUL:
56 type = isInt ? KindType::INT : KindType::FLOAT;
59 bool isValidIntKind = (type == KindType::INT) && isInt;
60 bool isValidFloatKind = (type == KindType::FLOAT) && (!isInt);
61 return (isValidIntKind || isValidFloatKind);
106 LogicalResult matchAndRewrite(vector::ScanOp scanOp,
107 PatternRewriter &rewriter)
const override {
108 auto loc = scanOp.getLoc();
109 VectorType destType = scanOp.getDestType();
110 ArrayRef<int64_t> destShape = destType.getShape();
111 auto elType = destType.getElementType();
112 bool isInt = elType.isIntOrIndex();
116 int64_t reductionDim = scanOp.getReductionDim();
117 bool inclusive = scanOp.getInclusive();
118 int64_t destRank = destType.getRank();
119 VectorType initialValueType = scanOp.getInitialValueType();
120 int64_t initialValueRank = initialValueType.getRank();
122 SmallVector<int64_t> reductionShape(destShape);
123 SmallVector<bool> reductionScalableDims(destType.getScalableDims());
127 if (reductionScalableDims[reductionDim])
129 scanOp,
"Trying to reduce scalable dimension - not yet supported!");
131 VectorType resType = destType;
132 Value
result = arith::ConstantOp::create(rewriter, loc, resType,
137 reductionShape[reductionDim] = 1;
138 VectorType reductionType =
139 VectorType::get(reductionShape, elType, reductionScalableDims);
141 SmallVector<int64_t> offsets(destRank, 0);
142 SmallVector<int64_t> strides(destRank, 1);
143 SmallVector<int64_t> sizes(destShape);
144 sizes[reductionDim] = 1;
148 Value lastOutput, lastInput;
149 for (
int i = 0; i < destShape[reductionDim]; i++) {
150 offsets[reductionDim] = i;
152 Value input = vector::ExtractStridedSliceOp::create(
153 rewriter, loc, reductionType, scanOp.getSource(), scanOffsets,
154 scanSizes, scanStrides);
160 if (initialValueRank == 0) {
162 output = vector::BroadcastOp::create(rewriter, loc, input.
getType(),
163 scanOp.getInitialValue());
165 output = vector::ShapeCastOp::create(rewriter, loc, input.
getType(),
166 scanOp.getInitialValue());
170 Value y = inclusive ? input : lastInput;
174 result = vector::InsertStridedSliceOp::create(rewriter, loc, output,
175 result, offsets, strides);
181 if (initialValueRank == 0) {
182 Value v = vector::ExtractOp::create(rewriter, loc, lastOutput, 0);
184 vector::BroadcastOp::create(rewriter, loc, initialValueType, v);
186 reduction = vector::ShapeCastOp::create(rewriter, loc, initialValueType,
198 patterns.
add<ScanToArithOps>(patterns.
getContext(), benefit);
static bool isValidKind(bool isInt, vector::CombiningKind kind)
This function checks to see if the vector combining kind is consistent with the integer or float elem...
TypedAttr getZeroAttr(Type type)
ArrayAttr getI64ArrayAttr(ArrayRef< int64_t > values)
This class represents the benefit of a pattern match in a unitless scheme that ranges from 0 (very li...
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,...
Type getType() const
Return the type of this value.
Value makeArithReduction(OpBuilder &b, Location loc, CombiningKind kind, Value v1, Value acc, arith::FastMathFlagsAttr fastmath=nullptr, Value mask=nullptr)
Returns the result value of reducing two scalar/vector values with the corresponding arith operation.
void populateVectorScanLoweringPatterns(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...