Blog

Platform intrinsics in .NET Core 3.0

September 20, 2019 | 3 Minute Read

Quick look at the new platform intrinsics feature.


.NET Conf 2019 Countdown series

I’m excited to be part of the .NET Conf with this every day mini-post series until the 23th September.

It’s definitely worth attending a .NET Conf 2019 local event to get together with other .NET friends. Join me on the 30th september at Community .NET Conf 2019 Event.

Prerequisites & Setup

You will need Visual Studio 2019 and .NET Core 3.0 SDK to try this out.

What are platform dependent intrinsics

In the previous post about Crossgen as build step with .NET Core 3.0, I explained some basic concepts of how .NET internally works. For instance that your source code is compiled into CIL and therefor is CPU/platform independent. This is also one of the key benefits that you get from .NET. But sometimes it does makes sense to be CPU/platform specific. Maybe you do have an application that you runs only on a Intel CPU - why shouldn’t you use specific feature of it?

Exactly that are platform dependent intrinsics - a specific feature or semantic that is not universally available on all platforms and isn’t easily recognizable by the JIT. For instance the AES instruction set for applications performing encryption and decryption using AES is supported by some Intel and AMD processors. The purpose of the instruction set is to improve speed and minimize the attack surface of a Side-channel Attacks.

How are they exposed in .NET

The high level .NET API’s are located in the System.Runtime.Intrinsics namespace - you could find a set of already implemented intrinsics in the CoreCLR repostiory. Furthermore they are separated by platform x64, x86, Arm and Arm64 and share when applicable the implementation (e.g. x86 and x64). What would happen when calling an hardware intrinsic method on a platform that doesn’t support it?
A PlatformNotSupportedException would be thrown. But you don’t have to catch exceptions, there is a easy way to check it:

static void Main(string[] args)
{
    Console.WriteLine("AES instruction set is supported: " + System.Runtime.Intrinsics.X86.Aes.IsSupported);
}

That returns true on my machine - aren’t you curious if it’s supported on your machine as well? It’s worth mentioning that the intrinsics are mainly implemented as part of the RyuJIT and recognized by the IntrinsicAttribute.

Conclusion

I’m seriously not a expert in that field of specialized CPU instructions. But knowing that something like this exists and is new to the .NET world, could become important in the future. Sure, Hardware Intrinsics definitely aren’t for everyone, but they can be used to boost perf in some computationally heavy workloads (ML.NET or graphical rendering for games). Even if you never use it directly - it will benefit performance of low-level implementations in libraries that you maybe use already today!