Skip to content

Commit c86c241

Browse files
[MLIR] Add sincos fusion pass
We see performance improvements from using sincos to reuse calculations in hot loops that compute sin() and cos() on the same operand. Add a pass to identify sin() and cos() calls in the same block with the same operand and fast-math flags, and fuse them into a sincos op. Follow-up to: * llvm#160561 * llvm#160772
1 parent 870e4f9 commit c86c241

File tree

4 files changed

+150
-0
lines changed

4 files changed

+150
-0
lines changed

mlir/include/mlir/Dialect/Math/Transforms/Passes.td

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,12 @@ def MathExpandOpsPass : Pass<"math-expand-ops"> {
6464
];
6565
}
6666

67+
def MathSincosFusionPass : Pass<"math-sincos-fusion"> {
68+
let summary = "Fuse sin and cos operations.";
69+
let description = [{
70+
Fuse sin and cos operations into a sincos operation.
71+
}];
72+
let dependentDialects = ["math::MathDialect"];
73+
}
74+
6775
#endif // MLIR_DIALECT_MATH_TRANSFORMS_PASSES

mlir/lib/Dialect/Math/Transforms/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ add_mlir_dialect_library(MLIRMathTransforms
33
ExpandOps.cpp
44
ExtendToSupportedTypes.cpp
55
PolynomialApproximation.cpp
6+
SincosFusion.cpp
67
UpliftToFMA.cpp
78

89
ADDITIONAL_HEADER_DIRS
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
9+
#include "mlir/Dialect/Math/IR/Math.h"
10+
#include "mlir/Dialect/Math/Transforms/Passes.h"
11+
#include "mlir/IR/PatternMatch.h"
12+
#include "mlir/Pass/Pass.h"
13+
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
14+
15+
using namespace mlir;
16+
using namespace mlir::math;
17+
18+
namespace {
19+
20+
/// Fuse a math.sin and math.cos in the same block that use the same operand and
21+
/// have identical fastmath flags into a single math.sincos.
22+
struct SincosFusionPattern : OpRewritePattern<math::SinOp> {
23+
using OpRewritePattern<math::SinOp>::OpRewritePattern;
24+
25+
LogicalResult matchAndRewrite(math::SinOp sinOp,
26+
PatternRewriter &rewriter) const override {
27+
Value operand = sinOp.getOperand();
28+
mlir::arith::FastMathFlags sinFastMathFlags = sinOp.getFastmath();
29+
30+
math::CosOp cosOp = nullptr;
31+
sinOp->getBlock()->walk([&](math::CosOp op) {
32+
if (op.getOperand() == operand && op.getFastmath() == sinFastMathFlags) {
33+
cosOp = op;
34+
return WalkResult::interrupt();
35+
}
36+
return WalkResult::advance();
37+
});
38+
39+
if (!cosOp)
40+
return failure();
41+
42+
Type elemType = sinOp.getType();
43+
auto sincos = rewriter.create<math::SincosOp>(
44+
sinOp.getLoc(), TypeRange{elemType, elemType}, operand,
45+
sinOp.getFastmathAttr());
46+
47+
rewriter.replaceOp(sinOp, sincos.getSin());
48+
rewriter.replaceOp(cosOp, sincos.getCos());
49+
return success();
50+
}
51+
};
52+
53+
} // namespace
54+
55+
namespace mlir::math {
56+
#define GEN_PASS_DEF_MATHSINCOSFUSIONPASS
57+
#include "mlir/Dialect/Math/Transforms/Passes.h.inc"
58+
} // namespace mlir::math
59+
60+
namespace {
61+
62+
struct MathSincosFusionPass final
63+
: math::impl::MathSincosFusionPassBase<MathSincosFusionPass> {
64+
using MathSincosFusionPassBase::MathSincosFusionPassBase;
65+
66+
void runOnOperation() override {
67+
RewritePatternSet patterns(&getContext());
68+
patterns.add<SincosFusionPattern>(&getContext());
69+
70+
GreedyRewriteConfig config;
71+
if (failed(
72+
applyPatternsGreedily(getOperation(), std::move(patterns), config)))
73+
return signalPassFailure();
74+
}
75+
};
76+
77+
} // namespace
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// RUN: mlir-opt -math-sincos-fusion %s | FileCheck %s
2+
3+
// CHECK-LABEL: func.func @sincos_fusion(
4+
// CHECK-SAME: %[[ARG0:.*]]: f32,
5+
// CHECK-SAME: %[[ARG1:.*]]: f32) -> (f32, f32, f32, f32) {
6+
// CHECK: %[[VAL_0:.*]], %[[VAL_1:.*]] = math.sincos %[[ARG0]] : f32
7+
// CHECK: %[[VAL_2:.*]], %[[VAL_3:.*]] = math.sincos %[[ARG1]] : f32
8+
// CHECK: return %[[VAL_0]], %[[VAL_1]], %[[VAL_3]], %[[VAL_2]] : f32, f32, f32, f32
9+
// CHECK: }
10+
func.func @sincos_fusion(%arg0 : f32, %arg1 : f32) -> (f32, f32, f32, f32) {
11+
%0 = math.sin %arg0 : f32
12+
%1 = math.cos %arg0 : f32
13+
14+
%2 = math.cos %arg1 : f32
15+
%3 = math.sin %arg1 : f32
16+
17+
func.return %0, %1, %2, %3 : f32, f32, f32, f32
18+
}
19+
20+
// CHECK-LABEL: func.func @sincos_fusion_no_match_fmf(
21+
// CHECK-SAME: %[[ARG0:.*]]: f32) -> (f32, f32) {
22+
// CHECK: %[[VAL_0:.*]] = math.sin %[[ARG0]] fastmath<contract> : f32
23+
// CHECK: %[[VAL_1:.*]] = math.cos %[[ARG0]] : f32
24+
// CHECK: return %[[VAL_0]], %[[VAL_1]] : f32, f32
25+
// CHECK: }
26+
func.func @sincos_fusion_no_match_fmf(%arg0 : f32) -> (f32, f32) {
27+
%0 = math.sin %arg0 fastmath<contract> : f32
28+
%1 = math.cos %arg0 : f32
29+
func.return %0, %1 : f32, f32
30+
}
31+
32+
// CHECK-LABEL: func.func @sincos_no_fusion_different_block(
33+
// CHECK-SAME: %[[ARG0:.*]]: f32,
34+
// CHECK-SAME: %[[ARG1:.*]]: i1) -> f32 {
35+
// CHECK: %[[VAL_0:.*]] = scf.if %[[ARG1]] -> (f32) {
36+
// CHECK: %[[VAL_1:.*]] = math.sin %[[ARG0]] : f32
37+
// CHECK: scf.yield %[[VAL_1]] : f32
38+
// CHECK: } else {
39+
// CHECK: %[[VAL_2:.*]] = math.cos %[[ARG0]] : f32
40+
// CHECK: scf.yield %[[VAL_2]] : f32
41+
// CHECK: }
42+
// CHECK: return %[[VAL_0]] : f32
43+
// CHECK: }
44+
func.func @sincos_no_fusion_different_block(%arg0 : f32, %flag : i1) -> f32 {
45+
%0 = scf.if %flag -> f32 {
46+
%s = math.sin %arg0 : f32
47+
scf.yield %s : f32
48+
} else {
49+
%c = math.cos %arg0 : f32
50+
scf.yield %c : f32
51+
}
52+
func.return %0 : f32
53+
}
54+
55+
// CHECK-LABEL: func.func @sincos_fusion_preserve_fastmath(
56+
// CHECK-SAME: %[[ARG0:.*]]: f32) -> (f32, f32) {
57+
// CHECK: %[[VAL_0:.*]], %[[VAL_1:.*]] = math.sincos %[[ARG0]] fastmath<contract> : f32
58+
// CHECK: return %[[VAL_0]], %[[VAL_1]] : f32, f32
59+
// CHECK: }
60+
func.func @sincos_fusion_preserve_fastmath(%arg0 : f32) -> (f32, f32) {
61+
%0 = math.sin %arg0 fastmath<contract> : f32
62+
%1 = math.cos %arg0 fastmath<contract> : f32
63+
func.return %0, %1 : f32, f32
64+
}

0 commit comments

Comments
 (0)