The general, cross-cutting guidance for SIMD and hardware intrinsics lives in the official .NET documentation. Read it first and defer to it for anything not specific to this repo:
The repo-specific nuance is in docs/coding-guidelines/vectorization-guidelines.md.
This skill distills what to actually enforce when authoring or reviewing vectorized changes here.
Core rules
- Reach for the highest-level API that already does the job.
Span<T>/stringmethods,TensorPrimitives, and the tensor types already vectorize many operations. LINQ is often vectorized too — operators such asSum,Max,Min, andAverageaccelerate when the source's underlying span can be extracted. Don't hand-roll what's already optimized and tested. - Start with
Vector128<T>. It's the common denominator accelerated on the broadest hardware, and you don't needVector256/Vector512for a correct, portable implementation. Add wider widths and platform intrinsics only for a measured hot path. - Keep platforms consistent. Prefer the cross-platform APIs on
Vector128/Vector256; they lower to the optimal instruction per target (for example(vector & mask) == Vector128<byte>.Zerobecomespteston x86/x64). Only drop toSystem.Runtime.Intrinsics.X86/Arm/Wasmwhen a specific instruction measurably beats the portable form, and guard it with the class'sIsSupported. - Read
IsHardwareAcceleratedandCountdirectly; don't cache them to locals. Both are JIT-time constants, so caching buys nothing, and a local obscures that constant-ness — read them at each use so the branches you don't take are eliminated. - Prefer operators over named methods (
+,&,<<) for readability, but mind precedence:a & b == cparses asa & (b == c), so parenthesize when mixing them.
Authoring checklist
- Structure: widest-supported width first, working down to a scalar fallback for small inputs and
non-accelerated hardware. Guard each width with
Vector128.IsHardwareAcceleratedandVector128<T>.IsSupported(the latter matters in generic code), then compare length againstCount. - Loads and stores: prefer the span-based
Vector128.Create(span)/CopyTo— the JIT keeps them efficient and they need no pinning or reference arithmetic. Theunsafeload/store variants are largely no longer needed; when you genuinely must walk a buffer by managed reference, use theLoadUnsafe(ref T, nuint elementOffset)/StoreUnsafeelement-offset overloads rather than raw pointer orrefarithmetic. - Empty buffers: get the starting reference from
MemoryMarshal.GetReference(orGetArrayDataReferencefor arrays), notref span[0]. - Reinterpreting unsupported types:
Vector128<T>supports the primitive numerics, notcharorbool. Reinterpret viaMemoryMarshal.Cast(a span) or the vector'sAs<TFrom, TTo>— for examplechar→ushort. Reinterpretation changes only the type, not the bits, so keeping the data well-formed is on you (aboolstays0/1, achara valid UTF-16 code unit); normalize any out-of-range result before writing it back. - Offset arithmetic is unsigned (
nuint). Always check the buffer length before computing an offset likebuffer.Length - Vector128<int>.Count; if the buffer is smaller than one vector that subtraction underflows to a huge value. - Always handle the remainder. Reprocess the last full vector's worth of elements, overlapping what
the loop already did. For an idempotent operation (a search) fold the overlap in directly; for a
non-idempotent operation (a sum) mask the overlap to the operation's identity with
ConditionalSelectfirst. - Watch backwards iteration. Never let an intermediate
refpoint outside its buffer, even transiently — a GC that runs at that moment won't update it, producing a GC hole. See theLastIndexOfcase study (#73768 / fix). - Account for buffer overlap when loading from one buffer and storing into another.
Testing checklist
- Cover every code path: the
Vector256path, theVector128path, and the scalar path — each with inputs both large enough and too small to benefit. - Toggle acceleration via environment variables (can't be done at the unit-test level): run the
suite with no overrides, with
DOTNET_EnableAVX2=0(disablesVector256), and withDOTNET_EnableHWIntrinsic=0(disables all intrinsics down to the software fallback). Build the affected library and run its test project per the build/test workflow in thebuild-and-testskill, with the relevantDOTNET_Enable*variable set in the environment. - Guard against out-of-bounds reads with
BoundedMemory.BoundedMemory.Allocate<T>(count)places a no-access page immediately after the buffer (usePoisonPagePlacement.Beforefor backwards-iterating algorithms), so on most targets a read past the end faults with an access violation instead of silently succeeding. It falls back to an unprotected allocation on Browser/WASI and .NET Framework, so don't rely on the guard there. Always include lengths that aren't an exact multiple of the vector width.
Benchmarking
Vectorization adds complexity, so measure that it pays off before keeping it. Use BenchmarkDotNet
and the same DOTNET_Enable* variables to compare scalar / Vector128 / Vector256 in one run. Keep
in mind: larger inputs benefit more (small buffers can be slower due to setup), speedups are rarely
the theoretical multiple (memory throughput, alignment, and latency all factor in), and randomized
allocation alignment adds noise — allocate aligned memory or enable BenchmarkDotNet's randomization for
stable/observable results. For non-trivial changes, use the performance-benchmark skill.
Review checklist
When reviewing a vectorized change, verify in priority order:
- Correctness first — does it match the scalar contract, including signed-zero/NaN/overflow and
endianness (
BitConverter.IsLittleEndian) edge cases? Verify any claim about existing behavior. - Reuse vs duplication — should this use an existing higher-level API, helper, or shared loop instead of an Nth hand-rolled copy?
- Remainder handling — is the tail covered, and is the idempotent-vs-masked choice correct?
- Memory safety — no unguarded
nuintunderflow, norefstraying outside its buffer, empty buffers handled, overlap considered. - Cross-platform consistency — does it diverge from other architectures without justification? Prefer the portable API unless a per-platform intrinsic is justified by numbers.
- Tests — all paths covered (including AV testing via
BoundedMemory) and run under the acceleration-toggle env vars? Ask for the missing test rather than just rejecting. - Perf claims — backed by concrete numbers (codegen bytes, throughput, ns with noise context), not assertions. A wider vector is not automatically faster.