This is an archive of the discontinued LLVM Phabricator instance.

[llvm-profgen] Filter out oversized LBR ranges.
ClosedPublic

Authored by wlei on May 11 2022, 10:54 PM.

Details

Summary

As a follow up to D123271: [llvm-profgen] Filter out invalid LBR ranges., LBR ranges that are too big should also be considered as invalid.

For example, the last two pairs in the following trace form a range [0x0d7b02b0, 0x368ba706] that covers a ton of functions in the binary. Such oversized range should also be ignored.

0x0c74505f/0x368b99a0 **0x368ba706**/0x0c745040  0x0d7b1c3f/**0x0d7b02b0**

Add a defensive check to filter out those ranges based that the valid range should not cross the unconditional branch(Call, return, unconditional jmp).

Diff Detail

Event Timeline

wlei created this revision.May 11 2022, 10:54 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 11 2022, 10:54 PM
Herald added subscribers: hoy, wenlei. · View Herald Transcript
wlei requested review of this revision.May 11 2022, 10:54 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 11 2022, 10:54 PM
wenlei added inline comments.May 11 2022, 11:04 PM
llvm/tools/llvm-profgen/PerfReader.h
213

nit: isValidLBRRange->isValidFallThroughRange.

llvm/tools/llvm-profgen/ProfiledBinary.h
99–105

I think we can skip the max func size check for simplicity. 99%+ ranges will go through uncond branch check still, so this extra complexity doesn't seem worth it.

243

How about keep CodeAddrOffsets and CallOffsets as unordered_set so other queries are still O(1), but have additional UncondBranchOffsets as set that contains all calls and rets. This also avoids querying multiple containers for isValidLBRRange. The size overhead should be small.

wlei updated this revision to Diff 428972.May 12 2022, 9:29 AM

addressing Wenlei's feedback

tweak the test case

wlei edited the summary of this revision. (Show Details)May 12 2022, 9:33 AM
wlei added reviewers: hoy, wenlei.
wenlei accepted this revision.May 12 2022, 9:35 AM

lgtm, thanks.

This revision is now accepted and ready to land.May 12 2022, 9:35 AM
hoy added inline comments.May 12 2022, 9:41 AM
llvm/tools/llvm-profgen/ProfiledBinary.h
404

upper_bound seems to find the first element that is greater than Start. How about element equals to Start?

wlei added inline comments.May 12 2022, 9:54 AM
llvm/tools/llvm-profgen/ProfiledBinary.h
404

So you mean it will immediately jump when ip is on the Start's address, then it should be lower_bound?

hoy added inline comments.May 12 2022, 9:57 AM
llvm/tools/llvm-profgen/ProfiledBinary.h
404

Yeah, lower_bound finds the first element that is not less than Start. It should be fine.

wlei added inline comments.May 12 2022, 10:10 AM
llvm/tools/llvm-profgen/ProfiledBinary.h
404

Yeah, that sounds reasonable. The Start is actually the LBR's Target, IMO, the Target should not be an unconditional jmp. I will run on our service to see if we have this case.

wlei added inline comments.May 12 2022, 10:27 AM
llvm/tools/llvm-profgen/ProfiledBinary.h
404

Okay.. This indeed happened and it's the HW bug. it can be a function the first inst is a JMP or a JMP follow a call

func:
xxxx : JMP
`

Fixed to lower_bound
Thanks!

wlei updated this revision to Diff 428998.May 12 2022, 10:30 AM

upper_bound to lower_bound

hoy accepted this revision.May 12 2022, 10:35 AM
hoy added inline comments.
llvm/tools/llvm-profgen/ProfiledBinary.h
404

Thanks for verifying that. Surprised to see it was really hit. I was thinking it'd mostly happen in theory.

wenlei added inline comments.May 12 2022, 10:35 AM
llvm/tools/llvm-profgen/ProfiledBinary.h
404

Target, IMO, the Target should not be an unconditional jmp.

jmp->jmp should be valid execution sequence..

wlei added inline comments.May 12 2022, 10:41 AM
llvm/tools/llvm-profgen/ProfiledBinary.h
404

Got it, thanks! No big profile changes(profile similarity: 100.000%) comparing to upper_bound, because almost all those large bogus ranges not only cross one unconditional JMP.

This revision was landed with ongoing or failed builds.May 12 2022, 10:59 AM
This revision was automatically updated to reflect the committed changes.