Repository navigation
Closed
Labels
area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIoptimization
Milestone
Description
e.g.
mov r8d,dword ptr [rax]
add rax,4
mov r8d,r8d ; pointless mov
...
movzx eax,byte ptr [rax]
mov eax,eax ; pointless mov Example
The Checksum routine Magma.Common/Checksum.cs
// Internet Checksum as defined by RFC 791, RFC 793, RFC 1071, RFC 1141, RFC 1624
public static ushort Calcuate(ref byte buffer, int length)
{
ref var current = ref buffer;
ulong sum = 0;
while (length >= sizeof(ulong))
{
length -= sizeof(ulong);
var ulong0 = Unsafe.As<byte, ulong>(ref current);
current = ref Unsafe.Add(ref current, sizeof(ulong));
// Add with carry
sum += ulong0;
if (sum < ulong0)
{
sum++;
}
}
if ((length & sizeof(uint)) != 0)
{
var uint0 = Unsafe.As<byte, uint>(ref current);
current = ref Unsafe.Add(ref current, sizeof(uint));
// Add with carry
sum += uint0;
if (sum < uint0)
{
sum++;
}
}
if ((length & sizeof(ushort)) != 0)
{
var ushort0 = Unsafe.As<byte, ushort>(ref current);
current = ref Unsafe.Add(ref current, sizeof(ushort));
// Add with carry
sum += ushort0;
if (sum < ushort0)
{
sum++;
}
}
if ((length & sizeof(byte)) != 0)
{
var byte0 = current;
// Add with carry
sum += byte0;
if (sum < byte0)
{
sum++;
}
}
// Fold down to 16 bits
var uint1 = (uint)(sum >> 32);
var uint2 = (uint)sum;
// Add with carry
uint1 += uint2;
if (uint1 < uint2)
{
uint1++;
}
var ushort2 = (ushort)uint1;
var ushort1 = (ushort)(uint1 >> 16);
// Add with carry
ushort1 = (ushort)(ushort1 + ushort2);
if (ushort1 < ushort2)
{
ushort1++;
}
// Invert to get ones-complement result
return (ushort)~ushort1;
}Generates asm with 3 pointless movs
mov rax,rcx
xor ecx,ecx
cmp edx,8
jl 00007FFB767741B4
add edx,0FFFFFFF8h
mov r8,qword ptr [rax]
add rax,8
add rcx,r8
cmp rcx,r8
jae 00007FFB767741AF
inc rcx
cmp edx,8
jge 00007FFB7677419A
test dl,4
je 00007FFB767741CE
mov r8d,dword ptr [rax]
add rax,4
mov r8d,r8d ; pointless mov
add rcx,r8
cmp rcx,r8
jae 00007FFB767741CE
inc rcx
test dl,2
je 00007FFB767741E9
movzx r8d,word ptr [rax]
add rax,2
mov r8d,r8d ; pointless mov
add rcx,r8
cmp rcx,r8
jae 00007FFB767741E9
inc rcx
test dl,1
je 00007FFB767741FE
movzx eax,byte ptr [rax]
mov eax,eax ; pointless mov
add rcx,rax
cmp rcx,rax
jae 00007FFB767741FE
inc rcx
mov rax,rcx
shr rax,20h
add eax,ecx
cmp eax,ecx
jae 00007FFB7677420D
inc eax
movzx edx,ax
shr eax,10h
movzx eax,ax
add eax,edx
movzx eax,ax
cmp eax,edx
jge 00007FFB76774224
inc eax
movzx eax,ax
not eax
movzx eax,ax
ret category:cq
theme:basic-cq
skill-level:intermediate
cost:medium
impact:medium
Metadata
Metadata
Assignees
Labels
area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIoptimization
Activity
mikedn commentedon May 11, 2018
Those aren't exactly pointless, they're zero extending casts from int to long. But yes, they're not necessary due to the fact that the loads that produced the values already zeroed out the upper 32 bits.
I have a PR in progress that's supposed to deal with some similar cases but I'm not sure it will have any effect here, the
Unsafe.Addthat's between the def and the use might cause problems. I'll check later today.gfoidl commentedon May 11, 2018
Aside from the codegen-issue: current CPUs (at least the Intel ones) will "optimize" these
movs away?!mikedn commentedon May 11, 2018
AFAIK Intel CPUs optimize register to register moves only if the source and destination are different. So unfortunately in this case these moves may end up wasting a cycle.
mikedn commentedon May 11, 2018
dotnet/coreclr#12676 should take care of the moves if the casts are done early, something like:
Otherwise it gets complicated and at the moment I don't know what needs to be done to fix this.
gfoidl commentedon May 11, 2018
When I read Intel® 64 and IA-32 Architectures Optimization Reference Manual -- Section 3.5.1.12 Zero-Latency MOV Instructions correct, so here the
movcan be eliminated. It doesn't state if it is definitely eliminated.Anyway it would be better to avoid these
movs in the asm.mikedn commentedon May 11, 2018
Yep, in rather typical Intel optimization manual fashion they have left out some details. Agner Fog's optimization manual tells a bit more - including that same register moves are not eliminated. And that
movzx eax, blis no longer eliminated on Haswell. And that move elimination succeeds in more than 80% case.Of course, Agner Fog could be wrong but then he put a ton of effort into gather and writing down all this information that there's a good chance that he is right.
mikedn commentedon May 11, 2018
Since the JIT has a habit to generate useless moves for various reasons it would be useful to get some numbers. Measuring some simple assembly code:
On my Haswell I get
So extra moves aren't without consequences and a single
mov ecx, ecxmove is more costly than 2 moves via a separate register.voinokin commentedon May 21, 2018
MOV r32, r32 is not exactly no-op in 64-bit mode - it zeroes upper 32 bits of registers
mikedn commentedon May 21, 2018
Yes, as already mentioned, they're used to implement zero extending casts. However, in this case they're useless because the upper bits are already 0.
In fact, they're probably useless most of the time because all (well, at least all "normal" ones, there may be some exceptions) 32 bit instructions zero out the upper 32 bits.
Zhentar commentedon Mar 23, 2019
What causes the generation of these movs? I'm getting quite a few in my hot loop here https://github.com/Zhentar/xxHash3.NET/blob/39961ae0e5f617216efc64797549de75a2b37dfa/xxHash3/xxHash3.cs#L292 and trying to figure out a workaround.
p.s. I am very disappointed this issue already exists and I don't get to open one titled "Jit likes to mov it, mov it"
stephentoub commentedon Mar 23, 2019
😄
mikedn commentedon Mar 23, 2019
@Zhentar Can you post the generated assembly code? I'm pretty sure the moves you're seeing are zero extending casts too but I can't see how it all fits together and tell if there's any simple solution for this. And please do yourself and everyone else a favour and do not use
varin that kind of code 😛As for the JIT, he definitely liked to mov it, mov it but a recent change calmed it down a bit. However, it's still a greedy bastard and wants a paycheck with many zeroes so it keeps zeroing in the hope it will get it.
Zhentar commentedon Mar 23, 2019
Sure, here:
mikedn commentedon Mar 23, 2019
So,
mov r11d, r11d,mov esi, esi,mov r9d, r9dare definitelyuint->ulongcasts. Themov r9d, r10dbefore the shift is likely too a cast (it may be a real move if r10d is subsequently used). They are unnecessary because previous operations have already zero out the upper 32 bits of those registers but the JIT doesn't know it. I think at least some of these cases could be fixed but I'm not sure yet. And I'm not sure it's strictly necessary, at least for this case.I'm more worried about stack spills at
[rsp+68h]and[rsp+60h]. Looks like bad struct handling, unfortunately the JIT has lots of it.However, it appears that you already changed accumulate to
and got rid of the structs. I'd go one step further and "hoist" the casts:
With this I'm seeing assembly code like
which should be better.
Structs are still causing problems, a bunch of pointless
leas are generated. Need to take a closer look, I have a bone to pick with the JIT about theseleas.Zhentar commentedon Mar 23, 2019
Woah! That slashed execution time for me by 20%!
3 remaining items
AndyAyersMS commentedon May 27, 2020
Most likely the root cause is residual "address exposure" that is blocking promotion of some of the intermediate temp structs. Will update once I've had a chance to confirm/refute.
weltkante commentedon Jun 30, 2020
@AndyAyersMS got a chance to look at it? Otherwise your guess sounds like it has a separate root cause and probably should get its own issue?
AndyAyersMS commentedon Jun 30, 2020
@weltkante Thanks for the reminder.
I haven't looked yet. Let me see if can find time in the next few days.
benaadams commentedon Apr 3, 2021
Eugh https://twitter.com/uops_info/status/1367961302845386752
ICL065 erratum says:
TIHan commentedon Nov 29, 2022
Will look into this next as I've been doing work to remove unnecessary
movsx/movzxinstructions lately.TIHan commentedon Dec 7, 2022
The emitter already has a mechanism for eliminating unnecessary
movinstructions.Its current implementation only looks at the last emitted instruction to see if its destination register is zero-extended 32-bits.
I did an experiment by allowing to look up to 4 previously emitted instructions to determine if the given register is zero-extended 32-bits. This eliminated the
movinstructions that were mentioned in this issue's description.I don't think it's trivial to try to resolve this issue in the earlier phases. In morph, it would either require removing
CASTnodes or adding a new flag to force them to be a no-op. RemovingCASTnodes in morph is troublesome and can lead to bad HIR. Adding a flag adds more complexity. Lowering could be possible, but we need a way to track the upper bits.As of now, I think simply extending the existing logic in
AreUpper32BitsZerowould be the way to go.