Repository navigation
Loops aren't cloned for Spans #82946
Copy link
Copy link
Closed
Labels
area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMICLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIin-prThere is an active PR which will close this issue when it is mergedThere is an active PR which will close this issue when it is merged
Milestone
Description
void Test(Span<byte> source)
{
for (int i = 0; i < 100; i++)
source[i] = 0;
}Codegen:
; Method Progr:Test(System.Span`1[ubyte]):this
G_M11420_IG01:
4883EC28 sub rsp, 40
G_M11420_IG02:
33C0 xor eax, eax
align [0 bytes for IG03]
G_M11420_IG03:
3B4208 cmp eax, dword ptr [rdx+08H]
7317 jae SHORT G_M11420_IG05
488B0A mov rcx, bword ptr [rdx]
448BC0 mov r8d, eax
42C6040100 mov byte ptr [rcx+r8], 0
FFC0 inc eax
83F864 cmp eax, 100
7CE9 jl SHORT G_M11420_IG03
G_M11420_IG04:
4883C428 add rsp, 40
C3 ret
G_M11420_IG05:
E829C64C5F call CORINFO_HELP_RNGCHKFAIL
CC int3
; Total bytes of code: 40*************** Starting PHASE Clone loops
*************** In optCloneLoops()
Considering loop L00 to clone for optimizations.
Not checking loop L00 -- no array bounds or type tests in this method
------------------------------------------------------------
No clonable loops
*************** Finishing PHASE Clone loops
Here it is expected to have two loops and one of them without bound checks.
cc @BruceForstall @dotnet/jit-contrib
category:cq
theme:loop-opt
skill-level:expert
cost:medium
impact:large
Metadata
Metadata
Assignees
Labels
area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMICLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIin-prThere is an active PR which will close this issue when it is mergedThere is an active PR which will close this issue when it is merged
Activity
AndyAyersMS commentedon Mar 3, 2023
Is this a regression? Back when we first implemented span we deliberately patterned the IR after array so that we'd get all the same optimizations.
SingleAccretion commentedon Mar 3, 2023
No, it has always been like that.
[-]Loops aren't clonned for Spans[/-][+]Loops aren't cloned for Spans[/+]AndyAyersMS commentedon Dec 18, 2024
And, guess it was me who made it so: dotnet/coreclr#19493
PavelCibulka commentedon Dec 22, 2024
What about having the JIT recognize Math.Min and eliminate the bounds check. This way, a programmer can guarantee bounds by using Math.Min.
Also it would be nice if JIT also recognize Downwards loop variants.
EgorBo commentedon Dec 22, 2024
It is expected to be handled by Loop Clonning as is today, just not for spans (yet). E.g. for Arrays:
Emits:
It is still emitting the slow loop here which is redundant, but it shouldn't affect performance
EgorBo commentedon Dec 22, 2024
cc @AndyAyersMS it looks like we can actually fold the "clonned" condition here via assertions (over PHI node).