[PATCH] D84582: [XRay] Account: recursion detection

Roman Lebedev via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Jul 25 05:34:42 PDT 2020


lebedev.ri created this revision.
lebedev.ri added reviewers: dberris, mboerger.
lebedev.ri added a project: LLVM.

Recursion detection can be non-trivial. Currently, the state-of-the-art for LLVM,
as far as i'm concerned, is D72362 <https://reviews.llvm.org/D72362> `[clang-tidy] misc-no-recursion: a new check`.
However, it is quite limited:

- It does very basic call-graph based analysis, in the sense it will report even dynamically-unreachable recursion.
- It is inherently limited to a single TU
- It is hard to gauge how problematic each recursion is in practice.

Some of that can be addressed by adding clang analyzer-based check,
then it would at least support multiple TU's.

However, we can approach this problem from another angle - dynamic run-time analysis.
We already have means to capture a run-time callgraph (XRay, duh),
and there are already means to reconstruct it within `llvm-xray` tool.

This proposes to add a `-recursive-calls-only` switch to the `account` tool.
When the switch is on, when re-constructing callgraph for latency reconstruction,
each time we enter/leave some function, we increment/decrement an entry for the function
in a "recursion depth" map. If, when we leave the function, said entry was at `1`,
then that means the function didn't call itself, however if it is at `2` or more,
then that means the function (possibly indirectly) called itself.

So if the depth is less than 2, we don't account the time spent there.
Note that yes, the time spent in the outermost invocation of recursive function is indeed not accounted.
There are several reasons:

- I'm mainly interested in just detecting recursion, so that's enough :)
- I'm not sure said outermost invocation affects the timings significantly
- It is less straight-forward to count it. Should be able to, but maybe later.

Note that we don't pay for recursion depth tracking when `recursive-calls-only` is not on.

The overhead of the option is pretty small, around +8% user time on a medium-sized (3.5G) XRay log.
As a practical example, that 3.5G log is a capture of the entire middle-end opt pipeline
at `-O3` for RawSpeed unity build. There are total of `5500` functions in the log,
however `-recursive-calls-only` says that `269`, or 5%, are recursive.

Having this functionality could be helpful for recursion eradication.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D84582

Files:
  llvm/test/tools/llvm-xray/X86/account-recursive-calls-only-tail-call-deduction.yaml
  llvm/test/tools/llvm-xray/X86/account-recursive-calls-only.yaml
  llvm/tools/llvm-xray/xray-account.cpp
  llvm/tools/llvm-xray/xray-account.h

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D84582.280669.patch
Type: text/x-patch
Size: 10990 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200725/8db84cee/attachment.bin>


More information about the llvm-commits mailing list