This is an archive of the discontinued LLVM Phabricator instance.

[flang] Folding for quadmath 128 bit floating point intrinsics
ClosedPublic

Authored by PeteSteinfeld on Jul 27 2023, 7:26 AM.

Details

Summary

This update allows constant folding for many 128 bit floating point intrinsics
through the library quadmath, which is only available on some platforms.

Diff Detail

Event Timeline

PeteSteinfeld created this revision.Jul 27 2023, 7:26 AM
Herald added projects: Restricted Project, Restricted Project. · View Herald Transcript
Herald added a subscriber: sunshaoce. · View Herald Transcript
PeteSteinfeld requested review of this revision.Jul 27 2023, 7:26 AM
PeteSteinfeld retitled this revision from [flang] Quadmath 128 bit floating point intrinsics to [flang] Folding for quadmath 128 bit floating point intrinsics.Jul 27 2023, 7:28 AM
PeteSteinfeld added a reviewer: klausler.
klausler added inline comments.Jul 27 2023, 8:39 AM
flang/lib/Evaluate/host.h
170

I'm confused by this specialization -- shouldn't the more general template now at line 186 work for 128-bit complex anyway?

PeteSteinfeld added inline comments.Jul 27 2023, 9:23 AM
flang/lib/Evaluate/host.h
170

Without this specialization, I get compilation errors from intrinsics-library.cpp. Here's an excerpt:

/local/home/psteinfeld/dev/llvm-project/flang/lib/Evaluate/intrinsics-library.cpp: In instantiation of ‘constexpr const Fortran::evaluate::DynamicType Fortran::evaluate::FuncTypeAnalyzer<__float128 (*)(__float128)>::result’:
/local/home/psteinfeld/dev/llvm-project/flang/lib/Evaluate/intrinsics-library.cpp:188:57:   required from ‘static constexpr Fortran::evaluate::HostRuntimeFunction Fortran::evaluate::FolderFactory<HostFuncType, func>::Create(const string_view&) [with HostFuncType = __float128 (*)(__float128); HostFuncType func = acosq; std::string_view = std::basic_string_view<char>]’
/local/home/psteinfeld/dev/llvm-project/flang/lib/Evaluate/intrinsics-library.cpp:325:50:   required from here
/local/home/psteinfeld/dev/llvm-project/flang/lib/Evaluate/intrinsics-library.cpp:105:67: error: ‘Fortran::evaluate::host::FortranType<__float128>’ {aka ‘struct Fortran::evaluate::host::UnknownType’} has no member named ‘GetType’
  105 |   static constexpr DynamicType result{host::FortranType<HostTR>{}.GetType()};
      |                                             ~~~~~~~~~~~~~~~~~~~~~~^~~~~~~
/local/home/psteinfeld/dev/llvm-project/flang/lib/Evaluate/intrinsics-library.cpp:105:32: error: no matching function for call to ‘Fortran::evaluate::DynamicType::DynamicType(<brace-enclosed initializer list>)’
  105 |   static constexpr DynamicType result{host::FortranType<HostTR>{}.GetType()};
      |                                ^~~~~~
In file included from /local/home/psteinfeld/dev/llvm-project/flang/lib/Evaluate/character.h:12,
                 from /local/home/psteinfeld/dev/llvm-project/flang/lib/Evaluate/fold-implementation.h:12,
                 from /local/home/psteinfeld/dev/llvm-project/flang/lib/Evaluate/intrinsics-library.cpp:15:
/local/home/psteinfeld/dev/llvm-project/flang/include/flang/Evaluate/type.h:243:13: note: candidate: ‘constexpr Fortran::evaluate::DynamicType::DynamicType()’
  243 |   constexpr DynamicType() {}
      |             ^~~~~~~~~~~
/local/home/psteinfeld/dev/llvm-project/flang/include/flang/Evaluate/type.h:243:13: note:   candidate expects 0 arguments, 1 provided
klausler added inline comments.Jul 27 2023, 9:36 AM
flang/lib/Evaluate/host.h
170

That seems to imply that HostTypeExists<RealT> is false, which seems wrong if you just defined it for KIND=16.

I think that the general template for COMPLEX should be made to work, with some if constexpr and #ifdef HAS_FLOAT128 guards to yield __complex128 instead of std::complex<HostType<RealT>> when it should do so.

If you can't make that happen, can you at least be consistent above with your use of the kind code 16 and the use of common::RealKindForPrecision(113)?

PeteSteinfeld added inline comments.Jul 27 2023, 1:52 PM
flang/lib/Evaluate/host.h
170

Thanks for your help with this.

I flailed around, but I cannot get things to compile without a separate specialization for 128 bit complex. I did clean up the KIND specification and moved the 128 bit version of the complex specialization closer to the other one.

Let me know if there's something I'm missing.

Responding to Peter's comments.

Attempting to get Debian to compile.

klausler accepted this revision.Jul 28 2023, 8:22 AM

LGTM once the build issue with a missing "quadmath.h" header is fixed.

This revision is now accepted and ready to land.Jul 28 2023, 8:22 AM

These changes are to try to get builds to succeed on Debian. I'm now testing
both if the quadmath library exists and if the header file quadmath.h exists in
order to enable use of the quadmath library for folding.

Polishing up the CMake files.

This revision was automatically updated to reflect the committed changes.