[flang-commits] [flang] [flang] LS-Dyna : Add support for COSD/SIND (PR #79546)
via flang-commits
flang-commits at lists.llvm.org
Thu Jan 25 20:02:20 PST 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-flang-fir-hlfir
Author: None (NimishMishra)
<details>
<summary>Changes</summary>
Added support for COSD and SIND. This is quick fix.
This support is needed for ANSYS LS-Dyna application validation.
ATAND, TAND, COSD and SIND needs to be revisited to make it a runtime call.
This patch has code changes and test cases.
Change-Id: Ib58ccb19a290d57277727c3ca48d1750a0e61414
---
Full diff: https://github.com/llvm/llvm-project/pull/79546.diff
5 Files Affected:
- (modified) flang/docs/Extensions.md (+2-2)
- (modified) flang/include/flang/Optimizer/Builder/IntrinsicCall.h (+2)
- (modified) flang/lib/Optimizer/Builder/IntrinsicCall.cpp (+32)
- (added) flang/test/Lower/Intrinsics/cosd.f90 (+26)
- (added) flang/test/Lower/Intrinsics/sind.f90 (+26)
``````````diff
diff --git a/flang/docs/Extensions.md b/flang/docs/Extensions.md
index 28f672ac28596d..6e34db182da7ba 100644
--- a/flang/docs/Extensions.md
+++ b/flang/docs/Extensions.md
@@ -314,8 +314,8 @@ end
fixed form source by a '0' in column 6, can contain spaces
between the letters of the word INCLUDE, and can have a
numeric character literal kind prefix on the file name.
-* Intrinsic procedures TAND and ATAND. Constant folding is currently
- not supported for these procedures but this is planned.
+* Intrinsic procedures SIND, COSD, TAND and ATAND. Constant folding
+ is currently not supported for these procedures but this is planned.
* When a pair of quotation marks in a character literal are split
by a line continuation in free form, the second quotation mark
may appear at the beginning of the continuation line without an
diff --git a/flang/include/flang/Optimizer/Builder/IntrinsicCall.h b/flang/include/flang/Optimizer/Builder/IntrinsicCall.h
index 80f79d42fc2b75..10180db26e9a39 100644
--- a/flang/include/flang/Optimizer/Builder/IntrinsicCall.h
+++ b/flang/include/flang/Optimizer/Builder/IntrinsicCall.h
@@ -205,6 +205,7 @@ struct IntrinsicLibrary {
void genCFProcPointer(llvm::ArrayRef<fir::ExtendedValue>);
fir::ExtendedValue genCFunLoc(mlir::Type, llvm::ArrayRef<fir::ExtendedValue>);
fir::ExtendedValue genCLoc(mlir::Type, llvm::ArrayRef<fir::ExtendedValue>);
+ mlir::Value genCosd(mlir::Type, llvm::ArrayRef<mlir::Value>);
void genDateAndTime(llvm::ArrayRef<fir::ExtendedValue>);
mlir::Value genDim(mlir::Type, llvm::ArrayRef<mlir::Value>);
fir::ExtendedValue genDotProduct(mlir::Type,
@@ -332,6 +333,7 @@ struct IntrinsicLibrary {
mlir::Value genShift(mlir::Type resultType, llvm::ArrayRef<mlir::Value>);
mlir::Value genShiftA(mlir::Type resultType, llvm::ArrayRef<mlir::Value>);
mlir::Value genSign(mlir::Type, llvm::ArrayRef<mlir::Value>);
+ mlir::Value genSind(mlir::Type, llvm::ArrayRef<mlir::Value>);
fir::ExtendedValue genSize(mlir::Type, llvm::ArrayRef<fir::ExtendedValue>);
mlir::Value genSpacing(mlir::Type resultType,
llvm::ArrayRef<mlir::Value> args);
diff --git a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
index a0baa409fe44b4..9f6d925b71537d 100644
--- a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
+++ b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
@@ -179,6 +179,7 @@ static constexpr IntrinsicHandler handlers[]{
{{{"x", asValue}, {"y", asValue, handleDynamicOptional}}}},
{"command_argument_count", &I::genCommandArgumentCount},
{"conjg", &I::genConjg},
+ {"cosd", &I::genCosd},
{"count",
&I::genCount,
{{{"mask", asAddr}, {"dim", asValue}, {"kind", asValue}}},
@@ -550,6 +551,7 @@ static constexpr IntrinsicHandler handlers[]{
{"shiftl", &I::genShift<mlir::arith::ShLIOp>},
{"shiftr", &I::genShift<mlir::arith::ShRUIOp>},
{"sign", &I::genSign},
+ {"sind", &I::genSind},
{"size",
&I::genSize,
{{{"array", asBox},
@@ -2639,6 +2641,21 @@ mlir::Value IntrinsicLibrary::genConjg(mlir::Type resultType,
cplx, negImag, /*isImagPart=*/true);
}
+// COSD
+mlir::Value IntrinsicLibrary::genCosd(mlir::Type resultType,
+ llvm::ArrayRef<mlir::Value> args) {
+ assert(args.size() == 1);
+ mlir::MLIRContext *context = builder.getContext();
+ mlir::FunctionType ftype =
+ mlir::FunctionType::get(context, {resultType}, {args[0].getType()});
+ llvm::APFloat pi = llvm::APFloat(llvm::numbers::pi);
+ mlir::Value dfactor = builder.createRealConstant(
+ loc, mlir::FloatType::getF64(context), pi / llvm::APFloat(180.0));
+ mlir::Value factor = builder.createConvert(loc, args[0].getType(), dfactor);
+ mlir::Value arg = builder.create<mlir::arith::MulFOp>(loc, args[0], factor);
+ return getRuntimeCallGenerator("cos", ftype)(builder, loc, {arg});
+}
+
// COUNT
fir::ExtendedValue
IntrinsicLibrary::genCount(mlir::Type resultType,
@@ -5593,6 +5610,21 @@ mlir::Value IntrinsicLibrary::genSign(mlir::Type resultType,
return genRuntimeCall("sign", resultType, args);
}
+// SIND
+mlir::Value IntrinsicLibrary::genSind(mlir::Type resultType,
+ llvm::ArrayRef<mlir::Value> args) {
+ assert(args.size() == 1);
+ mlir::MLIRContext *context = builder.getContext();
+ mlir::FunctionType ftype =
+ mlir::FunctionType::get(context, {resultType}, {args[0].getType()});
+ llvm::APFloat pi = llvm::APFloat(llvm::numbers::pi);
+ mlir::Value dfactor = builder.createRealConstant(
+ loc, mlir::FloatType::getF64(context), pi / llvm::APFloat(180.0));
+ mlir::Value factor = builder.createConvert(loc, args[0].getType(), dfactor);
+ mlir::Value arg = builder.create<mlir::arith::MulFOp>(loc, args[0], factor);
+ return getRuntimeCallGenerator("sin", ftype)(builder, loc, {arg});
+}
+
// SIZE
fir::ExtendedValue
IntrinsicLibrary::genSize(mlir::Type resultType,
diff --git a/flang/test/Lower/Intrinsics/cosd.f90 b/flang/test/Lower/Intrinsics/cosd.f90
new file mode 100644
index 00000000000000..677de3704d8943
--- /dev/null
+++ b/flang/test/Lower/Intrinsics/cosd.f90
@@ -0,0 +1,26 @@
+! RUN: bbc -emit-fir %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-FAST"
+! RUN: bbc --math-runtime=precise -emit-fir %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-PRECISE"
+! RUN: %flang_fc1 -emit-fir %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-FAST"
+
+function test_real4(x)
+ real :: x, test_real4
+ test_real4 = cosd(x)
+end function
+
+! CHECK-LABEL: @_QPtest_real4
+! CHECK: %[[dfactor:.*]] = arith.constant 0.017453292519943295 : f64
+! CHECK: %[[factor:.*]] = fir.convert %[[dfactor]] : (f64) -> f32
+! CHECK: %[[arg:.*]] = arith.mulf %{{[A-Za-z0-9._]+}}, %[[factor]] fastmath<contract> : f32
+! CHECK-PRECISE: %{{.*}} = fir.call @cosf(%[[arg]]) fastmath<contract> : (f32) -> f32
+! CHECK-FAST: %{{.*}} = math.cos %[[arg]] fastmath<contract> : f32
+
+function test_real8(x)
+ real(8) :: x, test_real8
+ test_real8 = cosd(x)
+end function
+
+! CHECK-LABEL: @_QPtest_real8
+! CHECK: %[[factor:.*]] = arith.constant 0.017453292519943295 : f64
+! CHECK: %[[arg:.*]] = arith.mulf %{{[A-Za-z0-9._]+}}, %[[factor]] fastmath<contract> : f64
+! CHECK-PRECISE: %{{.*}} = fir.call @cos(%[[arg]]) fastmath<contract> : (f64) -> f64
+! CHECK-FAST: %{{.*}} = math.cos %[[arg]] fastmath<contract> : f64
diff --git a/flang/test/Lower/Intrinsics/sind.f90 b/flang/test/Lower/Intrinsics/sind.f90
new file mode 100644
index 00000000000000..ce47d90bb73dc0
--- /dev/null
+++ b/flang/test/Lower/Intrinsics/sind.f90
@@ -0,0 +1,26 @@
+! RUN: bbc -emit-fir %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-FAST"
+! RUN: bbc --math-runtime=precise -emit-fir %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-PRECISE"
+! RUN: %flang_fc1 -emit-fir %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-FAST"
+
+function test_real4(x)
+ real :: x, test_real4
+ test_real4 = sind(x)
+end function
+
+! CHECK-LABEL: @_QPtest_real4
+! CHECK: %[[dfactor:.*]] = arith.constant 0.017453292519943295 : f64
+! CHECK: %[[factor:.*]] = fir.convert %[[dfactor]] : (f64) -> f32
+! CHECK: %[[arg:.*]] = arith.mulf %{{[A-Za-z0-9._]+}}, %[[factor]] fastmath<contract> : f32
+! CHECK-PRECISE: %{{.*}} = fir.call @sinf(%[[arg]]) fastmath<contract> : (f32) -> f32
+! CHECK-FAST: %{{.*}} = math.sin %[[arg]] fastmath<contract> : f32
+
+function test_real8(x)
+ real(8) :: x, test_real8
+ test_real8 = sind(x)
+end function
+
+! CHECK-LABEL: @_QPtest_real8
+! CHECK: %[[factor:.*]] = arith.constant 0.017453292519943295 : f64
+! CHECK: %[[arg:.*]] = arith.mulf %{{[A-Za-z0-9._]+}}, %[[factor]] fastmath<contract> : f64
+! CHECK-PRECISE: %{{.*}} = fir.call @sin(%[[arg]]) fastmath<contract> : (f64) -> f64
+! CHECK-FAST: %{{.*}} = math.sin %[[arg]] fastmath<contract> : f64
``````````
</details>
https://github.com/llvm/llvm-project/pull/79546
More information about the flang-commits
mailing list