This is an archive of the discontinued LLVM Phabricator instance.

[ValueTracking] fix maxnum miscompile for cannotBeOrderedLessThanZero (PR37776)
ClosedPublic

Authored by spatel on Jul 31 2018, 10:14 AM.

Details

Summary

The definition of maxnum may still be up for discussion on llvm-dev, but I think we need to fix this miscompile regardless of that.

This adds the NAN checks suggested by @t.p.northover in PR37776:
https://bugs.llvm.org/show_bug.cgi?id=37776

If both operands to maxnum are NAN, that should get constant folded, so I don't think we have to handle that case.

Copying from the bug report:

Currently, we have this for "when is cannotBeOrderedLessThanZero (mustBePositiveOrNaN) true for maxnum":
               L
        -------------------
        | Pos | Neg | NaN |
   ------------------------
   |Pos |  x  |  x  |  x  |
   ------------------------
 R |Neg |  x  |     |  x  |
   ------------------------
   |NaN |  x  |  x  |  x  |
   ------------------------


The cases with (Neg & NaN) are wrong. We should have:

                L
        -------------------
        | Pos | Neg | NaN |
   ------------------------
   |Pos |  x  |  x  |  x  |
   ------------------------
 R |Neg |  x  |     |     |
   ------------------------
   |NaN |  x  |     |  x  |
   ------------------------

Diff Detail

Event Timeline

spatel created this revision.Jul 31 2018, 10:14 AM

The nan, nan case may be constant folded, but I think this still needs to handle it correctly. A pass could still ask this on a call that hasn't been folded yet (although I guess this can't really be relied on for correctness anyway). The nan && nan case at least needs a comment

The nan, nan case may be constant folded, but I think this still needs to handle it correctly. A pass could still ask this on a call that hasn't been folded yet (although I guess this can't really be relied on for correctness anyway). The nan && nan case at least needs a comment

It's conservatively correct to return false, so I don't think maxnum is worth commenting specifically. For example, above here, we don't check for NaN inputs to the regular FP binops even though those would also return NaN (true). Should I add a general comment about that or leave it as-is?

arsenm accepted this revision.Aug 2 2018, 3:19 AM

LGTM

This revision is now accepted and ready to land.Aug 2 2018, 3:19 AM
xbolva00 added subscribers: hans, xbolva00.EditedAug 2 2018, 3:21 AM

Maybe this fix should be merged into 7.0 too?

@hans

hans added a comment.Aug 2 2018, 3:41 AM

Maybe this fix be should merged into 7.0 too?

@hans

Sounds good to me, but it needs to be committed first obviously :-)

This revision was automatically updated to reflect the committed changes.
hans added a comment.Aug 8 2018, 4:32 AM

Maybe this fix be should merged into 7.0 too?

@hans

Sounds good to me, but it needs to be committed first obviously :-)

Merged in r339234.

spatel added a comment.Aug 8 2018, 5:32 AM

Maybe this fix be should merged into 7.0 too?

@hans

Sounds good to me, but it needs to be committed first obviously :-)

Merged in r339234.

Thanks!