aboutsummaryrefslogtreecommitdiff
path: root/flang
diff options
context:
space:
mode:
authorDavid Truby <david@truby.dev>2023-07-06 15:59:16 +0100
committerDavid Truby <david@truby.dev>2023-07-19 14:42:04 +0100
commit1ae04d1c893d965148a4fba0b85392f794b571d2 (patch)
tree9ad26757901ab0d934d845d68d73f90eec36af9c /flang
parent3681a7dd7818aa93956fe42d37e97da6882f23ae (diff)
downloadllvm-1ae04d1c893d965148a4fba0b85392f794b571d2.zip
llvm-1ae04d1c893d965148a4fba0b85392f794b571d2.tar.gz
llvm-1ae04d1c893d965148a4fba0b85392f794b571d2.tar.bz2
[flang] Implement tand intrinsic
This implements the tand intrinsic by performing a multiplication by pi/180 to the argument before calling tan inline. This is a commonly provided extension that is used by OpenRadioss Differential Revision: https://reviews.llvm.org/D154614
Diffstat (limited to 'flang')
-rw-r--r--flang/docs/Extensions.md2
-rw-r--r--flang/include/flang/Optimizer/Builder/IntrinsicCall.h1
-rw-r--r--flang/lib/Optimizer/Builder/IntrinsicCall.cpp16
-rw-r--r--flang/test/Lower/Intrinsics/tand.f9026
4 files changed, 45 insertions, 0 deletions
diff --git a/flang/docs/Extensions.md b/flang/docs/Extensions.md
index 5f82b3f..5938ecc 100644
--- a/flang/docs/Extensions.md
+++ b/flang/docs/Extensions.md
@@ -289,6 +289,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.
### Extensions supported when enabled by options
diff --git a/flang/include/flang/Optimizer/Builder/IntrinsicCall.h b/flang/include/flang/Optimizer/Builder/IntrinsicCall.h
index b4b82ea..0018860 100644
--- a/flang/include/flang/Optimizer/Builder/IntrinsicCall.h
+++ b/flang/include/flang/Optimizer/Builder/IntrinsicCall.h
@@ -316,6 +316,7 @@ struct IntrinsicLibrary {
llvm::ArrayRef<fir::ExtendedValue>);
fir::ExtendedValue genSum(mlir::Type, llvm::ArrayRef<fir::ExtendedValue>);
void genSystemClock(llvm::ArrayRef<fir::ExtendedValue>);
+ mlir::Value genTand(mlir::Type, llvm::ArrayRef<mlir::Value>);
mlir::Value genTrailz(mlir::Type, llvm::ArrayRef<mlir::Value>);
fir::ExtendedValue genTransfer(mlir::Type,
llvm::ArrayRef<fir::ExtendedValue>);
diff --git a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
index 7d1a9ff..cb6bc26 100644
--- a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
+++ b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
@@ -511,6 +511,7 @@ static constexpr IntrinsicHandler handlers[]{
&I::genSystemClock,
{{{"count", asAddr}, {"count_rate", asAddr}, {"count_max", asAddr}}},
/*isElemental=*/false},
+ {"tand", &I::genTand},
{"trailz", &I::genTrailz},
{"transfer",
&I::genTransfer,
@@ -4997,6 +4998,21 @@ IntrinsicLibrary::genSize(mlir::Type resultType,
.getResults()[0];
}
+// TAND
+mlir::Value IntrinsicLibrary::genTand(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("tan", ftype)(builder, loc, {arg});
+}
+
// TRAILZ
mlir::Value IntrinsicLibrary::genTrailz(mlir::Type resultType,
llvm::ArrayRef<mlir::Value> args) {
diff --git a/flang/test/Lower/Intrinsics/tand.f90 b/flang/test/Lower/Intrinsics/tand.f90
new file mode 100644
index 0000000..b0f0c52
--- /dev/null
+++ b/flang/test/Lower/Intrinsics/tand.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 = tand(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 @tanf(%[[arg]]) fastmath<contract> : (f32) -> f32
+! CHECK-FAST: %{{.*}} = math.tan %[[arg]] fastmath<contract> : f32
+
+function test_real8(x)
+ real(8) :: x, test_real8
+ test_real8 = tand(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 @tan(%[[arg]]) fastmath<contract> : (f64) -> f64
+! CHECK-FAST: %{{.*}} = math.tan %[[arg]] fastmath<contract> : f64