From 1742f2aa7bc57123c4a7df53dc2976b0cd589bb8 Mon Sep 17 00:00:00 2001 From: Kelvin Li Date: Tue, 29 Oct 2024 13:36:24 -0400 Subject: [PATCH 1/9] [flang] Use libm routine for compile-time folding on AIX --- flang/lib/Evaluate/CMakeLists.txt | 1 + flang/lib/Evaluate/intrinsics-library.cpp | 24 +++++++++++++++++++++++ flang/lib/Evaluate/wrappers.c | 17 ++++++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 flang/lib/Evaluate/wrappers.c diff --git a/flang/lib/Evaluate/CMakeLists.txt b/flang/lib/Evaluate/CMakeLists.txt index b38f450d746ea..8ffbfc8848da7 100644 --- a/flang/lib/Evaluate/CMakeLists.txt +++ b/flang/lib/Evaluate/CMakeLists.txt @@ -58,6 +58,7 @@ add_flang_library(FortranEvaluate tools.cpp type.cpp variable.cpp + wrappers.c LINK_LIBS FortranCommon diff --git a/flang/lib/Evaluate/intrinsics-library.cpp b/flang/lib/Evaluate/intrinsics-library.cpp index bb439a6bb3a74..8b16d18751369 100644 --- a/flang/lib/Evaluate/intrinsics-library.cpp +++ b/flang/lib/Evaluate/intrinsics-library.cpp @@ -277,6 +277,26 @@ static std::complex StdPowF2B( return std::pow(x, y); } +#ifdef _AIX +extern "C" { +void csqrtf_wrapper(const float[], float[]); +void csqrt_wrapper(const double[], double[]); +} // extern "C" + +template +static std::complex CSQRT(const std::complex &x) { + HostT y[2]{x.real(), x.imag()}; + HostT r[2]; + if constexpr (std::is_same_v) { + csqrtf_wrapper(y, r); + } else if constexpr (std::is_same_v) { + csqrt_wrapper(y, r); + } + std::complex res(r[0], r[1]); + return res; +} +#endif + template struct HostRuntimeLibrary, LibraryVersion::Libm> { using F = FuncPointer, const std::complex &>; @@ -302,7 +322,11 @@ struct HostRuntimeLibrary, LibraryVersion::Libm> { FolderFactory::Create("pow"), FolderFactory::Create("sin"), FolderFactory::Create("sinh"), +#ifdef _AIX + FolderFactory::Create("sqrt"), +#else FolderFactory::Create("sqrt"), +#endif FolderFactory::Create("tan"), FolderFactory::Create("tanh"), }; diff --git a/flang/lib/Evaluate/wrappers.c b/flang/lib/Evaluate/wrappers.c new file mode 100644 index 0000000000000..b0823cac45ae4 --- /dev/null +++ b/flang/lib/Evaluate/wrappers.c @@ -0,0 +1,17 @@ +#include + +void csqrtf_wrapper(const float x[2], float res[2]) +{ + float complex c = x[0] + I * x[1]; + float complex r = csqrtf(c); + res[0] = crealf(r); + res[1] = cimagf(r); +} + +void csqrt_wrapper(const double x[2], double res[2]) +{ + double complex c = x[0] + I * x[1]; + double complex r = csqrt(c); + res[0] = creal(r); + res[1] = cimag(r); +} From d5219a9792ca177f8b4bca79a9ab718c6076ec64 Mon Sep 17 00:00:00 2001 From: Kelvin Li Date: Tue, 29 Oct 2024 14:23:26 -0400 Subject: [PATCH 2/9] Add copyright note and fix format --- flang/lib/Evaluate/wrappers.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/flang/lib/Evaluate/wrappers.c b/flang/lib/Evaluate/wrappers.c index b0823cac45ae4..360d638366b46 100644 --- a/flang/lib/Evaluate/wrappers.c +++ b/flang/lib/Evaluate/wrappers.c @@ -1,15 +1,21 @@ +//===-- lib/Evaluate/wrappers.c -------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + #include -void csqrtf_wrapper(const float x[2], float res[2]) -{ +void csqrtf_wrapper(const float x[2], float res[2]) { float complex c = x[0] + I * x[1]; float complex r = csqrtf(c); res[0] = crealf(r); res[1] = cimagf(r); } -void csqrt_wrapper(const double x[2], double res[2]) -{ +void csqrt_wrapper(const double x[2], double res[2]) { double complex c = x[0] + I * x[1]; double complex r = csqrt(c); res[0] = creal(r); From 22151341815e1e5aa9067558f14bc206c72d5f19 Mon Sep 17 00:00:00 2001 From: Kelvin Li Date: Wed, 6 Nov 2024 17:07:58 -0500 Subject: [PATCH 3/9] Fix windows build failure --- flang/lib/Evaluate/intrinsics-library.cpp | 18 ++++++++++-------- flang/lib/Evaluate/wrappers.c | 2 ++ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/flang/lib/Evaluate/intrinsics-library.cpp b/flang/lib/Evaluate/intrinsics-library.cpp index 8b16d18751369..66f5c58285382 100644 --- a/flang/lib/Evaluate/intrinsics-library.cpp +++ b/flang/lib/Evaluate/intrinsics-library.cpp @@ -282,9 +282,12 @@ extern "C" { void csqrtf_wrapper(const float[], float[]); void csqrt_wrapper(const double[], double[]); } // extern "C" +#endif template -static std::complex CSQRT(const std::complex &x) { +static std::complex CSqrt(const std::complex &x) { + std::complex res; +#if _AIX HostT y[2]{x.real(), x.imag()}; HostT r[2]; if constexpr (std::is_same_v) { @@ -292,10 +295,13 @@ static std::complex CSQRT(const std::complex &x) { } else if constexpr (std::is_same_v) { csqrt_wrapper(y, r); } - std::complex res(r[0], r[1]); + res.real(r[0]); + res.imag(r[1]); +#else + res = std::sqrt(x); +#endif return res; } -#endif template struct HostRuntimeLibrary, LibraryVersion::Libm> { @@ -322,11 +328,7 @@ struct HostRuntimeLibrary, LibraryVersion::Libm> { FolderFactory::Create("pow"), FolderFactory::Create("sin"), FolderFactory::Create("sinh"), -#ifdef _AIX - FolderFactory::Create("sqrt"), -#else - FolderFactory::Create("sqrt"), -#endif + FolderFactory::Create("sqrt"), FolderFactory::Create("tan"), FolderFactory::Create("tanh"), }; diff --git a/flang/lib/Evaluate/wrappers.c b/flang/lib/Evaluate/wrappers.c index 360d638366b46..19a681cf7db8a 100644 --- a/flang/lib/Evaluate/wrappers.c +++ b/flang/lib/Evaluate/wrappers.c @@ -6,6 +6,7 @@ // //===----------------------------------------------------------------------===// +#ifdef _AIX #include void csqrtf_wrapper(const float x[2], float res[2]) { @@ -21,3 +22,4 @@ void csqrt_wrapper(const double x[2], double res[2]) { res[0] = creal(r); res[1] = cimag(r); } +#endif From 77682f74ff5d87d4c60ffff945411714b081bac8 Mon Sep 17 00:00:00 2001 From: Kelvin Li Date: Thu, 7 Nov 2024 18:42:48 -0500 Subject: [PATCH 4/9] Address review comments - remove the wrapper files - simplify code --- flang/lib/Evaluate/CMakeLists.txt | 1 - flang/lib/Evaluate/intrinsics-library.cpp | 32 ++++++++++++++++------- flang/lib/Evaluate/wrappers.c | 25 ------------------ 3 files changed, 22 insertions(+), 36 deletions(-) delete mode 100644 flang/lib/Evaluate/wrappers.c diff --git a/flang/lib/Evaluate/CMakeLists.txt b/flang/lib/Evaluate/CMakeLists.txt index 8ffbfc8848da7..b38f450d746ea 100644 --- a/flang/lib/Evaluate/CMakeLists.txt +++ b/flang/lib/Evaluate/CMakeLists.txt @@ -58,7 +58,6 @@ add_flang_library(FortranEvaluate tools.cpp type.cpp variable.cpp - wrappers.c LINK_LIBS FortranCommon diff --git a/flang/lib/Evaluate/intrinsics-library.cpp b/flang/lib/Evaluate/intrinsics-library.cpp index 66f5c58285382..214dd2cf1dad4 100644 --- a/flang/lib/Evaluate/intrinsics-library.cpp +++ b/flang/lib/Evaluate/intrinsics-library.cpp @@ -278,25 +278,37 @@ static std::complex StdPowF2B( } #ifdef _AIX +#ifdef __clang_major__ +#pragma clang diagnostic ignored "-Wc99-extensions" +#endif + extern "C" { -void csqrtf_wrapper(const float[], float[]); -void csqrt_wrapper(const double[], double[]); -} // extern "C" +float _Complex csqrtf(float _Complex); +double _Complex csqrt(double _Complex); +} #endif template static std::complex CSqrt(const std::complex &x) { std::complex res; -#if _AIX - HostT y[2]{x.real(), x.imag()}; - HostT r[2]; +#ifdef _AIX if constexpr (std::is_same_v) { - csqrtf_wrapper(y, r); + float _Complex c; + reinterpret_cast(c)[0] = x.real(); + reinterpret_cast(c)[1] = x.imag(); + float _Complex r{csqrtf(c)}; + res.real(reinterpret_cast(r)[0]); + res.imag(reinterpret_cast(r)[1]); } else if constexpr (std::is_same_v) { - csqrt_wrapper(y, r); + double _Complex c; + reinterpret_cast(c)[0] = x.real(); + reinterpret_cast(c)[1] = x.imag(); + double _Complex r{csqrt(c)}; + res.real(reinterpret_cast(r)[0]); + res.imag(reinterpret_cast(r)[1]); + } else { + assert("bad complex component type"); } - res.real(r[0]); - res.imag(r[1]); #else res = std::sqrt(x); #endif diff --git a/flang/lib/Evaluate/wrappers.c b/flang/lib/Evaluate/wrappers.c deleted file mode 100644 index 19a681cf7db8a..0000000000000 --- a/flang/lib/Evaluate/wrappers.c +++ /dev/null @@ -1,25 +0,0 @@ -//===-- lib/Evaluate/wrappers.c -------------------------------------------===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// - -#ifdef _AIX -#include - -void csqrtf_wrapper(const float x[2], float res[2]) { - float complex c = x[0] + I * x[1]; - float complex r = csqrtf(c); - res[0] = crealf(r); - res[1] = cimagf(r); -} - -void csqrt_wrapper(const double x[2], double res[2]) { - double complex c = x[0] + I * x[1]; - double complex r = csqrt(c); - res[0] = creal(r); - res[1] = cimag(r); -} -#endif From 83e3cf6ad7f52d016499206e7b5fe8872b4e95b1 Mon Sep 17 00:00:00 2001 From: Kelvin Li Date: Thu, 7 Nov 2024 19:03:52 -0500 Subject: [PATCH 5/9] Use DIE --- flang/lib/Evaluate/intrinsics-library.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/flang/lib/Evaluate/intrinsics-library.cpp b/flang/lib/Evaluate/intrinsics-library.cpp index 214dd2cf1dad4..878cfca932b9e 100644 --- a/flang/lib/Evaluate/intrinsics-library.cpp +++ b/flang/lib/Evaluate/intrinsics-library.cpp @@ -15,6 +15,7 @@ #include "fold-implementation.h" #include "host.h" #include "flang/Common/erfc-scaled.h" +#include "flang/Common/idioms.h" #include "flang/Common/static-multimap-view.h" #include "flang/Evaluate/expression.h" #include @@ -307,7 +308,7 @@ static std::complex CSqrt(const std::complex &x) { res.real(reinterpret_cast(r)[0]); res.imag(reinterpret_cast(r)[1]); } else { - assert("bad complex component type"); + DIE("bad complex component type"); } #else res = std::sqrt(x); From 1b5253df9dc68bf83fb77574947aa032348bd432 Mon Sep 17 00:00:00 2001 From: Kelvin Li Date: Thu, 7 Nov 2024 19:10:25 -0500 Subject: [PATCH 6/9] Add comments --- flang/lib/Evaluate/intrinsics-library.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/flang/lib/Evaluate/intrinsics-library.cpp b/flang/lib/Evaluate/intrinsics-library.cpp index 878cfca932b9e..8fe5e272ea2e8 100644 --- a/flang/lib/Evaluate/intrinsics-library.cpp +++ b/flang/lib/Evaluate/intrinsics-library.cpp @@ -293,6 +293,8 @@ template static std::complex CSqrt(const std::complex &x) { std::complex res; #ifdef _AIX + // On AIX, the implementation of csqrt[f] and std::sqrt is different, + // use csqrt[f] in folding. if constexpr (std::is_same_v) { float _Complex c; reinterpret_cast(c)[0] = x.real(); From 612b6c12897a9d3178c047c1234170812d94f699 Mon Sep 17 00:00:00 2001 From: Kelvin Li Date: Fri, 8 Nov 2024 17:27:46 -0500 Subject: [PATCH 7/9] Add cacos --- flang/lib/Evaluate/intrinsics-library.cpp | 57 +++++++++++++++++------ 1 file changed, 44 insertions(+), 13 deletions(-) diff --git a/flang/lib/Evaluate/intrinsics-library.cpp b/flang/lib/Evaluate/intrinsics-library.cpp index 8fe5e272ea2e8..35c643ffa801b 100644 --- a/flang/lib/Evaluate/intrinsics-library.cpp +++ b/flang/lib/Evaluate/intrinsics-library.cpp @@ -284,9 +284,27 @@ static std::complex StdPowF2B( #endif extern "C" { +float _Complex cacosf(float _Complex); +double _Complex cacos(double _Complex); float _Complex csqrtf(float _Complex); double _Complex csqrt(double _Complex); } + +enum CRI { Real, Imag }; +template static TR &reIm(TA &x, CRI n) { + return reinterpret_cast(x)[n]; +} +template static TR CppToC(const std::complex &x) { + TR r; + reIm(r, CRI::Real) = x.real(); + reIm(r, CRI::Imag) = x.imag(); + return r; +} +template static std::complex CToCpp(const TA &x) { + TA &z{const_cast(x)}; + return std::complex(reIm(z, CRI::Real), + reIm(z, CRI::Imag)); +} #endif template @@ -296,19 +314,11 @@ static std::complex CSqrt(const std::complex &x) { // On AIX, the implementation of csqrt[f] and std::sqrt is different, // use csqrt[f] in folding. if constexpr (std::is_same_v) { - float _Complex c; - reinterpret_cast(c)[0] = x.real(); - reinterpret_cast(c)[1] = x.imag(); - float _Complex r{csqrtf(c)}; - res.real(reinterpret_cast(r)[0]); - res.imag(reinterpret_cast(r)[1]); + float _Complex r{csqrtf(CppToC(x))}; + res = CToCpp(r); } else if constexpr (std::is_same_v) { - double _Complex c; - reinterpret_cast(c)[0] = x.real(); - reinterpret_cast(c)[1] = x.imag(); - double _Complex r{csqrt(c)}; - res.real(reinterpret_cast(r)[0]); - res.imag(reinterpret_cast(r)[1]); + double _Complex r{csqrt(CppToC(x))}; + res = CToCpp(r); } else { DIE("bad complex component type"); } @@ -318,6 +328,27 @@ static std::complex CSqrt(const std::complex &x) { return res; } +template +static std::complex CAcos(const std::complex &x) { + std::complex res; +#ifdef _AIX + // On AIX, the implementation of cacos[f] and std::acos is different, + // use cacos[f] in folding. + if constexpr (std::is_same_v) { + float _Complex r{cacosf(CppToC(x))}; + res = CToCpp(r); + } else if constexpr (std::is_same_v) { + double _Complex r{cacos(CppToC(x))}; + res = CToCpp(r); + } else { + DIE("bad complex component type"); + } +#else + res = std::acos(x); +#endif + return res; +} + template struct HostRuntimeLibrary, LibraryVersion::Libm> { using F = FuncPointer, const std::complex &>; @@ -328,7 +359,7 @@ struct HostRuntimeLibrary, LibraryVersion::Libm> { using F2B = FuncPointer, const std::complex &, const HostT &>; static constexpr HostRuntimeFunction table[]{ - FolderFactory::Create("acos"), + FolderFactory::Create("acos"), FolderFactory::Create("acosh"), FolderFactory::Create("asin"), FolderFactory::Create("asinh"), From 0f07d9fa6f74f6b5801c84b5588d1d3a78b59e32 Mon Sep 17 00:00:00 2001 From: Kelvin Li Date: Fri, 8 Nov 2024 17:33:50 -0500 Subject: [PATCH 8/9] Fix format --- flang/lib/Evaluate/intrinsics-library.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/flang/lib/Evaluate/intrinsics-library.cpp b/flang/lib/Evaluate/intrinsics-library.cpp index 35c643ffa801b..45c6d1876b550 100644 --- a/flang/lib/Evaluate/intrinsics-library.cpp +++ b/flang/lib/Evaluate/intrinsics-library.cpp @@ -302,8 +302,7 @@ template static TR CppToC(const std::complex &x) { } template static std::complex CToCpp(const TA &x) { TA &z{const_cast(x)}; - return std::complex(reIm(z, CRI::Real), - reIm(z, CRI::Imag)); + return std::complex(reIm(z, CRI::Real), reIm(z, CRI::Imag)); } #endif From 08fc856b430bd868e0717c7b59406b8a63fb9e9a Mon Sep 17 00:00:00 2001 From: Kelvin Li Date: Fri, 8 Nov 2024 17:44:04 -0500 Subject: [PATCH 9/9] Format fix --- flang/lib/Evaluate/intrinsics-library.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flang/lib/Evaluate/intrinsics-library.cpp b/flang/lib/Evaluate/intrinsics-library.cpp index 45c6d1876b550..c1b270f518c0e 100644 --- a/flang/lib/Evaluate/intrinsics-library.cpp +++ b/flang/lib/Evaluate/intrinsics-library.cpp @@ -301,7 +301,7 @@ template static TR CppToC(const std::complex &x) { return r; } template static std::complex CToCpp(const TA &x) { - TA &z{const_cast(x)}; + TA &z{const_cast(x)}; return std::complex(reIm(z, CRI::Real), reIm(z, CRI::Imag)); } #endif