Search code examples
.netvectorizationsimdauto-vectorizationryujit

Under what conditions does the .NET JIT compiler perform automatic vectorization?


Does the new RyuJIT compiler ever generate vector (SIMD) CPU instructions, and when?

Side note: The System.Numerics namespace contains types that allow explicit use of Vector operations which may or may not generate SIMD instructions depending on the CPU, CLR version, JITer version, whether compiling directly to native code or not. This question is specifically about when non-vector code (e.g in C# or F#) will produce SIMD instructions.


Solution

  • SIMD code generation in RuyJIT is strictly limited to the types in the System.Numerics.Vectors namespace. Universal SIMD support is going to require a very significant revision of the CLR, such code can only be efficient if SIMD variables are aligned properly. At least to 16 for SSE2, to 32 to be able to use AVX2, to 64 for the upcoming AVX-512.

    That's far off right now, the 32-bit CLR can only align to 4, the 64-bit version to 8. The "natural" alignment for resp 32-bit and 64-bit code. The required changes are going to affect about every part of the CLR, the garbage collector and class loader up front. There is no buzz about such a major change being considered. And no sign that it was considered in the CoreCLR project, it would have been the most obvious target version.

    If you want to take advantage of SIMD beyond the current support in System.Numerics.Vectors then do so by using the C++ compiler, using the C++/CLI or C++/CX language extensions to interop.