How does JitIntrinsicAttribute affect code generation?
Asked Answered
O

2

8

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.

Osteoma answered 13/11, 2014 at 7:58 Comment(0)
W
17

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>.

Weisler answered 13/11, 2014 at 9:37 Comment(2)
2015 update: SIMD works (with beta-version bugs) in x64 in the 2015 Visual Studio Release Candidate. Presumably, it will work on x86 in the full version, although I don't know if it will also work on Any CPU.Sop
Hope they are improving this in the future to allow this optimizations on user code. The vector classes are not really complete in numerics.Etrem
F
3

2018 update: apparently, [JitIntrinsic] was not used for its intended purpose and was replaced with System.Runtime.CompilerServices.IntrinsicAttribute.

The attribute was mentioned in the discussion about moving Vector classes to the CoreLib:

@jkotas: We should not need the JitIntrinsicAttribute. As far as I know, this attribute was future proofing, never used for anything real. We should delete it, and use the IntrinsicAttribute from CoreLib instead.

And later replaced with Intrinsic: Replace JitIntrinsicAttribute with IntrinsicAttribute.

See, What does the [Intrinsic] attribute in C# do? for the similar discussion about [Intrinsic] atrribute, which is actually used to implement low-level optimizations.

Francie answered 10/6, 2019 at 11:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.