Skip to content

Poor codegen for Vector128.Create() with constants sharing the same value #63432

Closed

Description

@dubiousconst282

Description

Calling Vector128.Create(float, float, float, float) with constants that share the same value results in poor codegen, as demonstrated below.

Sharplab

using System;
using System.Runtime.Intrinsics;

public class C {
    static Vector128<float> M1() {
        return Vector128.Create(1f,2f,4f,4f);
    }
    static Vector128<float> M2() {
        return Vector128.Create(1f,2f,3f,4f);
    }
}

Output assembly (CoreCLR 6.0.21.52210 on amd64)

C.M1()
    L0000: vzeroupper
    L0003: vmovss xmm0, [0x7ffb22bb0480]
    L000b: vmovss xmm1, [0x7ffb22bb0484]
    L0013: vinsertps xmm0, xmm0, xmm1, 0x10
    L0019: vmovss xmm1, [0x7ffb22bb0488]
    L0021: vmovaps xmm2, xmm1
    L0025: vinsertps xmm0, xmm0, xmm2, 0x20
    L002b: vinsertps xmm0, xmm0, xmm1, 0x30
    L0031: vmovupd [rcx], xmm0
    L0035: mov rax, rcx
    L0038: ret

C.M2()
    L0000: vzeroupper
    L0003: vmovupd xmm0, [0x7ffb22bb04c0]
    L000b: vmovupd [rcx], xmm0
    L000f: mov rax, rcx
    L0012: ret

This seems to affect all other VectorXXX.Create() functions, but only for float and double.

Activity

  1. added
    tenet-performance
    on Jan 6, 2022
  2. ghost added
    area-CodeGen-coreclr
    on Jan 6, 2022
  3. ghost added
    untriaged
    on Jan 6, 2022
  4. EgorBo commented on Jan 6, 2022

    @EgorBo
    Member

    Nice find!
    It seems in case if a constant feeds Vector.Create it has to be marked as DONT_CSE (except the cases where we mix variables and constants) or use VNs when we put vector to data section, but I think DONT_CSE is a better choice.

  5. ghost added
    in-pr
    on Jan 6, 2022
  6. modified the milestones: 6.0.x, 7.0.0 on Jan 6, 2022
  7. removed
    untriaged
    on Jan 11, 2022
  8. ghost removed
    in-pr
    on Jan 15, 2022
  9. ghost locked as resolved and limited conversation to collaborators on Feb 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-CodeGen-coreclrtenet-performance

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions