Skip to content

CONCEPT Cited by 1 source

Auto-vectorization

Definition

Auto-vectorization is a compiler optimization where the compiler transforms scalar loop code into SIMD (Single Instruction, Multiple Data) instructions without explicit programmer annotations. The compiler pattern-matches loop bodies and emits vector instructions that process multiple data elements per instruction (e.g., 16 bytes at a time with 128-bit NEON, 32 with AVX2, 64 with AVX-512).

Preconditions

Auto-vectorization requires that the loop body:

  1. Has no data-dependent control flow — no break, no conditional early-exit, no branches whose outcome depends on the data being processed. This is the single most critical requirement.
  2. Performs uniform operations — the same operation on every element.
  3. Has no loop-carried dependencies — each iteration is independent of the previous one.
  4. Accesses contiguous memory — scatter/gather patterns may partially vectorize but at reduced efficiency.

The early-exit barrier (canonical example)

GitHub's casefold crate demonstrates that a single data-dependent break is sufficient to completely prevent vectorization, even if the loop body is otherwise perfectly branchless:

  • Branchless body + break on non-ASCII: 0 vector instructions, 2.6 GiB/s
  • Branchless body + no break (OR-accumulator): 41 vector instructions, >45 GiB/s

The compiler cannot vectorize across a data-dependent loop exit because it would need to speculatively process elements past the exit point and then discard the results — fundamentally incompatible with SIMD's operate-on-a-whole-register model.

(Source: sources/2026-07-31-github-dont-stop-early-case-folding-source-code-at-memory-speed)

Partially vectorized state

Between "no vectorization" and "fully vectorized" there's a middle ground: LLVM may vectorize parts of the loop body while leaving others scalar. GitHub measured this intermediate state at 7.6 GiB/s (25 vector instructions) — when the break was removed but the body still contained a conditional write (compare-blend-masked-store). Making the write unconditional via arithmetic moved to full vectorization (41 vector instructions, >45 GiB/s).

Seen in

Last updated · 607 distilled / 1,851 read