Search code examples
c#.netmonoclrintrinsics

How does JitIntrinsicAttribute affect code generation?


I was browsing .NET source code and saw this attribute. It says,

An attribute that can be attached to JIT Intrinsic methods/properties

and according to MSDN:

Indicates that a modified method is an intrinsic value for which the just-in-time (JIT) compiler can perform special code generation. This class cannot be inherited.

but I had trouble to find how. On what kind of code it performs a special code generation?

My main guess is that it mainly uses processor instructions like SIMD, something like Java JIT does. Here is an example. I'm curious about the acceleration it does and I wonder if Mono does this too.


Solution

  • This is specific to RyuJIT, the next generation 64-bit jitter that Microsoft is currently working on. Still in alpha (aka CTP), the next version of .NET and Visual Studio is slated to include it. Currently available in the .NET 4.6 Preview.

    One new feature in RyuJIT is its ability to generate SIMD machine code, taking advantage of vectorization instructions in Intel/AMD processors. Making floating point operations on arrays up to x8 times faster. The [JitIntrisic] attribute is a marker for C# code that RyuJIT has special built-in knowledge of, it will generate the SIMD version of the machine code instead of the normal non-vectorized version.

    Do keep in mind that this is still a million miles away from the kind of code that current C and C++ compilers can generate. RyuJIT can only do this for anointed types that it knows about. Like System.Numerics.Vector2. SIMD code has very strong alignment requirements to be efficient, aligned to 16 for SSE2 instructions, to 32 for AVX instructions. Getting such alignment in a .NET program is going to require a complete overhaul of the CLR, it currently can only align to 4 in 32-bit mode, to 8 in 64-bit mode.

    Long story short: the how you are asking about is the jitter. Mono has been tinkering with its own SIMD support, it appears to have gotten stuck 5 years ago. .NET Core was just recently announced to go open source with the very liberal MIT license, I assume (but don't know for a fact yet) that this is going to include the source code for RyuJIT. The github project is work in progress right now and very incomplete.


    UPDATE: This made it into .NET 4.6 RTM. Vector.IsHardwareAccelerated is now internal. Only the System.Numerics.Vector2, Vector3 and Vector4 types get the SIMD love. You can get System.Numerics.Vectors version 4.1.0.0 from Nuget. It exposes more, including Vector<T>.