Page MenuHomePhabricator

Teach InstSimplify transform -X + X --> 0.0 about unary FNeg
ClosedPublic

Authored by cameron.mcinally on May 14 2019, 1:32 PM.

Diff Detail

Repository
rL LLVM

Event Timeline

Herald added a project: Restricted Project. · View Herald TranscriptMay 14 2019, 1:32 PM
This revision is now accepted and ready to land.May 14 2019, 4:01 PM
kpn added a comment.May 15 2019, 5:38 AM

Is this an allowed transform?

If we transform "y = -x + x;" into "y = 0;" then won't that eliminate a trap? Is that allowed?

In D61916#1502826, @kpn wrote:

Is this an allowed transform?

If we transform "y = -x + x;" into "y = 0;" then won't that eliminate a trap? Is that allowed?

We're not dealing with constrained ops in this transform, so assume no traps/exceptions.

This revision was automatically updated to reflect the committed changes.
kpn added a comment.May 15 2019, 9:37 AM

If I recall, fneg is not a constrained intrinsic because an fneg will never itself trap.

But a transform of -inf+inf to zero would be invalid by the standard, would it not? Section 7.2 paragraph d? Do we have a way of noting strict conformance to IEEE is required/desired without constrained intrinsics?

In D61916#1503222, @kpn wrote:

If I recall, fneg is not a constrained intrinsic because an fneg will never itself trap.

That's right.

But a transform of -inf+inf to zero would be invalid by the standard, would it not? Section 7.2 paragraph d? Do we have a way of noting strict conformance to IEEE is required/desired without constrained intrinsics?

So this xform only happens under nnan. IEEE-754 specifies that FAdd(+inf,-inf) --> QNaN. I'm not certain about what LLVM does under nnan in all cases, but we've definitely already diverged from IEEE-754 if there are no NaNs.

In D61916#1503222, @kpn wrote:

If I recall, fneg is not a constrained intrinsic because an fneg will never itself trap.

But a transform of -inf+inf to zero would be invalid by the standard, would it not? Section 7.2 paragraph d? Do we have a way of noting strict conformance to IEEE is required/desired without constrained intrinsics?

Correct on -inf+inf, and (assuming no logic bugs) we do have basic IEEE conformance w/o constrained intrinsics. We've ruled the inf case out by using fast-math-flags (nnan) on the fadd. See code comment :

// With nnan: -X + X --> 0.0 (and commuted variant)
// We don't have to explicitly exclude infinities (ninf): INF + -INF == NaN.