This is an archive of the discontinued LLVM Phabricator instance.

[SimplifyCFG] Don't speculatively execute BB if it's predictably not taken
ClosedPublic

Authored by lebedev.ri on Jul 23 2021, 4:29 AM.

Details

Summary

If the branch isn't unpredictable, and it is predicted to *not* branch
to the block we are considering speculatively executing,
then it seems counter-productive to execute the code that is predicted not to be executed.

Diff Detail

Event Timeline

lebedev.ri created this revision.Jul 23 2021, 4:29 AM
lebedev.ri requested review of this revision.Jul 23 2021, 4:29 AM
spatel accepted this revision.Jul 24 2021, 4:08 AM

LGTM

llvm/lib/Transforms/Utils/SimplifyCFG.cpp
2380–2384

I think it would be easier to read without the potential double complement:

if (BI->extractProfMetadata(TWeight, FWeight) && (TWeight + FWeight) != 0) {
  uint64_t EndWeight = Invert ? TWeight : FWeight;
  BranchProbability BIEndProb =
      BranchProbability::getBranchProbability(EndWeight, TWeight + FWeight);

(although the meaning of Invert seems reversed from the code example in the function comment at line 2331)

llvm/test/Transforms/SimplifyCFG/speculatively-execute-block-profmd.ll
254

Add one more negative test to verify that we are respecting the TTI setting? It would need a different metadata - { 98, 2 } or similar.

This revision is now accepted and ready to land.Jul 24 2021, 4:08 AM

LGTM

Thank you for the review!
Please note that D106717 is another similar patch.
I do hope to merge these two functions, but later.

A bit of necro-posting, but i was looking at this code again, and i'm wondering,
if the profile says that the only block that we are about to speculate
is predictably taken, does it actually make sense to speculate it,
incurring the cost of the select?