Skip to content

RyuJIT: Mishandling of subrange assertion for rewritten call parameter #10925

Closed
dotnet/coreclr
#21322

Description

@jakobbotsch

The example is:

// Debug: Outputs -1
// Release: Outputs 1
struct S0
{
    public uint F1;
    public long F2;
    public ulong F3;
    public ushort F5;
    public bool F7;
    public S0(uint f1): this()
    {
        F1 = f1;
    }
}

public class Program
{
    static S0[] s_7 = new S0[]{new S0(0)};
    public static void Main()
    {
        M9(0);
    }

    static ushort M9(short arg1)
    {
        long var0 = 0;
        try
        {
            System.GC.KeepAlive(var0);
        }
        finally
        {
            var vr12 = new ulong[]{0, 2271009908085114245UL};
            S0[] vr18 = new S0[]{new S0(32768)};
            uint vr19 = vr18[0].F1;
            arg1 = (short)vr19;
            arg1 %= -32767;
            System.GC.KeepAlive(s_7[0]);
            System.GC.KeepAlive(s_7[0]);
        }

        System.Console.WriteLine(arg1);
        return s_7[0].F5;
    }
}

(remember to set COMPlus_TieredCompilation to 0 in release) (edit: updated example).

@mikedn has analyzed it here: https://github.com/dotnet/coreclr/issues/18867#issuecomment-413246494

Activity

  1. mikedn commented on Nov 26, 2018

    @mikedn
    Contributor

    Hrm, apparently this does not reproduce anymore. However, the initial example in https://github.com/dotnet/coreclr/issues/18867#issuecomment-411234557 still fails, though with a different value - -32766 instead of 1.

  2. mikedn commented on Nov 26, 2018

    @mikedn
    Contributor

    Yeah, so with or without/different repro the problem persists - fgMorphLocalVar uses subrange assertions to avoid inserting the "normalize on load" cast.

    That can't work correctly because such subrange assertions can be generated by an earlier small int assignment, if you store 16 bits to a variable then it's obvious that you must insert the normalization cast to get 32 bits out of a subsequent use of the variable.

    Unfortunately, fixing this results in a small CQ regression:

    Total bytes of diff: 59 (0.00% of base)
        diff is a regression.
    Top file regressions by size (bytes):
              21 : System.Text.RegularExpressions.dasm (0.02% of base)
              12 : System.Private.Xml.dasm (0.00% of base)
              11 : System.Private.CoreLib.dasm (0.00% of base)
               8 : Microsoft.CodeAnalysis.dasm (0.00% of base)
               3 : System.Private.Uri.dasm (0.00% of base)
    8 total files with size differences (0 improved, 8 regressed), 121 unchanged.
    Top method regressions by size (bytes):
              21 ( 4.82% of base) : System.Text.RegularExpressions.dasm - RegexCharClass:AddLowercaseRange(ushort,ushort,ref):this
              12 ( 2.80% of base) : System.Private.Xml.dasm - XmlSqlBinaryReader:ParseMB32_(ubyte):int:this
               8 ( 3.29% of base) : Microsoft.CodeAnalysis.dasm - DesktopAssemblyIdentityComparer:IsFrameworkAssembly(ref):bool
               4 (10.00% of base) : System.Private.CoreLib.dasm - Math:Abs(short):short
               4 (10.00% of base) : System.Private.CoreLib.dasm - Math:Abs(byte):byte
    Top method regressions by size (percentage):
               4 (10.00% of base) : System.Private.CoreLib.dasm - Math:Abs(short):short
               4 (10.00% of base) : System.Private.CoreLib.dasm - Math:Abs(byte):byte
              21 ( 4.82% of base) : System.Text.RegularExpressions.dasm - RegexCharClass:AddLowercaseRange(ushort,ushort,ref):this
               8 ( 3.29% of base) : Microsoft.CodeAnalysis.dasm - DesktopAssemblyIdentityComparer:IsFrameworkAssembly(ref):bool
              12 ( 2.80% of base) : System.Private.Xml.dasm - XmlSqlBinaryReader:ParseMB32_(ubyte):int:this
    10 total methods with size differences (0 improved, 10 regressed), 112828 unchanged.
    

    That's because there are valid cases when the normalization cast can be eliminated - when the variable is enregistered all 32 bits are valid, not only the lower 8/16 bits.

    So perhaps a better solution is to make "normalize on load" variables also "normalize on store", that will make the subrange assertion valid since we generate a 32 bit store. But there's at least one case where we can't actually do that - dependent promoted struct fields. Hrm.

  3. mikedn commented on Nov 29, 2018

    @mikedn
    Contributor

    Hrm, apparently this does not reproduce anymore.

    The example in this issue has actually been fixed by dotnet/coreclr#21184

    However, the initial example in #10660 (comment) still fails, though with a different value - -32766 instead of 1.

    And dotnet/coreclr#21184 is also the reason why that example generates a different value now. Of course, it's still incorrect so there's still a bug to be fixed somewhere.

  4. mikedn commented on Dec 1, 2018

    @mikedn
    Contributor

    @jakobbotsch Could you update the example in this issue to the one in https://github.com/dotnet/coreclr/issues/18867#issuecomment-411234557 ?

    As mentioned above, the current example was actually fixed by another change but the example in that comment still fails. And anyway that comment example is the "original" example that started this, the current one is supposed to be a simplification but it turns out that it is too simple :)

    I'm still not 100% sure where the bug really is. It could be that it's incorrect for morph to elide load normalization in some cases but it also looks like my magic division code in lowering is broken in this case. I think I'll just fix the later for now.

  5. jakobbotsch commented on Dec 1, 2018

    @jakobbotsch
    MemberAuthor

    @mikedn Done. Not sure if it helps, but there are probably some way simpler examples over in Fuzzlyn. For example:
    jakobbotsch/Fuzzlyn@f52cb42
    jakobbotsch/Fuzzlyn@89eb6a3
    jakobbotsch/Fuzzlyn@dcedff2
    These have not been tested on Windows, however. But you can check out the ubuntu-x64-examples branch if you want to hunt for some simpler ones.

  6. mikedn commented on Dec 1, 2018

    @mikedn
    Contributor

    Thanks!

    I tested all those 3 examples, they all fail even on Windows as well and they all pass after the fix. I ended up including the original example as test, due to its complexity it seems like it has a better chance to hit the bug. It's a rather elusive bug, at least because it requires spilled/not enregistered variables. That's why so far all examples that show the issue have exception handling in them.

    I'm not sure that there aren't other bugs related to load normalization, it's a rather convoluted affair. And if there aren't anymore bugs then there may be CQ issues. While in the beginning I was thinking that morph is perhaps wrong not to insert the normalization cast in some cases, now it looks to me that in fact it's too aggressive at doing that.

    But I'm 99.99% sure that the bug we're after now is actually in division lowering and that's what I fixed.

  7. transferred this issue fromdotnet/coreclron Jan 31, 2020
  8. ghost locked as resolved and limited conversation to collaborators on Dec 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-CodeGen-coreclr

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions