This is an archive of the discontinued LLVM Phabricator instance.

[clang] Add support for optional flag -fnew-infallible to restrict exception propagation
ClosedPublic

Authored by modimo on Jun 30 2021, 12:50 PM.

Details

Summary

The declaration for the global new function in C++ is generated in the compiler front-end. When examining exception propagation, we found that this is the largest root throw site propagator requiring unwind code to be generated for callers up the stack. Allowing this to be handled immediately with termination stops upward propagation and leads to significantly less landing pads generated. This in turns leads to a performance and .text size win.

With -fnew-infallible this annotates the declaration with throw() and __attribute__((returns_nonnull)). throw() allows the compiler to assume exceptions do not propagate out of new and eliminate it as a root throw site. Note that the definition of global new is user-replaceable so users should ensure that the one used follows these semantics.

Measuring internally, we're seeing at 0.5% CPU win in one of our large internal FB workload. Measuring on clang self-build (cd0a1226b50081e86eb75a89d01e8782423971a0) we get:

thinlto/

"dwarfehprepare.NumCleanupLandingPadsRemaining": 153494,
"dwarfehprepare.NumNoUnwind": 26309,

thinlto_newinfallible/

"dwarfehprepare.NumCleanupLandingPadsRemaining": 143660,
"dwarfehprepare.NumNoUnwind": 28744,

a 1-143660/153494 = 6.4% reduction in landing pads and a 28744/26309 = 9.3% increase in the number of nounwind functions.

Testing:
ninja check-all
new test case to make sure these attributes are added correctly to global new.

Diff Detail

Event Timeline

modimo created this revision.Jun 30 2021, 12:50 PM
modimo requested review of this revision.Jun 30 2021, 12:50 PM
urnathan added inline comments.Jul 6 2021, 5:16 AM
clang/docs/ClangCommandLineReference.rst
1947

I wonder if we should mention this is detectable in source -- noexcept(::operator new (5)) will return true, not false. Of course, why are you testing the noexceptness of a function with known throwingness?

urnathan accepted this revision.Jul 6 2021, 8:41 AM

Other than maybe commenting on non-conformance, LGTM

This revision is now accepted and ready to land.Jul 6 2021, 8:41 AM
jansvoboda11 added inline comments.
clang/include/clang/Driver/Options.td
2705

Moving Flags and MarshallingInfoFlag on a new line would make this more readable.

FYI I'll be out on vacation until Aug. 1st so I'll pick this back up when I get back.

Other than maybe commenting on non-conformance, LGTM

Is this talking about commenting somewhere that this is detectable in source?

Other than maybe commenting on non-conformance, LGTM

Is this talking about commenting somewhere that this is detectable in source?

Yes, sorry for not making that clear. Pedantically it is non-conforming, but I don't think code will be detrimentally impacted in practice.

modimo updated this revision to Diff 363588.Aug 2 2021, 3:36 PM
modimo marked an inline comment as done.

Rebase and formatting.

modimo marked an inline comment as done.Aug 2 2021, 3:40 PM
This revision was landed with ongoing or failed builds.Aug 2 2021, 3:47 PM
This revision was automatically updated to reflect the committed changes.
Herald added a project: Restricted Project. · View Herald TranscriptAug 2 2021, 3:47 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
rsmith added a subscriber: rsmith.Aug 3 2021, 12:06 AM

Sorry I'm late to the party here... is there any work ongoing to add this to GCC too? If not, would it make sense to send a quick note to the GCC development list pointing this out to reduce the chance that they add a similar feature with a different flag name?

Sorry I'm late to the party here... is there any work ongoing to add this to GCC too? If not, would it make sense to send a quick note to the GCC development list pointing this out to reduce the chance that they add a similar feature with a different flag name?

No ongoing work to add this to GCC. Pinged mailing list here: https://gcc.gnu.org/pipermail/gcc/2021-August/236969.html