MLIR 23.0.0git
SincosFusion.cpp
Go to the documentation of this file.
1//===- SincosFusion.cpp - Fuse sin/cos into sincos -----------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
13
14using namespace mlir;
15using namespace mlir::math;
16
17namespace {
18
19/// Fuse a math.sin and math.cos in the same block that use the same operand and
20/// have identical fastmath flags into a single math.sincos.
21struct SincosFusionPattern : OpRewritePattern<math::SinOp> {
22 using Base::Base;
23
24 LogicalResult matchAndRewrite(math::SinOp sinOp,
25 PatternRewriter &rewriter) const override {
26 Value operand = sinOp.getOperand();
27 mlir::arith::FastMathFlags sinFastMathFlags = sinOp.getFastmath();
28
29 math::CosOp cosOp = nullptr;
30 for (auto op : sinOp->getBlock()->getOps<math::CosOp>())
31 if (op.getOperand() == operand && op.getFastmath() == sinFastMathFlags) {
32 cosOp = op;
33 break;
34 }
35
36 if (!cosOp)
37 return failure();
38
39 Operation *firstOp = sinOp->isBeforeInBlock(cosOp) ? sinOp.getOperation()
40 : cosOp.getOperation();
41 rewriter.setInsertionPoint(firstOp);
42
43 Type elemType = sinOp.getType();
44 auto sincos = math::SincosOp::create(rewriter, firstOp->getLoc(),
45 TypeRange{elemType, elemType}, operand,
46 sinOp.getFastmathAttr());
47
48 rewriter.replaceOp(sinOp, sincos.getSin());
49 rewriter.replaceOp(cosOp, sincos.getCos());
50 return success();
51 }
52};
53
54} // namespace
55
56namespace mlir::math {
57#define GEN_PASS_DEF_MATHSINCOSFUSIONPASS
58#include "mlir/Dialect/Math/Transforms/Passes.h.inc"
59} // namespace mlir::math
60
61namespace {
62
63struct MathSincosFusionPass final
64 : math::impl::MathSincosFusionPassBase<MathSincosFusionPass> {
65 using MathSincosFusionPassBase::MathSincosFusionPassBase;
66
67 void runOnOperation() override {
68 RewritePatternSet patterns(&getContext());
69 patterns.add<SincosFusionPattern>(&getContext());
70
71 GreedyRewriteConfig config;
72 if (failed(
73 applyPatternsGreedily(getOperation(), std::move(patterns), config)))
74 return signalPassFailure();
75 }
76};
77
78} // namespace
return success()
b getContext())
void setInsertionPoint(Block *block, Block::iterator insertPoint)
Set the insertion point to the specified location.
Definition Builders.h:400
Location getLoc()
The source location the operation was defined or derived from.
Definition Operation.h:241
RewritePatternSet & add(ConstructorArg &&arg, ConstructorArgs &&...args)
Add an instance of each of the pattern types 'Ts' to the pattern list with the given arguments.
virtual void replaceOp(Operation *op, ValueRange newValues)
Replace the results of the given (original) operation with the specified list of values (replacements...
detail::InFlightRemark failed(Location loc, RemarkOpts opts)
Report an optimization remark that failed.
Definition Remarks.h:717
Include the generated interface declarations.
LogicalResult applyPatternsGreedily(Region &region, const FrozenRewritePatternSet &patterns, GreedyRewriteConfig config=GreedyRewriteConfig(), bool *changed=nullptr)
Rewrite ops in the given region, which must be isolated from above, by repeatedly applying the highes...
OpRewritePattern is a wrapper around RewritePattern that allows for matching and rewriting against an...