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

Consider optimizing constrained calls to base methods on readonly struct in some cases #66365

Open
jcouv opened this issue Jan 11, 2023 · 0 comments
Labels
Area-Compilers Code Gen Quality Room for improvement in the quality of the compiler's generated code
Milestone

Comments

@jcouv
Copy link
Member

jcouv commented Jan 11, 2023

Follow-up issue on comment #66189 (comment)

In the following test, we could avoid copying the instance of S1 as receiver for constrained call to object.ToString().
Aleksey's proposal: Perhaps we could optimize cases like this as long as consumer and the consumed type are in the same module.

        [Fact]
        public void InvokeOnThisBaseMethods()
        {
            var text = @"
class Program
{
    static void Main()
    {
        default(S1).Test();
    }
    readonly struct S1
    {
        public void Test()
        {
            System.Console.Write(this.GetType());
            System.Console.Write(ToString());
        }
    }
}
";

            var comp = CompileAndVerify(text, parseOptions: TestOptions.Regular, verify: Verification.Passes, expectedOutput: @"Program+S1Program+S1");

            comp.VerifyIL("Program.S1.Test()", @"
{
  // Code size       47 (0x2f)
  .maxstack  1
  .locals init (Program.S1 V_0)
  IL_0000:  ldarg.0
  IL_0001:  ldobj      ""Program.S1""
  IL_0006:  box        ""Program.S1""
  IL_000b:  call       ""System.Type object.GetType()""
  IL_0010:  call       ""void System.Console.Write(object)""
  IL_0015:  ldarg.0
  IL_0016:  ldobj      ""Program.S1""
  IL_001b:  stloc.0
  IL_001c:  ldloca.s   V_0
  IL_001e:  constrained. ""Program.S1""
  IL_0024:  callvirt   ""string object.ToString()""
  IL_0029:  call       ""void System.Console.Write(string)""
  IL_002e:  ret
}");
        }

If we can avoid this copy, we would instead have the following IL (like before):

            comp.VerifyIL("Program.S1.Test()", @"
{
  // Code size       39 (0x27)
  .maxstack  1
  IL_0000:  ldarg.0
  IL_0001:  ldobj      ""Program.S1""
  IL_0006:  box        ""Program.S1""
  IL_000b:  call       ""System.Type object.GetType()""
  IL_0010:  call       ""void System.Console.Write(object)""
  IL_0015:  ldarg.0
  IL_0016:  constrained. ""Program.S1""
  IL_001c:  callvirt   ""string object.ToString()""
  IL_0021:  call       ""void System.Console.Write(string)""
  IL_0026:  ret
}");

Relates to PR #66189 which fixed some correctness issues with such constrained calls.

@jcouv jcouv added Area-Compilers Code Gen Quality Room for improvement in the quality of the compiler's generated code labels Jan 11, 2023
@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged Issues and PRs which have not yet been triaged by a lead label Jan 11, 2023
@jcouv jcouv added this to the Compiler.Next milestone Jan 20, 2023
@jcouv jcouv removed the untriaged Issues and PRs which have not yet been triaged by a lead label Jan 20, 2023
@jaredpar jaredpar modified the milestones: Compiler.Next, Backlog Sep 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Code Gen Quality Room for improvement in the quality of the compiler's generated code
Projects
None yet
Development

No branches or pull requests

2 participants