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.