Skip to content

Commit

Permalink
Add HardwareIntrinsics AVX-512 info (#2412)
Browse files Browse the repository at this point in the history
Co-authored-by: Adam Sitnik <[email protected]>
  • Loading branch information
nietras and adamsitnik authored Nov 16, 2023
1 parent 630622b commit 536a28e
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/BenchmarkDotNet/BenchmarkDotNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Import Project="..\..\build\common.props" />
<PropertyGroup>
<AssemblyTitle>BenchmarkDotNet</AssemblyTitle>
<TargetFrameworks>netstandard2.0;net6.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;net6.0;net8.0</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<NoWarn>$(NoWarn);1701;1702;1705;1591;3005;NU1702;CS3001;CS3003</NoWarn>
<AssemblyName>BenchmarkDotNet</AssemblyName>
Expand Down
72 changes: 68 additions & 4 deletions src/BenchmarkDotNet/Portability/Cpu/HardwareIntrinsics.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Numerics;
using BenchmarkDotNet.Environments;
using System.Diagnostics.CodeAnalysis;
using System.Text;

#if NET6_0_OR_GREATER
using System.Runtime.Intrinsics.X86;
using System.Runtime.Intrinsics.Arm;
Expand All @@ -16,6 +18,8 @@ internal static class HardwareIntrinsics

internal static string GetShortInfo()
{
if (IsX86Avx512FSupported)
return GetShortAvx512Representation();
if (IsX86Avx2Supported)
return "AVX2";
else if (IsX86AvxSupported)
Expand Down Expand Up @@ -52,7 +56,9 @@ static IEnumerable<string> GetCurrentProcessInstructionSets(Platform platform)
{
case Platform.X86:
case Platform.X64:
if (IsX86Avx2Supported) yield return "AVX2";

if (IsX86Avx512FSupported) yield return GetShortAvx512Representation();
else if (IsX86Avx2Supported) yield return "AVX2";
else if (IsX86AvxSupported) yield return "AVX";
else if (IsX86Sse42Supported) yield return "SSE4.2";
else if (IsX86Sse41Supported) yield return "SSE4.1";
Expand Down Expand Up @@ -90,6 +96,18 @@ static IEnumerable<string> GetCurrentProcessInstructionSets(Platform platform)
}
}

private static string GetShortAvx512Representation()
{
StringBuilder avx512 = new ("AVX-512F");
if (IsX86Avx512CDSupported) avx512.Append("+CD");
if (IsX86Avx512BWSupported) avx512.Append("+BW");
if (IsX86Avx512DQSupported) avx512.Append("+DQ");
if (IsX86Avx512FVLSupported) avx512.Append("+VL");
if (IsX86Avx512VbmiSupported) avx512.Append("+VBMI");

return avx512.ToString();
}

internal static bool IsX86BaseSupported =>
#if NET6_0_OR_GREATER
X86Base.IsSupported;
Expand Down Expand Up @@ -153,6 +171,48 @@ static IEnumerable<string> GetCurrentProcessInstructionSets(Platform platform)
GetIsSupported("System.Runtime.Intrinsics.X86.Avx2");
#endif

internal static bool IsX86Avx512FSupported =>
#if NET8_0_OR_GREATER
Avx512F.IsSupported;
#else
GetIsSupported("System.Runtime.Intrinsics.X86.Avx512F");
#endif

internal static bool IsX86Avx512FVLSupported =>
#if NET8_0_OR_GREATER
Avx512F.VL.IsSupported;
#else
GetIsSupported("System.Runtime.Intrinsics.X86.Avx512F+VL");
#endif

internal static bool IsX86Avx512BWSupported =>
#if NET8_0_OR_GREATER
Avx512BW.IsSupported;
#else
GetIsSupported("System.Runtime.Intrinsics.X86.Avx512BW");
#endif

internal static bool IsX86Avx512CDSupported =>
#if NET8_0_OR_GREATER
Avx512CD.IsSupported;
#else
GetIsSupported("System.Runtime.Intrinsics.X86.Avx512CD");
#endif

internal static bool IsX86Avx512DQSupported =>
#if NET8_0_OR_GREATER
Avx512DQ.IsSupported;
#else
GetIsSupported("System.Runtime.Intrinsics.X86.Avx512DQ");
#endif

internal static bool IsX86Avx512VbmiSupported =>
#if NET8_0_OR_GREATER
Avx512Vbmi.IsSupported;
#else
GetIsSupported("System.Runtime.Intrinsics.X86.Avx512Vbmi");
#endif

internal static bool IsX86AesSupported =>
#if NET6_0_OR_GREATER
System.Runtime.Intrinsics.X86.Aes.IsSupported;
Expand Down Expand Up @@ -211,8 +271,12 @@ static IEnumerable<string> GetCurrentProcessInstructionSets(Platform platform)
GetIsSupported("System.Runtime.Intrinsics.X86.AvxVnni");
#endif

// X86Serialize was introduced in .NET 7.0, BDN does not target it so we need to use reflection
internal static bool IsX86SerializeSupported => GetIsSupported("System.Runtime.Intrinsics.X86.X86Serialize");
internal static bool IsX86SerializeSupported =>
#if NET7_0_OR_GREATER
X86Serialize.IsSupported;
#else
GetIsSupported("System.Runtime.Intrinsics.X86.X86Serialize");
#endif

internal static bool IsArmBaseSupported =>
#if NET6_0_OR_GREATER
Expand Down
2 changes: 2 additions & 0 deletions src/BenchmarkDotNet/Portability/Libc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace BenchmarkDotNet.Portability
{
#pragma warning disable CS8981 // The type name 'libc' only contains lower-cased ascii characters. Such names may become reserved for the language.
internal static class libc
#pragma warning restore CS8981
{
[DllImport(nameof(libc))]
internal static extern int getppid();
Expand Down
8 changes: 7 additions & 1 deletion src/BenchmarkDotNet/Toolchains/NativeAot/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ private string GetILCompilerPackageReference()

private string GetTrimmingSettings()
=> rootAllApplicationAssemblies
? "" // use the defaults
// Use the defaults
? ""
// TrimMode is set in explicit way as for older versions it might have different default value
: "<TrimMode>link</TrimMode><TrimmerDefaultAction>link</TrimmerDefaultAction>";

Expand Down Expand Up @@ -248,6 +249,11 @@ private IEnumerable<string> GetCurrentProcessInstructionSets(Platform platform)
if (HardwareIntrinsics.IsX86Sse42Supported) yield return "sse4.2";
if (HardwareIntrinsics.IsX86AvxSupported) yield return "avx";
if (HardwareIntrinsics.IsX86Avx2Supported) yield return "avx2";
if (HardwareIntrinsics.IsX86Avx512FSupported) yield return "avx-512f";
if (HardwareIntrinsics.IsX86Avx512BWSupported) yield return "avx-512bw";
if (HardwareIntrinsics.IsX86Avx512CDSupported) yield return "avx-512cd";
if (HardwareIntrinsics.IsX86Avx512DQSupported) yield return "avx-512dq";
if (HardwareIntrinsics.IsX86Avx512VbmiSupported) yield return "avx-512vbmi";
if (HardwareIntrinsics.IsX86AesSupported) yield return "aes";
if (HardwareIntrinsics.IsX86Bmi1Supported) yield return "bmi";
if (HardwareIntrinsics.IsX86Bmi2Supported) yield return "bmi2";
Expand Down

0 comments on commit 536a28e

Please sign in to comment.