title: Profiling inlined C++ functions with Visual Studio Compiler
body: <p>How can I make sense of C++ profiling data on Windows, when a lot of code gets inlined by the compiler? I.e. I of course want to measure the code that actually gets run, so by definition I'm going to measure an optimized build of the code. But it seems like none of the tools I try actually manage to resolve inline functions.</p>
  
  <p>I have tried both the sampling profiler in Visual Studio 2017 Professional as well as VTune 2018. I have tried to enable <code>/Zo</code>, but it does not seem to have any affect.</p>
  
  <p>I have found the following resource which seems to indicate that only Visual Studio Ultimate or Premium support inline frame information - is this still true for Visual Studio 2017? <a href="https://social.msdn.microsoft.com/Forums/en-US/9df15363-5aae-4f0b-a5ad-dd9939917d4c/which-functions-arent-pgo-optimized-using-profile-data?forum=vsdebug" rel="nofollow noreferrer">https://social.msdn.microsoft.com/Forums/en-US/9df15363-5aae-4f0b-a5ad-dd9939917d4c/which-functions-arent-pgo-optimized-using-profile-data?forum=vsdebug</a></p>
  
  <p>Here is an example code:</p>
  
  <pre><code>#include &lt;cmath&gt;
  #include &lt;random&gt;
  #include &lt;iostream&gt;
  
  inline double burn()
  {
      std::uniform_real_distribution&lt;double&gt; uniform(-1E5, 1E5);
      std::default_random_engine engine;
      double s = 0;
      for (int i = 0; i &lt; 100000000; ++i) {
          s += uniform(engine);
      }
      return s;
  }
  
  int main()
  {
      std::cout &lt;&lt; "random sum: " &lt;&lt; burn() &lt;&lt; '\n';
      return 0;
  }
  </code></pre>
  
  <p>Compile it with Visual Studio in Release mode. Or on the command line, try <code>cl /O2 /Zi /Zo /EHsc main.cpp</code>. Then try to profile it with the CPU Sampling Profiler in Visual Studio. You will at most see something like this:</p>
  
  <p><a href="https://i.stack.imgur.com/vkcwk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vkcwk.png" alt="confusing profile since inline frames are missing"></a></p>
  
  <p>VTune 2018 looks similar on Windows. On Linux, perf and VTune have no problem showing frames from inlined functions... Is this feature, which is in my opinion crucial for C++ tooling, really not part of the non-Premium/Ultimate Visual Studio toolchains? How do people on Windows deal with that? What is the point of <code>/Zo</code> then?</p>
  
  <p><strong>EDIT:</strong> I just tried to compile the minimal example above with clang and it produces different, but still unsatisfying results? I compiled clang 6.0.0 (trunk), build from LLVM rev 318844 and clang rev 318874. Then I compile my code with <code>clang++ -std=c++17 -O2 -g main.cpp -o main.exe</code> and run the resulting executable with the Sampling Profiler in Visual Studio again, the result is:</p>
  
  <p><a href="https://i.stack.imgur.com/Fxs8u.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Fxs8u.png" alt="inline frames are shown in profile after compiling with clang"></a></p>
  
  <p>So now I see the <code>burn</code> function, but lost the source file information. Also, the <code>uniform_real_distribution</code> is still not being shown anywhere.</p>
  
  <p><strong>EDIT 2:</strong> As suggested in the comments, I now also tried out <code>clang-cl</code> with the same arguments as <code>cl</code> above, i.e.: <code>clang-cl.exe /O2 /Zi /Zo /EHsc main.cpp</code>. This produces the same results as <code>clang.exe</code>, but we also get somewhat working source mappings:</p>
  
  <p><a href="https://i.stack.imgur.com/8DImx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8DImx.png" alt="clang-cl shows inliners and somewhat functional source mapping"></a></p>
  
  <p><strong>EDIT 3:</strong> I originally thought clang would magically solve this issue. It doesn't, sadly. Most inlined frames are still missing :(</p>
  
  <p><strong>EDIT 4:</strong> Inline frames are not supported in VTune for applicatoins build with MSVC/PDB builds: <a href="https://software.intel.com/en-us/forums/intel-vtune-amplifier-xe/topic/749363" rel="nofollow noreferrer">https://software.intel.com/en-us/forums/intel-vtune-amplifier-xe/topic/749363</a></p>