-
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
Odd side-effect check code generation with fixed keyword #10325
Comments
Right, The BTW - the disassembly you get by using G_M55886_IG01:
4883EC28 sub rsp, 40
33C0 xor rax, rax
4889442420 mov qword ptr [rsp+20H], rax
G_M55886_IG02:
33C0 xor eax, eax
48894C2420 mov gword ptr [rsp+20H], rcx
4885C9 test rcx, rcx
740B je SHORT G_M55886_IG03
488B542420 mov rdx, gword ptr [rsp+20H]
837A0800 cmp dword ptr [rdx+8], 0
7504 jne SHORT G_M55886_IG04
G_M55886_IG03:
33D2 xor rdx, rdx
EB14 jmp SHORT G_M55886_IG05
G_M55886_IG04:
488B542420 mov rdx, gword ptr [rsp+20H]
837A0800 cmp dword ptr [rdx+8], 0
7625 jbe SHORT G_M55886_IG09
488B542420 mov rdx, gword ptr [rsp+20H]
4883C210 add rdx, 16
G_M55886_IG05:
33C9 xor ecx, ecx
G_M55886_IG06:
4C63C1 movsxd r8, ecx
42030482 add eax, dword ptr [rdx+4*r8]
FFC1 inc ecx
83F904 cmp ecx, 4
7CF2 jl SHORT G_M55886_IG06
G_M55886_IG07:
33D2 xor rdx, rdx
4889542420 mov gword ptr [rsp+20H], rdx
G_M55886_IG08:
4883C428 add rsp, 40
C3 ret
G_M55886_IG09:
E805C4385F call CORINFO_HELP_RNGCHKFAIL
CC int3 |
Sorry, I was fixing my coreclr repository(its emitting unrolled loop so), so I couldn't use for it. I will dump it with |
I will figure it out after fixing basic loop unrolling issues. |
@mikedn Wait. there is another issues here.
pointers cannot be below. why isn't this code use test instead of cmp? |
|
How about this?
rax will be reinitialized here. |
That's a side effect of using
That's also why It may be possible to improve this but it's not very clear how. I suppose the register allocator might be able to figure out that |
figuring out either after fixup loop-unrolling issues. |
This reminds me that today there are better ways to avoid range checks than int s = 0;
ref int p = ref a[0];
for (int i = 0; i < a.Length; i++)
s += Unsafe.Add(ref p, i);
return s; This generates: G_M55887_IG01:
4883EC28 sub rsp, 40
G_M55887_IG02:
33C0 xor eax, eax
8B5108 mov edx, dword ptr [rcx+8]
83FA00 cmp edx, 0
761F jbe SHORT G_M55887_IG05
4883C110 add rcx, 16
4533C0 xor r8d, r8d
85D2 test edx, edx
7E0F jle SHORT G_M55887_IG04
G_M55887_IG03:
4D63C8 movsxd r9, r8d
42030489 add eax, dword ptr [rcx+4*r9]
41FFC0 inc r8d
413BD0 cmp edx, r8d
7FF1 jg SHORT G_M55887_IG03
G_M55887_IG04:
4883C428 add rsp, 40
C3 ret
G_M55887_IG05:
E8EE462E5F call CORINFO_HELP_RNGCHKFAIL
CC int3 |
Seems not really simple, friendly code for programmers. not for front-line programmers. the goal should be more simpler for front-line programmers. this is my opinion for example, if there is such soo many arithmetical process using Unsafe.*** on code. |
And also. this is not only for this problem.
this doesn't uses And here does correctly.
|
Certainly, but the whole point of this was to give you something that's more easy to unroll. Attempting to unroll a similar loop but with range checks is problematic due the increase in code size and number of branches. At the end of the day the difficulty is not to unroll the loop but to decide when it's beneficial to do so. And array range checks complicate that. |
Yeah, I know. I've made a fix for this a while ago but didn't bother pushing it.
I'm not sure what you're trying to say here. This is an array length, not a pointer. |
There is something wired side-effect check code generation on fixed pointers.
I don't know its because Roslyn generates wrong MSIL. but first. I am writing here.
Example here.
This code generates some odd check codes. here is disassembles.
As you can see. its double checking object.
But this code. generates as I excepted.
category:cq
theme:basic-cq
skill-level:intermediate
cost:small
impact:medium
The text was updated successfully, but these errors were encountered: