Repository navigation
Description
For such a test case:
public sealed class TestClass<T>
{
private const int MaxBuffersPerArraySizePerCore = 8;
private T[] _arrays;
[MethodImpl(MethodImplOptions.NoInlining)]
public void TestNullRef()
{
_arrays = new T[GetSize()] ;
}
}
we will generate a tree like:
[000008] -ACXG+------ * ASG ref
[000007] ---XG+-N---- +--* IND ref
[000014] -----+------ | \--* ADD byref
[000000] -----+------ | +--* LCL_VAR ref V00 this
[000013] -----+------ | \--* CNS_INT long 8 field offset Fseq[_arrays]
[000006] --CXG+------ \--* CALL help ref HELPER.CORINFO_HELP_NEWARR_1_R2R_DIRECT
[000017] -ACXGO----L- arg0 SETUP +--* ASG long
[000016] D------N---- | +--* LCL_VAR long V02 tmp1
[000004] --CXG+------ | \--* CALL help r2r_ind long HELPER.CORINFO_HELP_READYTORUN_GENERIC_HANDLE
[000003] #----+------ arg0 in rcx | \--* IND long
[000002] -----+------ | \--* LCL_VAR ref V00 this
[000018] ------------ arg0 in rcx +--* LCL_VAR long V02 tmp1
[000001] -----+------ arg1 in rdx \--* CNS_INT long 8
where 000003 INDIR doesn't have an exception flag and because clever morph can see that we have already dereferenced it in 000007 INDIR:
Morphing args for 4.CALL:
Non-null prop for index dotnet/coreclr#1 in BB01:
[000003] #--X-------- * IND long
argSlots=1, preallocatedArgCount=4, nextSlotNum=4, outgoingArgSpaceSize=32
Sorting the arguments:
Deferred argument ('rcx'):
[000003] #----+------ * IND long
[000002] -----+------ \--* LCL_VAR ref V00 this
so far so good, but then fgSetBlockOrder could decide to revert 000008 and guess what now it can clear the exception flag from 000007 because now we execute it after 000003 that is known not to throw (because it got GTF_IND_NONFAULTING during the previous phase) so we end up proving that this indirection can't throw:
N015 ( 41, 22) [000008] -ACXGO--R--- * ASG ref $VN.Void
N014 ( 4, 4) [000007] n---GO-N---- +--* IND ref $280
N013 ( 2, 2) [000014] -------N---- | \--* ADD byref $2c0
N011 ( 1, 1) [000000] ------------ | +--* LCL_VAR ref V00 this u:1 $80
N012 ( 1, 1) [000013] ------------ | \--* CNS_INT long 8 field offset Fseq[_arrays] $200
N010 ( 36, 17) [000006] -ACXGO------ \--* CALL help ref HELPER.CORINFO_HELP_NEWARR_1_R2R_DIRECT $103
N006 ( 17, 8) [000017] -ACXGO--R-L- arg0 SETUP +--* ASG long $180
N005 ( 1, 1) [000016] D------N---- | +--* LCL_VAR long V02 tmp1 d:1 $c1
N004 ( 17, 8) [000004] --CXGO------ | \--* CALL help r2r_ind long HELPER.CORINFO_HELP_READYTORUN_GENERIC_HANDLE $180
N003 ( 3, 2) [000003] #----O------ arg0 in rcx | \--* IND long $100
N002 ( 1, 1) [000002] ------------ | \--* LCL_VAR ref V00 this u:1 $80
N008 ( 1, 1) [000018] ------------ arg0 in rcx +--* LCL_VAR long V02 tmp1 u:1 (last use) $c1
N009 ( 1, 1) [000001] ------------ arg1 in rdx \--* CNS_INT long 8 $200
when it is, of course, incorrect:
Update: that is correct in this case because this is never null (we check it before a call to an instance method).
TestClass obj = null;
obj.TestNullRef();
we set revert flag here:
https://github.com/dotnet/coreclr/blob/68043c6306a0449bb5ed10fbdea9f14dafc99df4/src/jit/gentree.cpp#L4004-L4023
under conditions that are not clear to me.
Summary: if we fix #13758 we will have GTF_ASG on 000006. Then 000008 won't be reverted (because of the strange condition that I showed earlier that checks GTF_ASG), so we won't incorrectly drop GTF_EXC from both indirections. However, it will lead to 0.07% code size regression in System.Private.Corelib and 0.31% on framework libraries and it is not a complete fix for the problem.
Link #10813 and dotnet/coreclr#19334.
category:correctness
theme:optimization
skill-level:intermediate
cost:medium
Activity
sandreenko commentedon Nov 8, 2019
@CarolEidt could you please take a look and advise?
I will try to create an IL repo to show bad execution tomorrow, but I am not sure how that can be fixed in general (except by forbidding revert or
Non-null propin morph) preferably without a regression that big.CarolEidt commentedon Nov 8, 2019
The code you show in
gtSetEvalOrderis dancing around the fact that the address of an assignment must be evaluated first if it has any side-effects, but note that the assignment itself doesn't happen until both operands are evaluated. So aGT_INDon the lhs of an assignment is really not evaluated until the assignment actually happens. IMO the right fix for this is to eliminateGT_ASGearly (ideally in the importer), using store variants such asGT_STORE_INDwhere the ordering of the actual indirection is clear, and then assertion prop would see the correct order of the indirections. But that's a big effort, and this should be fixed sooner than that.I find it quite unfortunate that we are doing order-based assertions prior to actually setting the evaluation order, which seems like it has significant potential for issues like this. A couple of things come to mind:
GTF_IND_NON_FAULTINGon[000003]should be fixed to not process theGTF_INDon the lhs of an assignment (i.e. setting the assertion for[000007]) until after all the operands, since it is never evaluated first. There's aGTF_IND_ASG_LHSflag that I presume would be set on such a node.GTF_IND_NON_FAULTINGon aGT_INDwe should never reorder it, but I don't believe that's a flag we propagate, so I don't think there's anything to prevent us reordering an operation that has the same indirection on both sides such that the one that's markedGTF_IND_NON_FAULTINGwill now actually be the one to fault.I know that @erozenfeld has spent time with the side-effect flags and their propagation, and he might have some insight into this.
mikedn commentedon Nov 8, 2019
Actually I hope I'll get to ASG elimination sooner rather than later. The more stuff like this I the less I believe that it can actually be fixed in a reliable manner.
But yes, in the meantime there are better ways to deal with ASG. I already started this with dotnet/coreclr#27445 and next on the list is liveness. Eventually all the JIT should follow the same approach.
Yes, but we'll need to set it in morph. It's currently set during SSA construction.
I need to take a closer look at this, it's not clear to me what stops this kind of reordering from occurring in other situations that do not involve ASG nodes. Anyone knows?
mikedn commentedon Nov 8, 2019
Ah, but assertion prop sets
GTF_IND_NONFAULTINGand then also setsGTF_ORDER_SIDEEFFso reordering should not occur. But I'm pretty sureGTF_ORDER_SIDEEFFis somehow broken, I remember noticing weird things in the code around it.mikedn commentedon Nov 8, 2019
As far as I can tell there are 2 different problems here:
gtSetEvalOrdermanages to setGTF_REVERSE_OPSonGT_ASGwithout checkinggtCanSwapOrderand thus ignoringGTF_ORDER_SIDEEFFGT_ASGnode and not at the point of its LHS. But for some odd reasons pretty much the entire frontend gets it backwards and considers the LHS to be the store point.I'm guessing that the first problem may be easy to fix. The second one requires a bit more work :)
CarolEidt commentedon Nov 8, 2019
@mikedn - thanks for the excellent summary! Agree on the observation that the second issue will likely require more work.
sandreenko commentedon Nov 8, 2019
Thank you both, @CarolEidt and @mikedn.
I am trying to do a better repro now. The one in the header doesn't end up in an incorrect execution because there are exception flags on other calls, so the statement can't be deleted and there are no other exceptions that can be reordered.
Then I will work on fixing
gtSetEvalOrderto respectgtCanSwapOrder.mikedn commentedon Nov 8, 2019
Looking a bit more at
gtSetEvalOrder:Hmm, dotnet/coreclr#27445 changed SSA so this is no longer necessary.
Though there are still cases where
GTF_REVERSE_OPSgets set and I'm not sure why.mikedn commentedon Nov 19, 2019
BTW, memory liveness is messed up because of the way this flag is set. Since first liveness run is done before SSA the flag is not set so this code:
https://github.com/dotnet/coreclr/blob/a9f3fc16483eecfc47fb79c362811d870be02249/src/jit/liveness.cpp#L265-L279
will make a use out of every indir, including ones that are LHS and thus only defs.
Also, rationalization clears
GTF_IND_ASG_LHSbecause it shares a bit withGTF_IND_REQ_ADDR_IN_REG. And then liveness runs after lowering and hits the same bad condition, except that this time the whole thing is pointless - memory liveness is not needed in LIR.So...
sandreenko commentedon Nov 19, 2019
I have failed to create a repro that hits that bug and leads to an incorrect execution. However, I have found that
gtCanSwapOrder(GenTree* firstNode, GenTree* secondNode)is also incorrect, because for a tree like:or
res = V01 * (V01 = 1, V01)in normal languagegtCanSwapOrder(V01, (V01 = 1, V01))will return true becauseV01has no side-effects.Lucky for us, most of the internal assignments that we have now could write only to temporary variables (TMP0NN) that are not used in other parts of a tree.
And importer tries to spill everything it can, so it hides these errors.
Note, that it won't be fixed by
ASGelimination.Also,
gtCanSwapOrderdoesn't checkGTF_ORDER_SIDEEFFon the second node, but it could be ok, I have not checked that yet.I think the solution to this that requires minimal changes could be to better describe these special internal assignments. Like prove and check that it is safe to do what we do now. Add checks that we don't have assignments that write to
V0NNlocals and then fixgtCanSwapOrder/gtSetEvalOrder.These special assignments won't need to set
ASGflag and we would be able to delete it.An obvious issue could be VN and CSE that could replace
TMP0NNwithV0NNetc.mikedn commentedon Nov 20, 2019
Yes, so how did you manage to produce that tree? :)
sandreenko commentedon Nov 21, 2019
I was not able to do that honestly.
I hacked an existing tree and changed
lclVarNumin morph just before callinggtCanSwapOrder.Before that, I also add a check that I ran when
gtCanSwapOrderreturned true. The check collected all assignments in the right subtree and all uses in the left subtree and checks that the intersection is empty. It has not failed in any pri1 tests that I was running.mikedn commentedon Nov 25, 2019
FWIW CSE can create trees where swapping would lead to a use before def case:
generates:
But CSE runs in linear order and at that point swapping no longer happens.
Comma trees produced by
fgMorphArrayIndexmay also have in-tree variable interference but they have all sorts of side effects on them that should prevent swapping.sandreenko commentedon Nov 25, 2019
@mikedn thanks for that example. It means our current contract is less obvious then I thought.
So:
Am I missing something?
4 remaining items
AndyAyersMS commentedon May 1, 2020
Seems like there's a potential for a bug here, but we haven't found an actual test case.
Moving to future.
jakobbotsch commentedon May 31, 2024
#13758 was fixed by #97409, and if I'm reading the comments above correctly that should have fixed this problem. So I'm going to close this.