Skip to content

.Slice(X) vs .[X..] codegen #122042

@EgorBo

Description

@EgorBo

For span[8..] Roslyn emits span.Slice(8, values.Length - 8) (can be improved via dotnet/roslyn#74643) which should emit the same codegen as just span.Slice(8):

Span<int> Test1(Span<int> values) => values[8..];
Span<int> Test2(Span<int> values) => values.Slice(8, values.Length - 8);
Span<int> Test3(Span<int> values) => values.Slice(8);

Codegen for these 3:

; Method MyClass:Test1(System.Span`1[int]):System.Span`1[int]:this (FullOpts)
       sub      rsp, 24
       mov      bword ptr [rsp+0x08], rsi
       mov      qword ptr [rsp+0x10], rdx
       mov      edx, dword ptr [rsp+0x10]
       add      edx, -8
       mov      eax, edx
       add      rax, 8
       mov      ecx, dword ptr [rsp+0x10]
       cmp      rax, rcx
       ja       SHORT G_M8666_IG04
       mov      rax, bword ptr [rsp+0x08]
       add      rax, 32
       add      rsp, 24
       ret      
G_M8666_IG04:
       mov      rax, 0x7FFBDC03E898
       call     [rax]System.ThrowHelper:ThrowArgumentOutOfRangeException()
       int3     
; Total bytes of code: 63


; Method MyClass:Test2(System.Span`1[int]):System.Span`1[int]:this (FullOpts)
       push     rax
       lea      ecx, [rdx-0x08]
       mov      eax, ecx
       add      rax, 8
       mov      edx, edx
       cmp      rax, rdx
       ja       SHORT G_M26905_IG04
       lea      rax, bword ptr [rsi+0x20]
       mov      rdx, rcx
       add      rsp, 8
       ret      
G_M26905_IG04:
       mov      rax, 0x7FFBDC03E898
       call     [rax]System.ThrowHelper:ThrowArgumentOutOfRangeException()
       int3     
; Total bytes of code: 42


; Method MyClass:Test3(System.Span`1[int]):System.Span`1[int]:this (FullOpts)
       push     rax
       cmp      edx, 8
       jl       SHORT G_M25176_IG04
       lea      rax, bword ptr [rsi+0x20]
       add      edx, -8
       add      rsp, 8
       ret      
G_M25176_IG04:
       mov      rax, 0x7FFBDC03E898
       call     [rax]System.ThrowHelper:ThrowArgumentOutOfRangeException()
       int3     
; Total bytes of code: 31

Metadata

Metadata

Assignees

Labels

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

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions