41 using OpRewritePattern<tensor::PadOp>::OpRewritePattern;
43 LogicalResult matchAndRewrite(tensor::PadOp padOp,
44 PatternRewriter &rewriter)
const override {
46 Value padValue = padOp.getConstantPaddingValue();
52 Value source = padOp.getSource();
56 padOp,
"expected source to be linalg.generic op");
59 if (linalgOp.getNumLoops() != linalgOp.getNumParallelLoops()) {
61 padOp,
"only supported for ops with all parallel iterator types");
65 resultShape.size() != 1) {
67 padOp,
"failed to get shape of pad op result");
70 Location loc = padOp.
getLoc();
73 RankedTensorType padResultType = padOp.getResultType();
74 auto resultSizes = resultShape[0];
75 auto emptyTensor = tensor::EmptyOp::create(rewriter, loc, resultSizes,
76 padResultType.getElementType());
81 auto fillTensor = linalg::FillOp::create(rewriter, loc, padValue,
82 emptyTensor.getResult());
88 unsigned resultNumber = cast<OpResult>(source).getResultNumber();
89 SmallVector<OpFoldResult> offsets = padOp.getMixedLowPad();
90 SmallVector<OpFoldResult> sizes;
91 sizes.reserve(offsets.size());
92 for (
const auto &shape :
93 llvm::enumerate(cast<RankedTensorType>(source.
getType()).getShape())) {
94 if (ShapedType::isDynamic(shape.value())) {
96 tensor::DimOp::create(rewriter, loc, source, shape.index())
102 SmallVector<OpFoldResult> strides(offsets.size(), rewriter.
getIndexAttr(1));
103 auto slice = tensor::ExtractSliceOp::create(
104 rewriter, loc, fillTensor.getResult(0), offsets, sizes, strides);
108 cast<linalg::GenericOp>(rewriter.
clone(*linalgOp.getOperation()));
109 clonedOp.setDpsInitOperand(resultNumber, slice.getResult());
113 padOp, clonedOp.getResult(resultNumber), fillTensor.getResult(0),
114 offsets, sizes, strides);
IntegerAttr getIndexAttr(int64_t value)
Operation * clone(Operation &op, IRMapping &mapper)
Creates a deep copy of the specified operation, remapping any operands that use values outside of the...
Location getLoc()
The source location the operation was defined or derived from.
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...
Type getType() const
Return the type of this value.
Operation * getDefiningOp() const
If this value is the result of an operation, return the operation that defines it.
void populateFuseTensorPadWithProducerLinalgOpPatterns(RewritePatternSet &patterns)
Pattern to fuse a tensor.pad operation with the producer of its source, if the producer is a linalg o...
Include the generated interface declarations.
LogicalResult reifyResultShapes(OpBuilder &b, Operation *op, ReifiedRankedShapedTypeDims &reifiedReturnShapes)
Reify the shape of the result of an operation (typically in terms of the shape of its operands).
SmallVector< SmallVector< OpFoldResult > > ReifiedRankedShapedTypeDims
const FrozenRewritePatternSet & patterns
OpRewritePattern is a wrapper around RewritePattern that allows for matching and rewriting against an...