dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
14.56k stars 4.54k forks source link

Generate native image from IL the same way as produced by JIT #10394

Open voinokin opened 6 years ago

voinokin commented 6 years ago

Question: Is there any way I could get a dump of native executable code that .NET Core JIT creates, with some additional custom markup? It is not required to be runnable when transferred to different PC.

I'd like to try to run the tool called "Intel Architecture Code Analyzer" (https://software.intel.com/en-us/articles/intel-architecture-code-analyzer) to analyze the performance of my tool which is currently under development, in context of different CPUs - this is what the IACA tool does, but it normally accepts compiled native binary file with "magic" byte sequences inserted telling the region of code which is subject for the analysis. Thanks. category:design theme:ngen skill-level:expert cost:large

mikedn commented 6 years ago

IACA relies on inline assembly to generate those markers. Needless to say that this doesn't work in C# and I don't know a reasonable workaround. I suppose you could use a disassembler and a hex editor to manually insert those markers in the native image.

voinokin commented 6 years ago

How do you think - if I would clone and build my personal version of JIT with couple new IL instructions crafted which would map to such magic byte marks when emitted, will this make a viable solution? (The code does not need to be runnable)

mikedn commented 6 years ago

if I would clone and build my personal version of JIT with couple new IL instructions

That could work. But adding some intrinsic methods somewhere might be easier and more usable (you could simply call these methods from C#, but to use those new IL instructions you'd either need to modify Roslyn or insert them manually).

voinokin commented 6 years ago

Thanks, I will consider this. Another question is - how do I dump the JIT output to PE/COFF/whatever? I believe RVAs are free to be resolved to any addresses, since the tool does not analyze their values, but will only consider the influence of the encoding of instructions they are used by.

mikedn commented 6 years ago

how do I dump the JIT output to PE/COFF/whatever?

Hmm, that may be tricky. If you want a PE file containing the native code generated by the JIT then you can use crossgen. But if you work with SSE/AVX intrinsics you'll run into a problem - crossgen doesn't want to generate code for methods that contain intrinsics. And it's also possible that the code generated by crossgen is different from the code generated at runtime by the JIT (e.g. crossgen will generate SSE instructions for floating point code but it won't use the VEX encoding).

An alternative my be CoreRT, that will give you a true native PE file. And it also generates PDBs that can then be used by a disassembler. But AFAIR it too doesn't handle intrinsics.

voinokin commented 6 years ago

It looks like I'll have to dig into JIT source code to figure out. But anyway thanks for pointing out that CoreRT and crossgen will not help - I was thinking to use crossgen before.