Repository navigation
Description
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
tannergooding commentedon Mar 10, 2022
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
Serializeso we will need to disambiguate somewhere. Just an initial thought/suggestion isX86Serialize.Serializewhich would also ensure the class isn't itself more generally conflicting.I don't have a strong preference, however.
anthonycanino commentedon Mar 10, 2022
Thanks for looking at it!
X86Serialize.Serializeis fine with me. I'll adjust.anthonycanino commentedon Mar 30, 2022
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!
tannergooding commentedon Mar 30, 2022
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.
anthonycanino commentedon Apr 7, 2022
Thank you @tannergooding , I think this is ok for now. Thanks for moving it to the .NET 7 milestone.
terrajobst commentedon Apr 12, 2022
Video
1 remaining item
tannergooding commentedon Apr 12, 2022
@anthonycanino, this is approved now and we can feel free to implement it whenever someone has availability.
anthonycanino commentedon Apr 22, 2022
@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?
tannergooding commentedon Apr 22, 2022
It shouldn't be required since this is a new ISA and
IsSupportedwill reportfalse.However, it is generally preferable to bring both online where possible. CC. @fanyang-mono
fanyang-mono commentedon Apr 22, 2022
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.
anthonycanino commentedon Apr 22, 2022
Would that include the mini and llvm lowering pathway of mono, or just one?
anthonycanino commentedon Apr 25, 2022
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...
and generating with
gen.batin 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.
tannergooding commentedon Apr 25, 2022
Right,
superpmiwon'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
AndyAyersMS commentedon Apr 25, 2022
jit-diffs --pmiwill 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--pmiand add-f)anthonycanino commentedon Apr 27, 2022
@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.
AndyAyersMS commentedon Apr 27, 2022
Yes, it should work -- you would be comparing results from two entirely different builds.
fanyang-mono commentedon Apr 29, 2022
Both mini and LLVM.
tannergooding commentedon May 19, 2022
This was completed in #68677