This is an archive of the discontinued LLVM Phabricator instance.

[clang] make reciprocal estimate codegen a function attribute
ClosedPublic

Authored by spatel on Sep 21 2016, 3:04 PM.

Details

Summary

Technically, I suppose this patch is independent of the upcoming llvm sibling patch because we can still pass 'check-all' with this alone. But this patch should be tightly coupled with that patch when committed because there's nothing currently in llvm to read this new function attribute string.

The motivation for the change is that we can't have pseudo-global settings for codegen living in TargetOptions because that doesn't work with LTO. And yet, there are so many others there...

Ideally, these reciprocal attributes will be moved to the instruction-level via FMF, metadata, or something else. But making them function attributes is at least an improvement over the current mess.

Diff Detail

Repository
rL LLVM

Event Timeline

spatel updated this revision to Diff 72111.Sep 21 2016, 3:04 PM
spatel retitled this revision from to [clang] make reciprocal estimate codegen a function attribute.
spatel updated this object.
spatel added reviewers: echristo, evandro, hfinkel.
spatel added a subscriber: cfe-commits.
echristo accepted this revision.Oct 2 2016, 7:06 PM
echristo edited edge metadata.

Going to accept this pending the backend patch, but when that one is applied I wanted you to feel OK to add this. A couple of inline nitpick comments and some agreement that we should do something uniform for fast-math is all.

-eric

lib/CodeGen/CGCall.cpp
1735 ↗(On Diff #72111)

Would be nice to get these pulled into a single fast-math string that's set and then used all over for sure. :)

1755 ↗(On Diff #72111)

I commented on naming here in the backend patch, but just want to make them the same.

test/CodeGen/attr-mrecip.c
1 ↗(On Diff #72111)

Shouldn't need to disable the llvm optimizations here.

This revision is now accepted and ready to land.Oct 2 2016, 7:06 PM
spatel marked 2 inline comments as done.Oct 3 2016, 3:39 PM

Thanks, Eric. I actually drafted this with the name "recip-estimates", but thought there might be value in reusing the programmer-visible flag name. I'm good with "reciprocal-estimates" too.

spatel added inline comments.Oct 3 2016, 3:43 PM
lib/CodeGen/CGCall.cpp
1735 ↗(On Diff #72111)

I'm probably not imagining some use case, but I was hoping that we can just delete the 4 (fast/inf/nan/nsz) that are already covered by instruction-level FMF. An auto-upgrade might be needed within LLVM...and/or a big pile of regression test changes?

spatel updated this revision to Diff 73364.Oct 3 2016, 3:49 PM
spatel edited edge metadata.

Patch updated as suggested by Eric:

  1. The attribute is named "reciprocal-estimates".
  2. Remove unnecessary -disable-llvm-optzns flag from test file.

Quick fixes, but this will not go in until the LLVM side (D24816) is updated, and we've answered any remaining questions there.

echristo added inline comments.Oct 3 2016, 4:46 PM
lib/CodeGen/CGCall.cpp
1735 ↗(On Diff #72111)

No, that seems reasonable. I think it makes more sense as a follow-on patch though.

mehdi_amini added inline comments.Oct 3 2016, 5:03 PM
lib/CodeGen/CGCall.cpp
1735 ↗(On Diff #72111)

I agree with getting on a path to remove these function attributes that have an equivalent on per-instruction flag.

I wonder what is the status of these flags in SelectionDAG though? We still have a variant of the flags on the TargetOptions I believe. Are all the uses migrated to per-node flags?

spatel added inline comments.Oct 3 2016, 5:15 PM
lib/CodeGen/CGCall.cpp
1735 ↗(On Diff #72111)

Good point - I think we have to convert all codegen tests that have these function-level attributes to IR FMF and make sure that the output doesn't change. Definitely not part of this patch, but hopefully something that can be done incrementally, test-by-test.

mehdi_amini added inline comments.Oct 3 2016, 5:54 PM
lib/CodeGen/CGCall.cpp
1735 ↗(On Diff #72111)

I wonder if we couldn’t have this part of the bitcode/IR auto-upgrade: when we load a function with this attribute, we automatically add the individual flag on every instruction.

spatel added inline comments.Oct 4 2016, 8:17 AM
lib/CodeGen/CGCall.cpp
1735 ↗(On Diff #72111)

Auto-upgrading is part of the solution. Based on how we've been doing this with vector intrinsics that get converted to IR, it's a ~3-step process:

  1. Prepare the backend (DAG) to handle the expected new IR patterns and add tests for those.
  2. Auto-upgrade the IR, remove deprecated handling of the old IR patterns, and change/remove existing tests.
  3. Update clang to not produce the deprecated patterns.

The extra step for FMF in the DAG is that we still don't allow FMF on all SDNode subclasses. The DAG plumbing for FMF only applies to binops because that's all that FMF on IR worked on at the time (fmul/fadd/fsub/fdiv/frem). Later, I added FMF to IR calls so we could have that functionality on sqrt, fma and other calls. Assuming that is ok (and I realize that it may be controversial), we can now extend FMF in the DAG to all SDNodes and have a full node-level FMF solution for the DAG layer.

mehdi_amini added inline comments.Oct 4 2016, 10:12 AM
lib/CodeGen/CGCall.cpp
1735 ↗(On Diff #72111)

I think I remember folks being against FMF on calls (Chris Lattner?), I'll try to find the relevant discussion.
Otherwise your plan seems fine to me!

echristo added inline comments.Oct 4 2016, 10:32 AM
lib/CodeGen/CGCall.cpp
1735 ↗(On Diff #72111)

Agreed. Also shouldn't hold up this patch :)

spatel added inline comments.Oct 4 2016, 10:49 AM
lib/CodeGen/CGCall.cpp
1735 ↗(On Diff #72111)

Yes - Chris was opposed to FMF on intrinsics (preferring parameters/metadata as the delivery mechanism instead) in 2012:
http://lists.llvm.org/pipermail/llvm-commits/Week-of-Mon-20121217/159446.html

...which you mentioned and I replied to in the post-commit thread when I added FMF to any FPMathOperator calls earlier this year:
http://lists.llvm.org/pipermail/llvm-commits/Week-of-Mon-20160104/323154.html

There were no replies to that thread on llvm-dev since my January post. I will re-post the question to llvm-dev before proceeding.

This revision was automatically updated to reflect the committed changes.