1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
//! # rssn-advanced
//!
//! **rssn-advanced** is the symbolic computation engine of the
//! [rssn](https://github.com/Apich-Organization/rssn) project. It provides a
//! hash-consed expression DAG, a [Cranelift](https://cranelift.dev/)-backed JIT
//! compiler, heuristic and e-graph simplification, multi-architecture inline-asm
//! presets, and a flat `extern "C"` API for embedding in C/C++ and other languages.
//!
//! ---
//!
//! ## Architecture
//!
//! | Module | Role |
//! |--------|------|
//! | [`dag`] | Hash-consed expression DAG — the canonical, deduplicated store for all symbolic nodes |
//! | [`ast`] | Lightweight local tree projection of a DAG subgraph via relative `i32` pointers |
//! | [`parser`] | `nom`-based infix parser: `"x^2 + 2*x + 1"` → DAG root |
//! | [`jit`] *(feature: `cranelift-jit`)* | Cranelift JIT; emits scalar `f64` closures and 2-row ILP batch functions |
//! | [`heuristic`] | Configurable greedy/beam simplifier with a pluggable [`heuristic::rule_registry::RuleRegistry`] |
//! | [`egraph`] | Lightweight equality saturation over the DAG (no `egg` dependency) |
//! | [`custom`] | Unified custom-operator system — one [`custom::descriptor::CustomOpDescriptor`] wires into JIT + simplifier + e-graph |
//! | [`simd`] | Slice-level batch arithmetic using the inline-asm presets |
//! | [`asm_presets`] | Hand-written `f64×2` / `f64×4` kernels for `x86_64` (SSE2/AVX2/AES-NI), `AArch64` (NEON/crypto), riscv64 (RVV/Zkn) |
//! | [`ffi`] | Flat `extern "C"` surface generated by `cbindgen`; includes a fiber-backed async bridge |
//! | [`parallel`] | Fiber-based parallel simplification via the `dtact` runtime |
//! | [`storage`] | Disk-backed DAG spillover and a frequency-based hot-node cache |
//! | [`error`] | Cold-path error types and the `rssn_error!` macro |
//!
//! ---
//!
//! ## Quick start
//!
//! ### Parse and evaluate
//!
//! ```rust
//! use rssn_advanced::dag::builder::DagBuilder;
//! use rssn_advanced::parser::expr::parse_expression;
//!
//! let mut builder = DagBuilder::new();
//! // parse_expression(input, &mut builder) -> Result<DagNodeId, ParseError>
//! let root = parse_expression("x^2 + 2*x + 1", &mut builder).unwrap();
//! let _ = root;
//! ```
//!
//! ### JIT-compile and bulk-evaluate
//!
//! ```rust
//! # // cfg guard: skip when cranelift-jit feature is absent
//! # #[cfg(not(feature = "cranelift-jit"))] fn main() {}
//! # #[cfg(feature = "cranelift-jit")] fn main() {
//! use rssn_advanced::dag::builder::DagBuilder;
//! use rssn_advanced::parser::expr::parse_expression;
//! use rssn_advanced::ast::convert::dag_to_ast;
//! use rssn_advanced::jit::compiler::JitCompiler;
//!
//! let mut builder = DagBuilder::new();
//! let root = parse_expression("x^2 + 2*x + 1", &mut builder).unwrap();
//!
//! let mut compiler = JitCompiler::try_new().unwrap();
//! let ast = dag_to_ast(builder.arena(), root);
//! let f = compiler.compile(&ast).unwrap();
//!
//! // CompiledExprFn = extern "C" fn(*const f64) -> f64
//! assert_eq!(f([3.0_f64].as_ptr()), 16.0); // (3+1)^2
//!
//! // 2-row ILP batch path; returns None if expression is not vectorizable
//! let _batch = compiler.compile_batch_f64x2(&ast).unwrap();
//! # }
//! ```
//!
//! ### Register a custom operator
//!
//! ```rust
//! # // cfg guard: skip when cranelift-jit feature is absent
//! # #[cfg(not(feature = "cranelift-jit"))] fn main() {}
//! # #[cfg(feature = "cranelift-jit")] fn main() {
//! use std::sync::Arc;
//! use rssn_advanced::dag::builder::DagBuilder;
//! use rssn_advanced::custom::descriptor::{CustomOpDescriptor, CustomOpRegistry, EvalFn};
//! use rssn_advanced::egraph::egraph::{EGraph, EGraphConfig};
//! use rssn_advanced::jit::compiler::JitCompiler;
//!
//! extern "C" fn my_relu(x: f64) -> f64 { x.max(0.0) }
//!
//! let mut builder = DagBuilder::new();
//! // intern_function returns the FnId used to identify this operator
//! let fn_id = builder.intern_function("relu");
//!
//! let desc = CustomOpDescriptor::builder(fn_id, "relu", EvalFn::Arity1(my_relu))
//! .vectorizable() // safe to duplicate in the 2-row ILP batch path
//! .cost(1.0)
//! .build();
//!
//! let mut reg = CustomOpRegistry::new();
//! reg.register(desc).unwrap();
//! let reg = Arc::new(reg);
//!
//! // Wire into the JIT (registers the eval_fn pointer)
//! let mut compiler = JitCompiler::try_new().unwrap();
//! reg.apply_to_jit(&mut compiler);
//!
//! // Wire into the heuristic simplifier
//! let _rule_reg = reg.build_rule_registry();
//!
//! // Wire into the e-graph saturation engine
//! {
//! let mut egraph = EGraph::new(&mut builder, EGraphConfig::default());
//! reg.apply_to_egraph(&mut egraph);
//! }
//! # }
//! ```
//!
//! ---
//!
//! ## Performance
//!
//! Benchmark: bulk evaluation of N = 1,000,000 rows, best of 5 runs.
//! Hardware: Dell Latitude 5400, Intel i7-8665U @ 1.90 GHz (laptop-class, 16 MB L3),
//! Fedora Linux 44, kernel 6.19. Compared against hand-optimised `NumPy` 1.x
//! (BLAS-linked, SIMD-enabled).
//!
//! | Expression | JIT bulk | JIT batch | `NumPy` | Speedup (bulk / batch) |
//! |------------|--------:|----------:|------:|-----------------------:|
//! | `x + y + 10.0` (trivial baseline) | 1.87 ns | 1.13 ns | 2.82 ns | **1.5× / 2.5×** |
//! | `(x-y)^4` — degree-4 polynomial | 2.73 ns | 1.28 ns | 19.21 ns | **7× / 15×** |
//! | cubic surface (10 terms, 3 vars) | 3.73 ns | 1.77 ns | 75.75 ns | **20× / 43×** |
//! | rational expression w/ CSE | 2.53 ns | 1.27 ns | 15.91 ns | **6× / 13×** |
//!
//! **Why the gap grows with complexity:** `NumPy` allocates one `f64[N]` scratch array
//! per arithmetic operation. A 10-term expression at N = 10⁶ creates ~200 MB of
//! temporaries that thrash L3 cache. The JIT keeps every intermediate value in a
//! CPU register across the full expression, paying exactly one memory round-trip
//! per input column.
//!
//! **Honest caveats:**
//! - Numbers are from a single laptop; server CPUs with larger L3 caches will show a
//! smaller gap for simple expressions.
//! - The batch path unrolls 2 rows for ILP but does **not** emit wide-vector AVX/AVX-512
//! instructions; the [`asm_presets`] / [`simd`] paths are faster for fixed-width kernels.
//! - Cranelift's code quality is good but not hand-tuned; `gcc -O3` / LLVM can sometimes
//! produce tighter loops for simple expressions.
//!
//! ---
//!
//! ## Feature flags
//!
//! | Flag | Default | Effect |
//! |------|:-------:|--------|
//! | `cranelift-jit` | **on** | Enables the [`jit`] module, JIT compilation, and the batch-evaluate path |
//!
//! Disable with `--no-default-features` for embedded or size-constrained targets.
//! The parser, DAG, heuristic simplifier, e-graph, and SIMD presets are all
//! available without the JIT feature.
//!
//! ---
//!
//! ## Known limitations
//!
//! - **Parser scope:** handles `+`, `-`, `*`, `/`, `^`, `%`, unary negation, and
//! registered named functions. Transcendentals (`sin`, `exp`, …) must be
//! registered as custom operators.
//! - **Single-threaded JIT context:** compilation requests from multiple threads
//! serialise on a global `Mutex`.
//! - **No GPU or BLAS integration.**
//! - **`asm_presets` on Windows:** some NEON / RVV paths are x86/AArch64/riscv64
//! specific; the scalar fallback is always available.
//! - **e-graph extractor is greedy:** the current cost-minimising extractor uses a
//! greedy bottom-up pass; optimal extraction is NP-hard and not yet implemented.
// -------------------------------------------------------------------------
// Rust Lint Configuration: rssn-advanced
// -------------------------------------------------------------------------
// -------------------------------------------------------------------------
// LEVEL 1: CRITICAL ERRORS (Deny)
// -------------------------------------------------------------------------
// -------------------------------------------------------------------------
// LEVEL 2: STYLE WARNINGS (Warn)
// -------------------------------------------------------------------------
// -------------------------------------------------------------------------
// LEVEL 3: ALLOW/IGNORABLE (Allow)
// -------------------------------------------------------------------------
// =========================================================================
// Module Declarations
// =========================================================================
/// Inline-assembly preset suite (AVX2 / AES-NI / scalar fallback).
///
/// Each preset is a 4-lane `f64` kernel emitted via `core::arch::asm!`
/// — no reliance on auto-vectorization. Used by both `simd` (slice
/// wrappers) and indirectly by `jit` (peephole patterns). Lives at the
/// crate root so neither subsystem has to feature-gate the other.
/// Cold-path error infrastructure.
///
/// Hosts the `rssn_error!` macro and the module-level error enums.
/// Replaces the previous ad-hoc `unwrap()` / `expect()` / `assert_eq!`
/// pattern with `#[cold] #[inline(never)]` constructors so that error
/// handling stays off the hot path.
/// Zero-copy borrowed containers and `bincode-next` `BorrowDecode` glue.
///
/// `BorrowedSlice` / `BorrowedArena` decode by `take_bytes` directly off
/// the input buffer, and `MmapBuffer` provides file-backed storage with
/// 8-byte aligned bytes for safe reinterpretation as `&[T: Pod]`.
/// Fiber-based task runtime built on `dtact`.
///
/// Replaces `std::thread::spawn` with lightweight fibers throughout the
/// crate. `parallel_for_each` is the workhorse used by `parallel::solver`
/// and `ffi::async_bridge`.
/// Allocator-light shared utilities (worklist traversals, helpers).
/// Global DAG (Directed Acyclic Graph) storage for symbolic expressions.
///
/// Provides hash-consed, structurally-shared storage for all symbol nodes.
/// The DAG serves as the canonical representation — all expression data
/// ultimately lives here, deduplicated via structural hashing.
/// Local AST (Abstract Syntax Tree) projection for computation.
///
/// Projects a subgraph of the global DAG into a stack-local tree using
/// relative pointers (`i32` / `i64`). This provides an algorithm-friendly
/// tree view without duplicating the underlying metadata.
/// Symbolic expression parser.
///
/// Parses mathematical expressions (e.g. `"x^2 + 2*x + 1"`) into the
/// global DAG using `nom`-based combinators with precedence climbing.
/// JIT compilation pipeline for symbolic derivation rules.
///
/// Compiles algebraic rewrite rules (add, mul, div, custom) into native
/// machine code via Cranelift. Gated behind the `jit` feature (alias
/// `cranelift-jit` kept for backward compatibility).
/// Parallel computation engine.
///
/// Exploits commutativity to split expressions into independent chunks
/// for async parallel simplification, with staged global merging.
/// Streaming storage and dynamic caching.
///
/// Provides disk-backed spillover for large DAGs and a dynamic hotspot
/// table that tracks intermediate result frequency for auto-eviction.
/// Heuristic search toolbox for NP-hard pattern matching.
///
/// A configurable "knob-based" engine that allows controlled approximate
/// simplification when exact methods hit symbol explosion.
/// Lightweight E-graph for equality saturation.
///
/// Implements equality saturation over the hash-consed DAG without importing
/// the heavy `egg` crate. Uses a path-compressed union-find directly over
/// [`dag::node::DagNodeId`] values, and extracts the minimum-cost
/// representative after each saturation run.
/// SIMD-optimized preset function library.
///
/// Hardware-accelerated batch operations (arithmetic, hashing) with
/// runtime feature detection and scalar fallback.
/// Unified custom-operator extension system.
///
/// A single [`custom::descriptor::CustomOpDescriptor`] bundles every
/// pipeline-facing property of a user-defined operator: JIT eval function,
/// batch-vectorisability flag, heuristic simplification rules, and e-graph
/// rewrite rules. Register descriptors into a
/// [`custom::descriptor::CustomOpRegistry`], then call the three integration
/// methods to wire the operator into all pipeline stages simultaneously:
///
/// - `registry.apply_to_jit(&mut compiler)` — feeds `eval_fn` pointers to
/// the JIT and enables the 2-row ILP batch path for vectorizable operators.
/// - `registry.build_rule_registry()` — produces a [`heuristic::rule_registry::RuleRegistry`]
/// from all attached [`custom::descriptor::SimplifyRule`]s.
/// - `registry.apply_to_egraph(&mut egraph)` — injects all
/// [`custom::descriptor::EGraphRule`]s into an [`egraph::egraph::EGraph`].
///
/// See the [crate-level "Register a custom operator" example](crate) for a
/// runnable end-to-end demonstration.
///
/// C/C++ callers use the `rssn_custom_op_*` family in [`ffi::c_api`].
/// C/C++ Foreign Function Interface.
///
/// Exposes a flat, `extern "C"` API surface via `cbindgen`-compatible
/// types and opaque handles. Includes an async bridge for multi-language
/// integration.