Skip to content

MemoryExtensions.IndexOfAnyExcept produces invalid results in AVX-512 enabled hardware #89512

Closed

Description

@eiriktsarpalis

Consider the following repro:

Console.WriteLine(Avx512F.IsSupported);
var arr = new int[16];
arr[1] = 1;
Console.WriteLine(arr.AsSpan().ContainsAnyExcept(0));

Using a recent build of runtime with DOTNET_EnableAVX512F set to 0 the above prints:

False
True

While setting it to 1 prints

True
False

This is causing ConcurrentDictionary to misbehave under certain circumstances because the following method

private bool AreAllBucketsEmpty() =>
!_tables._countPerLock.AsSpan().ContainsAnyExcept(0);

can return invalid results.

cc @tannergooding @MihaZupan @stephentoub

Activity

  1. ghost added
    untriagedNew issue has not been triaged by the area owner
    on Jul 26, 2023
  2. added
    avx512Related to the AVX-512 architecture
    and removed
    untriagedNew issue has not been triaged by the area owner
    on Jul 26, 2023
  3. MihaZupan commented on Jul 26, 2023

    @MihaZupan
    Member

    Looks like a difference between ~Equals(a, b) != Zero and !EqualsAny(a, b).

    Vector128<int> input = Vector128.Create(0, 0, 0, 1);
    Vector128<int> value = Vector128<int>.Zero;
    
    Console.WriteLine($"containsAnyExcept: {~Vector128.Equals(input, value) != Vector128<int>.Zero}");
    Console.WriteLine($"!EqualsAny: {!Vector128.EqualsAny(input, value)}");
    // containsAnyExcept: True
    // !EqualsAny: False

    where the latter is being used by the Vector512 code path.

  4. added this to the 8.0.0 milestone on Jul 26, 2023
  5. ghost added
    in-prThere is an active PR which will close this issue when it is merged
    on Jul 26, 2023
  6. tannergooding commented on Jul 27, 2023

    @tannergooding
    Member

    Fix is up here: #89522, would appreciate a review/sign-off

  7. ghost removed
    in-prThere is an active PR which will close this issue when it is merged
    on Jul 27, 2023
  8. ghost locked as resolved and limited conversation to collaborators on Aug 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions