Page MenuHomePhabricator

[ConstantFolding] Use APFloat for constant folding. NFC
ClosedPublic

Authored by sepavloff on May 18 2021, 1:31 AM.

Details

Summary

Replace use of host floating types with operations on APFloat when it is
possible. Use of APFloat makes analysis more convenient and facilitates
constant folding in the case of non-default FP environment.

Diff Detail

Event Timeline

sepavloff created this revision.May 18 2021, 1:31 AM
sepavloff requested review of this revision.May 18 2021, 1:31 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 18 2021, 1:31 AM
Herald added a subscriber: wdng. · View Herald Transcript

It looks like this is just refactoring, besides the changes to the floating-point compares. That's okay, but not really consistent with the commit message; do you have some future work planned?

llvm/lib/Analysis/ConstantFolding.cpp
2146–2147

This isn't precisely equivalent to the existing code.

It looks like this is just refactoring, besides the changes to the floating-point compares. That's okay, but not really consistent with the commit message; do you have some future work planned?

The patch D102673 implements folding of constrained intrinsics, which are used in the case of non-default environment.

llvm/lib/Analysis/ConstantFolding.cpp
2146–2147

Could you please point out the case when this code behaves differently from the existing one?

efriedma added inline comments.May 18 2021, 10:12 AM
llvm/lib/Analysis/ConstantFolding.cpp
2146–2147

This handles NaNs differently.

sepavloff added inline comments.May 18 2021, 10:40 PM
llvm/lib/Analysis/ConstantFolding.cpp
2146–2147

Strictly speaking you are right. Comparison in C is ordered, so V > 0.0 gives false for NaN and ConstantFoldFP` is not called. However all these functions (log* and sqrt*) have specified behavior when their argument is NaN, so the final result must be the same. Other similar functions like sin are called for NaN arguments.

efriedma added inline comments.May 19 2021, 11:19 AM
llvm/lib/Analysis/ConstantFolding.cpp
2146–2147

If you want to change this, please add test coverage.

sepavloff added inline comments.May 19 2021, 10:15 PM
llvm/lib/Analysis/ConstantFolding.cpp
2146–2147

Things are even simpler. The code above contains a check:

if (!U.isFinite())
  return nullptr;

So NaNs cannot reach this code.

efriedma accepted this revision.May 20 2021, 11:03 AM

LGTM

llvm/lib/Analysis/ConstantFolding.cpp
2146–2147

Oh, that's fine then. :)

This revision is now accepted and ready to land.May 20 2021, 11:03 AM
This revision was landed with ongoing or failed builds.May 21 2021, 11:01 PM
This revision was automatically updated to reflect the committed changes.