Skip to content

Commit 1ee0d60

Browse files
committed
[mlir][tensor] Remove incorrect parallel_insert_slice folder
parallel_insert_slice doesn't return a value therefore we shouldn't try to fold the result. The insert folding don't apply to this op. The current folding would cause pattern rewrite to not be able to converge. Differential Revision: https://reviews.llvm.org/D132668
1 parent be19952 commit 1ee0d60

3 files changed

Lines changed: 31 additions & 83 deletions

File tree

‎mlir/include/mlir/Dialect/Tensor/IR/TensorOps.td‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1207,7 +1207,6 @@ def Tensor_ParallelInsertSliceOp : Tensor_Op<"parallel_insert_slice", [
12071207
];
12081208

12091209
let hasCanonicalizer = 1;
1210-
let hasFolder = 1;
12111210
let hasVerifier = 1;
12121211
}
12131212

‎mlir/lib/Dialect/Tensor/IR/TensorOps.cpp‎

Lines changed: 10 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -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

16131593
LogicalResult 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-
23742302
void ParallelInsertSliceOp::getCanonicalizationPatterns(
23752303
RewritePatternSet &results, MLIRContext *context) {
23762304
results.add<InsertSliceOpConstantArgumentFolder<ParallelInsertSliceOp>,

‎mlir/test/Dialect/Tensor/canonicalize.mlir‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,3 +1466,24 @@ func.func @canonicalize_parallel_insert_slice_indices(
14661466
}
14671467
return %2 : tensor<?x?xf32>
14681468
}
1469+
1470+
// -----
1471+
1472+
// CHECK-LABEL: func.func @dont_fold_parallel_insert_slice(
1473+
// CHECK-SAME: %[[arg0:[0-9a-z]*]]: tensor<1x5xf32>,
1474+
// CHECK-SAME: %[[arg1:[0-9a-z]*]]: tensor<1x5xf32>)
1475+
func.func @dont_fold_parallel_insert_slice(
1476+
%arg0 : tensor<1x5xf32>, %arg1: tensor<1x5xf32>) -> tensor<1x5xf32>
1477+
{
1478+
%c0 = arith.constant 0 : index
1479+
%c1 = arith.constant 1 : index
1480+
// CHECK: scf.foreach_thread () in () -> (tensor<1x5xf32>) {
1481+
// CHECK-NEXT: scf.foreach_thread.perform_concurrently {
1482+
// CHECK-NEXT: tensor.parallel_insert_slice %[[arg0]] into %[[arg1]][0, 0] [1, 5] [1, 1] : tensor<1x5xf32> into tensor<1x5xf32>
1483+
%2 = scf.foreach_thread () in () -> (tensor<1x5xf32>) {
1484+
scf.foreach_thread.perform_concurrently {
1485+
tensor.parallel_insert_slice %arg0 into %arg1[%c0, %c0] [1, 5] [%c1, %c1] : tensor<1x5xf32> into tensor<1x5xf32>
1486+
}
1487+
}
1488+
return %2 : tensor<1x5xf32>
1489+
}

0 commit comments

Comments
 (0)