4

I'm trying to profile with perf on Ubuntu 20.04, but the problem is that many functions do not appear in it (likely because they are inlined), or only their addresses appear (without names etc.). I'm using CMake's RelWithDebInfo build. But there are some template functions that I don't know how to bring to the profiler results. I think marking them noinline may help if this is legal in C++ for template functions, but this screws up the codebase and needs to be done per-function. Any suggestions how to make all functions noinline at once?

Serge Rogatch
  • 13,865
  • 7
  • 86
  • 158
  • Do you compile with the `-O` option? If so, remove it. – Sam Varshavchik Dec 03 '21 at 11:52
  • @SamVarshavchik , I would like the code to be optimized with `-O2` or `-O3`, just no functions inlined. Otherwise, the profiler results would be too far from reality. – Serge Rogatch Dec 03 '21 at 11:58
  • 3
    But if you profile forcing functions to not be inline and then trying to optimize based on theses result you will not be optimizing for the the inline case and could even make the program slower if you change the build setting later to allow inline code. Profiling and optimising one build (not inline) and release another (inline) is not going to produce the results you would like. – Richard Critten Dec 03 '21 at 11:59
  • 1
    @SamVarshavchik: profiling a build with inlining of template functions disabled, especially standard library ones, is already a significant distortion. Profiling an anti-optimized debug build is much worse. (*Benchmarking* debug builds is [totally useless](//stackoverflow.com/questions/32000917/c-loop-optimization-help-for-final-assignment-with-compiler-optimization-disabl/32001196#32001196), like different implementations of the *same* function, but there might still be some large-scale signal left in all the noise of -O0 slowing down some code more than others, major hotspots still hot) – Peter Cordes Dec 03 '21 at 12:11

1 Answers1

3

You could add -fno-inline to CMAKE_CXX_FLAGS.

From GCC man page:

-fno-inline
      Do not expand any functions inline apart from those marked
      with the "always_inline" attribute.  This is the default when
      not optimizing.

      Single functions can be exempted from inlining by marking
      them with the "noinline" attribute.
Xoozee
  • 370
  • 2
  • 9
  • 2
    Relevant: [How can I tell gcc not to inline a function?](https://stackoverflow.com/q/1474030/580083) (it might be not wanted to avoid inlining of all functions). – Daniel Langr Dec 03 '21 at 12:00
  • You don't need to gimp *all* your functions, just `__attribute__((noipa))` or `__attribute__((noinline,noclone))` one the one function you want to block. (Unless it's defined in a standard header so you can't easily add attributes to it). Of course, not inlining a small template function will often significantly hurt optimization of that one spot. Oh, but the question *is* asking how to globally disable inlining, not on a per-function basis. – Peter Cordes Dec 03 '21 at 12:03