This is an archive of the discontinued LLVM Phabricator instance.

[JumpThreading] Simplify FindMostPopularDest (NFC)
ClosedPublic

Authored by kazu on Jun 2 2020, 1:22 PM.

Details

Summary

This patch simplifies FindMostPopularDest without changing the
functionality.

Given a list of jump threading destinations, the function finds the
most popular destination. To ensure determinism when there are
multiple destinations with the highest popularity, the function picks
the first one in the successor list with the highest popularity.

Without this patch:

  • The function populates DestPopularity -- a histogram mapping destinations to their respective occurrence counts.
  • Then we iterate over DestPopularity, looking for the highest popularity while building a vector of destinations with the highest popularity.
  • Finally, we iterate the successor list, looking for the destination with the highest popularity.

With this patch:

  • We implement DestPopularity with MapVector instead of DenseMap. We populate the map with popularity 0 for all successors in the order they appear in the successor list.
  • We build the histogram in the same way as before.
  • We simply use std::max_element on DestPopularity to find the most popular destination. The use of MapVector ensures determinism.

Diff Detail

Event Timeline

kazu created this revision.Jun 2 2020, 1:22 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 2 2020, 1:22 PM
Herald added a subscriber: hiraditya. · View Herald Transcript
wmi accepted this revision.Jun 2 2020, 4:28 PM

LGTM.

This revision is now accepted and ready to land.Jun 2 2020, 4:28 PM
efriedma added inline comments.Jun 2 2020, 4:34 PM
llvm/lib/Transforms/Scalar/JumpThreading.cpp
1490

Initializing DestPopularity like this should be unnecessary, I think; as long as the ordering of PredToDestList is deterministic, the ordering of the resulting MapVector should also be deterministic.

Can we just assert !DestPopularity.empty(), instead of messing with nullptr? If PredToDestList is non-empty, DestPopularity should also be non-empty.over inserting nullptr; inserting nullptr simplifies the control flow, but it's not intuitive.

kazu marked an inline comment as done.Jun 2 2020, 5:30 PM

Thank you two for reviews!

llvm/lib/Transforms/Scalar/JumpThreading.cpp
1490

I am afraid that assuming a certain order in PredToDestList might make the interface a bit fragile because FindMostPopularDest doesn't have control over how PredToDestList is populated.

I agree that the nullptr trick is not very intuitive. However, we cannot assert !DestPopularity.empty() because DestPopularity could be empty when the sole entry in PredToDestList maps undef branch condition to nullptr destination basic block. This situation actually occurs during ninja check-llvm.

This revision was automatically updated to reflect the committed changes.