Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Poor code quality for Interlocked.{Compare}Exchange(nint) #47266

Closed
alexrp opened this issue Jan 21, 2021 · 2 comments · Fixed by #47368 or mono/mono#20778
Closed

Poor code quality for Interlocked.{Compare}Exchange(nint) #47266

alexrp opened this issue Jan 21, 2021 · 2 comments · Fixed by #47368 or mono/mono#20778
Assignees
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI tenet-performance Performance related issue
Milestone

Comments

@alexrp
Copy link
Contributor

alexrp commented Jan 21, 2021

C#:

using System;
using System.Runtime.CompilerServices;
using System.Threading;

static class Program
{
    [MethodImpl(MethodImplOptions.AggressiveInlining |
                MethodImplOptions.AggressiveOptimization)]
    static void PtrAtomic(nint x)
    {
        Interlocked.Exchange(ref x, x);
        Interlocked.CompareExchange(ref x, x, x);
    }

    [MethodImpl(MethodImplOptions.AggressiveInlining |
                MethodImplOptions.AggressiveOptimization)]
    static void IntAtomic(int x)
    {
        Interlocked.Exchange(ref x, x);
        Interlocked.CompareExchange(ref x, x, x);
    }

    [MethodImpl(MethodImplOptions.AggressiveInlining |
                MethodImplOptions.AggressiveOptimization)]
    static void LongAtomic(long x)
    {
        Interlocked.Exchange(ref x, x);
        Interlocked.CompareExchange(ref x, x, x);
    }
}

Result on .NET 5 (x64):

Program.PtrAtomic(IntPtr)
    L0000: sub rsp, 0x28
    L0004: mov [rsp+0x30], rcx
    L0009: lea rcx, [rsp+0x30]
    L000e: mov rdx, [rsp+0x30]
    L0013: call 0x00007ff973089af0
    L0018: lea rcx, [rsp+0x30]
    L001d: mov rdx, [rsp+0x30]
    L0022: mov r8, [rsp+0x30]
    L0027: call 0x00007ff9730920f0
    L002c: nop
    L002d: add rsp, 0x28
    L0031: ret

Program.IntAtomic(Int32)
    L0000: mov [rsp+8], ecx
    L0004: lea rax, [rsp+8]
    L0009: mov edx, [rsp+8]
    L000d: xchg [rax], edx
    L000f: lea rdx, [rsp+8]
    L0014: mov ecx, [rsp+8]
    L0018: mov eax, [rsp+8]
    L001c: lock cmpxchg [rdx], ecx
    L0020: ret

Program.LongAtomic(Int64)
    L0000: mov [rsp+8], rcx
    L0005: lea rax, [rsp+8]
    L000a: mov rdx, [rsp+8]
    L000f: xchg [rax], rdx
    L0012: lea rdx, [rsp+8]
    L0017: mov rcx, [rsp+8]
    L001c: mov rax, [rsp+8]
    L0021: lock cmpxchg [rdx], rcx
    L0026: ret

I would expect the JIT to expand the nint methods to the exact same code as either the int or long methods.

@dotnet-issue-labeler dotnet-issue-labeler bot added area-System.Threading untriaged New issue has not been triaged by the area owner labels Jan 21, 2021
@EgorBo
Copy link
Member

EgorBo commented Jan 21, 2021

The easiest fix is probably to just expand it: EgorBo@34fdd14

Or we can convert Interlocked intrinsics to the new-style NamedIntrinsics and handle it there.

@jkotas jkotas added area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI and removed area-System.Threading labels Jan 21, 2021
@jkotas jkotas added the tenet-performance Performance related issue label Jan 21, 2021
@jkotas jkotas modified the milestones: 5.0.x, 6.0.0 Jan 21, 2021
@jkotas
Copy link
Member

jkotas commented Jan 21, 2021

The easiest fix is probably to just expand it: EgorBo/runtime-1@34fdd14

I have left a few comments on the commit.

convert Interlocked intrinsics to the new-style NamedIntrinsics and handle it there.

That would be nice too. But the easiest fix above is still useful to reduce number of FCalls.

@JulieLeeMSFT JulieLeeMSFT removed the untriaged New issue has not been triaged by the area owner label Jan 22, 2021
@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Jan 23, 2021
monojenkins pushed a commit to monojenkins/mono that referenced this issue Jan 23, 2021
monojenkins pushed a commit to monojenkins/mono that referenced this issue Jan 23, 2021
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Jan 23, 2021
akoeplinger pushed a commit to mono/mono that referenced this issue Jan 24, 2021
Fixes dotnet/runtime#47266

Co-authored-by: EgorBo <EgorBo@users.noreply.github.com>
@ghost ghost locked as resolved and limited conversation to collaborators Feb 22, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI tenet-performance Performance related issue
Projects
None yet
4 participants