Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Optimize SIMD codegen for (in)equality check against zero that produces bool result.#7367

Merged
sivarv merged 1 commit into
dotnet:masterfrom
sivarv:simdeq
Sep 27, 2016
Merged

Optimize SIMD codegen for (in)equality check against zero that produces bool result.#7367
sivarv merged 1 commit into
dotnet:masterfrom
sivarv:simdeq

Conversation

@sivarv
Copy link
Copy Markdown
Member

@sivarv sivarv commented Sep 27, 2016

As per Intel TechEmPower benchmark analysis, Kestrel.Internal.Infrastructure.MemoryPoolIterator.Seek() is one of the hot methods. This method uses equality against Vector.Zero

var data = new Vector<byte>(array, index);

// The below logic is repeated 3 times in the method
// against 3 different vectors.
var byte0Equals = Vector.Equals(data, byte0Vector);
if (!byte0Equals.Equals(Vector<byte>.Zero))
{
     byte0Index = FindFirstEqualByte(ref byte0Equals);
}

That is, SIMD API used is

bool Vector<T>.Equals(Vector<T> v)

Right now RyuJIT backend is generating the following

// Assume that byte0Equals in ymm0 reg
// Code generated for 
// byte0Equals.Equals(Vector<byte>.Zero)

vpxor ymm1, ymm1, ymm1
vmovaps ymm2, ymm0
vpcmpeqd ymm2, ymm2, ymm1
vextractf128 xmm3, ymm2, 0x1
vandps ymm2, ymm2, ymm3
vpshufd xmm3, xmm2, 0x4e
vandps ymm2, ymm2, ymm3
vpshufd xmm3, xmm2, 0x1
vpand ymm2, ymm2, ymm3
vmovd edi, xmm2
cmp edi, 0xffffffff
setz dil
movzx edi, dil

RyuJIT backend should be able to detect equality check against is Vector.Zero and generate the following optimal code

ptest ymm0, ymm0
setz dil
movzx edx, dil

Similarly for in-equality check against zero, we can generate

ptest ymm0, ymm0
setnz dil
movzx edx, dil

Fix #7358

@sivarv
Copy link
Copy Markdown
Member Author

sivarv commented Sep 27, 2016

@dotnet-bot test Windows_NT jitstressregs1
@dotnet-bot test Windows_NT jitstressregs2
@dotnet-bot test Windows_NT jitstressregs3
@dotnet-bot test Windows_NT jitstressregs4
@dotnet-bot test Windows_NT jitstressregs8
@dotnet-bot test Windows_NT jitstressregs0x10
@dotnet-bot test Windows_NT jitstressregs0x80
@dotnet-bot test Windows_NT jitstress2
@dotnet-bot test Windows_NT corefx_baseline
@dotnet-bot test Ubuntu jitstressregs1
@dotnet-bot test Ubuntu jitstressregs2
@dotnet-bot test Ubuntu jitstressregs3
@dotnet-bot test Ubuntu jitstressregs4
@dotnet-bot test Ubuntu jitstressregs8
@dotnet-bot test Ubuntu jitstressregs0x10
@dotnet-bot test Ubuntu jitstressregs0x80
@dotnet-bot test Ubuntu jitstress2
@dotnet-bot test Ubuntu corefx_baseline

@sivarv sivarv changed the title [WIP]Optimize SIMD codegen for (in)equality check against zero that produces bool result. Optimize SIMD codegen for (in)equality check against zero that produces bool result. Sep 27, 2016
@sivarv
Copy link
Copy Markdown
Member Author

sivarv commented Sep 27, 2016

@CarolEidt - Please review this.
CC @dotnet/jit-contrib

@CarolEidt
Copy link
Copy Markdown

Nice. LGTM.


In reply to: 249947330 [](ancestors = 249947330)

@CarolEidt
Copy link
Copy Markdown

:shipit:

@sivarv sivarv merged commit f7b15f7 into dotnet:master Sep 27, 2016
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
Optimize SIMD codegen for (in)equality check against zero that produces bool result.

Commit migrated from dotnet/coreclr@f7b15f7
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants