[llvm] [mlir] [mlir][tensor] Fold linalg transpose with tensor pack (PR #74206)
Han-Chung Wang via llvm-commits
llvm-commits at lists.llvm.org
Tue Dec 5 09:31:53 PST 2023
================
@@ -81,10 +82,82 @@ struct FoldUnpackWithExtractSliceOp : public OpRewritePattern<ExtractSliceOp> {
return success();
}
};
+
+/// Fold 'pack' -> 'transpose' into 'pack' since 'pack' already has transpose
+/// semantics.
+struct FoldProducerPackWithConsumerLinalgTransposeOp
+ : public OpRewritePattern<linalg::TransposeOp> {
+ using OpRewritePattern<linalg::TransposeOp>::OpRewritePattern;
+
+ LogicalResult matchAndRewrite(linalg::TransposeOp transposeOp,
+ PatternRewriter &rewriter) const override {
+ auto transposeInputTensor = transposeOp.getOperand(0);
+ auto packOp = transposeInputTensor.getDefiningOp<PackOp>();
+
+ if (!packOp)
+ return failure();
+
+ auto packOuterDimsPerm = packOp.getOuterDimsPerm();
+ auto transposePerm = transposeOp.getPermutation();
+ SmallVector<int64_t> newPackOuterDimsPermVec;
+
+ for (unsigned int i = 0; i < packOuterDimsPerm.size(); ++i)
+ newPackOuterDimsPermVec.push_back(packOuterDimsPerm[transposePerm[i]]);
+
+ Value output = packOp.createDestinationTensor(
+ rewriter, transposeOp.getLoc(), packOp.getSource(),
+ packOp.getMixedTiles(), packOp.getInnerDimsPos(),
+ static_cast<llvm::ArrayRef<int64_t>>(newPackOuterDimsPermVec));
+
+ rewriter.replaceOpWithNewOp<PackOp>(
+ transposeOp, packOp.getSource(), output, packOp.getInnerDimsPos(),
+ packOp.getMixedTiles(), /*paddingValue=*/std::nullopt,
----------------
hanhanW wrote:
I think the padding value is needed when the pack op has padding value operand?
https://github.com/llvm/llvm-project/pull/74206
More information about the llvm-commits
mailing list