Skip to content

JIT: IV widening does not kick in for simple foreach over List<int> #101176

@jakobbotsch

Description

@jakobbotsch

Example taken from #9304:

    static int SumForeach(List<int> l)
    {
        int result = 0;

        foreach (int i in l)
        {
            result += i;
        }

        return result;
    }

Codegen:

; Assembly listing for method Program:SumForeach(System.Collections.Generic.List`1[int]):int (FullOpts)
; Emitting BLENDED_CODE for X64 with AVX - Windows
; FullOpts code
; optimized code
; rsp based frame
; fully interruptible
; No PGO data
; 2 inlinees with PGO data; 4 single block inlinees; 0 inlinees without PGO data
; Final local variable assignments
;
;  V00 arg0         [V00,T07] (  3,  3   )     ref  ->  rcx         class-hnd single-def <System.Collections.Generic.List`1[int]>
;  V01 loc0         [V01,T04] (  4, 12.27)     int  ->  rax        
;* V02 loc1         [V02    ] (  0,  0   )  struct (24) zero-ref    ld-addr-op <System.Collections.Generic.List`1+Enumerator[int]>
;* V03 loc2         [V03    ] (  0,  0   )     int  ->  zero-ref   
;  V04 OutArgs      [V04    ] (  1,  1   )  struct (32) [rsp+0x00]  do-not-enreg[XS] addr-exposed "OutgoingArgSpace"
;* V05 tmp1         [V05    ] (  0,  0   )  struct (24) zero-ref    ld-addr-op "NewObj constructor temp" <System.Collections.Generic.List`1+Enumerator[int]>
;* V06 tmp2         [V06,T06] (  0,  0   )   ubyte  ->  zero-ref    "Inline return value spill temp"
;* V07 tmp3         [V07    ] (  0,  0   )     ref  ->  zero-ref    class-hnd "Inline stloc first use temp" <System.Collections.Generic.List`1[int]>
;  V08 tmp4         [V08,T03] (  3, 12.63)     ref  ->  rcx         single-def "field V02._list (fldOffset=0x0)" P-INDEP
;  V09 tmp5         [V09,T00] (  6, 23.54)     int  ->  rdx         "field V02._index (fldOffset=0x8)" P-INDEP
;* V10 tmp6         [V10,T11] (  0,  0   )     int  ->  zero-ref    single-def "field V02._version (fldOffset=0xc)" P-INDEP
;  V11 tmp7         [V11,T05] (  2,  7.27)     int  ->   r8         "field V02._current (fldOffset=0x10)" P-INDEP
;  V12 tmp8         [V12,T08] (  3,  3   )     ref  ->  rcx         single-def "field V05._list (fldOffset=0x0)" P-INDEP
;* V13 tmp9         [V13,T12] (  0,  0   )     int  ->  zero-ref    single-def "field V05._index (fldOffset=0x8)" P-INDEP
;* V14 tmp10        [V14,T09] (  0,  0   )     int  ->  zero-ref    single-def "field V05._version (fldOffset=0xc)" P-INDEP
;* V15 tmp11        [V15    ] (  0,  0   )     int  ->  zero-ref    single-def "field V05._current (fldOffset=0x10)" P-INDEP
;  V16 tmp12        [V16,T01] (  3, 21.81)     ref  ->   r8         "arr expr"
;* V17 cse0         [V17,T10] (  0,  0   )     int  ->  zero-ref    "CSE #01: aggressive"
;  V18 cse1         [V18,T02] (  2, 16   )     int  ->   r8         "CSE #02: aggressive"
;
; Lcl frame size = 40

G_M40154_IG01:  ;; offset=0x0000
       sub      rsp, 40
						;; size=4 bbWeight=1 PerfScore 0.25
G_M40154_IG02:  ;; offset=0x0004
       xor      eax, eax
       mov      edx, dword ptr [rcx+0x14]
       xor      edx, edx
       jmp      SHORT G_M40154_IG04
       align    [0 bytes for IG03]
						;; size=9 bbWeight=1 PerfScore 4.50
G_M40154_IG03:  ;; offset=0x000D
       add      eax, r8d
						;; size=3 bbWeight=3.63 PerfScore 0.91
G_M40154_IG04:  ;; offset=0x0010
       mov      r8d, dword ptr [rcx+0x10]
       cmp      edx, r8d
       jae      SHORT G_M40154_IG06
						;; size=9 bbWeight=8 PerfScore 26.00
G_M40154_IG05:  ;; offset=0x0019
       mov      r8, gword ptr [rcx+0x08]
       cmp      edx, dword ptr [r8+0x08]
       jae      SHORT G_M40154_IG07
       mov      r10d, edx
       mov      r8d, dword ptr [r8+4*r10+0x10]
       inc      edx
       jmp      SHORT G_M40154_IG03
						;; size=22 bbWeight=3.63 PerfScore 38.16
G_M40154_IG06:  ;; offset=0x002F
       add      rsp, 40
       ret      
						;; size=5 bbWeight=4 PerfScore 5.00
G_M40154_IG07:  ;; offset=0x0034
       call     CORINFO_HELP_RNGCHKFAIL
       int3     
						;; size=6 bbWeight=0 PerfScore 0.00

; Total bytes of code 58, prolog size 4, PerfScore 74.82, instruction count 21, allocated bytes for code 58 (MethodHash=ae626325) for method Program:SumForeach(System.Collections.Generic.List`1[int]):int (FullOpts)
; ============================================================

The zero extension in

       mov      r10d, edx
       mov      r8d, dword ptr [r8+4*r10+0x10]

is not expected.

Metadata

Metadata

Assignees

Labels

Priority:2Work that is important, but not critical for the releasearea-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions