Repository navigation
Closed
Labels
area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIoptimization
Milestone
Description
The JIT doesn't properly remove dead code in certain instances. Here's an example that does some method dispatching based on CPU features:
public class C
{
public static bool IsAvxSupported()
{
return Avx.IsSupported;
}
public static void A(int a, Span<byte> b)
{
if (IsAvxSupported())
FuncAvx(a, b);
else
FuncNoAvx(a, b);
}
public static void A2(int a, Span<byte> b)
{
if (Avx.IsSupported)
FuncAvx(a, b);
else
FuncNoAvx(a, b);
}
public static void A3(int a, Span<byte> b)
{
if (IsAvxSupported())
FuncAvx(a, b);
else
{
// Removed
}
}
[MethodImpl(MethodImplOptions.NoInlining)]
public static void FuncAvx(int a, Span<byte> b) { }
[MethodImpl(MethodImplOptions.NoInlining)]
public static void FuncNoAvx(int a, Span<byte> b) { }
}
A1 Moves some arguments around on the stack unnecessarily.
G_M13277_IG01:
sub rsp, 56
vzeroupper
xor rax, rax
mov qword ptr [rsp+28H], rax
G_M13277_IG02:
vmovdqu xmm0, qword ptr [rdx]
vmovdqu qword ptr [rsp+28H], xmm0
G_M13277_IG03:
lea rdx, bword ptr [rsp+28H]
call C:FuncAvx(int,struct)
nop
G_M13277_IG04:
add rsp, 56
ret
Manually inlining IsAvxSupported or removing the code from the false branch like in A2 and A3 make the JIT remove all those instructions, leaving just the call.
G_M62574_IG01:
sub rsp, 40
nop
G_M62574_IG02:
call C:FuncAvx(int,struct)
nop
G_M62574_IG03:
add rsp, 40
ret
category:cq
theme:optimization
skill-level:expert
cost:large
Metadata
Metadata
Assignees
Labels
area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIoptimization
Activity
AndyAyersMS commentedon Dec 11, 2019
This particular issue is both specific to this coding pattern and also somewhat general.
The general issue is that the jit is fairly slow at propagating information from callees to callers, so wrapping things like
Avx.IsSupportedin methods often leads to missed optimization opportunities like you see here. This slowness is fairly fundamental to the structure of the early part of the jit. Because it imports the entire caller before inlining the callee, there is no way callee information can influence caller importation.The specific issue is that because of this slowness, a copy optimization in morph does not fire when jitting
A. The optimization is dependent on early reference counts to determine if a struct reference is the "last use" of a struct, and is easily confused by "dead" references that ultimately will be optimized away. So it is a particularly vulnerable optimization as it is trying to do something flow-specific before the jit is fully able to understand flow.Morph actually folds the branch before it processes either call, but doesn't have the ability to determine that because of this fold, one of the calls is now unreachable, and because of that, that the early reference counts should be adjusted, and because of that, the copy optimization can safely fire on the surviving call. Instead it thinks both calls are live, and processes them both, introducing copies of the local
bas is generally required when passing a structure to a callee. Only later does the jit eliminate the dead code, and by that point there is nothing that can eliminate the now-unnecessary copy.We might be able to rely on block ref counts to eliminate unreachable blocks after inlining, and add logic to the postorder post-inline callback (currently
fgLateDevirtualization) to try and optimize branches if inline substitution happened in the tree. This would let the jit eliminate the dead code earlier and unblock the morph optimization in this case. Doing this might save a bit of throughput too, there's no point in morphing code that we are just going to delete once we understand which blocks are truly reachable.Probably worth prototyping, though early block ref counts might not be accurate enough to do this safely.
A related issue is that we end up being handicapped early in the jit by lack of full flow graph connectivity; I've been burned by this in other places too (eg dotnet/coreclr#27113).
cc @dotnet/jit-contrib
AndyAyersMS commentedon Dec 13, 2019
Results from a simple, crude prototype which gets the same codegen for
AandA2:Interestingly the biggest percentage wins come from removing useless pinvoke frame setup. See for example,
Environment:get_Is64BitOperatingSystem():boolThe prototype can only very crudely remove unreachable blocks, and it fails on one of the jit-diff assemblies, so the logic may still be amiss somewhere. A more functional version would have some more robust way to clean things up -- likely flagging methods where we suspect unreachable code during inlining, and then running some cleanup post pass on those flagged methods.
AndyAyersMS commentedon Dec 13, 2019
Also note
A2should really be able to tail callFuncAvx. This doesn't happen right now because we don't do the copy elimination opt until after we've decided the call can't be a fast tail call because it has a byref arg. But if that byref arg comes from a byref param for the method it should be OK to tail call, provided we actually do the copy optimization.A second simple, crude prototype produces this code for
A2And once we support caller-byref params for fast tail calls, we can broaden out the copy opt elimination further since we know all tail-call uses must be last uses.
For future reference, some other things to play with in that copy opt clause:
This prototype sees a fair number of hits....
AndyAyersMS commentedon Dec 13, 2019
The intersection of copy avoidance for last-use implicit by-ref structs at calls and tail calls is interesting. I'm not sure I completely understand it yet.
Imagine we have a chain of methods that tail call one another, A->B->C, and B and C have a by-ref struct params. A passes a local struct (by-ref) to B, and B passes it directly to C.
A cannot safely tail call B, because A's frame is trashed on entry to B. But A can avoid copying the struct at the call site if it is last use.
One might think B can safely tail call C only if it avoids making a local copy.
But dotnet/coreclr#5394 shows this isn't always true; a chain of slow tail calls via helpers can "reach back" to earlier frames and trash them as well. The fix in dotnet/coreclr#5394 is to have B copy the struct if it is going to slow tail call C. But this would seemingly make it unsafe for B to tail call C, unless the slow helper does something magical.
It's possible the proposed new portable fast tail call mechanism (#341) will have similar reach-back abilities. Need to look into this more closely.
jakobbotsch commentedon Dec 14, 2019
JIT_TailCallis invoked with all args to be passed and will set them up in the frame of the caller's caller with special copying. For args passed implicitly by ref, it will copy the contents, not just the reference:runtime/src/coreclr/src/vm/i386/stublinkerx86.cpp
Line 5672 in 6a620e6
This optimization should be ok for the new tail call helpers. From the JIT's perspective the helpers will not do anything special with the arguments. They will simply be copied into TLS using standard IL instructions.
AndyAyersMS commentedon Feb 1, 2020
Fixed by #1751.