polydat_grammar/port_type.rs
1// Copyright 2024-2026 Jonathan Shook
2// SPDX-License-Identifier: Apache-2.0
3
4//! The port type vocabulary: every type a wire, a port, a cast, or a
5//! declaration can name, with its keyword. What a type means to a
6//! compiled buffer (its slot color, width, and scratch element) is
7//! the runtime's, defined on this type by `polydat-core`.
8
9use std::fmt;
10
11/// Compile-time type tag for a port on a Polydat node.
12///
13/// **Narrow types and runtime storage:**
14///
15/// `PortType` includes narrow integer and float variants (U8/U16/U32,
16/// I8/I16/I32, F16/F32) that have no `Value` of their own. At
17/// runtime, narrow values are stored in the wide `Value` of their
18/// kind, with the assumption that the bits fit:
19///
20/// - unsigned (`u8`, `u16`, `u32`) → zero-extended in `Value::U64`
21/// - signed (`i8`, `i16`, `i32`) → sign-extended in `Value::I64`
22/// - `f32` → losslessly widened in `Value::F64` (`f16` rides
23/// `Value::U64` as its bit pattern; see [`PortType::F16`])
24///
25/// The narrow `PortType` variants exist for compile-time type
26/// checking and auto-adapter insertion (`U32ToU64`, `F32ToF64`).
27/// P2/P3 compiled kernels use flat u64 buffers where this packing
28/// is natural. The `Value` enum stays small — no combinatorial
29/// explosion of narrow variant types.
30///
31/// Every input and output port declares its `PortType`. The assembler
32/// uses these to validate wiring and auto-insert type adapters (e.g.,
33/// `u64 → f64` widening). At runtime, the corresponding `Value`
34/// variant is used.
35///
36/// **Widening rules** (auto-inserted by the assembler):
37/// - `U32 → U64`, `I32 → I64`, `F32 → F64` (lossless widening)
38/// - `U64 → F64` (lossless for values < 2^53)
39/// - `Bool → U64` (true=1, false=0)
40/// - Any type → `Str` (via display conversion)
41///
42/// **Narrowing** is never implicit — use explicit cast functions.
43///
44/// **Exhaustive on purpose.** The language grows a type now and then,
45/// and a `match` over every variant is then a compile error until it
46/// decides what the new type means. Polydat's own code relies on that,
47/// and so should a host's code that behaves differently per type. A host
48/// that only names or classifies types should not match at all: use
49/// [`Self::to_keyword`] or `Display` for a label, [`Self::from_keyword`]
50/// to parse one, and [`Self::numeric_domain`] or polydat's `SlotShape`
51/// queries to classify, none of which breaks when a type is added.
52#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
53pub enum PortType {
54 /// 64-bit unsigned integer. The primary numeric type.
55 U64,
56 /// 64-bit IEEE 754 float. Used for math, distributions, noise.
57 F64,
58 /// 32-bit unsigned integer. Widens to U64 automatically.
59 U32,
60 /// 32-bit signed integer. Widens to I64 automatically.
61 I32,
62 /// 64-bit signed integer.
63 I64,
64 /// 32-bit IEEE 754 float. Widens to F64 automatically.
65 F32,
66 /// 8-bit unsigned integer (cranelift I8 lane, unsigned
67 /// interpretation). Zero-extended in `Value::U64`; widens to
68 /// U64 automatically.
69 U8,
70 /// 8-bit signed integer (cranelift I8 lane, signed
71 /// interpretation). Sign-extended in `Value::I64`; widens to
72 /// I64 automatically.
73 I8,
74 /// 16-bit unsigned integer (cranelift I16 lane, unsigned
75 /// interpretation). Zero-extended in `Value::U64`; widens to
76 /// U64 automatically.
77 U16,
78 /// 16-bit signed integer (cranelift I16 lane, signed
79 /// interpretation). Sign-extended in `Value::I64`; widens to
80 /// I64 automatically.
81 I16,
82 /// 16-bit IEEE 754-2008 binary16 float (cranelift F16).
83 /// Carried as its bit pattern in `Value::U64` (low 16 bits),
84 /// the same stuffing convention as `F32`; widens to F32/F64
85 /// automatically (every f16 is exactly representable in both).
86 F16,
87 /// 128-bit unsigned integer (cranelift I128, unsigned
88 /// interpretation). Real `Value::U128` two-limb carrier — a
89 /// 128-bit value cannot ride a 64-bit slot. Rides two
90 /// consecutive u64 slots (a limb pair) on the compiled engines.
91 U128,
92 /// 128-bit signed integer (cranelift I128, signed
93 /// interpretation). Same carrier story as `U128`.
94 I128,
95 /// 128-bit SIMD register word, raw view — the full word as
96 /// algorithm-defined buffer state (heterogeneous lane
97 /// roles). Free bitcast to/from every lane-typed view.
98 Reg128,
99 /// Register word viewed as 16 × i8 lanes.
100 RegI8x16,
101 /// Register word viewed as 8 × i16 lanes.
102 RegI16x8,
103 /// Register word viewed as 4 × i32 lanes.
104 RegI32x4,
105 /// Register word viewed as 2 × i64 lanes.
106 RegI64x2,
107 /// Register word viewed as 8 × f16 lanes.
108 RegF16x8,
109 /// Register word viewed as 4 × f32 lanes.
110 RegF32x4,
111 /// Register word viewed as 2 × f64 lanes.
112 RegF64x2,
113 /// Boolean (true/false). Widens to U64 (1/0).
114 Bool,
115 /// Heap-allocated string. Any type auto-converts to Str.
116 Str,
117 /// Raw byte buffer.
118 Bytes,
119 /// Structured JSON value.
120 Json,
121 /// Adapter-contributed reflected type (e.g., CQL UUID).
122 Ext,
123 /// Type-erased Arc handle to a resolved resource (dataset,
124 /// prepared statement, ...). The producer node populates an
125 /// `Arc<dyn Any + Send + Sync>`; the consumer node downcasts to
126 /// the concrete type via `Value::as_handle::<T>()`.
127 Handle,
128 /// Typed `f32` vector slice (`Arc<[f32]>`). Bound natively by
129 /// adapters that understand `[f32]` (CQL `vector<float, N>`).
130 VecF32,
131 /// Typed `i32` vector slice (`Arc<[i32]>`).
132 VecI32,
133 /// Typed `f64` vector slice (`Arc<[f64]>`). Bound natively
134 /// for CQL `vector<double, N>`.
135 VecF64,
136 /// Typed `i64` vector slice (`Arc<[i64]>`). Bound natively
137 /// for CQL `vector<bigint, N>`.
138 VecI64,
139 /// Typed half-precision float vector (`Arc<[half::f16]>`).
140 /// Bound natively for CQL `vector<half_float, N>`-style
141 /// columns; stays at f16 on the wire so embeddings stored
142 /// as 16-bit floats don't widen to f32 at the boundary.
143 VecF16,
144 /// Typed `i16` vector slice (`Arc<[i16]>`). Bound natively
145 /// for CQL `vector<smallint, N>`.
146 VecI16,
147 /// Typed `i8` vector slice (`Arc<[i8]>`). Completes the
148 /// cranelift lane family; CQL `vector<tinyint, N>`.
149 VecI8,
150 /// Any value, as written: the slot of an input whose type may vary
151 /// over a kernel's lifetime (input_variance.md). Only a converter
152 /// node reads it, turning the value into the type its consumers
153 /// read; no other port has this type, and no value is typed `Dyn`.
154 Dyn,
155}
156
157impl fmt::Display for PortType {
158 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
159 match self {
160 PortType::U64 => write!(f, "u64"),
161 PortType::F64 => write!(f, "f64"),
162 PortType::U32 => write!(f, "u32"),
163 PortType::I32 => write!(f, "i32"),
164 PortType::I64 => write!(f, "i64"),
165 PortType::F32 => write!(f, "f32"),
166 PortType::U8 => write!(f, "u8"),
167 PortType::I8 => write!(f, "i8"),
168 PortType::U16 => write!(f, "u16"),
169 PortType::I16 => write!(f, "i16"),
170 PortType::F16 => write!(f, "f16"),
171 PortType::U128 => write!(f, "u128"),
172 PortType::I128 => write!(f, "i128"),
173 PortType::Reg128 => write!(f, "reg128"),
174 PortType::RegI8x16 => write!(f, "reg_i8x16"),
175 PortType::RegI16x8 => write!(f, "reg_i16x8"),
176 PortType::RegI32x4 => write!(f, "reg_i32x4"),
177 PortType::RegI64x2 => write!(f, "reg_i64x2"),
178 PortType::RegF16x8 => write!(f, "reg_f16x8"),
179 PortType::RegF32x4 => write!(f, "reg_f32x4"),
180 PortType::RegF64x2 => write!(f, "reg_f64x2"),
181 PortType::Bool => write!(f, "bool"),
182 PortType::Str => write!(f, "String"),
183 PortType::Bytes => write!(f, "bytes"),
184 PortType::Json => write!(f, "json"),
185 PortType::Ext => write!(f, "ext"),
186 PortType::Handle => write!(f, "handle"),
187 PortType::VecF32 => write!(f, "vec_f32"),
188 PortType::VecI32 => write!(f, "vec_i32"),
189 PortType::VecF64 => write!(f, "vec_f64"),
190 PortType::VecI64 => write!(f, "vec_i64"),
191 PortType::VecF16 => write!(f, "vec_f16"),
192 PortType::VecI16 => write!(f, "vec_i16"),
193 PortType::VecI8 => write!(f, "vec_i8"),
194 PortType::Dyn => write!(f, "dyn"),
195 }
196 }
197}
198
199/// The set of numbers a scalar port type can carry, described by the
200/// properties that decide whether one of them holds every value of
201/// another: how many bits the representation has, and how it spends
202/// them.
203///
204/// `Bool` is the one-bit unsigned domain, which is what the type
205/// system already says of it — it widens to `U64` as 1 and 0.
206/// Non-scalar types (`Str`, `Bytes`, `Json`, the vectors, the
207/// register views, `Ext`, `Handle`) have no domain.
208#[derive(Clone, Copy, PartialEq, Eq, Debug)]
209pub enum NumericDomain {
210 /// Non-negative integers in `0 ..= 2^bits - 1`.
211 Unsigned {
212 /// Width of the representation.
213 bits: u32,
214 },
215 /// Two's-complement integers, one of whose bits is the sign.
216 Signed {
217 /// Width of the representation, sign bit included.
218 bits: u32,
219 },
220 /// An IEEE 754 binary float.
221 Float {
222 /// Significand bits, the implicit leading one included: the
223 /// largest integer represented exactly is `2^mantissa`.
224 mantissa: u32,
225 /// Exponent bits, which fix the magnitude range.
226 exponent: u32,
227 },
228}
229
230impl NumericDomain {
231 /// Whether every value of this domain is a value of `other` —
232 /// that is, whether a conversion into `other` is lossless.
233 ///
234 /// Integers fit by counting the bits each spends on magnitude: an
235 /// unsigned domain needs a signed one strictly wider, a signed
236 /// domain never fits an unsigned one. An integer fits a float
237 /// when its magnitude bits fit the float's significand, which is
238 /// why `U64 → F64` does not: 64 magnitude bits do not fit 53, and
239 /// the values above `2^53` round. A float fits a wider float when
240 /// both its significand and its exponent do.
241 pub fn fits_in(self, other: Self) -> bool {
242 use NumericDomain::{Float, Signed, Unsigned};
243 match (self, other) {
244 (Unsigned { bits: a }, Unsigned { bits: b }) => a <= b,
245 (Unsigned { bits: a }, Signed { bits: b }) => a < b,
246 (Signed { bits: a }, Signed { bits: b }) => a <= b,
247 (Signed { .. }, Unsigned { .. }) => false,
248 (Unsigned { bits }, Float { mantissa, .. }) => bits <= mantissa,
249 (Signed { bits }, Float { mantissa, .. }) => bits - 1 <= mantissa,
250 (Float { .. }, Unsigned { .. } | Signed { .. }) => false,
251 (
252 Float {
253 mantissa: m1,
254 exponent: e1,
255 },
256 Float {
257 mantissa: m2,
258 exponent: e2,
259 },
260 ) => m1 <= m2 && e1 <= e2,
261 }
262 }
263}
264
265impl PortType {
266 /// Every port type, once.
267 ///
268 /// A test that must hold for *all* types can walk this rather than
269 /// name the ones its author thought of, so a mapping such as the
270 /// value↔slot writers is checked complete rather than complete for
271 /// the types some program happened to use.
272 ///
273 /// [`Self::every_variant_is_listed`] keeps this honest — it is an
274 /// exhaustive `match`, so adding a variant without adding it here
275 /// fails to compile rather than quietly shrinking every sweep that
276 /// walks this list.
277 pub const ALL: &'static [PortType] = &[
278 PortType::U64,
279 PortType::F64,
280 PortType::U32,
281 PortType::I32,
282 PortType::I64,
283 PortType::F32,
284 PortType::U8,
285 PortType::I8,
286 PortType::U16,
287 PortType::I16,
288 PortType::F16,
289 PortType::U128,
290 PortType::I128,
291 PortType::Reg128,
292 PortType::RegI8x16,
293 PortType::RegI16x8,
294 PortType::RegI32x4,
295 PortType::RegI64x2,
296 PortType::RegF16x8,
297 PortType::RegF32x4,
298 PortType::RegF64x2,
299 PortType::Bool,
300 PortType::Str,
301 PortType::Bytes,
302 PortType::Json,
303 PortType::Ext,
304 PortType::Handle,
305 PortType::VecF32,
306 PortType::VecI32,
307 PortType::VecF64,
308 PortType::VecI64,
309 PortType::VecF16,
310 PortType::VecI16,
311 PortType::VecI8,
312 PortType::Dyn,
313 ];
314
315 /// `true` for every variant, by an exhaustive match: the compiler
316 /// refuses this function when a variant is added, and the test
317 /// beside it checks [`Self::ALL`] carries the one that was added.
318 #[doc(hidden)]
319 pub fn every_variant_is_listed(self) -> bool {
320 match self {
321 PortType::U64
322 | PortType::F64
323 | PortType::U32
324 | PortType::I32
325 | PortType::I64
326 | PortType::F32
327 | PortType::U8
328 | PortType::I8
329 | PortType::U16
330 | PortType::I16
331 | PortType::F16
332 | PortType::U128
333 | PortType::I128
334 | PortType::Reg128
335 | PortType::RegI8x16
336 | PortType::RegI16x8
337 | PortType::RegI32x4
338 | PortType::RegI64x2
339 | PortType::RegF16x8
340 | PortType::RegF32x4
341 | PortType::RegF64x2
342 | PortType::Bool
343 | PortType::Str
344 | PortType::Bytes
345 | PortType::Json
346 | PortType::Ext
347 | PortType::Handle
348 | PortType::VecF32
349 | PortType::VecI32
350 | PortType::VecF64
351 | PortType::VecI64
352 | PortType::VecF16
353 | PortType::VecI16
354 | PortType::VecI8
355 | PortType::Dyn => Self::ALL.contains(&self),
356 }
357 }
358
359 /// The numbers this type can carry, for the types that carry
360 /// numbers. `None` for every other type.
361 ///
362 /// This is what decides whether a conversion between two types
363 /// keeps the value, so that the answer is read off the types
364 /// themselves rather than kept in a list of pairs beside them.
365 pub fn numeric_domain(self) -> Option<NumericDomain> {
366 use NumericDomain::{Float, Signed, Unsigned};
367 Some(match self {
368 Self::Bool => Unsigned { bits: 1 },
369 Self::U8 => Unsigned { bits: 8 },
370 Self::U16 => Unsigned { bits: 16 },
371 Self::U32 => Unsigned { bits: 32 },
372 Self::U64 => Unsigned { bits: 64 },
373 Self::U128 => Unsigned { bits: 128 },
374 Self::I8 => Signed { bits: 8 },
375 Self::I16 => Signed { bits: 16 },
376 Self::I32 => Signed { bits: 32 },
377 Self::I64 => Signed { bits: 64 },
378 Self::I128 => Signed { bits: 128 },
379 Self::F16 => Float {
380 mantissa: 11,
381 exponent: 5,
382 },
383 Self::F32 => Float {
384 mantissa: 24,
385 exponent: 8,
386 },
387 Self::F64 => Float {
388 mantissa: 53,
389 exponent: 11,
390 },
391 Self::Str
392 | Self::Bytes
393 | Self::Json
394 | Self::Ext
395 | Self::Handle
396 | Self::Reg128
397 | Self::RegI8x16
398 | Self::RegI16x8
399 | Self::RegI32x4
400 | Self::RegI64x2
401 | Self::RegF16x8
402 | Self::RegF32x4
403 | Self::RegF64x2
404 | Self::VecF32
405 | Self::VecI32
406 | Self::VecF64
407 | Self::VecI64
408 | Self::VecF16
409 | Self::VecI16
410 | Self::VecI8
411 | Self::Dyn => return None,
412 })
413 }
414
415 /// The canonical lowercase keyword for this `PortType`.
416 ///
417 /// This is the single source of truth for the str↔PortType
418 /// mapping used by every synthesizer and parser in the
419 /// workspace — synthesized polydat source (`extern <name>:
420 /// <keyword>`), the workload-author `{name:<keyword>}` lvalue
421 /// spec, and reverse parsing via [`Self::from_keyword`].
422 /// Inverse of [`Self::from_keyword`].
423 ///
424 /// Exhaustive over the enum — adding a new `PortType` variant
425 /// is a compile error here, forcing the addition of its
426 /// canonical keyword and the round-trip closure to update.
427 pub fn to_keyword(&self) -> &'static str {
428 match self {
429 Self::U64 => "u64",
430 Self::F64 => "f64",
431 Self::U32 => "u32",
432 Self::I32 => "i32",
433 Self::I64 => "i64",
434 Self::F32 => "f32",
435 Self::U8 => "u8",
436 Self::I8 => "i8",
437 Self::U16 => "u16",
438 Self::I16 => "i16",
439 Self::F16 => "f16",
440 Self::U128 => "u128",
441 Self::I128 => "i128",
442 Self::Reg128 => "reg128",
443 Self::RegI8x16 => "reg_i8x16",
444 Self::RegI16x8 => "reg_i16x8",
445 Self::RegI32x4 => "reg_i32x4",
446 Self::RegI64x2 => "reg_i64x2",
447 Self::RegF16x8 => "reg_f16x8",
448 Self::RegF32x4 => "reg_f32x4",
449 Self::RegF64x2 => "reg_f64x2",
450 Self::Bool => "bool",
451 Self::Str => "str",
452 Self::Bytes => "bytes",
453 Self::Json => "json",
454 Self::Ext => "ext",
455 Self::Handle => "handle",
456 Self::VecF32 => "vec_f32",
457 Self::VecI32 => "vec_i32",
458 Self::VecF64 => "vec_f64",
459 Self::VecI64 => "vec_i64",
460 Self::VecF16 => "vec_f16",
461 Self::VecI16 => "vec_i16",
462 Self::VecI8 => "vec_i8",
463 Self::Dyn => "dyn",
464 }
465 }
466
467 /// Parse a polydat type keyword into a `PortType`.
468 ///
469 /// Inverse of [`Self::to_keyword`]: accepts every keyword
470 /// that `to_keyword` emits, plus a small set of legacy aliases
471 /// (`"String"`, `"Json"`, `"Ext"`) that survive in older
472 /// hand-written workload source. Returns `None` for any
473 /// unrecognized keyword so callers can surface a loud
474 /// diagnostic rather than silently coercing to a default.
475 ///
476 /// Used by the DSL `extern <name>: <keyword>` parser
477 /// (`polydat-core/src/dsl/compile.rs`). Round-trips cleanly with
478 /// any source `to_keyword` emits.
479 pub fn from_keyword(name: &str) -> Option<Self> {
480 match name {
481 "u64" => Some(Self::U64),
482 "f64" => Some(Self::F64),
483 "u32" => Some(Self::U32),
484 "i32" => Some(Self::I32),
485 "i64" => Some(Self::I64),
486 "f32" => Some(Self::F32),
487 "u8" => Some(Self::U8),
488 "i8" => Some(Self::I8),
489 "u16" => Some(Self::U16),
490 "i16" => Some(Self::I16),
491 "f16" => Some(Self::F16),
492 "u128" => Some(Self::U128),
493 "i128" => Some(Self::I128),
494 "reg128" => Some(Self::Reg128),
495 "reg_i8x16" => Some(Self::RegI8x16),
496 "reg_i16x8" => Some(Self::RegI16x8),
497 "reg_i32x4" => Some(Self::RegI32x4),
498 "reg_i64x2" => Some(Self::RegI64x2),
499 "reg_f16x8" => Some(Self::RegF16x8),
500 "reg_f32x4" => Some(Self::RegF32x4),
501 "reg_f64x2" => Some(Self::RegF64x2),
502 "bool" => Some(Self::Bool),
503 "str" | "Str" | "String" => Some(Self::Str),
504 "bytes" => Some(Self::Bytes),
505 "json" | "Json" => Some(Self::Json),
506 "ext" | "Ext" => Some(Self::Ext),
507 "handle" => Some(Self::Handle),
508 "vec_f32" => Some(Self::VecF32),
509 "vec_i32" => Some(Self::VecI32),
510 "vec_f64" => Some(Self::VecF64),
511 "vec_i64" => Some(Self::VecI64),
512 "vec_f16" => Some(Self::VecF16),
513 "vec_i16" => Some(Self::VecI16),
514 "vec_i8" => Some(Self::VecI8),
515 "dyn" => Some(Self::Dyn),
516 _ => None,
517 }
518 }
519
520 /// Workload-author-facing parser for the `{name:<keyword>}`
521 /// lvalue-spec surface. Strict subset of [`Self::from_keyword`]
522 /// — `handle` and `ext` are rejected because they're
523 /// internal-only types a workload author should never assert.
524 ///
525 /// Returns `None` for any unrecognized name; the caller
526 /// surfaces the unknown spec as a workload-shape diagnostic.
527 pub fn from_workload_name(name: &str) -> Option<Self> {
528 match Self::from_keyword(name)? {
529 Self::Handle | Self::Ext => None,
530 pt => Some(pt),
531 }
532 }
533}