-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ordering issues with DYN_BLK
nodes
#62014
Comments
Tagging subscribers to this area: @JulieLeeMSFT Issue DetailsThe Dynamic blocks are imported for two instructions:
Yet, we import the following code: public static void CpBlk(void* dst, void* src, uint count)
{
IL.Emit.Ldarg(0);
IL.Emit.Ldarg(1);
IL.Emit.Ldarg(2);
IL.Emit.Cpblk();
} As the following tree: ***** BB01
STMT00000 ( 0x000[E-] ... 0x004 )
[000005] -A-XG------- * ASG struct (copy)
[000003] ---XG--N---- +--* DYN_BLK struct
[000000] ------------ | +--* LCL_VAR long V00 arg0 // dst
[000002] ------------ | \--* LCL_VAR int V02 arg2 // size
[000004] -------N---- \--* IND struct
[000001] ------------ \--* LCL_VAR long V01 arg1 // src The order of evaluation for This does not matter for locals of course, but let's see what happens if we change our example to something like: public static void CpBlkSimpleSideEffects(void* dst, void** pSrc, long count)
{
IL.Emit.Ldarg(0);
IL.Emit.Ldarg(1);
IL.Emit.Ldind_I();
IL.Emit.Ldarg(2);
IL.Emit.Conv_Ovf_U4();
IL.Emit.Cpblk();
} Actually - this sample ends up generating correct code! And the reason for this is the fact that runtime/src/coreclr/jit/gentree.cpp Lines 4350 to 4355 in 90773ac
So we end up with this threaded code, which has correct evaluation order: N008 ( 16, 15) [000007] -A-XG---R--- * ASG struct (copy)
N007 ( 9, 10) [000005] D--XG--N---- +--* DYN_BLK struct
N004 ( 1, 1) [000000] ------------ | +--* LCL_VAR long V00 arg0 u:1 (last use)
N006 ( 8, 9) [000004] ---X-------- | \--* CAST_ovfl int <- uint <- long
N005 ( 1, 1) [000003] ------------ | \--* LCL_VAR long V02 arg2 u:1 (last use)
N003 ( 6, 4) [000006] ---XG--N---- \--* IND struct
N002 ( 3, 2) [000002] *--XG------- \--* IND long
N001 ( 1, 1) [000001] ------------ \--* LCL_VAR long V01 arg1 u:1 (last use) Of course, things don't work out quite so well if we have a more complex expression for the destination address: public static void CpBlkSideEffects(void** dst, void*[] src, long count)
{
IL.Emit.Ldarg(0);
IL.Emit.Ldind_I();
IL.Emit.Ldarg(1);
IL.Emit.Ldc_I4(0);
IL.Emit.Ldelem_I();
IL.Emit.Ldarg(2);
IL.Emit.Conv_Ovf_U4();
IL.Emit.Cpblk();
} And this program happily throws int dstSrc = 0;
int* dst = &dstSrc;
var src = new void*[0];
CpBlkSideEffects((void**)&dst, src, long.MaxValue); Fortunately:
So we're in no rush to fix it (which, I suspect, would involve getting rid of
|
CC @dotnet/jit-contrib |
The
ASG(DYN_BLK, IND)
representation makes it (almost) impossible, by design, to maintain the proper ordering.Dynamic blocks are imported for two instructions:
cpblk
andinitblk
. It is the first one that has issues. The order in which the operands are pushed onto the stack (and thus the proper evaluation order for the resulting tree) is as follows:Yet, we import the following code:
As the following tree:
The order of evaluation for
size
andsrc
has been swapped.This does not matter for locals of course, but let's see what happens if we change our example to something like:
Actually - this sample ends up generating correct code! And the reason for this is the fact that
gtSetEvalOrder
swaps the assignment order, which it always does for LHS indirections with addresses represented by locals:runtime/src/coreclr/jit/gentree.cpp
Lines 4350 to 4355 in 90773ac
So we end up with this threaded code, which has correct evaluation order:
Of course, things don't work out quite so well if we have a more complex expression for the destination address:
And this program happily throws
OverflowException
, while it should have thrownIndexOutOfRangeException
:Fortunately:
Unsafe.CopyBlock
.cpblk
.So we're in no rush to fix it (which, I suspect, would involve getting rid of
DYN_BLK
and importingSTORE_DYN_BLK
directly).The text was updated successfully, but these errors were encountered: