Improve codegen for Unsafe<> same size casts. #34156
Labels
area-CodeGen-coreclr
CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI
enhancement
Product code improvement that does NOT require public API changes/additions
Milestone
For C# code
int r = Unsafe.As<float, int>(ref l);
we generate:for
float r = Unsafe.As<int, float>(ref l);
we generate:the optimal codegen should be one mov instead of two, like
movq xmm0, rcx
in case ofUnsafe.As<double, long>(ref l);
The unoptimal code happens because we retype access to LCL_VAR as LCL_FIELD and it forbids LCL_VAR to be enregistered:
and we don't have a better representation of non-widening type changes that do not require LCL_VAR to be address exposed or non-enregistarable.
It will be a bigger problem when #33225 is merged because we will do the same for
Unsafe.As<long, 8 byte struct>
, because before for a method likeSystem.Runtime.Intrinsics.Vector64:Create(ubyte):System.Runtime.Intrinsics.Vector64
1[Byte] ` we had:and we were lucky because this did not forbid any optimization on the result LCL_VAR.
The number of affected methods is small (mostly Unsafe casts and constructors), but these methods are used where performance is important.
I have discussed with @CarolEidt a possible fix via a new node (call it like
OBJ_BITCAST
) that appears in morph phase, has object layout field and changes into a normal bitcast during lowering.Looking for thoughts and other ideas, PTAL @dotnet/jit-contrib .
category:cq
theme:optimization
skill-level:intermediate
cost:medium
The text was updated successfully, but these errors were encountered: