Skip to content

JIT: Incorrect redundant branch opts example #85892

Closed

Description

@jakobbotsch
// Generated by Fuzzlyn v1.5 on 2023-05-07 18:03:16
// Run on X64 Windows
// Seed: 7683094625213390431
// Reduced from 235.6 KiB to 1.4 KiB in 00:02:04
// Debug: Prints 0 line(s)
// Release: Prints 1 line(s)
public interface I1
{
}

public interface I2
{
}

public struct S0 : I2
{
    public ushort F0;
    public short F1;
    public uint F2;
    public uint F3;
    public S0(ushort f0, short f1, uint f2, uint f3): this()
    {
        F0 = f0;
        F1 = f1;
        F2 = f2;
        F3 = f3;
    }
}

public class C0 : I1
{
    public short F0;
}

public class Program
{
    public static I1[] s_17 = new I1[]{new C0()};
    public static long[] s_107;
    public static void Main()
    {
        bool vr2 = default(bool);
        long vr3 = 1234;
        bool vr4 = !!M51();
        if (vr4)
        {
            try
            {
                System.Console.WriteLine(12);
            }
            finally
            {
                var vr5 = new S0(0, 0, 0, 0);
                M58(vr5, new S0(0, 0, 0, 0));
            }

            if (!vr4)
            {
                System.Console.WriteLine(34);
            }
        }

        if (vr4)
        {
            s_107 = s_107;
        }

        if (vr4)
        {
            var vr6 = new C0();
            System.Console.WriteLine(5678);
        }
    }

    public static void M57(ref I1 arg2)
    {
    }

    public static void M58(S0 argThis, I2 arg2)
    {
    }

    public static bool M51()
    {
        bool var27 = default(bool);
        M57(ref s_17[0]);
        return var27;
    }
}

Does not repro with DOTNET_JitDoRedundantBranchOpts=0. cc @AndyAyersMS

Activity

  1. ghost added
    untriaged
    on May 7, 2023
  2. ghost added
    area-CodeGen-coreclr
    on May 7, 2023
  3. self-assigned this
    on May 8, 2023
  4. removed
    untriaged
    on May 8, 2023
  5. AndyAyersMS commented on May 8, 2023

    @AndyAyersMS
    Member

    RBO gets confused because after jump threading we haven't removed a now unreachable block from successor pred lists, and/or updated dominance information.

    So the inference it draws in the last step is wrong; the branch in BB09 is redundant but does not always fall through to BB10.

    Presumably the jump threading at BB07 was enabled by #85546; BB07 has three preds and would have only had two PHI args before that change.

    Before RBO BB05 always jumps to BB07 Jump thread BB07
    via dom BB01
    BB01->BB09, BB05->BB08
    Jump thread BB09
    via dom BB07 (?)
    BB07->BB11
    BB09 always falls through to BB10
    Ooops
  6. AndyAyersMS commented on May 8, 2023

    @AndyAyersMS
    Member

    Trying to figure out the right fix. A couple of observations:

    • the charts above (which I produced by hand editing the first) are a bit misleading; in the second one there should be an edge from BB06->BB07. This means when we jump thread BB07 we think BB06 is an ambiguous pred, so we don't recycle BB07 and leave it as is; this means it still looks viable when we consider jump threading BB09.
    • BB03/BB04 are a callfinally pair, and BB12 a finally; this means that oddly BB07 is dominated by BB01 but BB05 (which is a pred of BB07) is not. The dominance tree is broken at BB04.

    My initial fix attempt in optJumpThreadCheck is to verify that if domBlock dominates block, then domBlock dominates each of block's preds. If not, we decide the dominator information is stale and can't be used to justify the threading. Thus, when we check BB07 dominating BB09 in step 4 above, we bail because BB07 does not dominate BB01.

    However, this actually ends up disqualifying the earlier BB01 dominates BB07 threading case, because BB01 does not dominate BB05. The end result is that we avoid the problem, but not for the reason I'd expected.

    It's not clear to me if the try/finally here is essential for the bug or just incidental. Suspect the latter but obvious simplifications of the test case don't have this bug.


    An alternative fix is to try and prune out the unreachable blocks (or pred edges) earlier; say in step 2 we could actually produce the graph I show above (with no edge from BB06->BB07) or else remove BB06 entirely. It's not clear we can do a complete job of this cleanup (imagine if BB06 were a loop or more complex bundle of flow that was cyclic or required transitive handling) and so relying on cleanup for correctness seems risky. But doing some cleanup might unblock more optimizations—if BB06 truly were edited out of BB07's pred list, then BB07 in step 3 would not have an ambiguous case and so would be recycled, and so we would not attempt to jump thread BB09.

    Also note there is a potential divergence between editing the pred edges and editing the PHI ARGs. We will do the former in most cases, but never the latter.

    When we go do phi-based threading we walk the pred list and then search the PHI ARGs for the matching entry; this means dead PHI ARGs don't cause any problems.

  7. added a commit that references this issue on May 8, 2023
    5903b37
  8. ghost added
    in-pr
    on May 8, 2023
  9. added a commit that references this issue on May 9, 2023
    66b8aaf
  10. ghost removed
    in-pr
    on May 9, 2023
  11. ghost locked as resolved and limited conversation to collaborators on Jun 8, 2023
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

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions