Skip to content

System.Numerics.Vector: Recognize division by a constant and inline #43761

Closed
@nietras

Description

@nietras

Sharplab

https://sharplab.io/#v2:EYLgxg9gTgpgtADwGwBYA0AXEBDAzgWwB8ABABgAJiBGAOgDkBXfGKASzFwG4BYAKD+IBmcgwB2ubADMYlAEzkAwnwDefcuvKRxGcq1E6AYtjAZo5ALzlZpFAA5O5APSOr5AHrkqVNRq24dejoAygAWrJI6ll48vBrksNgAJhCiADYAnuQAajAm0AA8gQB82bmmUEZ5UBbkojAA7qVVhfpFABSV5QCUMXE+6kJN5S0YJQAirABurIkwbTnNxeSTXRYlk05D0J3QvRp8AL5AA

Given

using System.Numerics;

public unsafe class C
{
    const int Factor = 2048; // 2 ^ 11
    const int Shift = 11;
    readonly Vector<int> VectorFactor = new Vector<int>(Factor);
    
    public Vector<int> Divide(Vector<int> v) => v / VectorFactor;   
}

Output

The assembly output for x64 is:

; Core CLR v4.700.20.41105 on amd64

C..ctor()
    L0000: vzeroupper
    L0003: mov eax, 0x800
    L0008: vmovd xmm0, eax
    L000c: vpbroadcastd ymm0, xmm0
    L0011: vmovupd [rcx+8], ymm0
    L0016: vzeroupper
    L0019: ret

C.Divide(System.Numerics.Vector`1<Int32>)
    L0000: push rsi
    L0001: sub rsp, 0x50
    L0005: vzeroupper
    L0008: mov rsi, rdx
    L000b: mov rdx, rsi
    L000e: vmovupd ymm0, [rcx+8]
    L0013: vmovupd [rsp+0x20], ymm0
    L0019: mov rcx, rdx
    L001c: mov rdx, r8
    L001f: lea r8, [rsp+0x20]
    L0024: call System.Numerics.Vector`1[[System.Int32, System.Private.CoreLib]].op_Division(System.Numerics.Vector`1<Int32>, System.Numerics.Vector`1<Int32>)
    L0029: mov rax, rsi
    L002c: vzeroupper
    L002f: add rsp, 0x50
    L0033: pop rsi
    L0034: ret

Expected

The division is inlined and division by constant is recognized and "optimal" SIMD is emitted e.g. with shifts or similar.

cc: @tannergooding

category:cq
theme:inlining
skill-level:intermediate
cost:medium

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMItenet-performancePerformance related issue

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions