feat: toy tutorial chapter 3.
Signed-off-by: jackfiled <xcrenchangjun@outlook.com>
This commit is contained in:
41
lib/HelloCombine.cpp
Normal file
41
lib/HelloCombine.cpp
Normal file
@@ -0,0 +1,41 @@
|
||||
//
|
||||
// Created by ricardo on 02/06/25.
|
||||
//
|
||||
|
||||
#include <mlir/IR/PatternMatch.h>
|
||||
#include "Dialect.h"
|
||||
#include "HelloCombine.inc"
|
||||
|
||||
|
||||
struct SimplifyRedundantTranspose final : mlir::OpRewritePattern<mlir::hello::TransposeOp>
|
||||
{
|
||||
explicit SimplifyRedundantTranspose(mlir::MLIRContext* context) : OpRewritePattern(
|
||||
context)
|
||||
{
|
||||
}
|
||||
|
||||
/// Transpose(Transpose(x)) = x
|
||||
mlir::LogicalResult matchAndRewrite(mlir::hello::TransposeOp op, mlir::PatternRewriter& rewriter) const override
|
||||
{
|
||||
mlir::Value transposeInput = op.getOperand();
|
||||
auto transposeInputOp = transposeInput.getDefiningOp<mlir::hello::TransposeOp>();
|
||||
|
||||
if (!transposeInputOp)
|
||||
{
|
||||
return mlir::failure();
|
||||
}
|
||||
|
||||
rewriter.replaceOp(op, {transposeInputOp.getOperand()});
|
||||
return mlir::success();
|
||||
}
|
||||
};
|
||||
|
||||
void mlir::hello::TransposeOp::getCanonicalizationPatterns(RewritePatternSet& set, MLIRContext* context)
|
||||
{
|
||||
set.add<SimplifyRedundantTranspose>(context);
|
||||
}
|
||||
|
||||
void mlir::hello::ReshapeOp::getCanonicalizationPatterns(RewritePatternSet& set, MLIRContext* context)
|
||||
{
|
||||
set.add<ReshapeReshapeOptPattern, RedundantShapeOptPattern, FoldConstantReshapeOptPattern>(context);
|
||||
}
|
||||
Reference in New Issue
Block a user