Repository navigation
Open
Labels
api-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-System.Runtime.Intrinsicsneeds-further-triageIssue has been initially triaged, but needs deeper consideration or reconsideration
Milestone
Description
Background and motivation
Arm64 has PRFM instruction that prefetches the specified memory region into processor's cache.
A32 also has similar instructions.
It's nice to have support for Arm64's mandatory prefetch instructions, since we have ones for Sse.Prefetch* and Sve.Prefetch*.
API Proposal
namespace System.Runtime.Intrinsics.Arm;
public abstract class ArmBase
{
public static unsafe void PrefetchRead(void* address); // PLD
public static unsafe void PrefetchWrite(void* address); // PLDW
public static unsafe void PreloadInstructions(void* address); // PLI
public abstract class Arm64
{
public static unsafe void Prefetch(void* address, [ConstantExpected] Arm64PrefetchType prefetchType); // PRFM
}
}
public enum Arm64PrefetchType
{
/// <summary>PLDL1KEEP</summary>
LoadL1Temporal = 0b00000,
/// <summary>PLDL1STRM</summary>
LoadL1NonTemporal = 0b00001,
/// <summary>PLDL2KEEP</summary>
LoadL2Temporal = 0b00010,
/// <summary>PLDL2STRM</summary>
LoadL2NonTemporal = 0b00011,
/// <summary>PLDL3KEEP</summary>
LoadL3Temporal = 0b00100,
/// <summary>PLDL3STRM</summary>
LoadL3NonTemporal = 0b00101,
/// <summary>PLIL1KEEP</summary>
PreloadInstructionsL1Temporal = 0b01000,
/// <summary>PLIL1STRM</summary>
PreloadInstructionsL1NonTemporal = 0b01001,
/// <summary>PLIL2KEEP</summary>
PreloadInstructionsL2Temporal = 0b01010,
/// <summary>PLIL2STRM</summary>
PreloadInstructionsL2NonTemporal = 0b01011,
/// <summary>PLIL3KEEP</summary>
PreloadInstructionsL3Temporal = 0b01100,
/// <summary>PLIL3STRM</summary>
PreloadInstructionsL3NonTemporal = 0b01101,
/// <summary>PSTL1KEEP</summary>
StoreL1Temporal = 0b10000,
/// <summary>PSTL1STRM</summary>
StoreL1NonTemporal = 0b10001,
/// <summary>PSTL2KEEP</summary>
StoreL2Temporal = 0b10010,
/// <summary>PSTL2STRM</summary>
StoreL2NonTemporal = 0b10011,
/// <summary>PSTL3KEEP</summary>
StoreL3Temporal = 0b10100,
/// <summary>PSTL3STRM</summary>
StoreL3NonTemporal = 0b10101
}
API Usage
private static void SwapBytesV128(ref byte x, ref byte y, nuint length)
{
nuint i = 0;
var ol = length - ((nuint)Vector128<byte>.Count * 2 - 1);
if (ol < length)
{
for (; i < ol; i += (nuint)Vector128<byte>.Count * 2)
{
ArmBase.Arm64.Prefetch(Unsafe.AsPointer(ref Unsafe.Add(ref x, i + 2 * (nuint)Vector128<byte>.Count)), Arm64PrefetchType.LoadL1NonTemporal);
ArmBase.Arm64.Prefetch(Unsafe.AsPointer(ref Unsafe.Add(ref y, i + 2 * (nuint)Vector128<byte>.Count)), Arm64PrefetchType.LoadL1NonTemporal);
var v0_16b = Vector128.LoadUnsafe(ref x, i + 0 * (nuint)Vector128<byte>.Count);
var v1_16b = Vector128.LoadUnsafe(ref x, i + 1 * (nuint)Vector128<byte>.Count);
var v2_16b = Vector128.LoadUnsafe(ref y, i + 0 * (nuint)Vector128<byte>.Count);
var v3_16b = Vector128.LoadUnsafe(ref y, i + 1 * (nuint)Vector128<byte>.Count);
v0_16b.StoreUnsafe(ref y, i + 0 * (nuint)Vector128<byte>.Count);
v1_16b.StoreUnsafe(ref y, i + 1 * (nuint)Vector128<byte>.Count);
v2_16b.StoreUnsafe(ref x, i + 0 * (nuint)Vector128<byte>.Count);
v3_16b.StoreUnsafe(ref x, i + 1 * (nuint)Vector128<byte>.Count);
}
}
SwapSmall(ref x, ref y, length, i);
}
Alternative Designs
- Adding equivalent functions with
scoped ref readonly Tinstead ofvoid*would be fine since it shouldn't throw anything, even the read access to the same address would result in anAccessViolationException.
Risks
Nothing more than one for existing *.Prefetch* instructions
Reacted by Raphael DUCOM
Metadata
Metadata
Assignees
Labels
api-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-System.Runtime.Intrinsicsneeds-further-triageIssue has been initially triaged, but needs deeper consideration or reconsideration
Activity
dotnet-policy-service commentedon Aug 23, 2024
Tagging subscribers to this area: @dotnet/area-system-runtime-intrinsics
See info in area-owners.md if you want to be subscribed.
EgorBo commentedon Aug 23, 2024
Isn't it what
AdvSimd.Arm64.LoadPairVector128NonTemporalis for?tannergooding commentedon Aug 23, 2024
Prefetch and non-temporal load/store aren't necessarily the same and there's places you may want one, the other, or both. Both the Intel and AMD architecture manuals have lengthy sections discussing the nuance. I expect the Arm64 architecture manual has similar guidance, but I'm not as deeply familiar off the top of my head.
Modern CPUs are very good at predicting what memory needs to be prefetched, particularly for cases like loops where you're just iterating through memory sequentially or via consistent strided offsets (either forward or reverse iteration). They also document cases where prefetch can trivially hurt performance, such as
thrashing,cache pollution, andprefetch from unmapped pages(caused by using prefetch in a loop). It's one of those cases where most patterns (like the one given in the top post) don't want or needprefetch, instead you want to rely on the built-in hardware prefetching. You instead really only want software prefetch in cases where the memory accesses cannot be well predicted (but are still known to be valid) and where the prefetch can be offset by other work before the memory access is needed.There's also the general consideration that this is one of the concepts that when you do want it, you often want to do the same thing across all supported targets and so it may be something where a cross platform API is appropriate.
MineCake147E commentedon Aug 23, 2024
Not necessarily. Even worse, there are a ton of vendors that design cores on their own, like Apple.
Some implementation might be bad at it, like only detecting the simplest sequential pattern.
This is THE reason why I suggest these APIs.
My example is merely an example of how to use it.
Proposed APIs might help to implement cross-platform APIs.
Also, cross-platform APIs could lack some capability that hardware provides in certain platform, restricting potential hardware-specific optimizations.
touitou-dan commentedon Jun 23, 2026
For anyone following this: I've opened a collection-level proposal that would be a direct consumer of these prefetch intrinsics: batched + software-prefetch lookups and enumeration for large Dictionary / │ ImmutableDictionary: #129513.
It uses x86 Sse.Prefetch0 today and needs an ARM64 prefetch intrinsic to be portable, so it's a concrete motivating use case for the work proposed here. Measured roughly 2–3x on memory-bound workloads at 10M entries.