Skip to content

Conversation

@EgorBo
Copy link
Member

@EgorBo EgorBo commented Apr 27, 2023

Quick 5 lines-of-code change to enable ARR_LENGTH(JitNewArr) constant folding in VN:

int Test()
{
    var a = new int[100];
    return a.Length;
}

Current codegen:

; Method Proga:Test():int:this
       sub      rsp, 40
       mov      rcx, 0xD1FFAB1E      ; int[]
       mov      edx, 100
       call     CORINFO_HELP_NEWARR_1_VC
       mov      eax, dword ptr [rax+08H]
       add      rsp, 40
       ret      
; Total bytes of code: 32

New codegen:

; Method Proga:Test():int:this
       mov      eax, 100
       ret      
; Total bytes of code: 6

I recommend enabling "hide whitespaces" for code review since most of the changes are just extra { } block

@ghost ghost added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Apr 27, 2023
@ghost ghost assigned EgorBo Apr 27, 2023
@ghost
Copy link

ghost commented Apr 27, 2023

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

Issue Details

3 lines-of-code change to enable ARR_LENGTH(JitNewArr) in VN:

int Test()
{
    var a = new int[100];
    return a.Length;
}

Current codegen:

; Method Proga:Test():int:this
       sub      rsp, 40
       mov      rcx, 0xD1FFAB1E      ; int[]
       mov      edx, 100
       call     CORINFO_HELP_NEWARR_1_VC
       mov      eax, dword ptr [rax+08H]
       add      rsp, 40
       ret      
; Total bytes of code: 32

New codegen:

; Method Proga:Test():int:this
       mov      eax, 100
       ret      
; Total bytes of code: 6

I recommend enabling "ignore whitespaces" since most of the changes are just extra { } block

Author: EgorBo
Assignees: EgorBo
Labels:

area-CodeGen-coreclr

Milestone: -

@EgorBo
Copy link
Member Author

EgorBo commented Apr 28, 2023

@dotnet/jit-contrib PTAL, simple change (if you ignore whitespace changes). diffs

}
}
// Case 3: ARR_LENGTH(new T[CNS])
else if ((funcApp.m_func == VNF_JitNewArr) && IsVNConstant(funcApp.m_args[1]))
Copy link
Contributor

Choose a reason for hiding this comment

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

There is also VNF_JitReadyToRunNewArr.

There is ValueNumStore::GetNewArrSize, it would be ideal to extract a part from it that deals with an already-queried VNFuncApp (e. g. as bool TryGetNewArrSize(VNFuncApp* pFunc, int* pSize)).

Copy link
Member Author

Choose a reason for hiding this comment

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

yeah, my first impl used GetNewArrSize but for TP reasons I inlined it here, forgot about R2R

Copy link
Member Author

Choose a reason for hiding this comment

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

Simplified the impl, GetVNFunc is still here, if TP report is fine with that then I don't think I need to complicate it more.

Copy link
Member Author

Choose a reason for hiding this comment

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

image

@EgorBo EgorBo merged commit c46c5b8 into dotnet:main Apr 28, 2023
@EgorBo EgorBo deleted the arrlen-size branch April 28, 2023 13:10
@EgorBo
Copy link
Member Author

EgorBo commented Apr 28, 2023

Uploading image.png…

@ghost ghost locked as resolved and limited conversation to collaborators Jun 7, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-CodeGen-coreclr CLR 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.

3 participants