Skip to content
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

Vector256.Create() randomly throws PlatformNotSupportedException #72506

Closed
dubiousconst282 opened this issue Jul 20, 2022 · 6 comments · Fixed by #72522
Closed

Vector256.Create() randomly throws PlatformNotSupportedException #72506

dubiousconst282 opened this issue Jul 20, 2022 · 6 comments · Fixed by #72522

Comments

@dubiousconst282
Copy link
Contributor

Description

Calling Vector256.Create() on a machine with no support for AVX2 will throw PlatformNotSupportedException after a short amount of time.

Reproduction Steps

using System.Runtime.Intrinsics;

int i = 0;
while (true) {
    Vector256.Create(0f);
    if (i++ % 10000 == 0) Console.WriteLine("OK " + i);
}

Expected behavior

The program above should run indefinitely.

Actual behavior

The program crashes after a short amount of time:

...
OK 3390001
OK 3400001
Unhandled exception. System.PlatformNotSupportedException: Operation is not supported on this platform.
   at System.Runtime.Intrinsics.Vector256.Create(Single value)
   at Program.<Main>$(String[] args) in ...\Program.cs:line 4

Regression?

I'm pretty sure it is. Changing the target to .NET 6 works as expected, and I've only found this issue after upgrading to .NET 7 preview 6.

Known Workarounds

No response

Configuration

.NET SDK: 7.0.100-preview.6.22352.1 (commit 492644e08e)
OS: Windows 10 (6.3.9600, x64)
CPU: Intel i3 Gen2 (Sandy Bridge, with no AVX2 support)

Other information

No response

@dotnet-issue-labeler
Copy link

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Jul 20, 2022
@huoyaoyuan
Copy link
Member

The behavior looks related to tiered compilation. Or maybe dynamic PGO?

@dubiousconst282
Copy link
Contributor Author

dubiousconst282 commented Jul 20, 2022

I can confirm that dotnet run -p:TieredCompilation=false works as expected.

@ghost
Copy link

ghost commented Jul 20, 2022

Tagging subscribers to this area: @dotnet/area-system-runtime-intrinsics
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

Calling Vector256.Create() on a machine with no support for AVX2 will throw PlatformNotSupportedException after a short amount of time.

Reproduction Steps

using System.Runtime.Intrinsics;

int i = 0;
while (true) {
    Vector256.Create(0f);
    if (i++ % 10000 == 0) Console.WriteLine("OK " + i);
}

Expected behavior

The program above should run indefinitely.

Actual behavior

The program crashes after a short amount of time:

...
OK 3390001
OK 3400001
Unhandled exception. System.PlatformNotSupportedException: Operation is not supported on this platform.
   at System.Runtime.Intrinsics.Vector256.Create(Single value)
   at Program.<Main>$(String[] args) in ...\Program.cs:line 4

Regression?

I'm pretty sure it is. Changing the target to .NET 6 works as expected, and I've only found this issue after upgrading to .NET 7 preview 6.

Known Workarounds

No response

Configuration

.NET SDK: 7.0.100-preview.6.22352.1 (commit 492644e08e)
OS: Windows 10 (6.3.9600, x64)
CPU: Intel i3 Gen2 (Sandy Bridge, with no AVX2 support)

Other information

No response

Author: dubiousconst282
Assignees: -
Labels:

area-System.Runtime.Intrinsics, untriaged

Milestone: -

@EgorBo
Copy link
Member

EgorBo commented Jul 20, 2022

There are two problems here:

  1. We treat Vector256 "ISA" as not-supported on AVX1 only CPUs so Vector256.IsHardwareAccelerated returns false on e.g. Ivy Bridge

  2. Inside Vector256.Create we still check for AVX1 https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/Vector256.cs#L979-L984 and if it's true we think that Vector256.Create is an intrinsic (but it's actually not since Vector256 ISA is disabled).

Solution:

  1. Replace Avx.IsSupported checks with Vector256.IsHardwareAccelerated for consistency so we will always be using software fallbacks on Avx1
  2. Enable some Vector256 helper instrinsics on AVX1 (makes more sense)

@EgorBo EgorBo added this to the 7.0.0 milestone Jul 20, 2022
@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Jul 20, 2022
@EgorBo EgorBo removed untriaged New issue has not been triaged by the area owner in-pr There is an active PR which will close this issue when it is merged labels Jul 20, 2022
@EgorBo EgorBo self-assigned this Jul 20, 2022
@tannergooding
Copy link
Member

This is the same issue that ImageSharp hit that I was looking into.

@ghost ghost locked as resolved and limited conversation to collaborators Aug 25, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants