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

Forward substitution for relops misses LCL_FLD uses #62524

Closed
SingleAccretion opened this issue Dec 8, 2021 · 1 comment · Fixed by #62568
Closed

Forward substitution for relops misses LCL_FLD uses #62524

SingleAccretion opened this issue Dec 8, 2021 · 1 comment · Fixed by #62568
Assignees
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI bug
Milestone

Comments

@SingleAccretion
Copy link
Contributor

Reproduction:

using System;
using System.Runtime.CompilerServices;

Console.WriteLine(Problem(new() { Value = 1 }));

[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
private static int Problem(StructWithIndex a)
{
    bool k = a.Value == 1;
    a = default;
    if (k)
    {
        return 1;
    }

    return a.Index;
}

struct StructWithIndex
{
    public int Index;
    public int Value;
}

Run with DOTNET_JitNoStructPromotion=1 and DOTNET_JitNoCSE=1.

Expected result: prints 1.
Actual result: prints 0.

Cause: the interference checks in the forward substitution algorithm use gtHasRef which does not pay attention to LCL_FLD nodes:

optRedundantRelop in BB01; jump tree is
N004 (  5,  5) [000014] ------------              *  JTRUE     void
N003 (  3,  3) [000013] J------N----              \--*  EQ        int    $142
N001 (  1,  1) [000011] ------------                 +--*  LCL_VAR   int    V02 tmp1         u:1 (last use) $141
N002 (  1,  1) [000012] ------------                 \--*  CNS_INT   int    0 $40
 ... checking previous tree
N003 (  5,  4) [000008] IA------R---              *  ASG       struct (init) $VN.Void
N002 (  3,  2) [000005] D------N----              +--*  LCL_VAR   struct<StructWithIndex, 8> V00 arg0         d:2
N001 (  1,  1) [000007] ------------              \--*  CNS_INT   int    0 $40
 -- prev tree VN is not related
 ... checking previous tree
N005 (  8,  6) [000010] -A--G---R---              *  ASG       int    $141
N004 (  1,  1) [000009] D------N----              +--*  LCL_VAR   int    V02 tmp1         d:1 $141
N003 (  8,  6) [000004] ----G-------              \--*  EQ        int    $141
N001 (  3,  4) [000002] ------------                 +--*  LCL_FLD   int    V00 arg0         u:1[+4] Fseq[Value] (last use) $140
N002 (  1,  1) [000003] ------------                 \--*  CNS_INT   int    1 $42
  -- prev tree has relop with reversed liberal VN
 -- prev tree is viable candidate for relop fwd sub!
 -- done! new jump tree is
N004 (  5,  5) [000014] ----G-------              *  JTRUE     void
N003 (  8,  6) [000021] J---G--N----              \--*  NE        int    $142
N001 (  3,  4) [000022] ------------                 +--*  LCL_FLD   int    V00 arg0         [+4] Fseq[Value] (last use) $140
N002 (  1,  1) [000023] ------------                 \--*  CNS_INT   int    1 $42
@dotnet-issue-labeler dotnet-issue-labeler bot added area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI untriaged New issue has not been triaged by the area owner labels Dec 8, 2021
@ghost
Copy link

ghost commented Dec 8, 2021

Tagging subscribers to this area: @JulieLeeMSFT
See info in area-owners.md if you want to be subscribed.

Issue Details

Reproduction:

using System;
using System.Runtime.CompilerServices;

Console.WriteLine(Problem(new() { Value = 1 }));

[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
private static int Problem(StructWithIndex a)
{
    bool k = a.Value == 1;
    a = default;
    if (k)
    {
        return 1;
    }

    return a.Index;
}

struct StructWithIndex
{
    public int Index;
    public int Value;
}

Run with DOTNET_JitNoStructPromotion=1 and DOTNET_JitNoCSE=1.

Expected result: prints 1.
Actual result: prints 0.

Cause: the interference checks in the forward substitution algorithm use gtHasRef which does not pay attention to LCL_FLD nodes:

optRedundantRelop in BB01; jump tree is
N004 (  5,  5) [000014] ------------              *  JTRUE     void
N003 (  3,  3) [000013] J------N----              \--*  EQ        int    $142
N001 (  1,  1) [000011] ------------                 +--*  LCL_VAR   int    V02 tmp1         u:1 (last use) $141
N002 (  1,  1) [000012] ------------                 \--*  CNS_INT   int    0 $40
 ... checking previous tree
N003 (  5,  4) [000008] IA------R---              *  ASG       struct (init) $VN.Void
N002 (  3,  2) [000005] D------N----              +--*  LCL_VAR   struct<StructWithIndex, 8> V00 arg0         d:2
N001 (  1,  1) [000007] ------------              \--*  CNS_INT   int    0 $40
 -- prev tree VN is not related
 ... checking previous tree
N005 (  8,  6) [000010] -A--G---R---              *  ASG       int    $141
N004 (  1,  1) [000009] D------N----              +--*  LCL_VAR   int    V02 tmp1         d:1 $141
N003 (  8,  6) [000004] ----G-------              \--*  EQ        int    $141
N001 (  3,  4) [000002] ------------                 +--*  LCL_FLD   int    V00 arg0         u:1[+4] Fseq[Value] (last use) $140
N002 (  1,  1) [000003] ------------                 \--*  CNS_INT   int    1 $42
  -- prev tree has relop with reversed liberal VN
 -- prev tree is viable candidate for relop fwd sub!
 -- done! new jump tree is
N004 (  5,  5) [000014] ----G-------              *  JTRUE     void
N003 (  8,  6) [000021] J---G--N----              \--*  NE        int    $142
N001 (  3,  4) [000022] ------------                 +--*  LCL_FLD   int    V00 arg0         [+4] Fseq[Value] (last use) $140
N002 (  1,  1) [000023] ------------                 \--*  CNS_INT   int    1 $42
Author: SingleAccretion
Assignees: -
Labels:

area-CodeGen-coreclr, untriaged

Milestone: -

@SingleAccretion SingleAccretion added this to the 7.0.0 milestone Dec 8, 2021
@SingleAccretion SingleAccretion self-assigned this Dec 8, 2021
@SingleAccretion SingleAccretion removed the untriaged New issue has not been triaged by the area owner label Dec 8, 2021
@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Dec 9, 2021
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Dec 9, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Jan 9, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant