This is an archive of the discontinued LLVM Phabricator instance.

[llvm][misexpect] Avoid division by 0 when using sample profiling
ClosedPublic

Authored by paulkirth on Apr 22 2022, 2:32 PM.

Details

Summary

MisExpect diagnostics should not prevent compilation from succeeding, and the
assertion is insufficient to prevent division by zero in release builds.

This patch addresses that by replacing the assert with an early return.

Additionally, it disables MisExpect diagnostics when using sample profiling,
since this is the only known case where this error has manifested.

Diff Detail

Event Timeline

paulkirth created this revision.Apr 22 2022, 2:32 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 22 2022, 2:32 PM
Herald added a subscriber: hiraditya. · View Herald Transcript
paulkirth requested review of this revision.Apr 22 2022, 2:32 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 22 2022, 2:32 PM

One concern I have here, since someone reported this to me is that perhaps some of MisExpect's assumptions regarding when branch weight metadata will be attached is erroneous.

The key assumptions we make are that for frontend instrumentation that any branch weights that have been attached when LowerExpectIntrinsics runs will originate from profiling. The other key assumption we make is that when branch weights are assigned for IR instrumentation that LowerExpectIntrinsic is the only place that can add these weights before they are added by the IR or sample profiles. If that's not the case, then we need to discuss a way to address that shortcoming.

That concern doesn't change the need for this fix, but may require some more in depth discussion on discourse about how to address this in a comprehensive manner.

One concern I have here, since someone reported this to me is that perhaps some of MisExpect's assumptions regarding when branch weight metadata will be attached is erroneous.

The key assumptions we make are that for frontend instrumentation that any branch weights that have been attached when LowerExpectIntrinsics runs will originate from profiling. The other key assumption we make is that when branch weights are assigned for IR instrumentation that LowerExpectIntrinsic is the only place that can add these weights before they are added by the IR or sample profiles. If that's not the case, then we need to discuss a way to address that shortcoming.

That concern doesn't change the need for this fix, but may require some more in depth discussion on discourse about how to address this in a comprehensive manner.

The failing test case was with SamplePGO. I went back and looked at the original patch and realized that it doesn't seem to have any test cases for SamplePGO, even though it is being called there. It probably makes sense to look more closely at the IR in that case to see how this code is getting the 0 "expected" edge weights. Rather than this change, how about disabling misexpect verification for sample PGO as a short term fix, then doing some investigation and adding a fix along with sample PGO tests when re-enabling it there?

The failing test case was with SamplePGO. I went back and looked at the original patch and realized that it doesn't seem to have any test cases for SamplePGO, even though it is being called there. It probably makes sense to look more closely at the IR in that case to see how this code is getting the 0 "expected" edge weights. Rather than this change, how about disabling misexpect verification for sample PGO as a short term fix, then doing some investigation and adding a fix along with sample PGO tests when re-enabling it there?

That seems like a reasonable choice. I'm a bit apprehensive about relying on the assert to prevent this type of error though. Maybe we do both for now?

paulkirth updated this revision to Diff 424634.Apr 22 2022, 3:30 PM

Disable diagnostics when using sample profiling

paulkirth updated this revision to Diff 424636.Apr 22 2022, 3:32 PM

fix typo in comment

tejohnson accepted this revision.Apr 22 2022, 3:33 PM

lgtm

Sure, that seems fine to me. I would add a FIXME to add back the assert as well. There are a few typos in the patch description, can you fix those before submitting?

This revision is now accepted and ready to land.Apr 22 2022, 3:33 PM
paulkirth retitled this revision from [llvm][misexpect] Replace assertion with early return to [llvm][misexpect] Avoid division by 0 when using sample profiling.Apr 22 2022, 3:40 PM
paulkirth edited the summary of this revision. (Show Details)
paulkirth updated this revision to Diff 424644.Apr 22 2022, 3:46 PM

Add FIXME comment

This revision was landed with ongoing or failed builds.Apr 22 2022, 3:48 PM
This revision was automatically updated to reflect the committed changes.