#cache #instructions #builtin-clear-cache

no-std clear-cache

A native implementation of __builtin___clear_cache without dependency of GCC/Clang

1 unstable release

0.1.0 Aug 9, 2024

#65 in #instructions

Download history 167/week @ 2024-08-05 17/week @ 2024-08-12 62/week @ 2024-08-19 152/week @ 2024-08-26 12/week @ 2024-09-02 14/week @ 2024-09-09 55/week @ 2024-09-16 17/week @ 2024-09-23

106 downloads per month
Used in static-keys

MIT/Apache

9KB
109 lines

clear-cache

A native implementation of __builtin___clear_cache without dependency of GCC/Clang.

From GCC documentation:

This function is used to flush the processor’s instruction cache for the region of memory between begin inclusive and end exclusive. Some targets require that the instruction cache be flushed, after modifying memory containing code, in order to obtain deterministic behavior.

If the target does not require instruction cache flushes, __builtin___clear_cache has no effect. Otherwise either instructions are emitted in-line to clear the instruction cache or a call to the __clear_cache function in libgcc is made.

From LLVM documentation:

The llvm.clear_cache intrinsic ensures visibility of modifications in the specified range to the execution unit of the processor. On targets with non-unified instruction and data cache, the implementation flushes the instruction cache.

On platforms with coherent instruction and data caches (e.g. x86), this intrinsic is a nop. On platforms with non-coherent instruction and data cache (e.g. ARM, MIPS), the intrinsic is lowered either to appropriate instructions or a system call, if cache flushing requires special privileges.

The default behavior is to emit a call to __clear_cache from the run time library.

This intrinsic does not empty the instruction pipeline. Modifications of the current function are outside the scope of the intrinsic.

Current implementation is taken from LLVM's implementation

Current CI-tested platforms:

  • Linux

    • x86_64-unknown-linux-gnu
    • x86_64-unknown-linux-musl
    • i686-unknown-linux-gnu
    • aarch64-unknown-linux-gnu
    • riscv64gc-unknown-linux-gnu
    • loongarch64-unknown-linux-gnu
  • macOS

    • aarch64-apple-darwin
  • Windows

    • x86_64-pc-windows-msvc
    • i686-pc-windows-msvc

Dependencies

~0–35MB
~526K SLoC