Can I make a template function noinline or else force it to appear in the profiler?
Asked Answered
E

2

6

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?

Erdei answered 3/12, 2021 at 11:44 Comment(5)
Do you compile with the -O option? If so, remove it.Centring
@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.Erdei
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.Kurt
@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, 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)Maidy
you need to instantiate the templates where you want them to be instantiated. template <typename T> class Array { ... }; ... template class Array<int>; template class Array<float>;Myronmyrrh
R
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.
Remmer answered 3/12, 2021 at 11:53 Comment(2)
Relevant: How can I tell gcc not to inline a function? (it might be not wanted to avoid inlining of all functions).Jeffryjeffy
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.Maidy
N
0

you need to instantiate the templates where you want them to be instantiated

template <typename T> class Array { ... };`
...
//cpp
template class Array<int>;
template class Array<float>;

And maybe also mark methods __declspec(noinline) or something.

Nomism answered 22/2, 2024 at 21:7 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.