Skip to content

Unoptimal codegen for Vector<T> init if Vector is not a localVar. #33617

@sandreenko

Description

@sandreenko

For a method like this:

    struct StructWithVectorField
    {
        public int a;
        public Vector<float> b;
    }

    [MethodImpl(MethodImplOptions.NoInlining)]
    static void TestSIMDInit()
    {
        Vector<float> localVector = new Vector<float>();
        Console.WriteLine(localVector.ToString());
        StructWithVectorField structWithVectorField;
        structWithVectorField.a = 0;
        structWithVectorField.b = new Vector<float>();
        Console.WriteLine(structWithVectorField.b.ToString());
        Console.WriteLine(Vector<float>.Count);
    }

IL for these two Vector inits will look similar:

Vector<float> localVector = new Vector<float>();
->
IL_0000  12 00             ldloca.s     0x0
IL_0002  fe 15 01 00 00 1b initobj      0x1B000001 
 structWithVectorField.b = new Vector<float>();
->
IL_0024  7c 14 00 00 04    ldflda       0x4000014
IL_0029  fe 15 01 00 00 1b initobj      0x1B000001

but the generated code will be different:

1.
IN0001:        vxorps   ymm0, ymm0
IN0002:        vmovupd  ymmword ptr[V00 rsp+50H], ymm0
2.
IN0009:        vxorps   xmm0, xmm0
IN000a:        vmovdqu  xmmword ptr [V01+0x8 rsp+30H], xmm0
IN000b:        vmovdqu  xmmword ptr [V01+0x18 rsp+40H], xmm0

Of course, the first version is better (10 bytes against 16).

The problem happens because fgMorphOneAsgBlockOp doesn't do that optimization for LCL_FLD nodes, only for LCL_VAR.

fieldUnoptimalVectorT.txt

That will be probably fixed during #1231.

Note: that is why we need to pass to isBlkReqd == true to fgMorphBlockOperand for gtType != TYP_STRUCT.

The fix will cause diffs in ~10 pri1 tests like:

System.Linq.Set`1[Vector`1][System.Numerics.Vector`1[System.Single]]:Remove(System.Numerics.Vector`1[Single]):bool:this
System.Security.Principal.WindowsIdentity:RunImpersonated(Microsoft.Win32.SafeHandles.SafeAccessTokenHandle,System.Func`1[Vector`1]):System.Numerics.Vector`1[Single] (MethodHash=ffeb0926)
System.Threading.Tasks.RendezvousAwaitable`1[Vector`1][System.Numerics.Vector`1[System.Single]]:GetResult():System.Numerics.Vector`1[Single]:this (MethodHash=9b817fd3)

etc.

category:cq
theme:vector-codegen
skill-level:intermediate
cost:medium

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIenhancementProduct code improvement that does NOT require public API changes/additions

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions