[CSSPGO][llvm-profgen] Fix external address issues of perf reader (return to external...
authorwlei <wlei@fb.com>
Tue, 14 Dec 2021 04:33:33 +0000 (20:33 -0800)
committerwlei <wlei@fb.com>
Wed, 15 Dec 2021 00:40:54 +0000 (16:40 -0800)
commit0f53df864eb3b97fba74feb78132a38c87fa1db6
treedf09417e6f96600dd37bc8c0efe5e8ac0c7119ec
parent30c3aba998972aa042493e26dbe8f7cca37a30dd
[CSSPGO][llvm-profgen] Fix external address issues of perf reader (return to external addr part)

Before we have an issue with artificial LBR whose source is a return, recalling that "an internal code(A) can return to external address, then from the external address call a new internal code(B), making an artificial branch that looks like a return from A to B can confuse the unwinder". We just ignore the LBRs after this artificial LBR which can miss some samples. This change aims at fixing this by correctly unwinding them instead of ignoring them.

List some typical scenarios covered by this change.

1)  multiple sequential call back happen in external address, e.g.

```
[ext, call, foo] [foo, return, ext] [ext, call, bar]
```
Unwinder should avoid having foo return from bar. Wrong call stack is like [foo, bar]

2) the call stack before and after external call should be correctly unwinded.
```
 {call stack1}                                            {call stack2}
 [foo, call, ext]  [ext, call, bar]  [bar, return, ext]  [ext, return, foo ]
```
call stack 1 should be the same to call stack2. Both shouldn't be truncated

3) call stack should be truncated after call into external code since we can't do inlining with external code.

```
 [foo, call, ext]  [ext, call, bar]  [bar, call, baz] [baz, return, bar ] [bar, return, ext]
```
the call stack of code in baz should not include foo.

### Implementation:

We leverage artificial frame to fix #2 and #3: when we got a return artificial LBR, push an extra artificial frame to the stack. when we pop frame, check if the parent is an artificial frame to pop(fix #2). Therefore, call/ return artificial LBR is just the same as regular LBR which can keep the call stack.

While recording context on the trie, artificial frame is used as a tag indicating that we should truncate the call stack(fix #3).

To differentiate #1 and #2, we leverage `getCallAddrFromFrameAddr`.  Normally the target of the return should be the next inst of a call inst and `getCallAddrFromFrameAddr` will return the address of call inst. Otherwise, getCallAddrFromFrameAddr will return to 0 which is the case of #1.

Reviewed By: hoy, wenlei

Differential Revision: https://reviews.llvm.org/D115550
llvm/test/tools/llvm-profgen/Inputs/callback-external-addr.perfbin [new file with mode: 0755]
llvm/test/tools/llvm-profgen/Inputs/callback-external-addr.perfscript [new file with mode: 0644]
llvm/test/tools/llvm-profgen/callback-external-addr.test [new file with mode: 0644]
llvm/test/tools/llvm-profgen/inline-noprobe2.test
llvm/tools/llvm-profgen/PerfReader.cpp
llvm/tools/llvm-profgen/PerfReader.h
llvm/tools/llvm-profgen/ProfiledBinary.h