[llvm-commits] [PATCH] Preserve branch weight metadata when creating switches in SimplifyCFG
Manman Ren
mren at apple.com
Fri Aug 17 14:55:54 PDT 2012
Hi Alastair,
Thanks for the patch.
Some of the logic is not clear to me.
For example, in FoldValueComparisionIntoPredecessors, if PredDefault is not equal to BB:
P --> 1 --> S1 (weightP_1)
2 --> BB (weightP_2)
3 --> BB (weightP_3)
default -> PredDefault (weightP_0)
BB -> 2 --> S2 (weightB_1)
4 --> S3 (wegihtB_2)
default -> BBDefault (weightB_0)
We will create a new switch for P:
P --> 1 --> S1 (weightP_1)
2 --> S2 (weightP_2)
3 --> default (weightP_3)
default -> PredDefault (weightP_0)
But from you patch, I think we will get
P --> 1 --> S1 (weightP_1)
2 --> S2 (weightB_2)
3 --> default (0)
default -> PredDefault (not sure, it seems that you are updating DefaultWeight inside a loop)
The case where PredDefault is equal to BB is more complicated:
P --> 1 --> BB (weightP_1)
2 --> S1 (weightP_2)
3 --> S2 (weightP_3)
default -> BB (weightP_0)
BB -> 2 --> S3 (weightB_1)
4 --> S4 (weightB_2)
default -> BBDefault (weightB_0)
We will create:
P --> 2 --> S1 (weightP_2*(weightB_0 + weightB_2))
3 --> S2 (weightP_3*(weightB_0 + weightB_2))
4 --> S4 ((weightP_1+weightP_0)*weightB_2)
default -> BBDefault ((weightP_1+weightP_0)*weightB_0)
I may be wrong in what the weight should be, since it is just complicated :)
You can look at how we update the metadata in FoldBranchToCommonDest if you have not done so.
I also think we should have some utilities for fixed-point arithmetic such as (APInt with a scale factor) to avoid handling overflow all the time.
Thanks,
Manman
On Aug 17, 2012, at 11:45 AM, Alastair Murray wrote:
> Hello all,
>
> Evan Cheng is CC'ed because he originally suggested I look at this issue, Manman Ren is CC'ed because he has expressed interest in this topic.
>
> Please review the attached patch. Note: I don't have commit rights.
>
> The patch preserves branch weight metadata in SimplifyCFG in various situations where switch instructions are inserted (converting linked if-statements or icmp's to a switch instruction, merging switch instructions, merging an if-statement into a switch instruction).
>
> As metadata can not be assumed to be consistent with the current form of the IR the added code only preserves metadata if it seems consistent.
>
> It has been tested with both profiling data (over projects/test-suite) and __builtin_expect. In the case of __builtin_expect the branch weights are scaled when creating switch instructions to account for the different probability model. if (C1) { A } else if (C2) { B} means P(B) = P(C2 | !C1), but a switch statement just models the probability as P(C2).
>
> Regards,
> Alastair Murray
> <preserve_switch_branch_metadata.patch>
More information about the llvm-commits
mailing list