Skip to content

Commit 762964e

Browse files
authored
Add cosh op to the math dialect. (llvm#75153)
1 parent 7433120 commit 762964e

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

mlir/include/mlir/Dialect/Math/IR/MathOps.td

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,26 @@ def Math_AcosOp : Math_FloatUnaryOp<"acos"> {
327327
let hasFolder = 1;
328328
}
329329

330+
//===----------------------------------------------------------------------===//
331+
// CoshOp
332+
//===----------------------------------------------------------------------===//
330333

334+
def Math_CoshOp : Math_FloatUnaryOp<"cosh"> {
335+
let summary = "hyperbolic cosine of the specified value";
336+
let description = [{
337+
The `cosh` operation computes the hyperbolic cosine. It takes one operand
338+
of floating point type (i.e., scalar, tensor or vector) and returns one
339+
result of the same type. It has no standard attributes.
340+
341+
Example:
342+
343+
```mlir
344+
// Scalar hyperbolic cosine value.
345+
%a = math.cosh %b : f64
346+
```
347+
}];
348+
let hasFolder = 1;
349+
}
331350

332351
//===----------------------------------------------------------------------===//
333352
// SinOp

mlir/lib/Conversion/MathToLibm/MathToLibm.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ void mlir::populateMathToLibmConversionPatterns(RewritePatternSet &patterns) {
168168
populatePatternsForOp<math::CbrtOp>(patterns, ctx, "cbrtf", "cbrt");
169169
populatePatternsForOp<math::CeilOp>(patterns, ctx, "ceilf", "ceil");
170170
populatePatternsForOp<math::CosOp>(patterns, ctx, "cosf", "cos");
171+
populatePatternsForOp<math::CoshOp>(patterns, ctx, "coshf", "cosh");
171172
populatePatternsForOp<math::ErfOp>(patterns, ctx, "erff", "erf");
172173
populatePatternsForOp<math::ExpM1Op>(patterns, ctx, "expm1f", "expm1");
173174
populatePatternsForOp<math::FloorOp>(patterns, ctx, "floorf", "floor");

mlir/lib/Dialect/Math/IR/MathOps.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,24 @@ OpFoldResult math::CosOp::fold(FoldAdaptor adaptor) {
144144
});
145145
}
146146

147+
//===----------------------------------------------------------------------===//
148+
// CoshOp folder
149+
//===----------------------------------------------------------------------===//
150+
151+
OpFoldResult math::CoshOp::fold(FoldAdaptor adaptor) {
152+
return constFoldUnaryOpConditional<FloatAttr>(
153+
adaptor.getOperands(), [](const APFloat &a) -> std::optional<APFloat> {
154+
switch (a.getSizeInBits(a.getSemantics())) {
155+
case 64:
156+
return APFloat(cosh(a.convertToDouble()));
157+
case 32:
158+
return APFloat(coshf(a.convertToFloat()));
159+
default:
160+
return {};
161+
}
162+
});
163+
}
164+
147165
//===----------------------------------------------------------------------===//
148166
// SinOp folder
149167
//===----------------------------------------------------------------------===//

mlir/test/Conversion/MathToLibm/convert-to-libm.mlir

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
// CHECK-DAG: @truncf(f32) -> f32 attributes {llvm.readnone}
2525
// CHECK-DAG: @cos(f64) -> f64 attributes {llvm.readnone}
2626
// CHECK-DAG: @cosf(f32) -> f32 attributes {llvm.readnone}
27+
// CHECK-DAG: @cosh(f64) -> f64 attributes {llvm.readnone}
28+
// CHECK-DAG: @coshf(f32) -> f32 attributes {llvm.readnone}
2729
// CHECK-DAG: @sin(f64) -> f64 attributes {llvm.readnone}
2830
// CHECK-DAG: @sinf(f32) -> f32 attributes {llvm.readnone}
2931
// CHECK-DAG: @floor(f64) -> f64 attributes {llvm.readnone}
@@ -127,6 +129,18 @@ func.func @tanh_caller(%float: f32, %double: f64) -> (f32, f64) {
127129
return %float_result, %double_result : f32, f64
128130
}
129131

132+
// CHECK-LABEL: func @cosh_caller
133+
// CHECK-SAME: %[[FLOAT:.*]]: f32
134+
// CHECK-SAME: %[[DOUBLE:.*]]: f64
135+
func.func @cosh_caller(%float: f32, %double: f64) -> (f32, f64) {
136+
// CHECK-DAG: %[[FLOAT_RESULT:.*]] = call @coshf(%[[FLOAT]]) : (f32) -> f32
137+
%float_result = math.cosh %float : f32
138+
// CHECK-DAG: %[[DOUBLE_RESULT:.*]] = call @cosh(%[[DOUBLE]]) : (f64) -> f64
139+
%double_result = math.cosh %double : f64
140+
// CHECK: return %[[FLOAT_RESULT]], %[[DOUBLE_RESULT]]
141+
return %float_result, %double_result : f32, f64
142+
}
143+
130144
// CHECK-LABEL: func @atan2_caller
131145
// CHECK-SAME: %[[FLOAT:.*]]: f32
132146
// CHECK-SAME: %[[DOUBLE:.*]]: f64

0 commit comments

Comments
 (0)