This is an archive of the discontinued LLVM Phabricator instance.

[PGO] Avoid potential const_cast UB (NFC)
ClosedPublic

Authored by Dinistro on Apr 21 2023, 1:17 AM.

Details

Summary

This commit removes potential UB in the PGO instrumentation passes that
was caused by casting away constness and then potentially modifying the
object.

Diff Detail

Event Timeline

Dinistro created this revision.Apr 21 2023, 1:17 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 21 2023, 1:17 AM
Dinistro requested review of this revision.Apr 21 2023, 1:17 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 21 2023, 1:17 AM
gysit accepted this revision.Apr 23 2023, 11:51 PM

LGTM!

This revision is now accepted and ready to land.Apr 23 2023, 11:51 PM
xur added a comment.Apr 24 2023, 9:30 AM

Thanks for working on this.
PGOEdge is shared by PGO instrumentation pass and PGO profile-use pass. We cast away const for PGO instrumentation. But for PGO profile-use, we don't change the BB. I would prefer to keep the const for profile-use passes. So I think a better fix is to make PGOEdge a template class, one with const and one without.

Added a comment to the critical UB causing part. Again, while there might be a way to keep constness, it requires extended changes. Fixing UB should be done first, afterwards, we can think about refactorings that allow us to keep constness where possible.

llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp
840

This full function is shared between the instrumentation and the use pass. Keeping const will always result in UB, as the SplitCriticalEdge function will be called with TI, which should be of type const Instruction *.
I fail to see how a templated edge class would help in resolving this issue, could you elaborate @xur ?

I believe that splitting the logic between instrument and usage might help to reduce the complexity and push const in some places, but that requires extended changes to the algorithms.

@xur how should we proceed with this. It seems like eliminating UB in favor of less constness seems like a good initial step?

This revision was automatically updated to reflect the committed changes.