This is an archive of the discontinued LLVM Phabricator instance.

[SelectionDAG] fold 'fneg undef' to undef
ClosedPublic

Authored by spatel on May 3 2019, 8:37 AM.

Details

Summary

This is extracted from the original draft of D61419 with some additional tests.
We don't currently get this in IR (it's conservatively turned into a NaN), but presumably that'll get updated as we add real IR support for 'fneg' rather than 'fsub -0.0, x'.

The x86-32 run shows the following, and I haven't looked further to see why, but that seems to be independent:
Legalizing: t1: f32 = undef
Trying to expand node
Creating fp constant: t4: f32 = ConstantFP<0.000000e+00>

Diff Detail

Repository
rL LLVM

Event Timeline

spatel created this revision.May 3 2019, 8:37 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 3 2019, 8:37 AM
arsenm added inline comments.May 3 2019, 8:40 AM
llvm/test/CodeGen/X86/vec_fneg.ll
52 ↗(On Diff #198016)

It seems problematic that the DAG lowering is still producing an fneg for this

spatel marked an inline comment as done.May 3 2019, 9:46 AM
spatel added inline comments.
llvm/test/CodeGen/X86/vec_fneg.ll
52 ↗(On Diff #198016)

I'm not following. Is there some place before/after getNode() that we should also fix?

arsenm added inline comments.May 3 2019, 9:50 AM
llvm/test/CodeGen/X86/vec_fneg.ll
52 ↗(On Diff #198016)

I mean I don't expect fsub -0, x to be equivalent to fneg x anymore. This test shouldn't be hitting this path, and should be using the fneg instruction?

spatel marked an inline comment as done.May 3 2019, 10:12 AM
spatel added inline comments.
llvm/test/CodeGen/X86/vec_fneg.ll
52 ↗(On Diff #198016)

Ah, I see. So we need to get our patches in order.
I don't think we're ready to pull the plug on SDAG converting 'fsub -0.0, x' to fneg yet because we don't have that canonicalization in IR yet, but let me know if I'm wrong.
Either way, I should have added tests with fneg in the IR, so we don't lose coverage when we do flip that switch.

arsenm added inline comments.May 3 2019, 10:16 AM
llvm/test/CodeGen/X86/vec_fneg.ll
52 ↗(On Diff #198016)

Yes, I assumed this compatibility hack was still in here somewhere, but we need to start adding tests for the pure fneg

spatel updated this revision to Diff 198104.May 3 2019, 3:48 PM

Patch updated:
Rebased after adding tests with the real fneg IR instructions (currently, results in the same test diffs).

llvm/test/CodeGen/X86/vec_fneg.ll
52 ↗(On Diff #198016)

I mean I don't expect fsub -0, x to be equivalent to fneg x anymore. This test shouldn't be hitting this path, and should be using the fneg instruction?

That's actually ok to do. What isn't ok is FNEG(X)->FSUB(-0.0, X). FNEG(X) has clearly defined outputs for some edges cases, e.g. NaNs. FSUB(-0.0, X) does not.

spatel marked an inline comment as done.May 6 2019, 6:49 AM
spatel added inline comments.
llvm/test/CodeGen/X86/vec_fneg.ll
52 ↗(On Diff #198016)

I suspect the subtlety of the NaN behavior diff is not known/forgotten by most people. Should I add a blurb to the LangRef and/or SDAG node code comments about that?

llvm/test/CodeGen/X86/vec_fneg.ll
52 ↗(On Diff #198016)

Sure, or I can do it. I have some time to work on LLVM specific projects right now.

The problematic case is X=+/-NaN. This only applies to FNEG(X) -> FSUB(-0.0, X) transforms, since IEEE-754 *does not* specify the sign-bit of a NaN result for FSUB(-0.0, +/-NaN). IEEE-754 *does* specify the sign-bit for a FNEG(+/-NaN).

That said, IIRC, some architectures make mistakes in practice with FSUB where X=+/-0.0. But, that *is* well defined by IEEE-754. I can brush up on this case if you'd like more detail.

I'll also add that FSUB(-0.0, X) -> FNEG(X) may not be safe for the constrained intrinsics when rounding mode is in effect. I haven't studied that close enough yet, but I've seen enough verbiage in IEEE-754 to know I should be worried about it.

spatel marked an inline comment as done.May 6 2019, 8:19 AM
spatel added inline comments.
llvm/test/CodeGen/X86/vec_fneg.ll
52 ↗(On Diff #198016)

Great - please go ahead with additional examples/docs. IMO, we can always use more of that.

Anything left to do with this patch?

cameron.mcinally marked an inline comment as done.May 6 2019, 8:39 AM
cameron.mcinally added inline comments.
llvm/test/CodeGen/X86/vec_fneg.ll
52 ↗(On Diff #198016)

Great - please go ahead with additional examples/docs. IMO, we can always use more of that.

No problem. On my TODO list.

cameron.mcinally accepted this revision.May 8 2019, 5:37 AM

Anything left to do with this patch?

If there are no other objections, this LGTM.

This revision is now accepted and ready to land.May 8 2019, 5:37 AM
This revision was automatically updated to reflect the committed changes.