Skip to content

SIMD vectors (#1874): Vector[N, T], Mask[N, W], lane-wise operators, m.select, masks, c_import of C vectors (spec v7.11, v7.14, v7.16) - #1927

Merged
ehartford merged 6 commits into
mac-own-sdkfrom
simd-impl
Sep 30, 2026
Merged

ehartford merged 6 commits into
mac-own-sdkfrom
simd-impl

Conversation

@ehartford

@ehartford ehartford commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Implements §4.3d as blessed (D78, D80 and its amendment). Draft until the stack battery is green.

  • Vector[N, T] and Mask[N, W] with the native-width aliases; construction, splat only where one meaning is forced, swizzles, component writes, lane indexing; generic lane counts (fn dot[N]); lossless lane widening; 128-bit lanes (W = 128).
  • Lane-wise operators with scalar broadcast on either side; comparisons yield masks; m.select(a, b); masks construct, index (read and write), combine with & | ^ and not; two-meaning mask forms refused with hints.
  • Layout and C ABI follow clang on every target (checked against clang IR; x86_64 32-byte vectors land 32-byte aligned on the stack); with-abi.md records it (ABI v9).
  • c_import translates C vector types to Vector[N, T], printed as the alias.

Verified: 35 SIMD tests (each failed before its change), :gate, :deep-debug-tool-tests --no-deps, :drop-audit green.

@ehartford
ehartford added this pull request to stack #1928 September 30, 2026 05:23
@ehartford ehartford changed the title simd impl SIMD vectors (#1874): Vector[N, T], Mask[N, W], lane-wise operators, m.select, masks, c_import of C vectors (spec v7.11, v7.14, v7.16) Sep 30, 2026
Eric Hartford added 6 commits September 30, 2026 03:42
…ors, splat, swizzles (§4.3d, D78; #1874)

Sema owns every vector fact (src/SemaVector.w): TypeKind.TY_VECTOR
(d0 lane, d1 N) and TY_MASK (d0 W, d1 N), the one-token aliases
(f32x4 … u8x64, m8x16 … m64x8) registered for the native widths,
`Vector[N, T]` / `Mask[N, W]` in type and expression position,
construction (exactly N lanes), `T.splat(s)`, `T.from_bits(u)`, `.bits()`,
`select(m, a, b)`, `.all()`/`.any()`, the seven reductions, `.x`…`.wzyx`
swizzles for N <= 4, `v[i]` lane reads and writes (a constant index checked
at compile time), lane-wise `+ - * / %`, `& | ^ << >> ~`, wrapping and
saturating operators, comparisons yielding Mask[N, width(T)], lane-wise
`as`, lossless lane widening under §4.2.6 and refused narrowing. A scalar
broadcasts only where one meaning is forced: a literal in a vector context
and a scalar operand of a lane-wise operator; `let v: f32x4 = s` is refused
with the `f32x4.splat(s)` spelling. `select(` is the keyword `select`
followed by `(`, so `select await` is unchanged.

MIR materializes the decisions (src/MirVector.w): construction, splat and
swizzle are RK_AGGREGATE of the vector type, conversions RK_CAST, and
select / reductions / bitcasts SIMD_* intrinsic calls. Codegen
(src/CodegenVector.w) lowers to LLVM `<N x T>` (a mask `<N x iW>` of all-ones
lanes); integer lanes follow §4.2 per lane (a lane overflow panics under the
default mode), and a runtime lane index out of range panics.

Layout (TypeLayout.w): N x size(T) rounded up to a power of two; alignment
the target's — the size, capped at 16 bytes on AArch64 as clang does, and a
record holding such a vector gets the packed, padded body that places it
there. C ABI (FnAbi.w, compute_fn_abi): up to 16 bytes direct in a vector
register; larger, AAPCS64 passes a pointer to a caller copy and returns via
sret, SysV x86_64 passes byval. Checked against clang's IR for
arm64-apple-macos, aarch64-linux-gnu, x86_64-linux-gnu and
x86_64-pc-windows-msvc, and on this host by calling Apple libm's
_simd_cos_f4 / _simd_cos_d2 (behav_1874_simd_c_abi_darwin). with-abi.sha256
re-recorded (FnAbi.w, TypeLayout.w changed).

--emit-c refuses vector types loudly; a global vector folds to a constant
when its splat or construction folds (build_default_value now zero-fills a
vector instead of falling back to i32 0).

Not implemented, pending a ruling: a type parameter as the lane count
(`fn dot[N](a: Vector[N, f32])`) — there are no value generic parameters —
is refused with a diagnostic naming the gap.
… (§16.1, D78; #1874, retires #1872's omission)

A `vector_size` / `ext_vector_type` type translates to the With alias for
the native widths (`__m128` -> f32x4, `long long` x2 -> i64x2) and to
`Vector[N, T]` otherwise (`ext_vector_type(3)` -> Vector[3, f32]); the lane
is the exact primitive of the C element's width and signedness. A lane With
has no vector of (bool, half, long double) fails loudly as unsupported.

Spec v7.11 retires the omission these tests pinned: the typedef, a record
holding a vector and a function taking one now import.
behav_c_import_vector_types_are_unsupported becomes
behav_c_import_vector_types_import (the record is built by value and the
lane read); err_c_import_vector_fn_use_is_omitted and
err_c_import_vector_record_is_opaque are removed, their premise retired.
behav_1874_simd_c_import checks layout against clang's sizeof/_Alignof
(including a record holding a 32-byte vector, whose alignment is the
target's) and calls translated inline functions taking and returning
vectors.
…anes (§4.3d; #1874)

Four places the implementation fell short of §4.3d's words:

- `fn dot[N](a: Vector[N, f32], b: Vector[N, f32])` compiles and runs: a
  generic parameter used as a lane count binds, from the arguments, to a
  compile-time integer (TypeKind.TY_CONST_INT, a generic substitution
  only); each lane count is its own specialization. A second argument
  with another count is "cannot infer one lane count". Arrays `[T; N]`
  do not get this (no parser or Sema path for a named array length).
- `v.x = 5` writes lane 0: a single component is the lane's place
  (MirVector.lower_vector_component_place). A multi-lane swizzle write
  stays refused (not specified).
- Lanes follow §4.2 per lane: vectors of one lane count, kind and
  signedness meet at the wider lane (i32x4 + i64x4 is i64x4, f32x4 * f64x4
  is f64x4; the narrower operand converts); different signedness is
  refused with an `as` hint, as for scalars.
- i128/u128 lanes are vectors (arithmetic, bitwise, reductions,
  `.bits()` as u128); codegen builds lane limits at any width
  (Overflow's i64 helpers stop at 64 bits).
…ion, indexing and operators, W = 128 (§4.3d; #1874)

- `m.select(a, b)` is a method on the mask; the `select(` parser special
  case is gone, so `select` is the `select await` keyword alone again
  (examples/async-auction.w checks; err_1874_simd_select_is_a_method pins
  that `select(...)` is not a call).
- Masks construct (`m32x4(true, false, true, true)`, `m32x4.splat(true)`),
  index (`m[i]` reads a bool: SIMD_MASK_LANE, range-checked), combine with
  `& | ^` (a bool operand broadcasts; different widths refused with an
  `as` hint), negate with `not`, and convert width with `m as Mask[4, 8]`.
  `and`/`or` on a mask are refused (they short-circuit). Writing `m[i]` is
  not specified and refused.
- W includes 128: Mask[N, 128] and the m128x1/2/4 aliases (and
  i128x1..u128x4 vector aliases for the native widths), so 128-bit lanes
  compare and select.
- A scalar broadcasts on either side of every lane-wise operator (already
  so; now the spec's words): `s * v`, `0.0 < v`, `0xff & v`, `v >> 2`.
…d D80; #1874)

§4.2.4 lists << and >> among the bitwise operators and D80 broadcasts a
scalar on either side of a lane-wise operator, so a scalar shifted by a
vector is the broadcast scalar shifted lane by lane. The refusal is gone;
the scalar must fit the lane without loss (a scalar shift amount stays
any integer), and a literal left operand takes the vector's lane type.
…ors and masks (§4.3d; #1874)

- `m[i] = b` writes a mask lane: MIR stores the mask with lane i set
  (SIMD_MASK_LANE_SET, range-checked; a true lane all ones).
- A `bool` literal in a mask context broadcasts (`let m: m32x4 = true`).
- A scalar `a` or `b` of `m.select(a, b)` broadcasts as an operator's
  scalar operand does, the vector type coming from the other operand or
  the context; two scalars with no vector context stay refused.
- Refused with a diagnostic naming why: `m == n` (spell `(m ^ n).any()` or
  `not (m ^ n)`), mask<->vector casts and `.bits()`/`.from_bits` on a mask
  (a true lane is 1 or -1), `.x` components on a mask (its lanes are
  `m[i]`), `~m` (`not m` is the one spelling).
- err_1874_simd_mask_lane_write retired (the write is now the spec's).
- docs/spec/abi/with-abi.md records the vector/mask layout (§2) and the C
  vector call rule (§4); WITH_ABI_VERSION 9 with its history entry;
  with-abi.sha256 re-recorded.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant