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

Inline for lambda #13679

Open
blowin opened this issue Oct 29, 2019 · 3 comments
Open

Inline for lambda #13679

blowin opened this issue Oct 29, 2019 · 3 comments
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI
Milestone

Comments

@blowin
Copy link

blowin commented Oct 29, 2019

Now:

public int Calculate(Func<int, int, int> c)
{
 return c(10, 20);   
}

public int CallCalculate() 
{
 return Calculate((x1, x2) => x1 + x2);   
}

Compile to:

private sealed class <>c
{
    public static readonly <>c <>9 = new <>c();

    public static Func<int, int, int> <>9__1_0;

    internal int <CallCalculate>b__1_0(int x1, int x2)
    {
        return x1 + x2;
    }
}

public int Calculate(Func<int, int, int> c)
{
    return c(10, 20);
}

public int CallCalculate()
{
    return Calculate(<>c.<>9__1_0 ?? (<>c.<>9__1_0 = new Func<int, int, int>(<>c.<>9.<CallCalculate>b__1_0)));
}

Is inline lambda possible?
Like this:

public int Calculate()
{
 return 10 + 20;   
}

public int CallCalculate() 
{
 return Calculate();   
}

category:cq
theme:inlining
skill-level:expert
cost:large

@msftgits msftgits transferred this issue from dotnet/coreclr Jan 31, 2020
@msftgits msftgits added this to the Future milestone Jan 31, 2020
@BruceForstall BruceForstall added the JitUntriaged CLR JIT issues needing additional triage label Oct 28, 2020
@am11
Copy link
Member

am11 commented Nov 10, 2022

#44610 is planned for 8.0. @EgorBo, does #44610 cover these kind of (simple callback) calls for inlining?

current codegen: https://godbolt.org/z/Wc56zrcoq

@EgorBo
Copy link
Member

EgorBo commented Nov 10, 2022

#44610 is planned for 8.0. @EgorBo, does #44610 cover these kind of (simple callback) calls for inlining?

current codegen: https://godbolt.org/z/Wc56zrcoq

I think the current plan is to rely on guarded devirtualization for delegates for such things. Pure inlining is complicated because of that cache machinery Roslyn emits - hard to track types.

@am11
Copy link
Member

am11 commented Nov 10, 2022

Ah, I see. Perhaps in some future milestone, linker (or some other pre-processing step) can turn Func and Action into regular methods; at least for simple / non-capturing cases? Couple of attributes in IL are emitted to identify the compiler-generated machinery (more can be added if needed to disambiguate the patterns).

For reference, C++ lambdas: https://godbolt.org/z/b4GThT8z6 and Swift closures: https://godbolt.org/z/8jf1jxq38 get CallCalculate optimized to return 30.

@BruceForstall BruceForstall removed the JitUntriaged CLR JIT issues needing additional triage label Jan 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI
Projects
None yet
Development

No branches or pull requests

5 participants