Martijn
Martijn

Reputation: 12102

How can I see the code the JIT generates

Posts like http://aakinshin.net/en/blog/dotnet/ryujit-ctp5-and-loop-unrolling/ show the assembly code created by the JIT compiler. How can I get my hands on genereted assembly from a .NET program?

Upvotes: 3

Views: 220

Answers (1)

Hans Passant
Hans Passant

Reputation: 941515

That was simply copied from the Visual Studio debugger window. You just have to change a few options so you can debug optimized code, loop unrolling does not occur otherwise:

  • Tools > Options > Debugging > General > untick the "Suppress JIT optimization" checkbox. Also untick "Just My Code" to be on the safe side. This ensures that the optimizer is allowed to do its job even though you are debugging.
  • Build > Configuration Manager > upper left combo = Release. The Debug build is never optimized.
  • Project > Properties > Build > untick the "Prefer 32-bit" checkbox and ensure the Platform target combo is AnyCPU. Necessary so you get the new x64 jitter and not the x86 jitter to generate code.
  • Set a breakpoint on the code you want to look at. Beware that breakpoints act flaky in optimized code and might not break at all, best to set it on the method header or to single-step from the start.
  • Press F5. At the breakpoint use Debug > Windows > Disassembly. You'll see a mix of source code and machine code, just as it looks in the blog post.

Beware that you must write "substantial" code that has sufficient side-effects so it doesn't get optimized away completely. A Console.Write() can help for example. If you have trouble correlating the source code with the machine code then you might want to first do this for the Debug build.

Upvotes: 4

Related Questions