-
Notifications
You must be signed in to change notification settings - Fork 15k
[MLIR] Add sincos fusion pass #161413
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
ashermancinelli
merged 4 commits into
llvm:main
from
ashermancinelli:ajm/sincos-fusion-pass
Oct 1, 2025
Merged
[MLIR] Add sincos fusion pass #161413
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c86c241
[MLIR] Add sincos fusion pass
ashermancinelli 3e072be
Insertion point should be the dominant op
ashermancinelli 3a17b7b
Apply review suggestions
ashermancinelli 48dd58b
Merge branch 'main' of https://github.com/llvm/llvm-project into ajm/…
ashermancinelli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| //===- SincosFusion.cpp - Fuse sin/cos into sincos -----------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "mlir/Dialect/Math/IR/Math.h" | ||
| #include "mlir/Dialect/Math/Transforms/Passes.h" | ||
| #include "mlir/IR/PatternMatch.h" | ||
| #include "mlir/Transforms/GreedyPatternRewriteDriver.h" | ||
|
|
||
| using namespace mlir; | ||
| using namespace mlir::math; | ||
|
|
||
| namespace { | ||
|
|
||
| /// Fuse a math.sin and math.cos in the same block that use the same operand and | ||
| /// have identical fastmath flags into a single math.sincos. | ||
| struct SincosFusionPattern : OpRewritePattern<math::SinOp> { | ||
| using Base::Base; | ||
|
|
||
| LogicalResult matchAndRewrite(math::SinOp sinOp, | ||
| PatternRewriter &rewriter) const override { | ||
| Value operand = sinOp.getOperand(); | ||
| mlir::arith::FastMathFlags sinFastMathFlags = sinOp.getFastmath(); | ||
|
|
||
| math::CosOp cosOp = nullptr; | ||
| sinOp->getBlock()->walk([&](math::CosOp op) { | ||
| if (op.getOperand() == operand && op.getFastmath() == sinFastMathFlags) { | ||
| cosOp = op; | ||
| return WalkResult::interrupt(); | ||
| } | ||
| return WalkResult::advance(); | ||
| }); | ||
|
|
||
| if (!cosOp) | ||
| return failure(); | ||
|
|
||
| Operation *firstOp = sinOp->isBeforeInBlock(cosOp) ? sinOp.getOperation() | ||
| : cosOp.getOperation(); | ||
| rewriter.setInsertionPoint(firstOp); | ||
|
|
||
| Type elemType = sinOp.getType(); | ||
| auto sincos = math::SincosOp::create(rewriter, firstOp->getLoc(), | ||
| TypeRange{elemType, elemType}, operand, | ||
| sinOp.getFastmathAttr()); | ||
|
|
||
| rewriter.replaceOp(sinOp, sincos.getSin()); | ||
| rewriter.replaceOp(cosOp, sincos.getCos()); | ||
| return success(); | ||
| } | ||
| }; | ||
|
|
||
| } // namespace | ||
|
|
||
| namespace mlir::math { | ||
| #define GEN_PASS_DEF_MATHSINCOSFUSIONPASS | ||
| #include "mlir/Dialect/Math/Transforms/Passes.h.inc" | ||
| } // namespace mlir::math | ||
|
|
||
| namespace { | ||
|
|
||
| struct MathSincosFusionPass final | ||
| : math::impl::MathSincosFusionPassBase<MathSincosFusionPass> { | ||
| using MathSincosFusionPassBase::MathSincosFusionPassBase; | ||
|
|
||
| void runOnOperation() override { | ||
| RewritePatternSet patterns(&getContext()); | ||
| patterns.add<SincosFusionPattern>(&getContext()); | ||
|
|
||
| GreedyRewriteConfig config; | ||
| if (failed( | ||
| applyPatternsGreedily(getOperation(), std::move(patterns), config))) | ||
| return signalPassFailure(); | ||
| } | ||
| }; | ||
|
|
||
| } // namespace | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| // RUN: mlir-opt -math-sincos-fusion %s | FileCheck %s | ||
|
|
||
| // CHECK-LABEL: func.func @sincos_fusion( | ||
| // CHECK-SAME: %[[ARG0:.*]]: f32, | ||
| // CHECK-SAME: %[[ARG1:.*]]: f32) -> (f32, f32, f32, f32) { | ||
| // CHECK: %[[VAL_0:.*]], %[[VAL_1:.*]] = math.sincos %[[ARG0]] : f32 | ||
| // CHECK: %[[VAL_2:.*]], %[[VAL_3:.*]] = math.sincos %[[ARG1]] : f32 | ||
| // CHECK: return %[[VAL_0]], %[[VAL_1]], %[[VAL_3]], %[[VAL_2]] : f32, f32, f32, f32 | ||
| // CHECK: } | ||
| func.func @sincos_fusion(%arg0 : f32, %arg1 : f32) -> (f32, f32, f32, f32) { | ||
| %0 = math.sin %arg0 : f32 | ||
| %1 = math.cos %arg0 : f32 | ||
|
|
||
| %2 = math.cos %arg1 : f32 | ||
| %3 = math.sin %arg1 : f32 | ||
|
|
||
| func.return %0, %1, %2, %3 : f32, f32, f32, f32 | ||
| } | ||
|
|
||
| func.func private @sink(%arg0 : f32) | ||
|
|
||
| // CHECK: func.func private @sink(f32) | ||
| // CHECK-LABEL: func.func @sincos_ensure_ssa_dominance( | ||
| // CHECK-SAME: %[[ARG0:.*]]: f32, | ||
| // CHECK-SAME: %[[ARG1:.*]]: f32) -> (f32, f32, f32, f32) { | ||
| // CHECK: %[[VAL_0:.*]], %[[VAL_1:.*]] = math.sincos %[[ARG0]] : f32 | ||
| // CHECK: call @sink(%[[VAL_0]]) : (f32) -> () | ||
| // CHECK: %[[VAL_2:.*]], %[[VAL_3:.*]] = math.sincos %[[ARG1]] : f32 | ||
| // CHECK: call @sink(%[[VAL_3]]) : (f32) -> () | ||
| // CHECK: return %[[VAL_0]], %[[VAL_1]], %[[VAL_3]], %[[VAL_2]] : f32, f32, f32, f32 | ||
| // CHECK: } | ||
| func.func @sincos_ensure_ssa_dominance(%arg0 : f32, %arg1 : f32) -> (f32, f32, f32, f32) { | ||
| %0 = math.sin %arg0 : f32 | ||
| func.call @sink(%0) : (f32) -> () | ||
| %1 = math.cos %arg0 : f32 | ||
| %2 = math.cos %arg1 : f32 | ||
| func.call @sink(%2) : (f32) -> () | ||
| %3 = math.sin %arg1 : f32 | ||
| func.return %0, %1, %2, %3 : f32, f32, f32, f32 | ||
| } | ||
|
|
||
| // CHECK-LABEL: func.func @sincos_fusion_no_match_fmf( | ||
| // CHECK-SAME: %[[ARG0:.*]]: f32) -> (f32, f32) { | ||
| // CHECK: %[[VAL_0:.*]] = math.sin %[[ARG0]] fastmath<contract> : f32 | ||
| // CHECK: %[[VAL_1:.*]] = math.cos %[[ARG0]] : f32 | ||
| // CHECK: return %[[VAL_0]], %[[VAL_1]] : f32, f32 | ||
| // CHECK: } | ||
| func.func @sincos_fusion_no_match_fmf(%arg0 : f32) -> (f32, f32) { | ||
| %0 = math.sin %arg0 fastmath<contract> : f32 | ||
| %1 = math.cos %arg0 : f32 | ||
| func.return %0, %1 : f32, f32 | ||
| } | ||
|
|
||
| // CHECK-LABEL: func.func @sincos_no_fusion_different_block( | ||
| // CHECK-SAME: %[[ARG0:.*]]: f32, | ||
| // CHECK-SAME: %[[ARG1:.*]]: i1) -> f32 { | ||
| // CHECK: %[[VAL_0:.*]] = scf.if %[[ARG1]] -> (f32) { | ||
| // CHECK: %[[VAL_1:.*]] = math.sin %[[ARG0]] : f32 | ||
| // CHECK: scf.yield %[[VAL_1]] : f32 | ||
| // CHECK: } else { | ||
| // CHECK: %[[VAL_2:.*]] = math.cos %[[ARG0]] : f32 | ||
| // CHECK: scf.yield %[[VAL_2]] : f32 | ||
| // CHECK: } | ||
| // CHECK: return %[[VAL_0]] : f32 | ||
| // CHECK: } | ||
| func.func @sincos_no_fusion_different_block(%arg0 : f32, %flag : i1) -> f32 { | ||
| %0 = scf.if %flag -> f32 { | ||
| %s = math.sin %arg0 : f32 | ||
| scf.yield %s : f32 | ||
| } else { | ||
| %c = math.cos %arg0 : f32 | ||
| scf.yield %c : f32 | ||
| } | ||
| func.return %0 : f32 | ||
| } | ||
|
|
||
| // CHECK-LABEL: func.func @sincos_fusion_preserve_fastmath( | ||
| // CHECK-SAME: %[[ARG0:.*]]: f32) -> (f32, f32) { | ||
| // CHECK: %[[VAL_0:.*]], %[[VAL_1:.*]] = math.sincos %[[ARG0]] fastmath<contract> : f32 | ||
| // CHECK: return %[[VAL_0]], %[[VAL_1]] : f32, f32 | ||
| // CHECK: } | ||
| func.func @sincos_fusion_preserve_fastmath(%arg0 : f32) -> (f32, f32) { | ||
| %0 = math.sin %arg0 fastmath<contract> : f32 | ||
| %1 = math.cos %arg0 fastmath<contract> : f32 | ||
| func.return %0, %1 : f32, f32 | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please try an example where
cosprecedessin, and there is a use ofcosbeforesin.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The insertion point should be the dominating operation. Thanks for pointing that out!