This is an archive of the discontinued LLVM Phabricator instance.

[PGO] MST min edge selection heuristic to ensure non-zero entry count
ClosedPublic

Authored by davidxl on Dec 10 2017, 3:19 PM.

Details

Summary

In many programs, event handling loop (not statically infinite) runs forever and the normal exit path is not executed unless there is error condition. Profile dumping can happen asynchronously. For such functions, if the entry block is not instrumented, we will get zero entry count which can be bad -- PGO relies on entry count to get BB count via scaling. See test case in D41058

This patch implements a new heuristic to select min edges -- if entry edges and exit edges have similar weight, prefer to select exit edges to be min edges.

This patch also fixes the problem with the real infinite loop case where entry count is zero.

Diff Detail

Repository
rL LLVM

Event Timeline

davidxl created this revision.Dec 10 2017, 3:19 PM
xur added a comment.Dec 15 2017, 1:33 PM

This is a good improvement.

lib/Transforms/Instrumentation/CFGMST.h
183 ↗(On Diff #126303)

Will this ever be true? EntryInWeight should be always greater than or equal to weight of Exit BB.

198 ↗(On Diff #126303)

Do we need to make sure EntryInWeight less than MaxEntryOutWeight?

If Entry BB has more than one successors, and Exit BB has more than one predecessors,
even after adjustment, EntryIncoming->Weight might be greater than ExitOutgoing->Weight.

EntryIncoming ExitOutgoing and ExitIncoming will be in MST (thus not instrumented).
EntryOutgoing will be instrumented. But since it has two successors, we will instrumented the target BB.
(i.e. entryBB is not instrumented).

But I'm sure how this relates to the problem this patch wants to solve. It seems to me that choice b/w instrumenting entry and exit only applies the case of single successor/predecessor for entry and exit nodes.

davidxl added inline comments.Dec 15 2017, 2:48 PM
lib/Transforms/Instrumentation/CFGMST.h
183 ↗(On Diff #126303)

Probably not -- will remove.

198 ↗(On Diff #126303)

EntryIncomingWeight is adjusted to be smaller than MaxExitOutgoingWeight.

In your case, all EntryOutgoingEdges will be selected for instrumentation, and their sum will be the entry count. Unless One of entry outgoing edges is a critical edge which will be selected as Min(ST) edge, in which case, the entryIncomingEdge won't need to be in MST and therefore will be instrumented. That is why we need to make sure entryIncoming weight is smaller than exitoutoging weight.

davidxl updated this revision to Diff 127198.Dec 15 2017, 3:20 PM

address comment: remove unnecessary early return check.

The instrumentation point will be before the no-return call, so there is probably no issue with instrumenting direct successor of the entry.

xur accepted this revision.Dec 15 2017, 3:30 PM

Look good to me.

lib/Transforms/Instrumentation/CFGMST.h
184 ↗(On Diff #127198)

NIT: Should we remove this also. This is the same as the removed condition.

This revision is now accepted and ready to land.Dec 15 2017, 3:30 PM
This revision was automatically updated to reflect the committed changes.