Skip to content

CQ: Manually hoisted Vector{256,128} constant is reloaded inside the loop (sometimes) #76781

Open

Description

@gfoidl

Repro (.NET 7, applies to both Vector128 and Vector256):

static void Foo(ref byte src, nuint length, ref byte dest)
{
    nuint idx = 0;

    // Manually hoisted out of the loop
    Vector128<byte> mask = Vector128.Create((byte)0x7F);

    if (length >= (uint)Vector128<byte>.Count)
    {
        nuint vecEnd = length - (uint)Vector128<byte>.Count;

        do
        {
            Vector128<byte> vec = Vector128.LoadUnsafe(ref src, idx);
            vec &= mask;     // use of hoisted constant

            vec.StoreUnsafe(ref dest, idx);

            idx += (uint)Vector128<byte>.Count;
        } while (idx <= vecEnd);
    }

    // Remainder not relevant
}
; Assembly listing for method Program:<<Main>$>g__Foo|0_0(byref,long,byref)
; Emitting BLENDED_CODE for X64 CPU with AVX - Windows
; Tier-1 compilation
; optimized code
; rsp based frame
; fully interruptible
; No PGO data

G_M000_IG01:                ;; offset=0000H
       C5F877               vzeroupper

G_M000_IG02:                ;; offset=0003H
       33C0                 xor      eax, eax
       4883FA10             cmp      rdx, 16
       7223                 jb       SHORT G_M000_IG05

G_M000_IG03:                ;; offset=000BH
       4883C2F0             add      rdx, -16
       90                   align    [1 bytes for IG04]

G_M000_IG04:                ;; offset=0010H
       C5FA6F0401           vmovdqu  xmm0, xmmword ptr [rcx+rax]
       C5F9DB0513000000     vpand    xmm0, xmm0, xmmword ptr [reloc @RWD00]     ; should be kept hoisted out of the loop
       C4C17A7F0400         vmovdqu  xmmword ptr [r8+rax], xmm0
       4883C010             add      rax, 16
       483BC2               cmp      rax, rdx
       76E4                 jbe      SHORT G_M000_IG04

G_M000_IG05:                ;; offset=002CH
       C3                   ret

RWD00   dq      7F7F7F7F7F7F7F7Fh, 7F7F7F7F7F7F7F7Fh

; Total bytes of code 45

For this simple case the workaround is to just define the vector-constant inline, then the JIT will hoist the variable:

- vec &= mask;     // use of hoisted constant
+ vec &= Vector128.Create((byte)0x7F);    // here the JIT will hoist the constant
G_M000_IG03:                ;; offset=000BH
       4883C2F0             add      rdx, -16
       C4E179100528000000   vmovupd  xmm0, xmmword ptr [reloc @RWD00]	; hoisted by JIT
       0F1F840000000000     align    [8 bytes for IG04]

G_M000_IG04:                ;; offset=0020H
       C5F9DB0C01           vpand    xmm1, xmm0, xmmword ptr [rcx+rax]
       C4C17A7F0C00         vmovdqu  xmmword ptr [r8+rax], xmm1
       4883C010             add      rax, 16
       483BC2               cmp      rax, rdx
       76EC                 jbe      SHORT G_M000_IG04

Hower it's not always that easy. If the loop body is more complex (like e.g. having some method calls that will be inlined or for base64 encoding) then the constant vector probably isn't hoisted by the JIT. Hoisting or not seems to be a bit sensitive to actual code.

Of course one can check codegen to see what happens, but I'd like the JIT to keep manually hoisted constant outside the loop.
Porting code from say .NET 5 to .NET 7 has potential perf-traps, as constants that was kept hoisted in .NET 5 may get reloaded inside the loop with .NET 7. Even if this is known by inspecting the machine code, this would lead to #ifdefs to have optimal codegen for several targets.


I think it's the same codegen-issue if the lower lane of the Vector256 should be re-used as Vector128. The the Vector128 may be reloaded from memory instead of just using the xmm register of the ymm register.
E.g.

