Repository navigation
Closed
Labels
JitUntriagedCLR JIT issues needing additional triagearea-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
Milestone
Description
Byte array iteration code:
for (int i = 0; i < Iterations; i++)
{
unchecked
{
for (int j = 0; j < ArraySize; j++)
{
arr[j] = 123;
}
}
}Struct iteration code
[StructLayout(LayoutKind.Sequential, Pack = 1, Size = 1)]
struct MyByte
{
private readonly byte _byte;
public MyByte(byte b)
{
_byte = b;
}
public byte Value
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return _byte; }
}
}
// ...
for (int i = 0; i < Iterations; i++)
{
unchecked
{
for (int j = 0; j < ArraySize; j++)
{
arr[j] = new MyByte(123);
}
}
}Generated code (byte array):
for (int j = 0; j < ArraySize; j++)
00523003 xor edx,edx
00523005 mov ecx,dword ptr [esi+4]
{
arr[j] = 123;
00523008 cmp edx,ecx
0052300A jae 0052304F
0052300C mov byte ptr [esi+edx+8],7Bh
for (int j = 0; j < ArraySize; j++)
00523011 inc edx
00523012 cmp edx,0F4240h
00523018 jl 00523008
for (int i = 0; i < Iterations; i++)
0052301A inc edi
0052301B cmp edi,7D0h
00523021 jl 00523003
}
Generated code for struct - (expecting similar generated code - this is the simplest optimization you can think of here, I even provided struct size and readonly to make it easier)
for (int j = 0; j < ArraySize; j++)
00522F0B xor edx,edx
00522F0D mov ecx,dword ptr [edi+4]
00522F10 mov ebx,7Bh
{
arr[j] = new MyByte(123);
00522F15 cmp edx,ecx
00522F17 jae 00522F63
00522F19 lea esi,[edi+edx+8]
00522F1D mov byte ptr [esi],bl
for (int j = 0; j < ArraySize; j++)
00522F1F inc edx
00522F20 cmp edx,0F4240h
00522F26 jl 00522F10
for (int i = 0; i < Iterations; i++)
00522F28 inc dword ptr [ebp-14h]
00522F2B cmp dword ptr [ebp-14h],7D0h
00522F32 jl 00522F0B
}
This is more than 30% slower and this is just a simplest case.
category:cq
theme:assertion-prop
skill-level:expert
cost:extra-large
Reacted by Viktor Svub
Metadata
Metadata
Assignees
Labels
JitUntriagedCLR JIT issues needing additional triagearea-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
Activity
mikedn commentedon Nov 10, 2015
This is 32 bit code, it's not generated by RyuJIT.
For
byte[]RyuJIT generates:For
MyByte[]RyuJIT generates:The 2 versions have very similar codegen but even so the
MyByteversion is still some ~10% slower, probably due to the additionalmov r8d, 123.That makes no difference. So does the use of
unchcked.cmckinsey commentedon Nov 16, 2015
@krwq Thanks for the report. And @mikedn thanks for the analysis. There is definitely room for improvement. While this test case is just a synthetic example, it is actually representative of many CPU intensive inner loops that we care about.
I believe most of these are things the team is aware of. If I can summarize:
However we'll leave this issue open to cover solely the difference between the scalar and struct version and issues around handling struct fields as well as scalars for opts. @CarolEidt has been looking into some improvements recently in this area.
GSPP commentedon Mar 27, 2016
I think this benchmark highlights an architectural issue: Structs should not be inherently different from other primitives.
It might be architecturally necessary to treat them differently but any difference is a smell in my opinion.
CarolEidt commentedon May 14, 2018
Looking at the dump, it appears that this is actually an optimization issue (not a struct issue). After value numbering, we have the following for the assignment to the
MyBytearray:where
$c5is the value number for the constant123but it seems that assertion propagation fails to replaceLCL_VAR ubyte V04with the constant.mikedn commentedon May 15, 2018
There are 2 things that block this optimization. One is
optVNConstantPropCurStmtchecking forGTF_DONT_CSEon the lclvar node. The other isoptVNConstantPropOnTreefailure to handle small types.The later seems easy to fix but I'm not sure what's up with the former - I did not check why
GTF_DONT_CSEwas set on that node and it's not readily obvious why constant propagation is blocked in this case.mikedn commentedon May 15, 2018
GTF_DONT_CSEseems to be set here: https://github.com/dotnet/coreclr/blob/ba110d51de1994dd5f1a38e224b442dd1609d24f/src/jit/morph.cpp#L11249-l11251CarolEidt commentedon May 15, 2018
@mikedn - thanks for tracking that down! That's presumably one of the vestiges of the IR changes I made to represent struct assignments with
GT_ASGinstead ofGT_COPYBLKet al, while trying not to yet change the code being generated.SingleAccretion commentedon Aug 26, 2022
In .NET 8 (and 7) the code for both loops is identical.
VN-based constant propagation for small-typed locals in particular was enabled in #57726.