@@ -1552,7 +1552,6 @@ LogicalResult InsertSliceOp::verify() {
15521552
15531553// / If we have two consecutive InsertSliceOp writing to the same slice, we
15541554// / can mutate the second InsertSliceOp's destination to the first one's.
1555- // / This works similarly when the second op is a ParallelInsertSliceOp.
15561555// /
15571556// / Example:
15581557// /
@@ -1568,9 +1567,8 @@ LogicalResult InsertSliceOp::verify() {
15681567// / ```
15691568// /
15701569// / This pattern works with both InsertSliceOp and ParallelInsertSliceOp.
1571- template <typename InsertOpTy>
1572- static LogicalResult foldInsertAfterInsertSlice (InsertOpTy insertOp) {
1573- auto prevInsertOp = insertOp.getDest ().template getDefiningOp <InsertOpTy>();
1570+ static LogicalResult foldInsertAfterInsertSlice (InsertSliceOp insertOp) {
1571+ auto prevInsertOp = insertOp.getDest ().getDefiningOp <InsertSliceOp>();
15741572
15751573 auto isSame = [](OpFoldResult a, OpFoldResult b) { return a == b; };
15761574 if (!prevInsertOp ||
@@ -1582,32 +1580,14 @@ static LogicalResult foldInsertAfterInsertSlice(InsertOpTy insertOp) {
15821580 return success ();
15831581}
15841582
1585- // / Same logic for folding InsertSliceOp and ParallelInsertSliceOp, the return
1586- // / type varies though so we wrap it in a FailureOr.
1587- // /
1588- // / This pattern works with both InsertSliceOp and ParallelInsertSliceOp.
1589- template <typename InsertOpTy>
1590- FailureOr<OpFoldResult> foldInsertOp (InsertOpTy insertOp, ArrayRef<Attribute>) {
1591- if (insertOp.getSourceType ().hasStaticShape () &&
1592- insertOp.getDestType ().hasStaticShape () &&
1593- insertOp.getSourceType () == insertOp.getDestType () &&
1594- succeeded (foldIdentityOffsetSizeAndStrideOpInterface (
1595- insertOp, insertOp.getDestType ())))
1596- return static_cast <OpFoldResult>(insertOp.getSource ());
1597- if (succeeded (foldInsertAfterInsertSlice (insertOp))) {
1598- // InsertSliceOp has 1 result but ParallelInsertSliceOp has none and should
1599- // return OpFoldResult().
1600- if (std::is_same<InsertOpTy, InsertSliceOp>::value)
1601- return static_cast <OpFoldResult>(insertOp->getResult (0 ));
1602- else
1603- return OpFoldResult ();
1604- }
1605- return failure ();
1606- }
1607-
1608- OpFoldResult InsertSliceOp::fold (ArrayRef<Attribute> operands) {
1609- auto maybeOpFoldResult = foldInsertOp (*this , operands);
1610- return failed (maybeOpFoldResult) ? OpFoldResult () : *maybeOpFoldResult;
1583+ OpFoldResult InsertSliceOp::fold (ArrayRef<Attribute>) {
1584+ if (getSourceType ().hasStaticShape () && getType ().hasStaticShape () &&
1585+ getSourceType () == getType () &&
1586+ succeeded (foldIdentityOffsetSizeAndStrideOpInterface (*this , getType ())))
1587+ return this ->getSource ();
1588+ if (succeeded (foldInsertAfterInsertSlice (*this )))
1589+ return getResult ();
1590+ return OpFoldResult ();
16111591}
16121592
16131593LogicalResult InsertSliceOp::reifyResultShapes (
@@ -2319,58 +2299,6 @@ LogicalResult ParallelInsertSliceOp::verify() {
23192299 return produceSliceErrorMsg (result, *this , expectedType);
23202300}
23212301
2322- namespace {
2323- // / Pattern to rewrite a parallel_insert_slice op with constant arguments.
2324- class ParallelInsertSliceOpConstantArgumentFolder final
2325- : public OpRewritePattern<ParallelInsertSliceOp> {
2326- public:
2327- using OpRewritePattern<ParallelInsertSliceOp>::OpRewritePattern;
2328-
2329- LogicalResult matchAndRewrite (ParallelInsertSliceOp insertSliceOp,
2330- PatternRewriter &rewriter) const override {
2331- // No constant operand, just return.
2332- if (llvm::none_of (insertSliceOp.getOperands (), [](Value operand) {
2333- return matchPattern (operand, matchConstantIndex ());
2334- }))
2335- return failure ();
2336-
2337- // At least one of offsets/sizes/strides is a new constant.
2338- // Form the new list of operands and constant attributes from the
2339- // existing.
2340- SmallVector<OpFoldResult> mixedOffsets (insertSliceOp.getMixedOffsets ());
2341- SmallVector<OpFoldResult> mixedSizes (insertSliceOp.getMixedSizes ());
2342- SmallVector<OpFoldResult> mixedStrides (insertSliceOp.getMixedStrides ());
2343- canonicalizeSubViewPart (mixedOffsets, ShapedType::isDynamicStrideOrOffset);
2344- canonicalizeSubViewPart (mixedSizes, ShapedType::isDynamic);
2345- canonicalizeSubViewPart (mixedStrides, ShapedType::isDynamicStrideOrOffset);
2346-
2347- // Create the new op in canonical form.
2348- auto sourceType =
2349- tensor::ExtractSliceOp::inferCanonicalRankReducedResultType (
2350- insertSliceOp.getSourceType ().getRank (),
2351- insertSliceOp.getDestType (), mixedOffsets, mixedSizes,
2352- mixedStrides);
2353- Value toInsert = insertSliceOp.getSource ();
2354- if (sourceType != insertSliceOp.getSourceType ()) {
2355- OpBuilder::InsertionGuard g (rewriter);
2356- rewriter.setInsertionPoint (insertSliceOp->getParentOp ());
2357- toInsert = rewriter.create <tensor::CastOp>(insertSliceOp.getLoc (),
2358- sourceType, toInsert);
2359- }
2360- rewriter.replaceOpWithNewOp <ParallelInsertSliceOp>(
2361- insertSliceOp, toInsert, insertSliceOp.getDest (), mixedOffsets,
2362- mixedSizes, mixedStrides);
2363- return success ();
2364- }
2365- };
2366- } // namespace
2367-
2368- LogicalResult
2369- ParallelInsertSliceOp::fold (ArrayRef<Attribute> operands,
2370- SmallVectorImpl<OpFoldResult> &results) {
2371- return foldInsertOp (*this , operands);
2372- }
2373-
23742302void ParallelInsertSliceOp::getCanonicalizationPatterns (
23752303 RewritePatternSet &results, MLIRContext *context) {
23762304 results.add <InsertSliceOpConstantArgumentFolder<ParallelInsertSliceOp>,
0 commit comments