From 80280660f508d87cc8144844ee914a27097f6936 Mon Sep 17 00:00:00 2001 From: Michael Klemm Date: Thu, 25 Jan 2024 19:52:23 +0100 Subject: [PATCH] Provide initial implementation for COSD and SIND This implementation was providd by kirankumar.tp@amd.com --- flang/docs/Extensions.md | 4 +-- .../flang/Optimizer/Builder/IntrinsicCall.h | 2 ++ flang/lib/Optimizer/Builder/IntrinsicCall.cpp | 32 +++++++++++++++++++ flang/test/Lower/Intrinsics/cosd.f90 | 26 +++++++++++++++ flang/test/Lower/Intrinsics/sind.f90 | 26 +++++++++++++++ 5 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 flang/test/Lower/Intrinsics/cosd.f90 create mode 100644 flang/test/Lower/Intrinsics/sind.f90 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 genCFunLoc(mlir::Type, llvm::ArrayRef); fir::ExtendedValue genCLoc(mlir::Type, llvm::ArrayRef); + mlir::Value genCosd(mlir::Type, llvm::ArrayRef); void genDateAndTime(llvm::ArrayRef); mlir::Value genDim(mlir::Type, llvm::ArrayRef); fir::ExtendedValue genDotProduct(mlir::Type, @@ -332,6 +333,7 @@ struct IntrinsicLibrary { mlir::Value genShift(mlir::Type resultType, llvm::ArrayRef); mlir::Value genShiftA(mlir::Type resultType, llvm::ArrayRef); mlir::Value genSign(mlir::Type, llvm::ArrayRef); + mlir::Value genSind(mlir::Type, llvm::ArrayRef); fir::ExtendedValue genSize(mlir::Type, llvm::ArrayRef); mlir::Value genSpacing(mlir::Type resultType, llvm::ArrayRef 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}, {"shiftr", &I::genShift}, {"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 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("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 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("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 : f32 +! CHECK-PRECISE: %{{.*}} = fir.call @cosf(%[[arg]]) fastmath : (f32) -> f32 +! CHECK-FAST: %{{.*}} = math.cos %[[arg]] fastmath : 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 : f64 +! CHECK-PRECISE: %{{.*}} = fir.call @cos(%[[arg]]) fastmath : (f64) -> f64 +! CHECK-FAST: %{{.*}} = math.cos %[[arg]] fastmath : 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 : f32 +! CHECK-PRECISE: %{{.*}} = fir.call @sinf(%[[arg]]) fastmath : (f32) -> f32 +! CHECK-FAST: %{{.*}} = math.sin %[[arg]] fastmath : 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 : f64 +! CHECK-PRECISE: %{{.*}} = fir.call @sin(%[[arg]]) fastmath : (f64) -> f64 +! CHECK-FAST: %{{.*}} = math.sin %[[arg]] fastmath : f64