Description
E.g.,
static ref object GetRef1(object obj)
{
object[] arr = [obj];
return ref arr[0];
}
static ref object GetRef2(object obj)
{
object[] arr = [obj];
return ref MemoryMarshal.GetArrayDataReference(arr);
}
These should be able to end up with the same codegen, but the first still generates the ldelema helper call, despite knowing that the array is exactly an object[] (not a string[] or similar).
Similarly, it could apply to static readonly fields once the static constructor has run.
Godbolt.org: https://godbolt.org/z/YGEh3zPMv
Codegen:
Program:GetRef1(System.Object):byref (FullOpts):
push r15
push rbx
push rax
mov rbx, rdi
lea rdi, [(reloc 0x432ec0)] ; System.Object[]
mov esi, 1
call CORINFO_HELP_NEWARR_1_PTR
mov r15, rax
lea rdi, bword ptr [r15+0x10]
mov rsi, rbx
call CORINFO_HELP_ASSIGN_REF
mov rdi, r15
xor esi, esi
lea rdx, [(reloc 0x432fe8)] ; System.Object
call CORINFO_HELP_LDELEMA_REF
nop
add rsp, 8
pop rbx
pop r15
ret
Program:GetRef2(System.Object):byref (FullOpts):
push r15
push rbx
push rax
mov rbx, rdi
lea rdi, [(reloc 0x432ec0)] ; System.Object[]
mov esi, 1
call CORINFO_HELP_NEWARR_1_PTR
mov r15, rax
lea rdi, bword ptr [r15+0x10]
mov rsi, rbx
call CORINFO_HELP_ASSIGN_REF
lea rax, bword ptr [r15+0x10]
add rsp, 8
pop rbx
pop r15
ret
Regression?
No.
Analysis
Seems like #85256 handled it for sealed types in any scenario, but we can also handle it for when we know the type of the array object exactly.
May also be applicable for stelem.ref.
Description
E.g.,
These should be able to end up with the same codegen, but the first still generates the ldelema helper call, despite knowing that the array is exactly an
object[](not astring[]or similar).Similarly, it could apply to
static readonlyfields once the static constructor has run.Godbolt.org: https://godbolt.org/z/YGEh3zPMv
Codegen:
Regression?
No.
Analysis
Seems like #85256 handled it for sealed types in any scenario, but we can also handle it for when we know the type of the array object exactly.
May also be applicable for
stelem.ref.