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

Dead cloned loops are not removed #5820

Closed
erozenfeld opened this issue May 9, 2016 · 2 comments
Closed

Dead cloned loops are not removed #5820

erozenfeld opened this issue May 9, 2016 · 2 comments
Assignees
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI enhancement Product code improvement that does NOT require public API changes/additions optimization tenet-performance Performance related issue
Milestone

Comments

@erozenfeld
Copy link
Member

I noticed this problem in the code RyuJIT generates for several BenchI benchamrks. For example, here is the source code for Iniarray benchmark:

    [MethodImpl(MethodImplOptions.NoInlining)]
    static bool Bench() {
        char[] workarea = new char[Allotted];
        for (int i = 0; i < Iterations; i++) {
            for (int j = 0; j < Allotted; j++) {
                workarea[j] = ' ';
            }
        }
        Escape(workarea);
        return true;
    }

and here is the code the jit is generating:

; Assembly listing for method IniArray:Bench():bool
; Emitting BLENDED_CODE for X64 CPU with AVX
; optimized code
; rsp based frame
; fully interruptible
; Final local variable assignments
;
;  V00 loc0         [V00,T01] (  5,  38  )     ref  ->  rax
;  V01 loc1         [V01,T02] (  4,  13  )     int  ->  rdx
;  V02 loc2         [V02,T00] (  9, 147  )     int  ->  rcx
;  V03 OutArgs      [V03    ] (  1,   1  )  lclBlk (32) [rsp+0x00]
;
; Lcl frame size = 40

G_M24601_IG01:
       4883EC28             sub      rsp, 40

G_M24601_IG02:
       48B96AF65730FA7F0000 mov      rcx, 0x7FFA3057F66A
       BA10000000           mov      edx, 16
       E898B96C5F           call     CORINFO_HELP_NEWARR_1_VC
       33D2                 xor      edx, edx

G_M24601_IG03:
       33C9                 xor      ecx, ecx
       448B4008             mov      r8d, dword ptr [rax+8]
       4983F810             cmp      r8, 16
       7C15                 jl       SHORT G_M24601_IG05

G_M24601_IG04:
       4C63C1               movsxd   r8, ecx
       6642C74440102000     mov      word  ptr [rax+2*r8+16], 32
       FFC1                 inc      ecx
       83F910               cmp      ecx, 16
       7CEE                 jl       SHORT G_M24601_IG04
       EB14                 jmp      SHORT G_M24601_IG06

G_M24601_IG05:
       4C63C1               movsxd   r8, ecx
       6642C74440102000     mov      word  ptr [rax+2*r8+16], 32
       FFC1                 inc      ecx
       83F910               cmp      ecx, 16
       7CEE                 jl       SHORT G_M24601_IG05

G_M24601_IG06:
       FFC2                 inc      edx
       85D2                 test     edx, edx
       7EC8                 jle      SHORT G_M24601_IG03

G_M24601_IG07:
       48B9B027009017020000 mov      rcx, 0x217900027B0
       488BD0               mov      rdx, rax
       E88C4DF25E           call     CORINFO_HELP_CHECKED_ASSIGN_REF
       B801000000           mov      eax, 1

G_M24601_IG08:
       4883C428             add      rsp, 40
       C3                   ret

; Total bytes of code 110, prolog size 4 for method IniArray:Bench():bool
; ============================================================

Note that while the array bounds check in the G_M24601_IG05 loop (created by optCloneLoops) is eliminated, the loop itself is not eliminated even though it's dead.

category:cq
theme:loop-opt
skill-level:intermediate
cost:medium

@msftgits msftgits transferred this issue from dotnet/coreclr Jan 30, 2020
@msftgits msftgits added this to the Future milestone Jan 30, 2020
@BruceForstall BruceForstall added the JitUntriaged CLR JIT issues needing additional triage label Oct 28, 2020
@BruceForstall BruceForstall modified the milestones: Future, 6.0.0 Jan 15, 2021
@BruceForstall BruceForstall removed the JitUntriaged CLR JIT issues needing additional triage label Jan 15, 2021
@JulieLeeMSFT JulieLeeMSFT added the needs-further-triage Issue has been initially triaged, but needs deeper consideration or reconsideration label Mar 23, 2021
@JulieLeeMSFT JulieLeeMSFT removed the needs-further-triage Issue has been initially triaged, but needs deeper consideration or reconsideration label Jun 7, 2021
@BruceForstall BruceForstall modified the milestones: 6.0.0, 7.0.0 Jul 6, 2021
@AndyAyersMS
Copy link
Member

If the loop is truly unreachable this should be fixed by #66967

@kunalspathak can you verify?

@kunalspathak
Copy link
Member

We do remove the loops because of improvements in "redundant branch opt" phase. The new code gen:

; Assembly listing for method ConsoleApp15.loop_hoist:Bench():bool
; Emitting BLENDED_CODE for X64 CPU with AVX - Windows
; optimized code
; rsp based frame
; fully interruptible
; No PGO data
; Final local variable assignments
;
;  V00 loc0         [V00,T01] (  3, 17.84)     ref  ->  rax         class-hnd exact single-def
;  V01 loc1         [V01,T02] (  4, 13   )     int  ->  rcx
;  V02 loc2         [V02,T00] (  5, 67.36)     int  ->  rdx
;  V03 OutArgs      [V03    ] (  1,  1   )  lclBlk (32) [rsp+00H]   "OutgoingArgSpace"
;
; Lcl frame size = 40

G_M29985_IG01:
       sub      rsp, 40
                                                ;; size=4 bbWeight=1    PerfScore 0.25
G_M29985_IG02:
       mov      rcx, 0xD1FFAB1E      ; System.Char[]
       mov      edx, 16
       call     CORINFO_HELP_NEWARR_1_VC
       xor      ecx, ecx
                                                ;; size=22 bbWeight=1    PerfScore 1.75
G_M29985_IG03:
       xor      edx, edx
       align    [4 bytes for IG04]
                                                ;; size=6 bbWeight=4    PerfScore 2.00
G_M29985_IG04:
       mov      r8d, edx
       mov      word  ptr [rax+2*r8+16], 32
       inc      edx
       cmp      edx, 16
       jl       SHORT G_M29985_IG04
                                                ;; size=18 bbWeight=15.84 PerfScore 43.56
G_M29985_IG05:
       inc      ecx
       cmp      ecx, 0xD1FFAB1E
       jl       SHORT G_M29985_IG03
                                                ;; size=10 bbWeight=4    PerfScore 6.00
G_M29985_IG06:
       mov      rcx, rax
       call     [ConsoleApp15.loop_hoist:Escape(System.Char[])]
       mov      eax, 1
                                                ;; size=14 bbWeight=1    PerfScore 3.50
G_M29985_IG07:
       add      rsp, 40
       ret
                                                ;; size=5 bbWeight=1    PerfScore 1.25

@ghost ghost locked as resolved and limited conversation to collaborators May 29, 2022
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 enhancement Product code improvement that does NOT require public API changes/additions optimization tenet-performance Performance related issue
Projects
None yet
Development

No branches or pull requests

6 participants