[llvm] r272729 - Set machine block placement hot prob threshold for both static and runtime profile.

Xinliang David Li via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 14 22:25:11 PDT 2016


On Tue, Jun 14, 2016 at 10:10 PM, Dehao Chen <dehao at google.com> wrote:

> Thanks for fixing this. But I think the current equation is still
> incorrect because if ProfileLikelyProb>75, the probability will be > 100%
>
> I thinks the right equation is:
>
> Let's use S to denote Prob(BB->succ), P to denote Prob(BB->pred), L to
> denote ProfileLikelyProb
>
> S / (S + P * 2) = L
> the new threshold for triangle case = S / (S + P) = L / (2 - L)
>
> With this, when L is 50%, return threshold = 100 / 150
> when L is 100% return threshold = 100 / 100
>

I am not sure.   Branch cost based threshold computation should be
independent of value 'L'.  On the other hand, how do we honor user
specified branch bias is implementation defined, so I would like to have it
more intuitive (for >100% case we can simply cap it).   Your newly proposed
formula maps well to the range of 67% to 100% continuously, but the problem
I don't see good intuition -- or you can explain it to me ;)

thanks,

David



>
> Dehao
>
> On Tue, Jun 14, 2016 at 8:32 PM, Sean Silva <chisophugis at gmail.com> wrote:
>
>> Thanks!
>>
>> On Tue, Jun 14, 2016 at 8:12 PM, Xinliang David Li <xinliangli at gmail.com>
>> wrote:
>>
>>> Added documentation.
>>>
>>> I actually overlooked an error in the code review. The patch produces
>>> the right threshold by default, but when parameter is set to be
>>> non-default, the result is not correct. I have corrected it in this patch.
>>>
>>> David
>>>
>>>
>>>
>>> On Tue, Jun 14, 2016 at 7:11 PM, Sean Silva via llvm-commits <
>>> llvm-commits at lists.llvm.org> wrote:
>>>
>>>>
>>>>
>>>> On Tue, Jun 14, 2016 at 3:27 PM, Dehao Chen via llvm-commits <
>>>> llvm-commits at lists.llvm.org> wrote:
>>>>
>>>>> Author: dehao
>>>>> Date: Tue Jun 14 17:27:17 2016
>>>>> New Revision: 272729
>>>>>
>>>>> URL: http://llvm.org/viewvc/llvm-project?rev=272729&view=rev
>>>>> Log:
>>>>> Set machine block placement hot prob threshold for both static and
>>>>> runtime profile.
>>>>>
>>>>> Summary: With runtime profile, we have more confidence in branch
>>>>> probability, thus during basic block layout, we set a lower hot prob
>>>>> threshold so that blocks can be layouted optimally.
>>>>>
>>>>> Reviewers: djasper, davidxl
>>>>>
>>>>> Subscribers: llvm-commits
>>>>>
>>>>> Differential Revision: http://reviews.llvm.org/D20991
>>>>>
>>>>> Modified:
>>>>>     llvm/trunk/lib/CodeGen/MachineBlockPlacement.cpp
>>>>>     llvm/trunk/lib/CodeGen/MachineBranchProbabilityInfo.cpp
>>>>>     llvm/trunk/test/CodeGen/X86/block-placement.ll
>>>>>
>>>>> Modified: llvm/trunk/lib/CodeGen/MachineBlockPlacement.cpp
>>>>> URL:
>>>>> http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/MachineBlockPlacement.cpp?rev=272729&r1=272728&r2=272729&view=diff
>>>>>
>>>>> ==============================================================================
>>>>> --- llvm/trunk/lib/CodeGen/MachineBlockPlacement.cpp (original)
>>>>> +++ llvm/trunk/lib/CodeGen/MachineBlockPlacement.cpp Tue Jun 14
>>>>> 17:27:17 2016
>>>>> @@ -125,6 +125,7 @@ BranchFoldPlacement("branch-fold-placeme
>>>>>                cl::init(true), cl::Hidden);
>>>>>
>>>>>  extern cl::opt<unsigned> StaticLikelyProb;
>>>>> +extern cl::opt<unsigned> ProfileLikelyProb;
>>>>>
>>>>>  namespace {
>>>>>  class BlockChain;
>>>>> @@ -520,13 +521,20 @@ bool MachineBlockPlacement::shouldPredBl
>>>>>      return false;
>>>>>  }
>>>>>
>>>>> -// FIXME (PGO handling)
>>>>> -// For now this method just returns a fixed threshold. It needs to be
>>>>> enhanced
>>>>> -// such that BB and Succ is passed in so that CFG shapes are examined
>>>>> such that
>>>>> -// the threshold is computed with more precise cost model when PGO is
>>>>> on.
>>>>> -static BranchProbability getLayoutSuccessorProbThreshold() {
>>>>> -  BranchProbability HotProb(StaticLikelyProb, 100);
>>>>> -  return HotProb;
>>>>> +// When profile is not present, return the StaticLikelyProb.
>>>>> +// When profile is available, we need to handle the triangle-shape
>>>>> CFG.
>>>>> +static BranchProbability getLayoutSuccessorProbThreshold(
>>>>> +      MachineBasicBlock *BB) {
>>>>> +  if (!BB->getParent()->getFunction()->getEntryCount())
>>>>> +    return BranchProbability(StaticLikelyProb, 100);
>>>>> +  if (BB->succ_size() == 2) {
>>>>> +    const MachineBasicBlock *Succ1 = *BB->succ_begin();
>>>>> +    const MachineBasicBlock *Succ2 = *(BB->succ_begin() + 1);
>>>>> +    if (Succ1->isSuccessor(Succ2) || Succ2->isSuccessor(Succ1))
>>>>> +      return BranchProbability(
>>>>> +          200 - 2 * ProfileLikelyProb, 200 - ProfileLikelyProb);
>>>>>
>>>>
>>>> Please comment on how these numbers were derived.
>>>>
>>>>
>>>>> +  }
>>>>> +  return BranchProbability(ProfileLikelyProb, 100);
>>>>>  }
>>>>>
>>>>>  /// Checks to see if the layout candidate block \p Succ has a better
>>>>> layout
>>>>> @@ -593,7 +601,7 @@ bool MachineBlockPlacement::hasBetterLay
>>>>>    // edge: Prob(Succ->BB) needs to >= HotProb in order to be selected
>>>>> (without
>>>>>    // profile data).
>>>>>
>>>>> -  BranchProbability HotProb = getLayoutSuccessorProbThreshold();
>>>>> +  BranchProbability HotProb = getLayoutSuccessorProbThreshold(BB);
>>>>>
>>>>>    // Forward checking. For case 2, SuccProb will be 1.
>>>>>    if (SuccProb < HotProb) {
>>>>>
>>>>> Modified: llvm/trunk/lib/CodeGen/MachineBranchProbabilityInfo.cpp
>>>>> URL:
>>>>> http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/MachineBranchProbabilityInfo.cpp?rev=272729&r1=272728&r2=272729&view=diff
>>>>>
>>>>> ==============================================================================
>>>>> --- llvm/trunk/lib/CodeGen/MachineBranchProbabilityInfo.cpp (original)
>>>>> +++ llvm/trunk/lib/CodeGen/MachineBranchProbabilityInfo.cpp Tue Jun 14
>>>>> 17:27:17 2016
>>>>> @@ -29,6 +29,12 @@ cl::opt<unsigned> StaticLikelyProb(
>>>>>      cl::desc("branch probability threshold to be considered very
>>>>> likely"),
>>>>>      cl::init(80), cl::Hidden);
>>>>>
>>>>> +cl::opt<unsigned> ProfileLikelyProb(
>>>>> +    "profile-likely-prob",
>>>>> +    cl::desc("branch probability threshold to be considered very
>>>>> likely "
>>>>> +             "when profile is available"),
>>>>> +    cl::init(51), cl::Hidden);
>>>>>
>>>>
>>>> Please mention in the description that this is a percentage.
>>>>
>>>> -- Sean Silva
>>>>
>>>>
>>>>> +
>>>>>  char MachineBranchProbabilityInfo::ID = 0;
>>>>>
>>>>>  void MachineBranchProbabilityInfo::anchor() {}
>>>>>
>>>>> Modified: llvm/trunk/test/CodeGen/X86/block-placement.ll
>>>>> URL:
>>>>> http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/block-placement.ll?rev=272729&r1=272728&r2=272729&view=diff
>>>>>
>>>>> ==============================================================================
>>>>> --- llvm/trunk/test/CodeGen/X86/block-placement.ll (original)
>>>>> +++ llvm/trunk/test/CodeGen/X86/block-placement.ll Tue Jun 14 17:27:17
>>>>> 2016
>>>>> @@ -1207,4 +1207,82 @@ exit:
>>>>>    ret void
>>>>>  }
>>>>>
>>>>> +define void @test_hot_branch_profile(i32* %a) !prof !6 {
>>>>> +; Test that a hot branch that has a probability a little larger than
>>>>> 60% will
>>>>> +; break CFG constrains when doing block placement when profile is
>>>>> available.
>>>>> +; CHECK-LABEL: test_hot_branch_profile:
>>>>> +; CHECK: %entry
>>>>> +; CHECK: %then
>>>>> +; CHECK: %exit
>>>>> +; CHECK: %else
>>>>> +
>>>>> +entry:
>>>>> +  %gep1 = getelementptr i32, i32* %a, i32 1
>>>>> +  %val1 = load i32, i32* %gep1
>>>>> +  %cond1 = icmp ugt i32 %val1, 1
>>>>> +  br i1 %cond1, label %then, label %else, !prof !5
>>>>> +
>>>>> +then:
>>>>> +  call void @hot_function()
>>>>> +  br label %exit
>>>>> +
>>>>> +else:
>>>>> +  call void @cold_function()
>>>>> +  br label %exit
>>>>> +
>>>>> +exit:
>>>>> +  call void @hot_function()
>>>>> +  ret void
>>>>> +}
>>>>> +
>>>>> +define void @test_hot_branch_triangle_profile(i32* %a) !prof !6 {
>>>>> +; Test that a hot branch that has a probability a little larger than
>>>>> 80% will
>>>>> +; break triangle shaped CFG constrains when doing block placement if
>>>>> profile
>>>>> +; is present.
>>>>> +; CHECK-LABEL: test_hot_branch_triangle_profile:
>>>>> +; CHECK: %entry
>>>>> +; CHECK: %exit
>>>>> +; CHECK: %then
>>>>> +
>>>>> +entry:
>>>>> +  %gep1 = getelementptr i32, i32* %a, i32 1
>>>>> +  %val1 = load i32, i32* %gep1
>>>>> +  %cond1 = icmp ugt i32 %val1, 1
>>>>> +  br i1 %cond1, label %exit, label %then, !prof !5
>>>>> +
>>>>> +then:
>>>>> +  call void @hot_function()
>>>>> +  br label %exit
>>>>> +
>>>>> +exit:
>>>>> +  call void @hot_function()
>>>>> +  ret void
>>>>> +}
>>>>> +
>>>>> +define void @test_hot_branch_triangle_profile_topology(i32* %a) !prof
>>>>> !6 {
>>>>> +; Test that a hot branch that has a probability between 50% and 66%
>>>>> will not
>>>>> +; break triangle shaped CFG constrains when doing block placement if
>>>>> profile
>>>>> +; is present.
>>>>> +; CHECK-LABEL: test_hot_branch_triangle_profile_topology:
>>>>> +; CHECK: %entry
>>>>> +; CHECK: %then
>>>>> +; CHECK: %exit
>>>>> +
>>>>> +entry:
>>>>> +  %gep1 = getelementptr i32, i32* %a, i32 1
>>>>> +  %val1 = load i32, i32* %gep1
>>>>> +  %cond1 = icmp ugt i32 %val1, 1
>>>>> +  br i1 %cond1, label %exit, label %then, !prof !7
>>>>> +
>>>>> +then:
>>>>> +  call void @hot_function()
>>>>> +  br label %exit
>>>>> +
>>>>> +exit:
>>>>> +  call void @hot_function()
>>>>> +  ret void
>>>>> +}
>>>>> +
>>>>>  !5 = !{!"branch_weights", i32 84, i32 16}
>>>>> +!6 = !{!"function_entry_count", i32 10}
>>>>> +!7 = !{!"branch_weights", i32 60, i32 40}
>>>>>
>>>>>
>>>>> _______________________________________________
>>>>> llvm-commits mailing list
>>>>> llvm-commits at lists.llvm.org
>>>>> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits
>>>>>
>>>>
>>>>
>>>> _______________________________________________
>>>> llvm-commits mailing list
>>>> llvm-commits at lists.llvm.org
>>>> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits
>>>>
>>>>
>>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160614/08c58dd0/attachment.html>


More information about the llvm-commits mailing list