Skip to content

[mlir][linalg] Add pattern to propagate pack up through tensor.pad #82035

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 86 additions & 3 deletions mlir/lib/Dialect/Linalg/Transforms/DataLayoutPropagation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,88 @@ struct BubbleUpPackOpThroughGenericOpPattern
ControlPropagationFn controlFn;
};

/// Propagate a tensor.pack operation up through a tensor.pad. The idea is to
/// add as many zero padding dimensions in `high` and `low` based on the number
/// of point loops.
class BubbleUpPackThroughPadOp final : public OpRewritePattern<tensor::PackOp> {
public:
BubbleUpPackThroughPadOp(MLIRContext *context, ControlPropagationFn fun)
: OpRewritePattern<tensor::PackOp>(context), controlFn(std::move(fun)) {}

LogicalResult matchAndRewrite(tensor::PackOp packOp,
PatternRewriter &rewriter) const override {
auto padOp = packOp.getSource().getDefiningOp<tensor::PadOp>();
if (!padOp)
return failure();

// User controlled propagation function.
if (!controlFn(padOp))
return failure();

if (!padOp.getResult().hasOneUse())
return failure();

// TODO: Enable padding when the padding values are the same.
if (packOp.getPaddingValue())
return failure();

// Fail for non-constant padding values. The body of the pad could
// depend on the padding indices and/or properties of the padded
// tensor so for now we fail.
// TODO: Support non-constant padding values.
Value paddingVal = padOp.getConstantPaddingValue();
if (!paddingVal)
return failure();

if (!packOp.getDest().getDefiningOp<tensor::EmptyOp>())
return failure();

ArrayRef<int64_t> innerDimsPos = packOp.getInnerDimsPos();
ArrayRef<int64_t> outerDimsPerm = packOp.getOuterDimsPerm();

// Bail out if one of the padded dimension is a tiled one.
llvm::SmallBitVector paddedDims = padOp.getPaddedDims();
llvm::SmallBitVector innerDims(paddedDims.size());
for (int64_t dim : innerDimsPos)
innerDims.flip(dim);
if (paddedDims.anyCommon(innerDims))
return failure();

Location loc = padOp->getLoc();
OpBuilder::InsertionGuard guard(rewriter);
rewriter.setInsertionPoint(padOp);

auto empty = tensor::PackOp::createDestinationTensor(
rewriter, loc, padOp.getSource(), packOp.getMixedTiles(), innerDimsPos,
outerDimsPerm);
Value packedSource = rewriter.create<tensor::PackOp>(
loc, padOp.getSource(), empty, innerDimsPos, packOp.getMixedTiles(),
/*padding=*/std::nullopt, outerDimsPerm);

// If we have `outer_dims_perms` we need to adjust the padded dimensions.
SmallVector<OpFoldResult> lowPad = padOp.getMixedLowPad();
SmallVector<OpFoldResult> highPad = padOp.getMixedHighPad();
if (!outerDimsPerm.empty()) {
applyPermutationToVector<OpFoldResult>(lowPad, outerDimsPerm);
applyPermutationToVector<OpFoldResult>(highPad, outerDimsPerm);
}
// The tiled dimensions were verified to be unpadded above, so here we
// just append 0 for the inner tile dimensions.
size_t pointLoopsSize = innerDimsPos.size();
lowPad.append(pointLoopsSize, rewriter.getIndexAttr(0));
highPad.append(pointLoopsSize, rewriter.getIndexAttr(0));

auto newPadOp = rewriter.create<tensor::PadOp>(
loc, /*result=*/Type(), packedSource, lowPad, highPad, paddingVal,
padOp.getNofold());
rewriter.replaceOp(packOp, newPadOp.getResult());
return success();
}

private:
ControlPropagationFn controlFn;
};

// TODO: Relax this restriction. We should unpack a generic op also
// in the presence of multiple unpack ops as producers.
/// Return the unpacked operand, if present, for the current generic op.
Expand Down Expand Up @@ -690,7 +772,8 @@ struct PushDownUnPackThroughPadOp : public OpRewritePattern<tensor::PadOp> {
void mlir::linalg::populateDataLayoutPropagationPatterns(
RewritePatternSet &patterns,
const ControlPropagationFn &controlPackUnPackPropagation) {
patterns.insert<BubbleUpPackOpThroughGenericOpPattern,
PushDownUnPackOpThroughGenericOp, PushDownUnPackThroughPadOp>(
patterns.getContext(), controlPackUnPackPropagation);
patterns
.insert<BubbleUpPackOpThroughGenericOpPattern, BubbleUpPackThroughPadOp,
PushDownUnPackOpThroughGenericOp, PushDownUnPackThroughPadOp>(
patterns.getContext(), controlPackUnPackPropagation);
}
Loading