Skip to main content

vyre_libs/
lib.rs

1//! # vyre-libs  -  Category A composition ecosystem
2//!
3//! `vyre-libs` is the library layer that sits ON TOP of `vyre-ops`.
4//!
5//! Almost every function is a **pure Category A composition**: it returns a
6//! [`vyre::Program`] built entirely from existing vyre IR primitives. The
7//! sole exception is the `math::atomic` family, which are **Category B**
8//! (`Category::Intrinsic`) because they require the backend to own the
9//! `Expr::Atomic` target builder emitter arm (F-IR-35).
10//!
11//! This is the ML/DSP/cryptographic ecosystem layer. Examples:
12//!
13//! ```ignore
14//! use vyre_libs::nn::linear;
15//! let program = linear(/* input_buf */ "x", /* weights */ "w", /* bias */ "b");
16//! // `program` is a standard vyre::Program you dispatch against any backend.
17//! ```
18//!
19//! ## Why a single `vyre-libs` crate, not five?
20//!
21//! The initial proposal suggested `vyre-nn`, `vyre-math`, `vyre-match`,
22//! `vyre-crypto`, `vyre-graph-stitch` as five standalone crates. That
23//! is the right endpoint  -  each becomes its own crates.io identity
24//! with its own community  -  but the migration cost at 0.6 is wrong.
25//! This crate starts as one, with public modules for each domain; when
26//! a module has its own consumer base + maturity, it promotes to a
27//! dedicated crate without breaking downstream code (the
28//! `vyre-libs::nn` path moves to `vyre-nn::` via a re-export shim).
29//!
30//! `vyre-graph-stitch` was deliberately omitted  -  "logical linker for
31//! emitted graphs" is a `vyre-foundation` concern (IR composition),
32//! not a library crate.
33//!
34//! ## Region wrapping
35//!
36//! Every public composition wraps its body in a
37//! [`vyre::ir::Node::Region`] with a stable generator name. The
38//! optimizer treats Regions as atomic by default (preserves
39//! debuggability + source-mapping); explicit inline passes can unroll
40//! them. This is LLVM's function-vs-always-inline split at IR level.
41//!
42//! ## Feature flags
43//!
44//! Each domain lives behind a feature flag so minimal consumers pay
45//! for only what they use:
46//!
47//! - `math` (default)  -  linear algebra, scans, broadcasts
48//! - `nn` (default, implies `math`)  -  neural-net primitives
49//! - `matching` (default)  -  regex, DFA, substring, multi-pattern
50//! - `crypto` (default)  -  hashing, MAC, checksums
51//!
52//! Turn defaults off with `default-features = false` and cherry-pick
53//! what you need.
54
55// P1.11 (closed): `OpEntry` is now POD over `&'static str` + `fn(...)`,
56// so stdlib auto-traits give us `Send + Sync` for free. No `unsafe`
57// anywhere in vyre-libs  -  `forbid` catches any future regression.
58#![forbid(unsafe_code)]
59#![deny(missing_docs)]
60#![allow(
61    clippy::too_many_arguments,
62    clippy::needless_range_loop,
63    clippy::double_must_use,
64    clippy::items_after_test_module,
65    clippy::assertions_on_constants,
66    clippy::overly_complex_bool_expr,
67    clippy::filter_map_bool_then
68)]
69// P3.3 nested-dialect reshape: each sub-dialect's single op file
70// shares the sub-dialect's module name (e.g. `math/broadcast/broadcast.rs`).
71// That's the intended shape for community packs that add second/
72// third ops to the same sub-dialect later; the lint would fight
73// the architectural decision.
74#![allow(clippy::module_inception)]
75
76/// Build a trap-only program for registry fixtures or infallible composition wrappers.
77#[allow(dead_code)]
78pub(crate) fn invalid_program(
79    op_id: &'static str,
80    message: impl Into<String>,
81) -> vyre::ir::Program {
82    let message = message.into();
83    vyre::ir::Program::wrapped(
84        Vec::new(),
85        [1, 1, 1],
86        vec![region::wrap_anonymous(
87            op_id,
88            vec![vyre::ir::Node::trap(vyre::ir::Expr::u32(0), message)],
89        )],
90    )
91}
92
93/// Region builder  -  the shared helper every composition routes through.
94/// Library component.
95/// Library component.
96pub mod region;
97
98/// Domain-neutral byte-range ordering predicates. Previously lived inside
99/// `vyre-libs::security::topology`; hoisted out so non-security callers
100/// (a downstream analyzer's `Before`/`After` predicates, future dialects) do not pull the
101/// security dialect through the import graph. See CRITIQUE_VISION_ALIGNMENT_2026-04-23 V5.
102/// Library component.
103/// Library component.
104pub mod range_ordering;
105
106/// `TensorRef`  -  typed buffer-argument wrapper used by every Cat-A
107/// composition for dtype + shape + name-uniqueness validation.
108/// Library component.
109/// Library component.
110pub mod tensor_ref;
111
112/// Library component.
113/// Library component.
114pub use tensor_ref::{check_dtype, check_shape, check_unique_names, TensorRef, TensorRefError};
115
116/// Shared builder helpers every Cat-A composition reuses.
117/// Library component.
118/// Library component.
119pub mod builder;
120mod substrate_catalog;
121
122/// Library component.
123/// Library component.
124pub use builder::{check_tensors, BuildOptions};
125
126/// Library component.
127/// Library component.
128pub mod buffer_names;
129
130/// `ProgramDescriptor`  -  introspection surface for Cat-A Programs.
131/// Library component.
132/// Library component.
133pub mod descriptor;
134
135/// Library component.
136/// Library component.
137pub use descriptor::{BufferDescriptor, ProgramDescriptor};
138
139#[cfg(feature = "math-linalg")]
140pub use math::{matmul_bias_tiled, matmul_tiled, MatmulBias, MatmulBiasTiled, MatmulTiled};
141
142/// Universal op harness  -  auto-testing infrastructure for every composition.
143///
144/// Each composition registers an `OpEntry` via
145/// `inventory::submit!`. The harness discovers all entries at test
146/// time and runs validation, wire round-trip, CSE stability, and
147/// reference interpreter tests automatically.
148///
149/// Hidden from docs.rs  -  external consumers of vyre-libs don't need
150/// this module's surface; it exists for internal test infrastructure
151/// only. Kept `pub` so `inventory::submit!` can reference `OpEntry`
152/// from per-op source files at crate-root scope.
153#[doc(hidden)]
154/// Library component.
155/// Library component.
156pub mod harness;
157
158/// Math dialect  -  linear algebra, scans, broadcasting.
159#[cfg(any(
160    feature = "math-linalg",
161    feature = "math-scan",
162    feature = "math-broadcast"
163))]
164/// Library component.
165/// Library component.
166pub mod math;
167
168/// Logical dialect  -  element-wise boolean composition.
169#[cfg(feature = "logical")]
170/// Library component.
171/// Library component.
172pub mod logical;
173
174/// Neural-network dialect  -  activation, normalization, attention, linear.
175#[cfg(any(
176    feature = "nn-activation",
177    feature = "nn-linear",
178    feature = "nn-norm",
179    feature = "nn-attention"
180))]
181/// Library component.
182/// Library component.
183pub mod nn;
184
185/// Pattern-scanning dialect  -  substring, DFA, Aho-Corasick, rule
186/// dispatch, secfinding generation. Renamed from `matching` per
187/// ROADMAP T032 (SEPARATION_AUDIT S7)  -  "scan" reflects the actual
188/// semantic surface (not just substring matching). The original
189/// `matching` name is kept as a deprecated alias for backwards
190/// compatibility.
191#[cfg(any(
192    feature = "matching-substring",
193    feature = "matching-dfa",
194    feature = "matching-nfa"
195))]
196pub mod scan;
197
198/// Backwards-compat alias for [`scan`]. New code should use
199/// `vyre_libs::scan::*`. The alias will be removed in a future
200/// breaking release; until then `vyre_libs::scan::Foo` and
201/// `vyre_libs::scan::Foo` resolve to the same item.
202#[cfg(any(
203    feature = "matching-substring",
204    feature = "matching-dfa",
205    feature = "matching-nfa"
206))]
207#[deprecated(
208    since = "0.4.1",
209    note = "use `vyre_libs::scan` instead  -  the `matching` name is kept as a transition alias only"
210)]
211pub mod matching;
212
213/// Decode / decompression compositions  -  base64, hex, DEFLATE (stored),
214/// more coming. Pairs with `vyre-libs::matching::dfa` in the fused
215/// decode→scan pipeline (Innovation I.1).
216#[cfg(feature = "decode")]
217/// Library component.
218/// Library component.
219pub mod decode;
220
221/// Hash / checksum dialect  -  FNV-1a-32, FNV-1a-64, CRC-32, Adler-32,
222/// BLAKE3 compression. Consolidated from the former `vyre-libs::crypto`
223/// module per Migration 3. Every op lives here as a pure Cat-A
224/// composition over existing IR primitives (no dedicated target builder emitter
225/// arm required, per the intrinsic-vs-library rule).
226#[cfg(feature = "hash")]
227/// Library component.
228/// Library component.
229pub mod hash;
230
231/// Text-processing compositions for the GPU C parser pipeline
232/// (Phase L1+): byte classification, UTF-8 validation, line index.
233/// Library component.
234/// Library component.
235pub mod text;
236
237/// Representation sub-dialect: bit-packing and unpacking.
238/// Library component.
239/// Library component.
240pub mod representation;
241
242/// GPU parser infrastructure (Phase L3+): bracket matching, DFA
243/// lexer driver, LR(1) table walker. Grammar tables are generated
244/// host-side by `downstream analyzer-grammar-gen` and loaded as ReadOnly buffers.
245/// Library component.
246/// Library component.
247pub mod parsing;
248
249/// Front-end-agnostic borrow-check engine: the neutral `BorrowFacts` IR and the
250/// dataflow analysis over it. Producers (the Rust front-end now, a rustc adapter
251/// later) lower to `BorrowFacts`; the engine never depends on any front-end,
252/// which is what lets the borrow checker eventually run standalone.
253pub mod borrowck;
254
255/// Packed AST walks (`ast_walk_*` catalog ops).
256/// Library component.
257/// Library component.
258pub mod graph;
259
260/// GPU-native compiler middle-end (CFG and ELF emission helpers) for the C pipeline.
261#[cfg(feature = "c-parser")]
262/// Library component.
263/// Library component.
264pub mod compiler;
265
266#[cfg(feature = "c-parser")]
267pub use compiler::{
268    cfg::c11_build_cfg_and_gotos, object_writer::opt_lower_elf,
269    regalloc::opt_x86_64_register_allocation, stack_layout::opt_stack_layout_generation,
270    types_layout::c11_compute_alignments,
271};
272
273/// Security / taint compositions for static program analysis.
274/// Every op registers via `inventory::submit!` and lives under a
275/// stable op id. The implementations compose graph and dataflow
276/// primitives so downstream analyzers lower to one production GPU-facing
277/// surface.
278#[cfg(feature = "security")]
279pub mod security;
280
281/// GPU-accelerated visual effects  -  blur, shadow, filter chain,
282/// gradient, compositing, and glass material. Tier 3 compositions
283/// over `math::conv1d` (Tier 2.5) and bare IR expressions. The
284/// Molten web engine's visual effect substrate.
285#[cfg(feature = "visual")]
286/// Library component.
287/// Library component.
288pub mod visual;
289
290/// Compatibility facade for GPU dataflow compositions.
291/// This path remains for older `vyre-libs::dataflow::*` consumers and must
292/// not grow a parallel dataflow implementation tree.
293pub mod dataflow;
294
295mod primitive_catalog;
296
297pub use dataflow::{Soundness, SoundnessTagged};
298
299// vyre-libs::hardware removed (audit 2026-04-21 BLOCKER-1/6).
300// Canonical Cat-C intrinsics live exclusively in the `vyre-intrinsics`
301// crate; library compositions of atomic / clamp / lzcnt / tzcnt ops
302// live in `vyre-libs::math::*` (which uses `Expr::Atomic`, `Expr::min`,
303// `Expr::max`, `Expr::popcount` directly per library-tiers.md).
304//
305// vyre-libs::crypto removed (audit 2026-04-21 BLOCKER-3). Deprecated
306// shim deleted in favor of the canonical path at `vyre-libs::hash`.
307//
308// vyre-libs::composite removed (audit 2026-04-21 BLOCKER-3). The three
309// hash ops that lived there (adler32, crc32, fnv1a64) are canonical at
310// `vyre-libs::hash::*`.
311
312/// Rule-engine dialect  -  typed conditions, formulas, and program builder used
313/// by detection rule compilers.
314#[cfg(feature = "rule")]
315/// Library component.
316/// Library component.
317pub mod rule;
318
319/// Vector-widened string interning. CHD perfect hash
320/// over Tier-B label families  -  60k+ function-name strings reduce
321/// to one subgroup-shuffle + one DRAM load on the GPU.
322#[cfg(feature = "intern")]
323/// Library component.
324/// Library component.
325pub mod intern;
326
327/// Operation contract presets used by catalog entries.
328/// Library component.
329/// Library component.
330pub mod contracts;
331/// Type-signature constants shared across op definitions.
332/// Library component.
333/// Library component.
334pub mod signatures;
335/// Re-exports every type-signature constant at the crate root for convenient access.
336/// Library component.
337/// Library component.
338pub use signatures::{
339    BOOL_OUTPUTS, BYTES_TO_BYTES_INPUTS, BYTES_TO_BYTES_OUTPUTS, BYTES_TO_U32_OUTPUTS,
340    F32_F32_F32_INPUTS, F32_F32_INPUTS, F32_INPUTS, F32_OUTPUTS, I32_OUTPUTS, U32_INPUTS,
341    U32_OUTPUTS, U32_U32_INPUTS,
342};
343/// Pre-sweep shader snapshot migration entries, collected via inventory.
344/// `pub(crate)` because the registry is an internal pre-sweep tool  -
345/// downstream dialects do not submit through this path.
346pub(crate) mod test_migration;
347/// Test support components for vyre-libs.
348pub mod test_support;
349
350/// Driver-tier observability re-export so vyre-libs consumers can
351/// snapshot substrate counters + decision histograms without taking a
352/// direct vyre-driver dependency.
353pub mod observability {
354    pub use vyre_driver::observability::{BackendObservabilityProvider, DriverObservability};
355}
356
357/// Re-export the small set of vyre types every composition function
358/// returns. Consumers can `use vyre_libs::prelude::*` and get the API
359/// plus the types it returns.
360pub mod prelude {
361    pub use vyre::ir::{BufferAccess, BufferDecl, DataType, Expr, Node, Program};
362    pub use vyre::{BackendError, DispatchConfig};
363    pub use vyre_foundation::ir::model::expr::GeneratorRef;
364
365    // P2.1 / P2.2: the typed-tensor API + shared builder primitives.
366    // Every Cat-A op ships with a TensorRef-accepting builder; the
367    // prelude exposes the full construction surface so `use
368    // vyre_libs::prelude::*;` is enough to author a new Cat-A op.
369    pub use crate::builder::{check_tensors, BuildOptions};
370    pub use crate::tensor_ref::{
371        check_dtype, check_shape, check_unique_names, TensorRef, TensorRefError,
372    };
373
374    // Region wrapper  -  every composition emits its body through this.
375    pub use crate::region::{wrap, wrap_anonymous, wrap_child};
376
377    // Built-in Cat-A builders (gated on the relevant feature flags so
378    // minimum-footprint consumers don't pay for the ones they skip).
379    #[cfg(feature = "decode")]
380    pub use crate::decode::{base64_decode, hex_decode, inflate, ziftsieve_gpu};
381    #[cfg(feature = "crypto-blake3")]
382    pub use crate::hash::blake3_compress;
383    #[cfg(feature = "crypto-fnv")]
384    pub use crate::hash::fnv1a32;
385    #[cfg(feature = "logical")]
386    pub use crate::logical::{and, nand, nor, or, xor};
387    #[cfg(feature = "math-broadcast")]
388    pub use crate::math::broadcast;
389    #[cfg(feature = "math-scan")]
390    pub use crate::math::scan_prefix_sum;
391    #[cfg(feature = "math-algebra")]
392    pub use crate::math::{
393        bool_semiring_matmul, lattice_join, lattice_meet, semiring_min_plus_mul, sketch_mix,
394        try_bool_semiring_matmul, try_lattice_join, try_lattice_meet, try_semiring_min_plus_mul,
395        try_sketch_mix,
396    };
397    #[cfg(feature = "math-linalg")]
398    pub use crate::math::{dot, matmul, matmul_tiled, Matmul, MatmulTiled};
399    #[cfg(feature = "math-succinct")]
400    pub use crate::math::{rank1_query, rank1_superblocks, try_rank1_query, try_rank1_superblocks};
401    #[cfg(feature = "nn-linear")]
402    pub use crate::nn::linear;
403    #[cfg(feature = "nn-activation")]
404    pub use crate::nn::relu;
405    #[cfg(feature = "nn-attention")]
406    pub use crate::nn::{attention, softmax, Attention, Softmax};
407    #[cfg(feature = "nn-norm")]
408    pub use crate::nn::{layer_norm, LayerNorm};
409    #[cfg(feature = "matching-substring")]
410    pub use crate::scan::substring_search;
411    #[cfg(feature = "matching-dfa")]
412    pub use crate::scan::{aho_corasick, dfa_compile, CompiledDfa, DfaCompileError};
413}