Skip to content

Sub-optimal code generated for Unsafe.As #55064

@hez2010

Description

@hez2010

Description

using System.Runtime.CompilerServices;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
byte Create<T>(T value)
{
    return Unsafe.As<T, byte>(ref value);
}

Console.WriteLine(Create(500));
Console.WriteLine(Create(500L));

JIT dump:

mov     dword ptr [rsp+0x44], 0x1f4
movzx   ecx, byte ptr [rsp+0x44]
call    System.Console.WriteLine(Int32)
mov     qword ptr [rsp+0x38], 0x1f4
movzx   ecx, byte ptr [rsp+0x38]
call    System.Console.WriteLine(Int32)

I think the ref can be omitted so that value won't be spilled to stack,
which is expected to be (on a little endian system):

mov	     ecx, 0xf4
call    System.Console.WriteLine(Int32)
mov     ecx, 0xf4
call    System.Console.WriteLine(Int32)

Configuration

Version: .NET 6 preview 5
Tiered Compilation: off

category:cq
theme:codegen
skill-level:intermediate
cost:small
impact:medium

Metadata

Metadata

Assignees

Labels

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

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions