Skip to content

GDV: don't emit fallback call if classes are "exact" - #87055

Merged
EgorBo merged 9 commits into
dotnet:mainfrom
EgorBo:gdv-omit-fallback
Jun 19, 2023
Merged

GDV: don't emit fallback call if classes are "exact"#87055
EgorBo merged 9 commits into
dotnet:mainfrom
EgorBo:gdv-omit-fallback

Conversation

@EgorBo

@EgorBoEgorBo commented Jun 2, 2023

Copy link
Copy Markdown
Member

Contributes to #86769 and closes#86235

usingSystem;usingSystem.Runtime.CompilerServices;usingSystem.Threading;publicinterfaceIAnimal{voidMakeSound();}publicclassCat:IAnimal{publicvoidMakeSound()=>Console.WriteLine("Meow!");}publicclassDog:IAnimal{publicvoidMakeSound()=>Console.WriteLine("Woof!");}publicclassCow:IAnimal{publicvoidMakeSound()=>Console.WriteLine("Moo!");}// This class is not used and will be trimmed outpublicclassLion:IAnimal{publicvoidMakeSound()=>Console.WriteLine("Roar!");}publicclassProgram{publicstaticvoidMain(string[]args){Test(newCat());Test(newDog());Test(newCow());}[MethodImpl(MethodImplOptions.NoInlining)]staticvoidTest(IAnimala)=>a.MakeSound();}

NativeAOT codegen for Test:

; Method Program:Test(IAnimal)subrsp,40learax,[(reloc 0x4000000000423000)]cmp qword ptr [rcx],rax ;; is it 'Cat'?jne SHORT G_M31564_IG04learcx, gword ptr [(reloc 0x4000000000423028)] ;; '"Meow!"'call System.Console:WriteLine(System.String)jmp SHORT G_M31564_IG07G_M31564_IG04:learax,[(reloc 0x4000000000423010)]cmp qword ptr [rcx],rax ;; is it 'Dog'?jne SHORT G_M31564_IG06learcx, gword ptr [(reloc 0x4000000000422f98)] ;; '"Woof!"'call System.Console:WriteLine(System.String)jmp SHORT G_M31564_IG07G_M31564_IG06:learcx, gword ptr [(reloc 0x4000000000422f70)] ;; '"Moo!"'call System.Console:WriteLine(System.String)G_M31564_IG07:nopaddrsp,40ret; Total bytes of code: 74

Virtual callback is not emitted since we took care of all possible classes implementing the given interface ("closed world" on NativeAOT). We might still emit it if we e.g. fail to inline one of the candidates (we currently don't do GDV just to devirtualize a call, we only do that when we're able to inline it) -- we might want to do that for cases where interface calls are expensive but that's a different task in #86769

@ghostghost added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Jun 2, 2023
@ghostghost assigned EgorBoJun 2, 2023
@ghost

ghost commented Jun 2, 2023

Copy link
Copy Markdown

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

Issue Details
usingSystem;usingSystem.Runtime.CompilerServices;usingSystem.Threading;publicinterfaceIAnimal{voidMakeSound();}publicclassCat:IAnimal{publicvoidMakeSound()=>Console.WriteLine("Meow!");}publicclassDog:IAnimal{publicvoidMakeSound()=>Console.WriteLine("Woof!");}publicclassCow:IAnimal{publicvoidMakeSound()=>Console.WriteLine("Moo!");}publicclassProgram{publicstaticvoidMain(string[]args){Test(newCat());Test(newDog());Test(newCow());}[MethodImpl(MethodImplOptions.NoInlining)]staticvoidTest(IAnimala)=>a.MakeSound();}

NativeAOT codegen for Test:

; Method Program:Test(IAnimal)subrsp,40learax,[(reloc 0x4000000000423000)]cmp qword ptr [rcx],rax ;; is it 'Cat'?jne SHORT G_M31564_IG04learcx, gword ptr [(reloc 0x4000000000423028)] ;; '"Meow!"'call System.Console:WriteLine(System.String)jmp SHORT G_M31564_IG07G_M31564_IG04: ;; offset=001EHlearax,[(reloc 0x4000000000423010)]cmp qword ptr [rcx],rax ;; is it 'Dog'?jne SHORT G_M31564_IG06learcx, gword ptr [(reloc 0x4000000000422f98)] ;; '"Woof!"'call System.Console:WriteLine(System.String)jmp SHORT G_M31564_IG07G_M31564_IG06: ;; offset=0038Hlearcx, gword ptr [(reloc 0x4000000000422f70)] ;; '"Moo!"'call System.Console:WriteLine(System.String)G_M31564_IG07: ;; offset=0044Hnopaddrsp,40ret; Total bytes of code: 74

