Skip to content

Commit b8dca4f

Browse files
[mlir][math] Add math.acosh|asin|asinh|atanh op (#77463)
Signed-Off By: Vivek Khandelwal <[email protected]>
1 parent cc21aa1 commit b8dca4f

File tree

4 files changed

+350
-0
lines changed

4 files changed

+350
-0
lines changed

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

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,87 @@ def Math_AbsIOp : Math_IntegerUnaryOp<"absi"> {
135135
let hasFolder = 1;
136136
}
137137

138+
//===----------------------------------------------------------------------===//
139+
// AcoshOp
140+
//===----------------------------------------------------------------------===//
141+
142+
def Math_AcoshOp : Math_FloatUnaryOp<"acosh">{
143+
let summary = "Hyperbolic arcus cosine of the given value";
144+
let description = [{
145+
Syntax:
146+
147+
```
148+
operation ::= ssa-id `=` `math.acosh` ssa-use `:` type
149+
```
150+
151+
The `acosh` operation computes the arcus cosine of a given value. It takes
152+
one operand of floating point type (i.e., scalar, tensor or vector) and returns
153+
one result of the same type. It has no standard attributes.
154+
155+
Example:
156+
157+
```mlir
158+
// Hyperbolic arcus cosine of scalar value.
159+
%a = math.acosh %b : f64
160+
```
161+
}];
162+
let hasFolder = 1;
163+
}
164+
165+
//===----------------------------------------------------------------------===//
166+
// AsinOp
167+
//===----------------------------------------------------------------------===//
168+
169+
def Math_AsinOp : Math_FloatUnaryOp<"asin">{
170+
let summary = "arcus sine of the given value";
171+
let description = [{
172+
Syntax:
173+
174+
```
175+
operation ::= ssa-id `=` `math.asin` ssa-use `:` type
176+
```
177+
178+
The `asin` operation computes the arcus sine of a given value. It takes
179+
one operand of floating point type (i.e., scalar, tensor or vector) and returns
180+
one result of the same type. It has no standard attributes.
181+
182+
Example:
183+
184+
```mlir
185+
// Arcus sine of scalar value.
186+
%a = math.asin %b : f64
187+
```
188+
}];
189+
let hasFolder = 1;
190+
}
191+
192+
//===----------------------------------------------------------------------===//
193+
// AsinhOp
194+
//===----------------------------------------------------------------------===//
195+
196+
def Math_AsinhOp : Math_FloatUnaryOp<"asinh">{
197+
let summary = "hyperbolic arcus sine of the given value";
198+
let description = [{
199+
Syntax:
200+
201+
```
202+
operation ::= ssa-id `=` `math.asinh` ssa-use `:` type
203+
```
204+
205+
The `asinh` operation computes the hyperbolic arcus sine of a given value. It takes
206+
one operand of floating point type (i.e., scalar, tensor or vector) and returns
207+
one result of the same type. It has no standard attributes.
208+
209+
Example:
210+
211+
```mlir
212+
// Hyperbolic arcus sine of scalar value.
213+
%a = math.asinh %b : f64
214+
```
215+
}];
216+
let hasFolder = 1;
217+
}
218+
138219
//===----------------------------------------------------------------------===//
139220
// AtanOp
140221
//===----------------------------------------------------------------------===//
@@ -156,6 +237,33 @@ def Math_AtanOp : Math_FloatUnaryOp<"atan">{
156237
let hasFolder = 1;
157238
}
158239

240+
//===----------------------------------------------------------------------===//
241+
// AtanhOp
242+
//===----------------------------------------------------------------------===//
243+
244+
def Math_AtanhOp : Math_FloatUnaryOp<"atanh">{
245+
let summary = "hyperbolic arcus tangent of the given value";
246+
let description = [{
247+
Syntax:
248+
249+
```
250+
operation ::= ssa-id `=` `math.atanh` ssa-use `:` type
251+
```
252+
253+
The `atanh` operation computes the hyperbolic arcus tangent of a given value. It takes
254+
one operand of floating point type (i.e., scalar, tensor or vector) and returns
255+
one result of the same type. It has no standard attributes.
256+
257+
Example:
258+
259+
```mlir
260+
// Hyperbolic arcus tangent of scalar value.
261+
%a = math.atanh %b : f64
262+
```
263+
}];
264+
let hasFolder = 1;
265+
}
266+
159267
//===----------------------------------------------------------------------===//
160268
// Atan2Op
161269
//===----------------------------------------------------------------------===//

mlir/lib/Conversion/MathToLibm/MathToLibm.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,12 @@ void mlir::populateMathToLibmConversionPatterns(RewritePatternSet &patterns) {
163163
MLIRContext *ctx = patterns.getContext();
164164

165165
populatePatternsForOp<math::AcosOp>(patterns, ctx, "acosf", "acos");
166+
populatePatternsForOp<math::AcoshOp>(patterns, ctx, "acoshf", "acosh");
167+
populatePatternsForOp<math::AsinOp>(patterns, ctx, "asinf", "asin");
168+
populatePatternsForOp<math::AsinhOp>(patterns, ctx, "asinhf", "asinh");
166169
populatePatternsForOp<math::Atan2Op>(patterns, ctx, "atan2f", "atan2");
167170
populatePatternsForOp<math::AtanOp>(patterns, ctx, "atanf", "atan");
171+
populatePatternsForOp<math::AtanhOp>(patterns, ctx, "atanhf", "atanh");
168172
populatePatternsForOp<math::CbrtOp>(patterns, ctx, "cbrtf", "cbrt");
169173
populatePatternsForOp<math::CeilOp>(patterns, ctx, "ceilf", "ceil");
170174
populatePatternsForOp<math::CosOp>(patterns, ctx, "cosf", "cos");

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

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,60 @@ OpFoldResult math::AcosOp::fold(FoldAdaptor adaptor) {
5959
});
6060
}
6161

62+
//===----------------------------------------------------------------------===//
63+
// AcoshOp folder
64+
//===----------------------------------------------------------------------===//
65+
66+
OpFoldResult math::AcoshOp::fold(FoldAdaptor adaptor) {
67+
return constFoldUnaryOpConditional<FloatAttr>(
68+
adaptor.getOperands(), [](const APFloat &a) -> std::optional<APFloat> {
69+
switch (a.getSizeInBits(a.getSemantics())) {
70+
case 64:
71+
return APFloat(acosh(a.convertToDouble()));
72+
case 32:
73+
return APFloat(acoshf(a.convertToFloat()));
74+
default:
75+
return {};
76+
}
77+
});
78+
}
79+
80+
//===----------------------------------------------------------------------===//
81+
// AsinOp folder
82+
//===----------------------------------------------------------------------===//
83+
84+
OpFoldResult math::AsinOp::fold(FoldAdaptor adaptor) {
85+
return constFoldUnaryOpConditional<FloatAttr>(
86+
adaptor.getOperands(), [](const APFloat &a) -> std::optional<APFloat> {
87+
switch (a.getSizeInBits(a.getSemantics())) {
88+
case 64:
89+
return APFloat(asin(a.convertToDouble()));
90+
case 32:
91+
return APFloat(asinf(a.convertToFloat()));
92+
default:
93+
return {};
94+
}
95+
});
96+
}
97+
98+
//===----------------------------------------------------------------------===//
99+
// AsinhOp folder
100+
//===----------------------------------------------------------------------===//
101+
102+
OpFoldResult math::AsinhOp::fold(FoldAdaptor adaptor) {
103+
return constFoldUnaryOpConditional<FloatAttr>(
104+
adaptor.getOperands(), [](const APFloat &a) -> std::optional<APFloat> {
105+
switch (a.getSizeInBits(a.getSemantics())) {
106+
case 64:
107+
return APFloat(asinh(a.convertToDouble()));
108+
case 32:
109+
return APFloat(asinhf(a.convertToFloat()));
110+
default:
111+
return {};
112+
}
113+
});
114+
}
115+
62116
//===----------------------------------------------------------------------===//
63117
// AtanOp folder
64118
//===----------------------------------------------------------------------===//
@@ -77,6 +131,24 @@ OpFoldResult math::AtanOp::fold(FoldAdaptor adaptor) {
77131
});
78132
}
79133

134+
//===----------------------------------------------------------------------===//
135+
// AtanhOp folder
136+
//===----------------------------------------------------------------------===//
137+
138+
OpFoldResult math::AtanhOp::fold(FoldAdaptor adaptor) {
139+
return constFoldUnaryOpConditional<FloatAttr>(
140+
adaptor.getOperands(), [](const APFloat &a) -> std::optional<APFloat> {
141+
switch (a.getSizeInBits(a.getSemantics())) {
142+
case 64:
143+
return APFloat(atanh(a.convertToDouble()));
144+
case 32:
145+
return APFloat(atanhf(a.convertToFloat()));
146+
default:
147+
return {};
148+
}
149+
});
150+
}
151+
80152
//===----------------------------------------------------------------------===//
81153
// Atan2Op folder
82154
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)