Skip to content

JIT: fgLateDevirtualization should try and simplify type compares #11436

Closed
@AndyAyersMS

Description

@AndyAyersMS

fgLateDevirtualization may be able to optimize type compares in cases where type information arrives late. For example a better type can be exposed by inlining or from a readonly static. For example:

class X 
{
    static object S() => "s";

    [MethodImpl(MethodImplOptions.NoInlining)]
    static bool IsString()
    {
        // This should be a jit-time constant
        return(S().GetType() == typeof(string));
    }

    public static int Main()
    {
        var b = IsString();
        return b ? 100 : -1;
    }
}

and once dotnet/coreclr#20886 is in place,

class X 
{
    static readonly object S;

    static X()
    {
        S = "s";
    }

    [MethodImpl(MethodImplOptions.NoInlining)]
    static bool IsString()
    {
        // This should be a jit-time constant
        return(S.GetType() == typeof(string));
    }

    public static int Main()
    {
        var p = S;
        var b = IsString();
        return b ? 100 : -1;
    }
}

category:cq
theme:devirtualization
skill-level:intermediate
cost:medium
impact:small

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIoptimization

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions