Skip to content

RyuJIT: Invalidly optimized negated division #60297

Closed

Description

@jakobbotsch

Description

The following program gives an invalid result when optimizations are turned on.

Reproduction Steps

// Generated by Fuzzlyn v1.5 on 2021-10-12 17:42:07
// Run on .NET 6.0.0-rc.1.21451.13 on X64 Windows
// Seed: 4133580165890247722
// Reduced from 169.8 KiB to 0.3 KiB in 00:01:15
// Debug: Outputs 65538
// Release: Outputs -65538
public class Program
{
    public static int s_7;
    public static void Main()
    {
        var vr5 = new long[]{0};
        vr5[0] = s_7--;
        var vr14 = new long[]{0};
        vr14[0] = (-(1 << s_7) / -32767);
        System.Console.WriteLine(vr14[0]);
    }
}

Expected behavior

Same result

Actual behavior

Not same result

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

1 << s_7 is INT_MIN in the example, so negating it gives INT_MIN again. We probably have an optimization that is not valid for all inputs somewhere.

cc @dotnet/jit-contrib

Activity

  1. ghost added
    area-CodeGen-coreclr
    on Oct 12, 2021
  2. ghost added
    untriaged
    on Oct 12, 2021
  3. changed the title [-]RyuJIT: Invalidly optimized shift[/-] [+]RyuJIT: Invalidly optimized negated division[/+] on Oct 12, 2021
  4. EgorBo commented on Oct 12, 2021

    @EgorBo
    Member

    Even more reduced sample:

    using System.Runtime.CompilerServices;
    
    public class Program
    {
        public static void Main() => System.Console.WriteLine(Foo(31));
    
        [MethodImpl(MethodImplOptions.NoInlining)]
        static int Foo(int x) => -(1 << x) / 32767;
    }

    It feels like it steps on UB territory, even native compilers are confused here: https://godbolt.org/z/zr9PP6MTc

  5. jakobbotsch commented on Oct 12, 2021

    @jakobbotsch
    MemberAuthor

    It feels like it steps on UB territory

    I think it's UB in C++ on platforms with two's complement but ECMA-335 defines that negating INT_MIN yield sINT_MIN, so I don't think it's UB for us.

  6. EgorBo commented on Oct 12, 2021

    @EgorBo
    Member

    It feels like it steps on UB territory

    I think it's UB in C++ on platforms with two's complement but ECMA-335 defines that negating INT_MIN yield sINT_MIN, so I don't think it's UB for us.

    We have an opt -(expr) / cns => (expr) / -cns I don't see how we can disable it here

  7. SingleAccretion commented on Oct 12, 2021

    @SingleAccretion
    Contributor

    We may have to give up on it, unless we know that the expr cannot be int.MinValue.

  8. EgorBo commented on Oct 12, 2021

    @EgorBo
    Member

    which means basically remove this opt for DIV entirely (native compilers are fine with it: https://godbolt.org/z/ov94qrsfv)

  9. EgorBo commented on Oct 12, 2021

    @EgorBo
    Member

    I removed the opt and launched spmi to see diffs

  10. removed
    untriaged
    on Oct 12, 2021
  11. added this to the 7.0.0 milestone on Oct 12, 2021
  12. ghost added
    in-pr
    on Oct 12, 2021
  13. ghost removed
    in-pr
    on Oct 13, 2021
  14. ghost locked as resolved and limited conversation to collaborators on Nov 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclr

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions