Skip to content

JIT: Suboptimal inlined unsafe method #11531

@JeremyKuhne

Description

@JeremyKuhne

From discussion here:

dotnet/corefxlab#2595 (review)

        public unsafe bool TryGetValue<T>(out T value) where T : unmanaged
        {
            value = default;
            bool success = false;

            if((typeof(T) == typeof(bool) && Type == VariantType.Boolean)
                // ... elided for brevity
                || (typeof(T) == typeof(uint) && Type == VariantType.UInt32)
                || (typeof(T) == typeof(ulong) && Type == VariantType.UInt64))
            {
                fixed (void* u = &_union)
                {
                    value = *(T*)u;
                }

                // value = CastTo<T>();
                success = true;
            }

            return success;
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        private unsafe T CastTo<T>() where T : unmanaged
        {
            fixed (void* u = &_union)
            {
                return *(T*)u;
            }
        }

This generates the following:

00007ffa`ae692da0 System.Variant.TryGetValue[[System.Int32, System.Private.CoreLib]](Int32 ByRef)
00007ffa`ae692da7 33c0            xor     eax,eax
00007ffa`ae692da9 8902            mov     dword ptr [rdx],eax
00007ffa`ae692dab 83790807        cmp     dword ptr [rcx+8],7
00007ffa`ae692daf 751d            jne     00007ffa`ae692dce
00007ffa`ae692db1 3909            cmp     dword ptr [rcx],ecx
00007ffa`ae692db3 4883c110        add     rcx,10h
00007ffa`ae692db7 48890c24        mov     qword ptr [rsp],rcx
00007ffa`ae692dbb 488b0424        mov     rax,qword ptr [rsp]
00007ffa`ae692dbf 8b00            mov     eax,dword ptr [rax]
00007ffa`ae692dc1 8902            mov     dword ptr [rdx],eax
00007ffa`ae692dc3 33c0            xor     eax,eax
00007ffa`ae692dc5 48890424        mov     qword ptr [rsp],rax
00007ffa`ae692dc9 b801000000      mov     eax,1
00007ffa`ae692dce 4883c408        add     rsp,8

If including the same fixed block through the inlined helper we end up with less optimal generated assembly:

00007ffa`ae673370 System.Variant.TryGetValue[[System.Int32, System.Private.CoreLib]](Int32 ByRef)
00007ffa`ae673377 33c0            xor     eax,eax
00007ffa`ae673379 8902            mov     dword ptr [rdx],eax
00007ffa`ae67337b 83790807        cmp     dword ptr [rcx+8],7
00007ffa`ae67337f 7523            jne     00007ffa`ae6733a4
00007ffa`ae673381 33c0            xor     eax,eax
00007ffa`ae673383 48890424        mov     qword ptr [rsp],rax
00007ffa`ae673387 3909            cmp     dword ptr [rcx],ecx
00007ffa`ae673389 4883c110        add     rcx,10h
00007ffa`ae67338d 48890c24        mov     qword ptr [rsp],rcx
00007ffa`ae673391 488b0424        mov     rax,qword ptr [rsp]
00007ffa`ae673395 8b00            mov     eax,dword ptr [rax]
00007ffa`ae673397 33c9            xor     ecx,ecx
00007ffa`ae673399 48890c24        mov     qword ptr [rsp],rcx
00007ffa`ae67339d 8902            mov     dword ptr [rdx],eax
00007ffa`ae67339f b801000000      mov     eax,1
00007ffa`ae6733a4 4883c408        add     rsp,8

category:cq
theme:inlining
skill-level:expert
cost:medium

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIoptimization

Type

No type
No fields configured for issues without a type.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions