This is an archive of the discontinued LLVM Phabricator instance.

Statically analyze likely and unlikely blocks based on metadata
ClosedPublic

Authored by hiraditya on Aug 31 2023, 11:02 PM.

Details

Summary

The builtin_expect(), and C++20's likely, unlikely attributes assign branch_weights to annotated branches.

This patch adds the the ability to query branch !prof metadata and improve static analysis based on that.

https://github.com/llvm/llvm-project/issues/64998

Diff Detail

Event Timeline

hiraditya created this revision.Aug 31 2023, 11:02 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 31 2023, 11:02 PM
Herald added subscribers: hoy, ormris. · View Herald Transcript
hiraditya requested review of this revision.Aug 31 2023, 11:02 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 31 2023, 11:02 PM
hiraditya edited the summary of this revision. (Show Details)
tejohnson added inline comments.Sep 1 2023, 2:10 PM
llvm/lib/Transforms/IPO/HotColdSplitting.cpp
143

I'm confused about how this works. It appears that analyzeProfMetadata analyzes the successors of BB, but then we query the information for the current BB. Ah, it looks like this works because this is called in a reversed PO traversal of the BBs?

It would be good to add some comments here. Otherwise it looks a little confusing as the back to back call and set lookup make it appear as though the call is doing the work for the set lookup in the same invocation, but really it is doing the work for a later invocation, with unstated traversal ordering assumptions.

147

When does this happen?

150

Should this use BranchProbability::getBranchProbability instead of the constructor directly?

Also, is the (1,1000) threshold related to or the same as the branch probability assigned for builtin_expect and likely/unlikely?

659–660

Should the static analysis i.e. the branch probabilities only be looked at if there is no BFI aka profile information? I think if there is profile info that the creation of the BFI/PSI has already taken expect and unlikely/likely attributes into account?

llvm/test/Transforms/HotColdSplit/split-static-profile.ll
35

Missing "CHECK:"?

hiraditya marked 2 inline comments as done.Sep 6 2023, 3:17 PM
hiraditya added inline comments.
llvm/lib/Transforms/IPO/HotColdSplitting.cpp
143

yes it is due to RPOT. i'have added comments.

147

i see this in https://llvm.org/doxygen/JumpThreading_8cpp_source.html so added as precaution. although i'm not 100% sure about it.

150

builtins adds 1/2000 but i think that is too low. i've modified this to reflect builtins but added a flag for users to provide as they see fit.

659–660

that is possible for unlikely/likely attributes true. but unlikelyExecuted does more analysis taking into account ehpad, noreturn functions etc. i'll refactor this code a little.

hiraditya updated this revision to Diff 556088.Sep 6 2023, 3:18 PM
hiraditya marked an inline comment as done.
hiraditya marked 2 inline comments as done.
tejohnson added inline comments.Sep 9 2023, 5:56 AM
llvm/lib/Transforms/IPO/HotColdSplitting.cpp
128

Is there an internal option that determines the value used there that can be used in the code here? Looks like it is controlled by LikelyBranchWeight - can that be made a global option and referenced here so that they don't get out of sync?

llvm/test/Transforms/HotColdSplit/split-static-profile.ll
5

Add baz code here too?

27

I think these cases should also be the same in the CHECK-PROB case - probably want a common check label to check the common cases between the two invocations, and a different check label just for the different parts.

Also, probably want to ensure that the hot calls/funcs are all above the cold calls/funcs - the checks below don't guarantee that.

hiraditya added inline comments.Sep 11 2023, 3:45 PM
llvm/lib/Transforms/IPO/HotColdSplitting.cpp
128

found one. it has a higher probability but that is what other parts of llvm is using so i'll use the same here.

llvm/test/Transforms/HotColdSplit/split-static-profile.ll
27

makes sense. updated the tests.

hiraditya updated this revision to Diff 556495.Sep 11 2023, 3:46 PM
hiraditya added inline comments.Sep 11 2023, 3:49 PM
llvm/lib/Transforms/IPO/HotColdSplitting.cpp
128

TTI.getPredictableBranchThreshold is what i used. it seems this is the recommended API to use for branch threshold (https://llvm.org/doxygen/LowerExpectIntrinsic_8cpp_source.html)

tejohnson accepted this revision.Sep 11 2023, 7:36 PM

lgtm wth a couple of small test tweaks.

llvm/test/Transforms/HotColdSplit/split-static-profile.ll
5

Suggest moving this comment to the top of file

99

Suggest moving these CHECK-PROB lines up earlier, to where you have the CHECK-NOOUTLINE-BAZ-NOT, and the checks for the other 2 functions, so that all the checks are consolidated.

103

Probably check the attributes once with CHECK-OUTLINE, which covers both cases (I think the CHECK-OUTLINE,CHECK-NOOUTLINE-BAZ case is not checking the attributes currently).

This revision is now accepted and ready to land.Sep 11 2023, 7:36 PM
hiraditya marked 2 inline comments as done.
hiraditya marked 2 inline comments as done.