'How to see JIT-Compiled code in .NET VM (CLR)

How can I have a trace of native code generated by the JIT-Compiler ?

Thanks



Solution 1:[1]

If you just use Debug->Windows->Disassembly on a standard Debug or Release exe, without modifying Visual Studio Debugging options, you will just see a version of non optimized .NET code.

Have a look at this article "How to see the Assembly code generated by the JIT using Visual Studio". It explains how to inspect generated JIT optimized code.

One relevant quote from the article:

  1. Configure the Debugging Options in Visual Studio to allow the JIT to generate optimized code and to allow you to debug the optimized code.

Go to Tools => Options => Debugging => General · Make sure that box labeled ‘Suppress JIT optimization on module load’ is Unchecked.

· Make sure that the box labeled ‘Enable Just My Code’ is Unchecked.

Solution 2:[2]

You should look for the files output from the NGen tool. NGen compiles and stores pre-jitted versions of assemblies in the Global Assembly Cache.

Solution 3:[3]

You can even use Sharplab to see generated code => https://sharplab.io/ . In this, you can quickly see the generated code based on what C# code you write in both Debug and Release configuration.

Recently popular compiler explorer also started supporting .NET languages. Here is the example => https://godbolt.org/z/P49Y6Ejh6 It's not fast as SharpLab, but still it's a viable option to look for.

Solution 4:[4]

There's a more cross-platform, cross-architecture, local-only and open source approach possible with recent versions of .NET. This also allows you to build/modify the JIT itself and see the results. The complete steps are described at:

https://github.com/dotnet/runtime/blob/main/docs/design/coreclr/jit/viewing-jit-dumps.md

The downside is that it's not as "simple" or easy to use out of the box.

It boils down to:

  • Build your application and publish it. Something like this:

    dotnet publish -c Release -r linux-x64

    But replace linux-x64 with the appropriate OS/architecture combo, like win-x64 or osx-arm64 as appropriate.

  • Build a Debug build of the clrjit:

    git clone https://github.com/dotnet/runtime cd runtime ./build clr -c Debug

  • Replace your application's clrjit with the one you built

    cp /path/to/dotnet/runtime/artifacts/bin/coreclr/Linux.x64.Debug/* bin/Release/net6.0/linux-x64/publish/

    Adjust Linux.x64, net6.0 and linux-x64 as appropriate.

  • Set the COMPlus_JitDump=<Method> environment variable and run the application to dump the JIT output to the standard output.

    COMPlus_JitDump=Main ./bin/Release/net6.0/linux-x64/publish/Application

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Evgeniy Berezovsky
Solution 2 Peter Lillevold
Solution 3
Solution 4