Skip to content

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

Closed

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

Activity

  1. added
    enhancement
    area-CodeGen-coreclr
    on Mar 16, 2020
  2. added
    untriaged
    on Mar 16, 2020
  3. sandreenko commented on Mar 16, 2020

    @sandreenko
    ContributorAuthor

    cc @CarolEidt That is part of what we have discussed last week, I am still preparing the main issue description.

  4. added this to the 5.0 milestone on Apr 4, 2020
  5. removed
    untriaged
    on Apr 4, 2020
  6. AndyAyersMS commented on Apr 30, 2020

    @AndyAyersMS
    Member

    @sandreenko is there work here beyond fixing #1231?

  7. sandreenko commented on Apr 30, 2020

    @sandreenko
    ContributorAuthor

    It is not part #1231, but after #1231 we will have more LCL_VAR and less LCL_FLD so the number of cases where we see this issue will be smaller.

    But the real fix for that was in #33665, but I got a push back and was requested to do work that I do not have time to do currently.

  8. AndyAyersMS commented on May 1, 2020

    @AndyAyersMS
    Member

    Ok, let's mark this as future.

  9. modified the milestones: 5.0, Future on May 1, 2020
  10. added
    JitUntriaged
    on Oct 28, 2020
  11. removed
    JitUntriaged
    on Jan 4, 2021
  12. sandreenko commented on Sep 18, 2021

    @sandreenko
    ContributorAuthor

    Fixed by #55604

  13. ghost locked as resolved and limited conversation to collaborators on Nov 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclrenhancement

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions