Skip to content

Commit

Permalink
[mlir][linalg] Fix invalid IR in FoldInsertPadIntoFill (#74418)
Browse files Browse the repository at this point in the history
`FoldInsertPadIntoFill` used to generate an invalid
`tensor.insert_slice` op:
```
error: expected type to be 'tensor<?x?x?xf32>' or a rank-reduced version. (size mismatch)
```

This commit fixes tests such as
`mlir/test/Dialect/Linalg/canonicalize.mlir` when verifying the IR after
each pattern application (#74270).
  • Loading branch information
matthias-springer authored Dec 19, 2023
1 parent 3a087c1 commit 9b21866
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions mlir/lib/Dialect/Linalg/IR/LinalgOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -716,11 +716,16 @@ struct FoldInsertPadIntoFill : public OpRewritePattern<tensor::InsertSliceOp> {
rewriter, loc, addMap, {std::get<0>(p), std::get<1>(p)}));
}

RankedTensorType srcPadType = srcPadOp.getSourceType();
SmallVector<OpFoldResult, 4> newSizes;
for (int i = 0, e = srcPadOp.getSourceType().getRank(); i < e; ++i) {
newSizes.push_back(
rewriter.create<tensor::DimOp>(loc, srcPadOp.getSource(), i)
.getResult());
for (int i = 0, e = srcPadType.getRank(); i < e; ++i) {
if (srcPadType.isDynamicDim(i)) {
newSizes.push_back(
rewriter.create<tensor::DimOp>(loc, srcPadOp.getSource(), i)
.getResult());
} else {
newSizes.push_back(rewriter.getIndexAttr(srcPadType.getDimSize(i)));
}
}

rewriter.replaceOpWithNewOp<tensor::InsertSliceOp>(
Expand Down

0 comments on commit 9b21866

Please sign in to comment.