35 #define DEBUG_TYPE "vector-broadcast-lowering"
42 static bool isValidKind(
bool isInt, vector::CombiningKind kind) {
43 using vector::CombiningKind;
44 enum class KindType { FLOAT, INT, INVALID };
45 KindType type{KindType::INVALID};
47 case CombiningKind::MINNUMF:
48 case CombiningKind::MINIMUMF:
49 case CombiningKind::MAXNUMF:
50 case CombiningKind::MAXIMUMF:
51 type = KindType::FLOAT;
54 case CombiningKind::MINSI:
55 case CombiningKind::MAXUI:
56 case CombiningKind::MAXSI:
57 case CombiningKind::AND:
58 case CombiningKind::OR:
59 case CombiningKind::XOR:
62 case CombiningKind::ADD:
63 case CombiningKind::MUL:
64 type = isInt ? KindType::INT : KindType::FLOAT;
67 bool isValidIntKind = (type == KindType::INT) && isInt;
68 bool isValidFloatKind = (type == KindType::FLOAT) && (!isInt);
69 return (isValidIntKind || isValidFloatKind);
114 LogicalResult matchAndRewrite(vector::ScanOp scanOp,
116 auto loc = scanOp.getLoc();
117 VectorType destType = scanOp.getDestType();
119 auto elType = destType.getElementType();
120 bool isInt = elType.isIntOrIndex();
127 int64_t reductionDim = scanOp.getReductionDim();
128 bool inclusive = scanOp.getInclusive();
129 int64_t destRank = destType.getRank();
130 VectorType initialValueType = scanOp.getInitialValueType();
131 int64_t initialValueRank = initialValueType.getRank();
134 reductionShape[reductionDim] = 1;
139 sizes[reductionDim] = 1;
143 Value lastOutput, lastInput;
144 for (
int i = 0; i < destShape[reductionDim]; i++) {
145 offsets[reductionDim] = i;
147 Value input = rewriter.
create<vector::ExtractStridedSliceOp>(
148 loc, reductionType, scanOp.getSource(), scanOffsets, scanSizes,
155 if (initialValueRank == 0) {
157 output = rewriter.
create<vector::BroadcastOp>(
158 loc, input.
getType(), scanOp.getInitialValue());
160 output = rewriter.
create<vector::ShapeCastOp>(
161 loc, input.
getType(), scanOp.getInitialValue());
165 Value y = inclusive ? input : lastInput;
169 result = rewriter.
create<vector::InsertStridedSliceOp>(
170 loc, output, result, offsets, strides);
176 if (initialValueRank == 0) {
177 Value v = rewriter.
create<vector::ExtractOp>(loc, lastOutput, 0);
179 rewriter.
create<vector::BroadcastOp>(loc, initialValueType, v);
181 reduction = rewriter.
create<vector::ShapeCastOp>(loc, initialValueType,
185 rewriter.
replaceOp(scanOp, {result, reduction});
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)
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
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...
virtual void replaceOp(Operation *op, ValueRange newValues)
Replace the results of the given (original) operation with the specified list of values (replacements...
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.
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.
const FrozenRewritePatternSet & patterns
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
OpRewritePattern is a wrapper around RewritePattern that allows for matching and rewriting against an...
OpRewritePattern(MLIRContext *context, PatternBenefit benefit=1, ArrayRef< StringRef > generatedNames={})
Patterns must specify the root operation name they match against, and can also specify the benefit of...