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

1

16

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.

Squint answered 20/2, 2016 at 16:0 Comment(0)
O
14

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.

Olnay answered 20/2, 2016 at 16:17 Comment(2)
Regarding memory alignment, I found this comment from Intel... "Most VEX-encoded SIMD numeric and data processing instruction semantics with memory operand have relaxed memory alignment requirements than instructions encoded using SIMD prefixes" Intel® 64 and IA-32 Architectures Software Developer’s ManualSquint
"Can only be efficient" is the key phrase. Unaligned loads and stores are very expensive.Olnay

© 2022 - 2024 — McMap. All rights reserved.