Skip to content

Mono's Vector128 support incorrectly handles Abs for signed integer MinValue #95592

Closed

Description

@stephentoub

Repro:
#95588

        [Fact]
        public void Vector128SByteAbs_MinValue()
        {
            Vector128<sbyte> vector = Vector128.Create(sbyte.MinValue);
            Vector128<sbyte> abs = Vector128.Abs(vector);
            for (int index = 0; index < Vector128<sbyte>.Count; index++)
            {
                Assert.Equal(sbyte.MinValue, vector.GetElement(index));
            }
        }

This test passes on coreclr. On mono it fails with:

    System.Runtime.Intrinsics.Tests.Vectors.Vector128Tests.Vector128SByteAbs_MinValue [FAIL]
      System.OverflowException : Negating the minimum value of a twos complement number is invalid.
      Stack Trace:
        /_/src/libraries/System.Runtime.Intrinsics/tests/Vectors/Vector128Tests.cs(163,0): at System.Runtime.Intrinsics.Tests.Vectors.Vector128Tests.Vector128SByteAbs_MinValue()
        /_/src/mono/System.Private.CoreLib/src/System/Reflection/MethodBaseInvoker.Mono.cs(22,0): at System.Reflection.MethodBaseInvoker.InterpretedInvoke_Method(Object obj, IntPtr* args)
        /_/src/libraries/System.Private.CoreLib/src/System/Reflection/MethodBaseInvoker.cs(57,0): at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)

This is the source of the test failures in #94555 (comment).

cc: @radekdoulik, @tannergooding

Activity

  1. ghost added
    area-CodeGen-coreclr
    on Dec 4, 2023
  2. ghost added
    untriaged
    on Dec 4, 2023
  3. added and removed
    area-CodeGen-coreclr
    on Dec 4, 2023
  4. lambdageek commented on Dec 4, 2023

    @lambdageek
    Member

    Self contained repro with net8.0:

    using System.Runtime.Intrinsics;
    
    Vector128<sbyte> vector = Vector128.Create(sbyte.MinValue);
    var vector2 = Vector128.Abs(vector);
    Console.WriteLine(vector);
    Console.WriteLine(vector2);
    <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net8.0</TargetFramework>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
      </PropertyGroup>
      <PropertyGroup Condition="true">
        <UseMonoRuntime>true</UseMonoRuntime>
        <SelfContained>true</SelfContained>
      </PropertyGroup>
    </Project>

    This throws with both JIT and interp:

    $ dotnet run
    
    Unhandled Exception:
    System.OverflowException: Negating the minimum value of a twos complement number is invalid.
       at Program.<Main>$(String[] args) in /tmp/abcd/Program.cs:line 5
    [ERROR] FATAL UNHANDLED EXCEPTION: System.OverflowException: Negating the minimum value of a twos complement number is invalid.
       at Program.<Main>$(String[] args) in /tmp/abcd/Program.cs:line 5
    
    $ MONO_ENV_OPTIONS=--interp dotnet run
    
    Unhandled Exception:
    System.OverflowException: Negating the minimum value of a twos complement number is invalid.
       at Program.<Main>$(String[] args) in /tmp/abcd/Program.cs:line 5
    [ERROR] FATAL UNHANDLED EXCEPTION: System.OverflowException: Negating the minimum value of a twos complement number is invalid.
       at Program.<Main>$(String[] args) in /tmp/abcd/Program.cs:line 5
  5. added this to the 8.0.x milestone on Dec 4, 2023
  6. ghost removed
    untriaged
    on Dec 4, 2023
  7. MichalPetryka commented on Dec 4, 2023

    @MichalPetryka
    Contributor

    return (T)(object)Math.Abs((sbyte)(object)value);

    The issue is here, the Mono JIT doesn't expand Abs for integers and uses the managed fallback which is broken so this isn't Mono specific.

  8. tannergooding commented on Dec 4, 2023

    @tannergooding
    Member

    This might be a bug in Scalar<T>.Abs which is used for the software fallback. Looks like its just deferring to Math.Abs which would throw for MinValue rather than returning MinValue

  9. added a commit that references this issue on Dec 4, 2023
    ce6d5a6
  10. ghost added
    in-pr
    on Dec 4, 2023
  11. added a commit that references this issue on Dec 6, 2023
    42255ea
  12. ghost removed
    in-pr
    on Dec 6, 2023
  13. akoeplinger commented on Dec 6, 2023

    @akoeplinger
    Member

    @lambdageek I saw you added this to 8.0.x milestone, do we want to backport the fix or is 9.0 ok?

  14. lambdageek commented on Dec 6, 2023

    @lambdageek
    Member

    Probably net9.0 is ok unless we hear from a customer

  15. modified the milestones: 8.0.x, 9.0.0 on Dec 6, 2023
  16. locked and limited conversation to collaborators on Jan 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions