Skip to content

[API Proposal]: Expose xarch serialize instruction. #66467

Closed

Description

@anthonycanino

Background and motivation

Currently, the cpuid instruction also functions as a full serializing instruction --- one that ensures all modifications to flags, registers, and memory are drained before the next instruction can execute --- on x86 hardware. Executing a serializing instructions on P6 and more recent processor families constrain speculative execution because the results of speculatively executed instructions are discarded (see Chapter 8: Multiple-Processor Management in the Intel 64 and IA-32 Architectures Software Developer’s Manual, Volume 3A)

However, cpuid has overhead in that it clobbers EAX, EBX, ECX, and EDX registers. Intel exposes a new instruction, serialize under the cpuid feature flag SERIALIZE which acts as a full serializing instruction without the overhead of clobbering the registers as cpuid does.

Full details of the serialize instruction can be found in Chapter 2.1 Instruction Set Reference in Intel Architecture Instruction Set Extensions and Future Features.

API Proposal

namespace System.Runtime.Intrinsics.X86
{
    /// <summary>
    /// This class provides access to Intel SERIALIZE hardware instruction via intrinsics
    /// </summary>
    [Intrinsic]
    [CLSCompliant(false)]
    public abstract class X86Serialize : X86Base
    {
        internal Serialize() { }

        public static new bool IsSupported { get => IsSupported; }

        [Intrinsic]
        public new abstract class X64 : X86Base.X64
        {
            internal X64() {  }

            public static new bool IsSupported { get => IsSupported; }
        }

        /// <summary>
        /// void _serialize (void);
        /// </summary>
        public static void Serialize() => Serialize();

    }
}

API Usage

Broadly speaking, the developer can issue X86Serialize.Serialize() whenever they want to serialize the instruction stream before a branching conditional:

X86Serialize.Serialize();
if (someCondition)
{
  // ...
}

Alternative Designs

dotnet developers currently need to call System.Runtime.Intrinsics.X86.X86Base.CpuId() to issue a full serializing instruction which has the extra overhead stated above as opposed to issuing the serialize instruction.

Risks

As serialize functions as an alternative to cpuid with less overhead and a 0-arg use, we don't foresee any additional risks here.

Activity

  1. added
    api-suggestion
    on Mar 10, 2022
  2. ghost added
    untriaged
    on Mar 10, 2022
  3. added
    api-ready-for-review
    and removed
    api-suggestion
    untriaged
    on Mar 10, 2022
  4. tannergooding commented on Mar 10, 2022

    @tannergooding
    Member

    Thanks for the proposal here! I've gone ahead and marked it ready for review.

    Notably, we can't name the class and method both Serialize so we will need to disambiguate somewhere. Just an initial thought/suggestion is X86Serialize.Serialize which would also ensure the class isn't itself more generally conflicting.

    I don't have a strong preference, however.

  5. anthonycanino commented on Mar 10, 2022

    @anthonycanino
    ContributorAuthor

    Thanks for looking at it! X86Serialize.Serialize is fine with me. I'll adjust.

  6. anthonycanino commented on Mar 30, 2022

    @anthonycanino
    ContributorAuthor

    Hi @tannergooding , I can see this API hasn't been discussed yet (I believe) in the API proposal review meetings.

    Is there a long backlog of discussions before this would get discussed? Thanks!

  7. added this to the 7.0.0 milestone on Mar 30, 2022
  8. tannergooding commented on Mar 30, 2022

    @tannergooding
    Member

    There is a decent backlog of issues which can be seen here: https://apireview.net/.

    If it is important for this to make it into .NET 7 (which will ship around November) than we can prioritize it further.

  9. anthonycanino commented on Apr 7, 2022

    @anthonycanino
    ContributorAuthor

    Thank you @tannergooding , I think this is ok for now. Thanks for moving it to the .NET 7 milestone.

  10. terrajobst commented on Apr 12, 2022

    @terrajobst
    Contributor

    Video

    • Looks good as proposed
    namespace System.Runtime.Intrinsics.X86;
    
    [Intrinsic]
    [CLSCompliant(false)]
    public abstract class X86Serialize : X86Base
    {
        public static new bool IsSupported { get; }
    
        [Intrinsic]
        public new abstract class X64 : X86Base.X64
        {
            public static new bool IsSupported { get; }
        }
    
        public static void Serialize();
    }
  11. 1 remaining item

  12. added
    api-approved
    on Apr 12, 2022
  13. tannergooding commented on Apr 12, 2022

    @tannergooding
    Member

    @anthonycanino, this is approved now and we can feel free to implement it whenever someone has availability.

  14. anthonycanino commented on Apr 22, 2022

    @anthonycanino
    ContributorAuthor

    @tannergooding is adding the intrinsic lowering for the mono llvm path required for this implemtation, as seen in (#61707) or is adding it to RyuJIT enough?

  15. tannergooding commented on Apr 22, 2022

    @tannergooding
    Member

    It shouldn't be required since this is a new ISA and IsSupported will report false.

    However, it is generally preferable to bring both online where possible. CC. @fanyang-mono

  16. fanyang-mono commented on Apr 22, 2022

    @fanyang-mono
    Member

    Yes, if it is possible, it would be nice to add intrinsics support to Mono at the same time as CoreCLR. And I could answer questions if there is any, regarding to Mono.

  17. anthonycanino commented on Apr 22, 2022

    @anthonycanino
    ContributorAuthor

    Would that include the mini and llvm lowering pathway of mono, or just one?

  18. anthonycanino commented on Apr 25, 2022

    @anthonycanino
    ContributorAuthor

    Hi @tannergooding can you answer a question regarding adding a new ISA to RyuJIT? Currently, I am adding just two lines to the InstructionSetDesc.txt...

    instructionset     ,X86   ,X86Serialize   ,   ,27 ,X86SERIALIZE ,x86serialize
    ; ...
    implication        ,X86   ,X86SERIALIZE   ,X86Base
    

    and generating with gen.bat in the directory. This change alone makes superpmi report a bunch of missing methods and method diffs, with no other adjustments. I assume it is because the JIT/EE interface has changed? Updating the jit guuid didn't fix the issue.

    I'm trying to get the superpmi diffs stable so I can test a branch of mine that does implement the intrinsic.

  19. tannergooding commented on Apr 25, 2022

    @tannergooding
    Member

    Right, superpmi won't work as expected when you change the JIT/EE version.

    I'm not sure what the best way to get a diff in this case is. CC. @dotnet/jit-contrib who may have advice here

  20. AndyAyersMS commented on Apr 25, 2022

    @AndyAyersMS
    Member

    jit-diffs --pmi will work but it requires a couple of steps and before/after builds.

    You can find instructions here: https://gist.github.com/EgorBo/a72bfa70f08a4d890241037c0fc1fb93

    (replace --crossgen <path> with --pmi and add -f )

  21. anthonycanino commented on Apr 27, 2022

    @anthonycanino
    ContributorAuthor

    @AndyAyersMS will this work in my case though? Since I am creating a new ISA, I don't think I can isolate the base jit changes from the C# changes because of the re-generated corejit interface via InstructionSetDesc.txt.

    Essentially, I cannot only edit the jit due to the new ISA being needed for the JIT intrinsic nodes etc.

  22. AndyAyersMS commented on Apr 27, 2022

    @AndyAyersMS
    Member

    Yes, it should work -- you would be comparing results from two entirely different builds.

  23. fanyang-mono commented on Apr 29, 2022

    @fanyang-mono
    Member

    Would that include the mini and llvm lowering pathway of mono, or just one?

    Both mini and LLVM.

  24. tannergooding commented on May 19, 2022

    @tannergooding
    Member

    This was completed in #68677

  25. ghost locked as resolved and limited conversation to collaborators on Jun 18, 2022
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

    api-approvedarea-System.Runtime.Intrinsics

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions