Skip to content

[mlir][math] Add math.acosh|asin|asinh|atanh op #77463

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
merged 1 commit into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 108 additions & 0 deletions mlir/include/mlir/Dialect/Math/IR/MathOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,87 @@ def Math_AbsIOp : Math_IntegerUnaryOp<"absi"> {
let hasFolder = 1;
}

//===----------------------------------------------------------------------===//
// AcoshOp
//===----------------------------------------------------------------------===//

def Math_AcoshOp : Math_FloatUnaryOp<"acosh">{
let summary = "Hyperbolic arcus cosine of the given value";
let description = [{
Syntax:

```
operation ::= ssa-id `=` `math.acosh` ssa-use `:` type
```

The `acosh` operation computes the arcus cosine of a given value. It takes
one operand of floating point type (i.e., scalar, tensor or vector) and returns
one result of the same type. It has no standard attributes.

Example:

```mlir
// Hyperbolic arcus cosine of scalar value.
%a = math.acosh %b : f64
```
}];
let hasFolder = 1;
}

//===----------------------------------------------------------------------===//
// AsinOp
//===----------------------------------------------------------------------===//

def Math_AsinOp : Math_FloatUnaryOp<"asin">{
let summary = "arcus sine of the given value";
let description = [{
Syntax:

```
operation ::= ssa-id `=` `math.asin` ssa-use `:` type
```

The `asin` operation computes the arcus sine of a given value. It takes
one operand of floating point type (i.e., scalar, tensor or vector) and returns
one result of the same type. It has no standard attributes.

Example:

```mlir
// Arcus sine of scalar value.
%a = math.asin %b : f64
```
}];
let hasFolder = 1;
}

//===----------------------------------------------------------------------===//
// AsinhOp
//===----------------------------------------------------------------------===//

def Math_AsinhOp : Math_FloatUnaryOp<"asinh">{
let summary = "hyperbolic arcus sine of the given value";
let description = [{
Syntax:

```
operation ::= ssa-id `=` `math.asinh` ssa-use `:` type
```

The `asinh` operation computes the hyperbolic arcus sine of a given value. It takes
one operand of floating point type (i.e., scalar, tensor or vector) and returns
one result of the same type. It has no standard attributes.

Example:

```mlir
// Hyperbolic arcus sine of scalar value.
%a = math.asinh %b : f64
```
}];
let hasFolder = 1;
}

//===----------------------------------------------------------------------===//
// AtanOp
//===----------------------------------------------------------------------===//
Expand All @@ -156,6 +237,33 @@ def Math_AtanOp : Math_FloatUnaryOp<"atan">{
let hasFolder = 1;
}

//===----------------------------------------------------------------------===//
// AtanhOp
//===----------------------------------------------------------------------===//

def Math_AtanhOp : Math_FloatUnaryOp<"atanh">{
let summary = "hyperbolic arcus tangent of the given value";
let description = [{
Syntax:

```
operation ::= ssa-id `=` `math.atanh` ssa-use `:` type
```

The `atanh` operation computes the hyperbolic arcus tangent of a given value. It takes
one operand of floating point type (i.e., scalar, tensor or vector) and returns
one result of the same type. It has no standard attributes.

Example:

```mlir
// Hyperbolic arcus tangent of scalar value.
%a = math.atanh %b : f64
```
}];
let hasFolder = 1;
}

//===----------------------------------------------------------------------===//
// Atan2Op
//===----------------------------------------------------------------------===//
Expand Down
4 changes: 4 additions & 0 deletions mlir/lib/Conversion/MathToLibm/MathToLibm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,12 @@ void mlir::populateMathToLibmConversionPatterns(RewritePatternSet &patterns) {
MLIRContext *ctx = patterns.getContext();

populatePatternsForOp<math::AcosOp>(patterns, ctx, "acosf", "acos");
populatePatternsForOp<math::AcoshOp>(patterns, ctx, "acoshf", "acosh");
populatePatternsForOp<math::AsinOp>(patterns, ctx, "asinf", "asin");
populatePatternsForOp<math::AsinhOp>(patterns, ctx, "asinhf", "asinh");
populatePatternsForOp<math::Atan2Op>(patterns, ctx, "atan2f", "atan2");
populatePatternsForOp<math::AtanOp>(patterns, ctx, "atanf", "atan");
populatePatternsForOp<math::AtanhOp>(patterns, ctx, "atanhf", "atanh");
populatePatternsForOp<math::CbrtOp>(patterns, ctx, "cbrtf", "cbrt");
populatePatternsForOp<math::CeilOp>(patterns, ctx, "ceilf", "ceil");
populatePatternsForOp<math::CosOp>(patterns, ctx, "cosf", "cos");
Expand Down
72 changes: 72 additions & 0 deletions mlir/lib/Dialect/Math/IR/MathOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,60 @@ OpFoldResult math::AcosOp::fold(FoldAdaptor adaptor) {
});
}

//===----------------------------------------------------------------------===//
// AcoshOp folder
//===----------------------------------------------------------------------===//

OpFoldResult math::AcoshOp::fold(FoldAdaptor adaptor) {
return constFoldUnaryOpConditional<FloatAttr>(
adaptor.getOperands(), [](const APFloat &a) -> std::optional<APFloat> {
switch (a.getSizeInBits(a.getSemantics())) {
case 64:
return APFloat(acosh(a.convertToDouble()));
case 32:
return APFloat(acoshf(a.convertToFloat()));
default:
return {};
}
});
}

//===----------------------------------------------------------------------===//
// AsinOp folder
//===----------------------------------------------------------------------===//

OpFoldResult math::AsinOp::fold(FoldAdaptor adaptor) {
return constFoldUnaryOpConditional<FloatAttr>(
adaptor.getOperands(), [](const APFloat &a) -> std::optional<APFloat> {
switch (a.getSizeInBits(a.getSemantics())) {
case 64:
return APFloat(asin(a.convertToDouble()));
case 32:
return APFloat(asinf(a.convertToFloat()));
default:
return {};
}
});
}

//===----------------------------------------------------------------------===//
// AsinhOp folder
//===----------------------------------------------------------------------===//

OpFoldResult math::AsinhOp::fold(FoldAdaptor adaptor) {
return constFoldUnaryOpConditional<FloatAttr>(
adaptor.getOperands(), [](const APFloat &a) -> std::optional<APFloat> {
switch (a.getSizeInBits(a.getSemantics())) {
case 64:
return APFloat(asinh(a.convertToDouble()));
case 32:
return APFloat(asinhf(a.convertToFloat()));
default:
return {};
}
});
}

//===----------------------------------------------------------------------===//
// AtanOp folder
//===----------------------------------------------------------------------===//
Expand All @@ -77,6 +131,24 @@ OpFoldResult math::AtanOp::fold(FoldAdaptor adaptor) {
});
}

//===----------------------------------------------------------------------===//
// AtanhOp folder
//===----------------------------------------------------------------------===//

OpFoldResult math::AtanhOp::fold(FoldAdaptor adaptor) {
return constFoldUnaryOpConditional<FloatAttr>(
adaptor.getOperands(), [](const APFloat &a) -> std::optional<APFloat> {
switch (a.getSizeInBits(a.getSemantics())) {
case 64:
return APFloat(atanh(a.convertToDouble()));
case 32:
return APFloat(atanhf(a.convertToFloat()));
default:
return {};
}
});
}

//===----------------------------------------------------------------------===//
// Atan2Op folder
//===----------------------------------------------------------------------===//
Expand Down
Loading