Virtual callback is not emitted since we took care of all possible classes implementing the given interface ("closed world" on NativeAOT). We might still emit it if we e.g. fail to inline one of the candidates (we currently don't do GDV just to devirtualize a call, we only do that when we're able to inline it).

Author:EgorBo
Assignees:-
Labels:

area-CodeGen-coreclr

Milestone:-

@EgorBoEgorBo mentioned this pull request Jun 2, 2023
14 tasks
@EgorBo

Copy link
Copy Markdown
MemberAuthor

For reference, codegen for JIT:

staticvoidMain(){for(inti=0;i<200;i++){Test(newCat());Test(newDog());Test(newCow());Thread.Sleep(16);}}[MethodImpl(MethodImplOptions.NoInlining)]staticvoidTest(IAnimala)=>a.MakeSound();
; Assembly listing for method Program:Test(IAnimal)pushrsisubrsp,32movrsi, qword ptr [rcx]movrax,0x7FFF53C41C88 ; Catcmprsi,raxjne SHORT G_M31564_IG04movrcx,0x26780208F80 ; 'Meow!'call[System.Console:WriteLine(System.String)]jmp SHORT G_M31564_IG07G_M31564_IG04:movrax,0x7FFF53C41E48 ; Dogcmprsi,raxjne SHORT G_M31564_IG06movrcx,0x26780209128 ; 'Woof!'call[System.Console:WriteLine(System.String)]jmp SHORT G_M31564_IG07G_M31564_IG06:movrax,0x7FFF53C42008 ; Cowcmprsi,raxjne SHORT G_M31564_IG09movrcx,0x26780209148 ; 'Moo!'call[System.Console:WriteLine(System.String)]G_M31564_IG07:nopaddrsp,32poprsiretG_M31564_IG09:movr11,0x7FFF53130280call[r11]IAnimal:MakeSound():thisjmp SHORT G_M31564_IG07

(PGO-driven)

@EgorBo

Copy link
Copy Markdown
MemberAuthor

/azp run runtime-extra-platforms, runtime-coreclr pgo

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 2 pipeline(s).

@EgorBo

Copy link
Copy Markdown
MemberAuthor

/azp run runtime-extra-platforms

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@EgorBo

Copy link
Copy Markdown
MemberAuthor

/azp run runtime-extra-platforms

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@EgorBo
EgorBo marked this pull request as draft June 4, 2023 00:30
@EgorBo

Copy link
Copy Markdown
MemberAuthor

/azp run runtime-extra-platforms

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@EgorBo

Copy link
Copy Markdown
MemberAuthor

/azp run runtime-extra-platforms

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@EgorBo

Copy link
Copy Markdown
MemberAuthor

/azp run runtime-extra-platforms

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@EgorBo

Copy link
Copy Markdown
MemberAuthor

/azp run runtime-extra-platforms

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@EgorBo

Copy link
Copy Markdown
MemberAuthor

@AndyAyersMS PTAL, it passes NativeAOT outerloop tests + 3 type checks

@EgorBo
EgorBo requested a review from AndyAyersMSJune 11, 2023 10:34
@EgorBo
EgorBo marked this pull request as ready for review June 11, 2023 11:40
// Distribute the rounding error and just apply it to the first entry.
// Assume that there is no error If we have unknown handles.
if (numberOfClasses == h.m_totalCount)
if (!containsUnknownHandles)

@EgorBoEgorBoJun 11, 2023

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an unrelated change, it's just that I used a wrong check in #86965 so it didn't work properly

@EgorBo
EgorBo merged commit ce61999 into dotnet:mainJun 19, 2023
@EgorBo
EgorBo deleted the gdv-omit-fallback branch June 19, 2023 07:50
@ghostghost locked as resolved and limited conversation to collaborators Jul 19, 2023
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Guarded devirtualization based on whole program view

2 participants

@EgorBo@AndyAyersMS