2

In my code (either C or C++; let's say it's C++) I have a one-liner inline function foo() which gets called from many places in the code. I'm using a profiling tool which gathers statistics by line in the object code, which it translates into statistics by using the source-code-line information (which we get with -g in clang or GCC). Thus the profiler can't distinguish between calls to foo() from different places.

I would like the stats to be counted separately for the different places foo() get called. For this to happen, I need the compiler to "fully" inline foo() - including forgetting about it when it comes to the source location information.

Now, I know I can achieve this by using a macro - that way, there is no function, and the code is just pasted where I use it. But that wont work for operators, for example; and it may be a problem with templates. So, can I tell the compiler to do what I described?

Notes:

  • Compiler-specific answers are relevant; I'm mainly interested in GCC and clang.
  • I'm not compiling a debug build, i.e. optimizations are turned on.
einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • I'm guessing (maybe you could add some clarity) that, for debug builds, any `inline` keyword is ignored because inlining is considered an optimization. – Adrian Mole Oct 18 '22 at 09:49
  • [Here](https://learn.microsoft.com/en-us/answers/questions/240228/can39t-debug-inline-functions-vc-2019.html) is a blog about debug/inline for MSVC. May it helps? – Adrian Mole Oct 18 '22 at 09:51
  • 2
    I have not tried it, but maybe [__attribute__((artificial))](https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#Common-Function-Attributes) would [help](https://stackoverflow.com/a/21936099/4074081) in this case as well. – dewaffled Oct 18 '22 at 09:55
  • The debug info has all the information if and where a function was inlined. – ssbssa Oct 18 '22 at 10:33
  • Can't you look at the places where `foo()` is called? The output of your profiling tool should give you counts there. And a simple "grep" will reveal them. – the busybee Oct 18 '22 at 10:45
  • @thebusybee: It's not giving me the counts at the call sites. The counts are by object code line, and each of those is associated with a single line in the source. – einpoklum Oct 18 '22 at 14:23
  • @AdrianMole: I'm not profiling a debug build. – einpoklum Oct 18 '22 at 14:23
  • I might be misled by a lack of experience with gprof, I used gcov mainly. Its coverage counts are by source lines. -- So you want a profiler to count inlined machine code instructions generated by an inlined function? On heavily optimized code this seems impossible. – the busybee Oct 18 '22 at 14:29
  • @thebusybee: My profiling tool (not gcov, not gprof) counts by object code lines, and using that, counts by source code lines. I just want to change the mapping between the two. – einpoklum Oct 18 '22 at 14:36
  • So you have a connection between object code and source. Can't you use it? – the busybee Oct 18 '22 at 14:43
  • @thebusybee: I can, but if my count has "123456 mcguffins" for the the instruction at 0x1f00, and that instruction is mapped to the single line of `foo()`, that doesn't help me know which of these million mcguffins happen at which call to `foo()`. – einpoklum Oct 18 '22 at 16:28
  • Huh? The instruction at 0x1f00 was executed 123456 times, so this specific call of `foo()` happened 123456 times. -- If this is not what you want, please elaborate your question. Especially, why you think that you cannot differentiate different call places that are at separate instruction locations. A [mre] would be most helpful. – the busybee Oct 18 '22 at 19:51
  • @thebusybee: I elaborated my question fine. I want that count broken down by where the function was called for. – einpoklum Oct 18 '22 at 20:24
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/248894/discussion-between-the-busybee-and-einpoklum). – the busybee Oct 19 '22 at 05:41

0 Answers0