<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/121103>121103</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [AArch64][RISCV][Loongarch] return address should be passed into `_mcount`
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            backend:AArch64,
            backend:RISC-V,
            miscompilation,
            backend:loongarch
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
            dtcxzyw
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          dtcxzyw
      </td>
    </tr>
</table>

<pre>
    Reproducer:
```
; bin/clang -pg test.c && ./a.out && gprof -b a.out gmon.out
int fib(int n) {return n <=1 ? 1 : fib(n-1) + fib(n-2); }
int main() {fib(10); return 0;}
```
The call graph is missing if it is compiled with clang:
```
Flat profile:

Each sample counts as 0.01 seconds.
 no time accumulated

  % cumulative   self              self     total           
 time   seconds seconds    calls  Ts/call  Ts/call  name
```
With gcc, it works as expected:
```
Flat profile:

Each sample counts as 0.01 seconds.
 no time accumulated

  %   cumulative   self              self     total 
 time   seconds   seconds    calls  Ts/call  Ts/call  name    
  0.00 0.00     0.00        1     0.00     0.00  fib


 Call graph


granularity: each sample hit covers 2 byte(s) no time propagated

index % time    self  children    called     name
 176             fib [1]
                0.00    0.00 1/1           main [6]
[1]      0.0    0.00    0.00       1+176     fib [1]
                                 176             fib [1]
-----------------------------------------------


Index by function name

   [1] fib
```
This issue can be reproduced on AArch64/RISCV/Loongarch platforms.

After investing this issue, I found that we should pass the return address into `_mcount`:
https://github.com/gcc-mirror/gcc/blob/c6b7d0343f2c0eb3102341c9326fe314a047bee6/gcc/config/riscv/riscv.h#L466-L473
https://github.com/bminor/glibc/blob/757ac24f8f2344e5f8afd2465c808d0f6adcc948/sysdeps/riscv/machine-gmon.h

This issue was originally reported by @wzssyqa.

</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJy8VkGv26oS_jVkM0qEM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