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

See if we can optimize blocks under null checks #111256

Open
MichalStrehovsky opened this issue Jan 9, 2025 · 3 comments
Open

See if we can optimize blocks under null checks #111256

MichalStrehovsky opened this issue Jan 9, 2025 · 3 comments
Assignees
Labels
area-NativeAOT-coreclr in-pr There is an active PR which will close this issue when it is merged
Milestone

Comments

@MichalStrehovsky
Copy link
Member

In the below program we should know that something typed as ITestOutput won't ever be non-null. We should be able to optimize out the null check branch in Foo.

using System;
using System.Runtime.CompilerServices;

class Program
{
    [MethodImpl(MethodImplOptions.NoInlining)]
    static void Foo(ITestOutput? to = null)
    {
        to?.Log("hello");
    }

    static void Main() => Foo(null);
}

interface ITestOutput
{
    void Log(string s);
}

Cc @nagya @jevansaks @EgorBo

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Jan 9, 2025
Copy link
Contributor

Tagging subscribers to this area: @agocke, @MichalStrehovsky, @jkotas
See info in area-owners.md if you want to be subscribed.

@EgorBo EgorBo self-assigned this Jan 10, 2025
@EgorBo EgorBo added this to the 10.0.0 milestone Jan 10, 2025
@EgorBo EgorBo removed the untriaged New issue has not been triaged by the area owner label Jan 10, 2025
@EgorBo
Copy link
Member

EgorBo commented Jan 15, 2025

@MichalStrehovsky hm.. how do we check that? I see we have

if (info.compCompHnd->getExactClasses(cls, 0, nullptr) == 0)

to check if a type can exist, but it returns -1 for this case, because the interface itself exists, just has no implementators

@dotnet-policy-service dotnet-policy-service bot added the in-pr There is an active PR which will close this issue when it is merged label Jan 15, 2025
@MichalStrehovsky
Copy link
Member Author

@MichalStrehovsky hm.. how do we check that? I see we have

Yeah, in the top-post repro case the interface method call brings the interface type to life. We have some conservative assumptions within the whole program view due to IDynamicInterfaceCastable. I'm looking if I can narrow those assumptions down somehow. What should give you 0 instead of -1 today is:

    [MethodImpl(MethodImplOptions.NoInlining)]
    static void Foo(ITestOutput? to = null)
    {
        if (to != null)
            Console.WriteLine("I'm unreachable");
    }

(i.e. do not call any methods on the interface, do not cast anything to the interface.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-NativeAOT-coreclr in-pr There is an active PR which will close this issue when it is merged
Projects
Status: No status
Development

No branches or pull requests

2 participants