Skip to content

Add AvxVnni support for NativeAOT #63563

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/coreclr/nativeaot/Runtime/IntrinsicConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ enum XArchIntrinsicConstants
XArchIntrinsicConstants_Bmi1 = 0x0400,
XArchIntrinsicConstants_Bmi2 = 0x0800,
XArchIntrinsicConstants_Lzcnt = 0x1000,
XArchIntrinsicConstants_AvxVnni = 0x2000,
};
#endif //HOST_X86 || HOST_AMD64

Expand Down
6 changes: 6 additions & 0 deletions src/coreclr/nativeaot/Runtime/startup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,12 @@ bool DetectCPUFeatures()
if ((cpuidInfo[EBX] & (1 << 5)) != 0) // AVX2
{
g_cpuFeatures |= XArchIntrinsicConstants_Avx2;

__cpuidex(cpuidInfo, 0x00000007, 0x00000001);
if ((cpuidInfo[EAX] & (1 << 4)) != 0) // AVX-VNNI
{
g_cpuFeatures |= XArchIntrinsicConstants_AvxVnni;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ private static class XArchIntrinsicConstants
public const int Bmi1 = 0x0400;
public const int Bmi2 = 0x0800;
public const int Lzcnt = 0x1000;
public const int AvxVnni = 0x2000;

public static int FromHardwareIntrinsicId(string id)
{
Expand All @@ -113,6 +114,7 @@ public static int FromHardwareIntrinsicId(string id)
"Bmi1" => Bmi1,
"Bmi2" => Bmi2,
"Lzcnt" => Lzcnt,
"AvxVnni" => AvxVnni,
_ => throw new NotSupportedException(),
};
}
Expand Down Expand Up @@ -155,6 +157,8 @@ public static int FromInstructionSetFlags(InstructionSetFlags instructionSets)
InstructionSet.X64_BMI2_X64 => Bmi2,
InstructionSet.X64_LZCNT => Lzcnt,
InstructionSet.X64_LZCNT_X64 => Popcnt,
InstructionSet.X64_AVXVNNI => AvxVnni,
InstructionSet.X64_AVXVNNI_X64 => AvxVnni,

// SSE and SSE2 are baseline ISAs - they're always available
InstructionSet.X64_SSE => 0,
Expand Down
3 changes: 2 additions & 1 deletion src/coreclr/tools/aot/ILCompiler/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -420,13 +420,14 @@ private int Run(string[] args)
optimisticInstructionSetSupportBuilder.AddSupportedInstructionSet("popcnt");
optimisticInstructionSetSupportBuilder.AddSupportedInstructionSet("lzcnt");

// If AVX was enabled, we can opportunistically enable FMA/BMI
// If AVX was enabled, we can opportunistically enable FMA/BMI/VNNI
Debug.Assert(InstructionSet.X64_AVX == InstructionSet.X86_AVX);
if (supportedInstructionSet.HasInstructionSet(InstructionSet.X64_AVX))
{
optimisticInstructionSetSupportBuilder.AddSupportedInstructionSet("fma");
optimisticInstructionSetSupportBuilder.AddSupportedInstructionSet("bmi");
optimisticInstructionSetSupportBuilder.AddSupportedInstructionSet("bmi2");
optimisticInstructionSetSupportBuilder.AddSupportedInstructionSet("avxvnni");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is going to add AVX2 to the list. Is that problematic?

My guess is no since we only use the supportedInstructionSet flags to determine things like Vector<T> length, but seeing as AVX2 is already "missing" here, I thought I'd ask.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, AVX2 is probably an omission. I don't think there's a reason why we wouldn't be able to support it under the runtime check.

}
}
else if (_targetArchitecture == TargetArchitecture.ARM64)
Expand Down
8 changes: 8 additions & 0 deletions src/tests/nativeaot/SmokeTests/HardwareIntrinsics/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ static int Main()
bool? PopCnt = null;
bool? Avx12 = false;
bool? FmaBmi12 = false;
bool? Avxvnni = false;
#elif NON_VEX_INTRINSICS
bool vectorsAccelerated = true;
int byteVectorLength = 16;
Expand All @@ -40,6 +41,7 @@ static int Main()
bool? PopCnt = null;
bool? Avx12 = false;
bool? FmaBmi12 = false;
bool? Avxvnni = false;
#elif VEX_INTRINSICS
bool vectorsAccelerated = true;
int byteVectorLength = 32;
Expand All @@ -50,6 +52,7 @@ static int Main()
bool? PopCnt = null;
bool? Avx12 = true;
bool? FmaBmi12 = null;
bool? Avxvnni = null;
#else
#error Who dis?
#endif
Expand Down Expand Up @@ -109,6 +112,9 @@ static int Main()
Check("Popcnt", PopCnt, &PopcntIsSupported, Popcnt.IsSupported, () => Popcnt.PopCount(0) == 0);
Check("Popcnt.X64", PopCnt, &PopcntX64IsSupported, Popcnt.X64.IsSupported, () => Popcnt.X64.PopCount(0) == 0);

Check("AvxVnni", Avxvnni, &AvxVnniIsSupported, AvxVnni.IsSupported, () => AvxVnni.MultiplyWideningAndAdd(Vector128<int>.Zero, Vector128<byte>.Zero, Vector128<sbyte>.Zero).Equals(Vector128<int>.Zero));
Check("AvxVnni.X64", Avxvnni, &AvxVnniX64IsSupported, AvxVnni.X64.IsSupported, null);

return s_success ? 100 : 1;
}

Expand Down Expand Up @@ -145,6 +151,8 @@ static int Main()
static bool PclmulqdqX64IsSupported() => Pclmulqdq.X64.IsSupported;
static bool PopcntIsSupported() => Popcnt.IsSupported;
static bool PopcntX64IsSupported() => Popcnt.X64.IsSupported;
static bool AvxVnniIsSupported() => AvxVnni.IsSupported;
static bool AvxVnniX64IsSupported() => AvxVnni.X64.IsSupported;

static bool IsConstantTrue(delegate*<bool> code)
{
Expand Down