26struct DropRedundantRankExpansionOnExtractSliceOfInsertSlice
30 LogicalResult matchAndRewrite(ExtractSliceOp extractSliceOp,
31 PatternRewriter &rewriter)
const override {
33 llvm::SmallBitVector droppedDims = extractSliceOp.getDroppedDims();
34 if (droppedDims.none())
39 extractSliceOp.getSource().getDefiningOp<InsertSliceOp>();
42 llvm::SmallBitVector expandedDims = insertSliceOp.getDroppedDims();
46 if (expandedDims != droppedDims)
50 if (!insertSliceOp->hasOneUse())
59 OpBuilder::InsertionGuard g(rewriter);
61 SmallVector<OpFoldResult> newOffsets, newSizes, newStrides;
62 for (int64_t i = 0, e = extractSliceOp.getSourceType().getRank(); i < e;
64 if (droppedDims.test(i))
66 newOffsets.push_back(extractSliceOp.getMixedOffsets()[i]);
67 newSizes.push_back(extractSliceOp.getMixedSizes()[i]);
68 newStrides.push_back(extractSliceOp.getMixedStrides()[i]);
71 extractSliceOp, insertSliceOp.getSource(), newOffsets,
72 newSizes, newStrides);
73 rewriter.
eraseOp(insertSliceOp);
94struct DropRedundantRankExpansionOnInsertSliceOfExtractSlice final
96 using OpRewritePattern<tensor::InsertSliceOp>::OpRewritePattern;
98 LogicalResult matchAndRewrite(tensor::InsertSliceOp insertSliceOp,
99 PatternRewriter &rewriter)
const override {
100 auto extractSliceOp =
101 insertSliceOp.getSource().getDefiningOp<tensor::ExtractSliceOp>();
102 if (!extractSliceOp) {
104 "source is not extract_slice");
108 if (!extractSliceOp->hasOneUse()) {
110 "source has multi-uses");
116 "insert_slice is not cast-like");
119 llvm::SmallBitVector extractDroppedDims = extractSliceOp.getDroppedDims();
120 llvm::SmallBitVector insertDroppedDims = insertSliceOp.getDroppedDims();
122 if (extractDroppedDims.size() < insertDroppedDims.size()) {
124 "insert_slice expands more dims");
131 unsigned insertDimPos = 0;
132 for (
unsigned extractDimPos = 0; extractDimPos < extractDroppedDims.size();
135 if (insertDimPos == insertDroppedDims.size())
138 bool isExtractDropped = extractDroppedDims[extractDimPos];
139 bool isInsertDropped = insertDroppedDims[insertDimPos];
142 if (isExtractDropped == isInsertDropped) {
144 }
else if (!isExtractDropped && isInsertDropped) {
147 "insert_slice drops more unit dims");
154 if (insertDimPos != insertDroppedDims.size()) {
156 "insert_slice has unmatched dims");
160 insertSliceOp, insertSliceOp.getType(), extractSliceOp.getSource(),
161 extractSliceOp.getMixedOffsets(), extractSliceOp.getMixedSizes(),
162 extractSliceOp.getMixedStrides());
163 rewriter.
eraseOp(extractSliceOp);
172 patterns.
add<DropRedundantRankExpansionOnExtractSliceOfInsertSlice,
173 DropRedundantRankExpansionOnInsertSliceOfExtractSlice>(
void setInsertionPoint(Block *block, Block::iterator insertPoint)
Set the insertion point to the specified location.
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 eraseOp(Operation *op)
This method erases an operation that is known to have no uses.
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...
bool isCastLikeInsertSliceOp(InsertSliceOp op)
A tensor.insert_slice is a cast-like operation if it merely rank-extends the source tensor or inserts...
void populateDropRedundantInsertSliceRankExpansionPatterns(RewritePatternSet &patterns)
Populates patterns with patterns that drop redundant tensor.insert_slice rank expansions.
Include the generated interface declarations.
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...