From 1ae04d1c893d965148a4fba0b85392f794b571d2 Mon Sep 17 00:00:00 2001 From: David Truby Date: Thu, 6 Jul 2023 15:59:16 +0100 Subject: [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 --- flang/docs/Extensions.md | 2 ++ .../flang/Optimizer/Builder/IntrinsicCall.h | 1 + flang/lib/Optimizer/Builder/IntrinsicCall.cpp | 16 +++++++++++++ flang/test/Lower/Intrinsics/tand.f90 | 26 ++++++++++++++++++++++ 4 files changed, 45 insertions(+) create mode 100644 flang/test/Lower/Intrinsics/tand.f90 (limited to 'flang') 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 genSum(mlir::Type, llvm::ArrayRef); void genSystemClock(llvm::ArrayRef); + mlir::Value genTand(mlir::Type, llvm::ArrayRef); mlir::Value genTrailz(mlir::Type, llvm::ArrayRef); fir::ExtendedValue genTransfer(mlir::Type, llvm::ArrayRef); 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 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(loc, args[0], factor); + return getRuntimeCallGenerator("tan", ftype)(builder, loc, {arg}); +} + // TRAILZ mlir::Value IntrinsicLibrary::genTrailz(mlir::Type resultType, llvm::ArrayRef 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 : f32 +! CHECK-PRECISE: %{{.*}} = fir.call @tanf(%[[arg]]) fastmath : (f32) -> f32 +! CHECK-FAST: %{{.*}} = math.tan %[[arg]] fastmath : 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 : f64 +! CHECK-PRECISE: %{{.*}} = fir.call @tan(%[[arg]]) fastmath : (f64) -> f64 +! CHECK-FAST: %{{.*}} = math.tan %[[arg]] fastmath : f64 -- cgit v1.1