static void Foo(ref byte src, ref byte dest)
{
    Vector256<byte> mask256 = Vector256.Create((byte)0x7F);

    Vector256<byte> vec256 = Vector256.LoadUnsafe(ref src);
    vec256 &= mask256;
    vec256.StoreUnsafe(ref dest);

    Vector128<byte> mask128 = mask256.GetLower();    // reuse the already present vector instead of reloading from memory

    Vector128<byte> vec128 = Vector128.LoadUnsafe(ref src);
    vec128 &= mask128;
    vec128.StoreUnsafe(ref dest);
}
; Assembly listing for method Program:<<Main>$>g__Foo|0_0(byref,byref)
; Emitting BLENDED_CODE for X64 CPU with AVX - Windows
; Tier-1 compilation
; optimized code
; rsp based frame
; partially interruptible
; No PGO data

G_M000_IG01:                ;; offset=0000H
       C5F877               vzeroupper

G_M000_IG02:                ;; offset=0003H
       C5FE6F01             vmovdqu  ymm0, ymmword ptr[rcx]
       C5FDDB0531000000     vpand    ymm0, ymm0, ymmword ptr[reloc @RWD00]
       C5FE7F02             vmovdqu  ymmword ptr[rdx], ymm0
       C5FE6F0525000000     vmovdqu  ymm0, ymmword ptr[reloc @RWD00]    ; instead of reuse like written in C# it's reloaded
       C5F9DB01             vpand    xmm0, xmm0, xmmword ptr [rcx]
       C5FA7F02             vmovdqu  xmmword ptr [rdx], xmm0

G_M000_IG03:                ;; offset=0023H
       C5F877               vzeroupper
       C3                   ret

RWD00   dq      7F7F7F7F7F7F7F7Fh, 7F7F7F7F7F7F7F7Fh, 7F7F7F7F7F7F7F7Fh, 7F7F7F7F7F7F7F7Fh

; Total bytes of code 39

Here I'd expect code similar to

+      C5FE6F0528000000     vmovdqu  ymm1, ymmword ptr[reloc @RWD00]
G_M000_IG02:                ;; offset=0003H
       C5FE6F01             vmovdqu  ymm0, ymmword ptr[rcx]
-      C5FDDB0531000000     vpand    ymm0, ymm0, ymmword ptr[reloc @RWD00]
+      C5FDDB0531000000     vpand    ymm0, ymm0, ymm1
       C5FE7F02             vmovdqu  ymmword ptr[rdx], ymm0
-      C5FE6F0525000000     vmovdqu  ymm0, ymmword ptr[reloc @RWD00]
       C5F9DB01             vpand    xmm0, xmm1, xmmword ptr [rcx]
       C5FA7F02             vmovdqu  xmmword ptr [rdx], xmm0

