[flang-commits] [flang] 2dd2545 - [flang] Add support for COSD/SIND (#79546)
via flang-commits
flang-commits at lists.llvm.org
Mon Jan 29 05:16:31 PST 2024
Author: NimishMishra
Date: 2024-01-29T05:16:27-08:00
New Revision: 2dd254566d0242be2d78f1656cee12a84e7aea00
URL: https://github.com/llvm/llvm-project/commit/2dd254566d0242be2d78f1656cee12a84e7aea00
DIFF: https://github.com/llvm/llvm-project/commit/2dd254566d0242be2d78f1656cee12a84e7aea00.diff
LOG: [flang] Add support for COSD/SIND (#79546)
Added support for COSD and SIND. This is quick fix. ATAND, TAND, COSD
and SIND needs to be revisited to make it a runtime call. This patch has
code changes and test cases.
Added:
flang/test/Lower/Intrinsics/cosd.f90
flang/test/Lower/Intrinsics/sind.f90
Modified:
flang/docs/Extensions.md
flang/include/flang/Optimizer/Builder/IntrinsicCall.h
flang/lib/Optimizer/Builder/IntrinsicCall.cpp
Removed:
################################################################################
diff --git a/flang/docs/Extensions.md b/flang/docs/Extensions.md
index 28f672ac28596da..6e34db182da7ba0 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 04f6ab4a35bb0df..a2d2f4ccad04873 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 273aee3733bfa40..b4ac7f5bd52b84b 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}}},
@@ -554,6 +555,7 @@ static constexpr IntrinsicHandler handlers[]{
&I::genSignalSubroutine,
{{{"number", asValue}, {"handler", asAddr}, {"status", asAddr}}},
/*isElemental=*/false},
+ {"sind", &I::genSind},
{"size",
&I::genSize,
{{{"array", asBox},
@@ -2644,6 +2646,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,
@@ -5610,6 +5627,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 000000000000000..677de3704d89435
--- /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 000000000000000..ce47d90bb73dc0f
--- /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
More information about the flang-commits
mailing list