RyuJIT: suboptimal code after inlining a method accessing a field via ref
There is a small difference between code generated for direct reading of a volatile variable and doing it via Volatile.Read.
volatile int sharedVar;
public int Test()
{
return sharedVar;
}
public int Test2()
{
return Volatile.Read(ref sharedVar);
}
mov eax, dword ptr [rcx+8]
ret
;vs
cmp dword ptr [rcx], ecx
lea rax, bword ptr [rcx+8]
mov eax, dword ptr [rax]
ret
category:cq theme:basic-cq skill-level:expert cost:large
Ah, those lea instructions, they're like cockroaches, as soon as you get rid of one another one pops up in another place.
Note that this has nothing to do with volatile, the following produces the same assembly code:
using System.Runtime.CompilerServices;
class Program {
int sharedVar;
public int Read(ref int x) {
return x;
}
[MethodImpl(MethodImplOptions.NoInlining)]
public int Test() {
return Read(ref sharedVar);
}
static int Main() {
return new Program().Test();
}
}
Note that this has nothing to do with volatile
I hoped that as Volatile.Read suffered from the issue it would get more attention :wink:
@mikedn What's so bad about lea, aside from the horrendously unintuitive name?
@masonwheeler There's nothing wrong with the instruction itself. The problem is that the x86 JIT has a propensity for using them in places where they're not needed. I reported this kind of stuff on Connect in the past and supposedly the issues were fixed but every time I look at a new version they just pop somewhere else. And RyuJIT inherited this from the x86 JIT.
That sequence is pretty bad in all dimensions (size, speed, registers). We need forward-propagation of addressing-expressions followed by null-check folding. This should be doable.
Ah, those lea instructions, they're like cockroaches, as soon as you get rid of one another one pops up in another place.
@mikedn, Reading this: http://stackoverflow.com/a/11041820 after your comment, now lea instruction seems to have unique significance... and like @leppie, i am confused too! :smile: (not sure if this behavior/issue exists in CoreCLR as well?)
@jasonwilliams200OK That's a different use of lea and it's completely unrelated to this issue. Besides, that SO thread looks like it was hit by the truck of fail with an answer post that doesn't really answer anything and a most voted post that concludes that lea ecx, [ecx+ecx*4] is somehow slower than the equivalent imul without even bothering to check the latency of those instructions. And the only post that actually says something useful gets only one vote. Awesome.
@mikedn imul is 3 cycles and lea is 1, correct? The conclusion of the highest voted answer seems correct. Also, he tested that theory by plugging in different constants.
@GSPP Yes, on Intels imul needs 3 cycles and that particular form of lea needs only 1 (there are forms that need 3 cycles as well). His conclusion appears to be that using lea like that is a pessimization and that's pretty much impossible. He claims that changing the constant affects performance but he doesn't provide any actual results and I cannot reproduce that. Additionally, RyuJIT doesn't use lea in that case but the performance issue described in the question persists.
@mikedn I see what you mean. I read the answer the other way around.
@erozenfeld
Is it still for the Future?
@omariom I have some changes that eliminate the null check in your example. Do you have a real scenario where this fix would speed up some product? With my prototype a number of null checks were eliminated but none of the benchmarks we have got faster (the code size did get smaller of course). If you have such a real example (not a benchmark specifically tailored for this), that would help us prioritize getting this optimization checked in.
@erozenfeld No, I don't have such an example. And I understand there are more important things to do in JIT. Just thought after 1.0 released it would get a more specific milestone )
dotnet/coreclr#6631 eliminated the null check in this issue's test case. The lea is still there so I'm keeping the issue open.
@erozenfeld I think it can be closed now.