Activity

  1. ghost added
    untriaged
    on Oct 8, 2022
  2. ghost added
    area-CodeGen-coreclr
    on Oct 8, 2022
  3. EgorBo commented on Oct 8, 2022

    @EgorBo
    Member

    looks like another form of #70355 I can take a look since I fixed it for obvious cases in #70378 but the heuristics need to be adjusted

  4. self-assigned this
    on Oct 8, 2022
  5. added this to the 8.0.0 milestone on Oct 8, 2022
  6. removed
    untriaged
    on Oct 8, 2022
  7. ghost added
    in-pr
    on Oct 9, 2022
  8. ghost removed
    in-pr
    on Oct 14, 2022
  9. ghost added
    in-pr
    on Nov 6, 2022
  10. ghost removed
    in-pr
    on Nov 8, 2022
  11. Daniel-Svensson commented on Mar 25, 2023

    @Daniel-Svensson
    Contributor

    An alternative workaround when doing manual hoisting that seems to work is to save the constant in a static readonly variable and load that one instead of the constant (but it has it's own downsides).

  12. modified the milestones: 8.0.0, Future on Apr 10, 2023
  13. saucecontrol commented on Oct 17, 2023

    @saucecontrol
    Member

    I've seen this come up a few more times, and it can have a measurable impact on perf beyond the obvious code size increase.

    Here's a sample benchmark that showed up as a regression between net6.0 and net8.0, with the main difference in codegen being constant vectors encoded as memory operands over and over.
    https://gist.github.com/saucecontrol/0dfaefc7c25d9078336871996025aeef

    Codegen - .NET 6.0.23 (6.0.2323.48002), X64 RyuJIT AVX2
    ; DiffBench.Score()
           sub       rsp,38
           vzeroupper
           vmovaps   [rsp+20],xmm6
           vmovaps   [rsp+10],xmm7
           vmovaps   [rsp],xmm8
           mov       rax,[rcx+8]
           mov       rdx,rax
           add       rax,40
           xor       ecx,ecx
           xor       r8d,r8d
           vmovupd   ymm0,[7FFC200FD800]
           vmovupd   ymm1,[7FFC200FD820]
           vmovupd   ymm2,[rdx]
           vpxor     ymm2,ymm2,[rax]
           add       rdx,20
           add       rax,20
           cmp       [rax],eax
    M00_L00:
           vmovaps   ymm3,ymm0
           vmovaps   ymm4,ymm1
           vmovupd   ymm5,[7FFC200FD840]
           vmovaps   ymm6,ymm2
           vmovupd   ymm7,[rdx]
           vpxor     ymm7,ymm7,[rax]
           vpand     ymm8,ymm6,ymm3
           vpsrld    ymm6,ymm6,1
           vpand     ymm6,ymm6,ymm3
           vpor      ymm6,ymm8,ymm6
           vpand     ymm8,ymm7,ymm3
           vpsrld    ymm7,ymm7,1
           vpand     ymm3,ymm7,ymm3
           vpor      ymm3,ymm8,ymm3
           vpand     ymm7,ymm6,ymm4
           vpsrld    ymm6,ymm6,4
           vpand     ymm6,ymm6,ymm4
           vpaddb    ymm6,ymm7,ymm6
           vpand     ymm7,ymm3,ymm4
           vpsrld    ymm3,ymm3,4
           vpand     ymm3,ymm3,ymm4
           vpaddb    ymm3,ymm7,ymm3
           vpshufb   ymm6,ymm5,ymm6
           vpshufb   ymm3,ymm5,ymm3
           vpaddb    ymm3,ymm6,ymm3
           vxorps    ymm4,ymm4,ymm4
           vpsadbw   ymm3,ymm3,ymm4
           vextracti128 xmm4,ymm3,1
           vpaddw    xmm3,xmm3,xmm4
           vpunpckhqdq xmm4,xmm3,xmm3
           vpaddq    xmm3,xmm3,xmm4
           vmovd     r9d,xmm3
           cmp       r9d,0DA
           jbe       short M00_L01
           add       r9d,0FFFFFF80
           imul      r9d,64
           shr       r9d,7
           jmp       short M00_L02
    M00_L01:
           xor       r9d,r9d
    M00_L02:
           add       ecx,r9d
           inc       r8d
           cmp       r8d,0A
           jl        near ptr M00_L00
           mov       eax,ecx
           vmovaps   xmm6,[rsp+20]
           vmovaps   xmm7,[rsp+10]
           vmovaps   xmm8,[rsp]
           vzeroupper
           add       rsp,38
           ret
    ; Total bytes of code 284
    Codegen - .NET 8.0.0 (8.0.23.47906), X64 RyuJIT AVX2
    ; DiffBench.Score()
           vzeroupper
           mov       rax,[rcx+8]
           mov       rcx,rax
           add       rax,40
           xor       edx,edx
           xor       r8d,r8d
           vmovups   ymm0,[7FFC29E88BE0]
           vmovups   ymm1,[7FFC29E88C00]
           vmovups   ymm2,[7FFC29E88C20]
           vmovups   ymm3,[rcx]
           vpxor     ymm3,ymm3,[rax]
           vmovups   ymm4,[rcx+20]
           vpxor     ymm4,ymm4,[rax+20]
    M00_L00:
           vmovaps   ymm5,ymm3
           vmovaps   ymm16,ymm4
           vpandd    ymm17,ymm5,[7FFC29E88BE0]
           vpsrld    ymm5,ymm5,1
           vpand     ymm5,ymm5,[7FFC29E88BE0]
           vpord     ymm5,ymm17,ymm5
           vpandd    ymm17,ymm16,[7FFC29E88BE0]
           vpsrld    ymm16,ymm16,1
           vpandd    ymm16,ymm16,[7FFC29E88BE0]
           vpord     ymm16,ymm17,ymm16
           vpandd    ymm17,ymm5,[7FFC29E88C00]
           vpsrld    ymm5,ymm5,4
           vpand     ymm5,ymm5,[7FFC29E88C00]
           vpaddb    ymm5,ymm17,ymm5
           vmovups   ymm17,[7FFC29E88C20]
           vpshufb   ymm5,ymm17,ymm5
           vpandd    ymm17,ymm16,[7FFC29E88C00]
           vpsrld    ymm16,ymm16,4
           vpandd    ymm16,ymm16,[7FFC29E88C00]
           vpaddb    ymm16,ymm17,ymm16
           vmovups   ymm17,[7FFC29E88C20]
           vpshufb   ymm16,ymm17,ymm16
           vpaddb    ymm5,ymm5,ymm16
           vxorps    ymm16,ymm16,ymm16
           vpsadbw   ymm5,ymm5,ymm16
           vmovaps   ymm16,ymm5
           vextracti128 xmm5,ymm5,1
           vpaddw    xmm5,xmm16,xmm5
           vpunpckhqdq xmm16,xmm5,xmm5
           vpaddq    xmm5,xmm5,xmm16
           vmovd     ecx,xmm5
           cmp       ecx,0DA
           ja        short M00_L02
           xor       r10d,r10d
    M00_L01:
           add       edx,r10d
           inc       r8d
           cmp       r8d,0A
           jl        near ptr M00_L00
           mov       eax,edx
           vzeroupper
           ret
    M00_L02:
           add       ecx,0FFFFFF80
           imul      r10d,ecx,64
           shr       r10d,7
           jmp       short M00_L01
    ; Total bytes of code 325

    And benchmark results

    BenchmarkDotNet v0.13.9+228a464e8be6c580ad9408e98f18813f6407fb5a, Windows 10 (10.0.19045.3448/22H2/2022Update)
    Intel Xeon W-11855M CPU 3.20GHz, 1 CPU, 12 logical and 6 physical cores
    .NET SDK 8.0.100-rc.2.23502.2
      [Host]     : .NET 6.0.23 (6.0.2323.48002), X64 RyuJIT AVX2
      Job-XEIFAO : .NET 6.0.23 (6.0.2323.48002), X64 RyuJIT AVX2
      Job-GIFGJZ : .NET 8.0.0 (8.0.23.47906), X64 RyuJIT AVX2
    
    
    | Method | Runtime  | Length  | Mean          | Error       | StdDev      | Ratio | RatioSD | Code Size |
    |------- |--------- |-------- |--------------:|------------:|------------:|------:|--------:|----------:|
    | Score  | .NET 6.0 | 256     |      6.534 ns |   0.0951 ns |   0.0889 ns |  1.00 |    0.00 |     302 B |
    | Score  | .NET 8.0 | 256     |      6.684 ns |   0.1505 ns |   0.1733 ns |  1.02 |    0.03 |     325 B |
    |        |          |         |               |             |             |       |         |           |
    | Score  | .NET 6.0 | 4096    |     92.877 ns |   1.5814 ns |   1.4019 ns |  1.00 |    0.00 |     302 B |
    | Score  | .NET 8.0 | 4096    |     95.968 ns |   1.9055 ns |   2.0388 ns |  1.03 |    0.03 |     325 B |
    |        |          |         |               |             |             |       |         |           |
    | Score  | .NET 6.0 | 65536   |  1,521.684 ns |  29.5860 ns |  30.3827 ns |  1.00 |    0.00 |     302 B |
    | Score  | .NET 8.0 | 65536   |  1,584.538 ns |  30.1567 ns |  30.9687 ns |  1.04 |    0.03 |     325 B |
    |        |          |         |               |             |             |       |         |           |
    | Score  | .NET 6.0 | 1048576 | 24,811.413 ns | 184.7721 ns | 172.8360 ns |  1.00 |    0.00 |     302 B |
    | Score  | .NET 8.0 | 1048576 | 25,656.797 ns | 149.0833 ns | 139.4526 ns |  1.03 |    0.01 |     325 B |
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclr

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions