This is an archive of the discontinued LLVM Phabricator instance.

[LoopUnswitch] Split unswitch quota based on branch weight
AbandonedPublic

Authored by reames on Oct 14 2015, 10:22 PM.

Details

Summary

This patch teaches LoopUnswitch to split unswitch quota to old loop and newly generated loop based on branch weight.

Consider the following example

for (...)
   if (cond1) 
     dummy1()
   else
     dummy2()
   if (cond2)
     dummy3()
   else
     break

This loop can be unswitched twice based on cond1 and cond2, ending with 4 loops if we have enough quota. However, if we dont have enough quota, we should spend most of them on the hot branch based on branch weight. In this example, if the branch to dummy1() is hotter than dummy2(), we should give the quota to loop with dummy1() and end up with:

if (cond1)
  if (cond2)
    for (...)
      dummy1()
      dummy3()
  else
    for (...)
      dummy1()
      break
else
  for (...) 
    dummy2()
    if (cond2)
      dummy3()
    else
      break

If we don't spilt quota based on branch weight, the colder branch will get unswitched and not help performance.

Diff Detail

Event Timeline

chenli retitled this revision from to [LoopUnswitch] Spill unswitch quota based on branch weight.
chenli updated this object.
chenli added reviewers: reames, davidxl.
chenli added a subscriber: llvm-commits.
davidxl added inline comments.Oct 15 2015, 5:09 PM
lib/Transforms/Scalar/LoopUnswitch.cpp
378

spill --> split?

384

TotalWeight = OldLoopWeight + NewLoopWeight;

... = (Quota * NewLoopWeight + TotalWeight/2)/TotalWeight;

for the right rounding.

571

Why not using BPI interfaces?

reames added inline comments.Oct 15 2015, 5:22 PM
lib/Transforms/Scalar/LoopUnswitch.cpp
187

This should probably be a cl::opt. One should be 100-TheOther.

573

This isn't quite right when the LoopCond isn't BI->getCondition. The branch's weight doesn't tell us anything about LoopConds likelyhood of failing.

580

Wait, what?! Oh, these aren't normalized. I'd probably do the normalization here rather than at the use site. It'd be more clear.

test/Transforms/LoopUnswitch/unswitch-with-branch-weight.ll
18

This comment (both here and in the change description) is really confusing. You're not choosing between cond1 and cond2 at all. You're choosing which *version* of cond2 might get unswitched once the unswitch of cond1 is done.

45

This test case is really hard to follow. Please write this out in full so that the unswitched loop structure can be seen.

chenli retitled this revision from [LoopUnswitch] Spill unswitch quota based on branch weight to [LoopUnswitch] Split unswitch quota based on branch weight.Oct 15 2015, 8:45 PM
chenli updated this object.
reames requested changes to this revision.Oct 28 2015, 9:21 PM
reames edited edge metadata.

Marking as "Request Changes" since previous review comments don't seem to have been addressed. Just getting it more clearly marked in my review queue.

This revision now requires changes to proceed.Oct 28 2015, 9:21 PM
reames commandeered this revision.May 18 2017, 9:22 AM
reames edited reviewers, added: chenli; removed: reames.
reames abandoned this revision.May 18 2017, 9:23 AM