Skip to content

Dead cloned loops are not removed #5820

@erozenfeld

Description

@erozenfeld

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

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIenhancementProduct code improvement that does NOT require public API changes/additionsoptimizationtenet-performancePerformance related issue

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions