oarust 0.12.0

GPU-first semantic computing engine written in Rust
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
//! Device-owned counters of fusion decisions and decline reasons.
//!
//! Every fusion layer records one outcome at its existing decision point;
//! the census never changes a routing decision. Elementwise-pass node
//! outcomes are exclusive: each node offered to the elementwise pass lands in
//! exactly one node bucket, so `examined_node_count` equals their sum. JIT
//! counters count admission requests, which can exceed fused regions because
//! a rejected longer segment retries as a shorter one. Counters use relaxed
//! atomics; a snapshot is a diagnostic, not a synchronization point.

use std::{
	fmt,
	sync::atomic::{AtomicU64, Ordering},
};

use super::{
	region::RegionDecline,
	rules::{MergeRule, PairRule},
};
use crate::runtime::FusionStats;

const PAIR_RULE_COUNT: usize = PairRule::ALL.len();
const PAIR_OUTCOME_COUNT: usize = 2 + RegionDecline::ALL.len();

/// One proposed incumbent pair, counted only after its matcher succeeds.
/// A shape/ABI mismatch, disabled rule, or preempted lower-priority rule is
/// not a proposal and is never misreported as a resource-legality decline.
#[derive(Clone, Copy)]
pub(super) enum PairOutcome {
	Applied,
	LegacyGuard,
	Declined(RegionDecline),
}

impl PairOutcome {
	const fn index(self) -> usize {
		match self {
			Self::Applied => 0,
			Self::LegacyGuard => 1,
			Self::Declined(decline) => 2 + decline as usize,
		}
	}

	const fn name(self) -> &'static str {
		match self {
			Self::Applied => "applied",
			Self::LegacyGuard => "legacy_guard",
			Self::Declined(decline) => decline.name(),
		}
	}
}

fn pair_outcomes() -> impl Iterator<Item = PairOutcome> {
	[PairOutcome::Applied, PairOutcome::LegacyGuard]
		.into_iter()
		.chain(RegionDecline::ALL.into_iter().map(PairOutcome::Declined))
}

/// Private diagnostic only: public FusionStats stays schema-owned. A
/// snapshot of relaxed counters is not a concurrent completion barrier.
pub(in crate::runtime) struct PairRuleStats {
	rule: PairRule,
	counters: [u64; PAIR_OUTCOME_COUNT],
}

fn rule_stats(
	formatter: &mut fmt::Formatter<'_>,
	name: &'static str,
	counters: &[u64; PAIR_OUTCOME_COUNT],
) -> fmt::Result {
	let proposals = counters
		.iter()
		.fold(0u64, |total, count| total.wrapping_add(*count));
	write!(formatter, "rule={name} proposals={proposals}")?;
	for outcome in pair_outcomes() {
		write!(
			formatter,
			" {}={}",
			outcome.name(),
			counters[outcome.index()]
		)?;
	}
	Ok(())
}

impl fmt::Display for PairRuleStats {
	fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
		rule_stats(formatter, self.rule.name(), &self.counters)
	}
}

/// One contiguous-merge rule's proposals, same outcome vocabulary as pairs.
pub(in crate::runtime) struct MergeRuleStats {
	rule: MergeRule,
	counters: [u64; PAIR_OUTCOME_COUNT],
}

impl fmt::Display for MergeRuleStats {
	fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
		rule_stats(formatter, self.rule.name(), &self.counters)
	}
}

/// Exclusive outcome of one node offered to the elementwise pass.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(in crate::runtime) enum NodeOutcome {
	/// Elementwise fusion is disabled or its tape kernel is unavailable.
	Disabled,
	/// The node is not in the fusible elementwise vocabulary.
	NotElementwise,
	/// The node is elementwise but excluded by `OA_DISABLE_RL_FUSION`.
	PolicyExcluded,
	/// The write is read before its producer or written by another node.
	Hazard,
	/// No admissible elementwise neighbour remains.
	Isolated,
	/// Adjacent elementwise neighbours have a different element count.
	ExtentBoundary,
	/// A gather or broadcast-mapped read consumes an in-region producer.
	CrossInvocation,
	/// The candidate failed program construction or validation.
	ProgramRejected,
	/// An operand or output belongs to another device.
	ForeignDevice,
	/// Above the tape element bound with specialization disabled.
	TapeBound,
	/// The tape encoder rejected register, operand, store or step capacity.
	TapeCapacity,
	/// Tape storage could not be allocated.
	Resource,
	/// Above the tape element bound and specialization declined the region.
	SpecializationDeclined,
	/// Fused into a tape-interpreter dispatch.
	TapeFused,
	/// Fused into a specialized JIT dispatch.
	SpecializedFused,
}

/// Outcome of one specialization admission request.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(in crate::runtime) enum JitOutcome {
	/// `OA_JIT_FUSION` is not set.
	Disabled,
	/// A new specialization pipeline was admitted.
	Admitted,
	/// An already admitted pipeline was reused.
	Reused,
	/// Route policy kept the tape incumbent or ran a tape probe.
	PolicyTape,
	/// This program failed admission earlier in the device lifetime.
	CachedNegative,
	/// The bounded admission table is full.
	Bound,
	/// The runtime Slang compiler could not be opened.
	CompilerUnavailable,
	/// Compilation, SPIR-V validation or ABI reflection rejected the program.
	CompileRejected,
	/// Descriptor layout, pipeline cache or pipeline creation failed.
	PipelineRejected,
	/// A new specialization was accepted onto the background compile queue.
	Queued,
	/// The program's background compile is still in flight.
	Pending,
}

#[derive(Clone, Copy)]
#[repr(usize)]
enum Counter {
	Examined,
	Disabled,
	NotElementwise,
	PolicyExcluded,
	Hazard,
	Isolated,
	ExtentBoundary,
	CrossInvocation,
	ProgramRejected,
	ForeignDevice,
	TapeBound,
	TapeCapacity,
	Resource,
	SpecializationDeclined,
	TapeFused,
	SpecializedFused,
	Pass,
	StructuralRegion,
	StructuralNode,
	TapeRegion,
	SpecializedRegion,
	JitDisabled,
	JitAdmitted,
	JitReused,
	JitPolicyTape,
	JitCachedNegative,
	JitBound,
	JitCompilerUnavailable,
	JitCompileRejected,
	JitPipelineRejected,
	JitQueued,
	JitPending,
	JitCompile,
	JitCompileNs,
	JitPipelineNs,
	DnnApplied,
	DnnFallback,
}

const COUNTER_COUNT: usize = Counter::DnnFallback as usize + 1;

impl NodeOutcome {
	const fn counter(self) -> Counter {
		match self {
			Self::Disabled => Counter::Disabled,
			Self::NotElementwise => Counter::NotElementwise,
			Self::PolicyExcluded => Counter::PolicyExcluded,
			Self::Hazard => Counter::Hazard,
			Self::Isolated => Counter::Isolated,
			Self::ExtentBoundary => Counter::ExtentBoundary,
			Self::CrossInvocation => Counter::CrossInvocation,
			Self::ProgramRejected => Counter::ProgramRejected,
			Self::ForeignDevice => Counter::ForeignDevice,
			Self::TapeBound => Counter::TapeBound,
			Self::TapeCapacity => Counter::TapeCapacity,
			Self::Resource => Counter::Resource,
			Self::SpecializationDeclined => Counter::SpecializationDeclined,
			Self::TapeFused => Counter::TapeFused,
			Self::SpecializedFused => Counter::SpecializedFused,
		}
	}
}

impl JitOutcome {
	const fn counter(self) -> Counter {
		match self {
			Self::Disabled => Counter::JitDisabled,
			Self::Admitted => Counter::JitAdmitted,
			Self::Reused => Counter::JitReused,
			Self::PolicyTape => Counter::JitPolicyTape,
			Self::CachedNegative => Counter::JitCachedNegative,
			Self::Bound => Counter::JitBound,
			Self::CompilerUnavailable => Counter::JitCompilerUnavailable,
			Self::CompileRejected => Counter::JitCompileRejected,
			Self::PipelineRejected => Counter::JitPipelineRejected,
			Self::Queued => Counter::JitQueued,
			Self::Pending => Counter::JitPending,
		}
	}
}

pub(in crate::runtime) struct FusionCensus {
	counters: [AtomicU64; COUNTER_COUNT],
	pairs: [[AtomicU64; PAIR_OUTCOME_COUNT]; PAIR_RULE_COUNT],
	merges: [[AtomicU64; PAIR_OUTCOME_COUNT]; MergeRule::COUNT],
	/// Private evidence counter: host nanoseconds inside the region and
	/// elementwise fusion passes. Not part of the public `FusionStats`
	/// key-value record.
	fusion_ns: AtomicU64,
}

impl FusionCensus {
	pub(in crate::runtime) fn new() -> Self {
		Self {
			counters: std::array::from_fn(|_| AtomicU64::new(0)),
			pairs: std::array::from_fn(|_| std::array::from_fn(|_| AtomicU64::new(0))),
			merges: std::array::from_fn(|_| std::array::from_fn(|_| AtomicU64::new(0))),
			fusion_ns: AtomicU64::new(0),
		}
	}

	pub(super) fn pair(&self, rule: PairRule, outcome: PairOutcome) {
		self.pairs[rule as usize][outcome.index()].fetch_add(1, Ordering::Relaxed);
	}

	pub(in crate::runtime) fn pair_diagnostics(&self) -> [PairRuleStats; PAIR_RULE_COUNT] {
		std::array::from_fn(|index| PairRuleStats {
			rule: PairRule::ALL[index],
			counters: std::array::from_fn(|outcome| self.pairs[index][outcome].load(Ordering::Relaxed)),
		})
	}

	pub(super) fn merge(&self, rule: MergeRule, outcome: PairOutcome) {
		self.merges[rule as usize][outcome.index()].fetch_add(1, Ordering::Relaxed);
	}

	pub(in crate::runtime) fn merge_diagnostics(&self) -> Vec<MergeRuleStats> {
		MergeRule::ALL
			.into_iter()
			.enumerate()
			.map(|(index, rule)| MergeRuleStats {
				rule,
				counters: std::array::from_fn(|outcome| {
					self.merges[index][outcome].load(Ordering::Relaxed)
				}),
			})
			.collect()
	}

	fn add(&self, counter: Counter, amount: u64) {
		// Wrapping needs 2^64 recorded decisions and would only corrupt a
		// diagnostic, never execution.
		self.counters[counter as usize].fetch_add(amount, Ordering::Relaxed);
	}

	fn get(&self, counter: Counter) -> u64 {
		self.counters[counter as usize].load(Ordering::Relaxed)
	}

	/// Record one elementwise pass offered `nodes` nodes.
	pub(in crate::runtime) fn pass(&self, nodes: usize) {
		self.add(Counter::Pass, 1);
		self.add(Counter::Examined, nodes as u64);
	}

	pub(in crate::runtime) fn nodes(&self, outcome: NodeOutcome, count: usize) {
		self.add(outcome.counter(), count as u64);
	}

	/// Record one fused elementwise region and its source nodes.
	pub(in crate::runtime) fn fused(&self, specialized: bool, nodes: usize) {
		let (region, outcome) = if specialized {
			(Counter::SpecializedRegion, NodeOutcome::SpecializedFused)
		} else {
			(Counter::TapeRegion, NodeOutcome::TapeFused)
		};
		self.add(region, 1);
		self.nodes(outcome, nodes);
	}

	/// Record structural replacements and the source nodes they absorbed.
	pub(in crate::runtime) fn structural(&self, regions: usize, nodes: usize) {
		self.add(Counter::StructuralRegion, regions as u64);
		self.add(Counter::StructuralNode, nodes as u64);
	}

	pub(in crate::runtime) fn jit(&self, outcome: JitOutcome) {
		self.add(outcome.counter(), 1);
	}

	pub(in crate::runtime) fn jit_compile(&self, elapsed: std::time::Duration) {
		self.add(Counter::JitCompile, 1);
		self.add(Counter::JitCompileNs, duration_ns(elapsed));
	}

	pub(in crate::runtime) fn jit_pipeline(&self, elapsed: std::time::Duration) {
		self.add(Counter::JitPipelineNs, duration_ns(elapsed));
	}

	/// Record host wall time for one region/elementwise fusion pass over a
	/// captured graph. Private evidence for the host-overhead exit; the
	/// public `FusionStats` contract is unchanged.
	pub(in crate::runtime) fn fusion_pass(&self, elapsed: std::time::Duration) {
		self
			.fusion_ns
			.fetch_add(duration_ns(elapsed), Ordering::Relaxed);
	}

	/// Total recorded fusion-pass host time, in nanoseconds.
	pub(in crate::runtime) fn fusion_ns(&self) -> u64 {
		self.fusion_ns.load(Ordering::Relaxed)
	}

	/// Record one captured plan's DNN partition outcomes.
	pub(in crate::runtime) fn dnn(&self, applied: u32, fallback: u32) {
		self.add(Counter::DnnApplied, u64::from(applied));
		self.add(Counter::DnnFallback, u64::from(fallback));
	}

	pub(in crate::runtime) fn snapshot(&self) -> FusionStats {
		FusionStats {
			examined_node_count: self.get(Counter::Examined),
			disabled_node_count: self.get(Counter::Disabled),
			not_elementwise_node_count: self.get(Counter::NotElementwise),
			policy_excluded_node_count: self.get(Counter::PolicyExcluded),
			hazard_node_count: self.get(Counter::Hazard),
			isolated_node_count: self.get(Counter::Isolated),
			extent_boundary_node_count: self.get(Counter::ExtentBoundary),
			cross_invocation_node_count: self.get(Counter::CrossInvocation),
			program_rejected_node_count: self.get(Counter::ProgramRejected),
			foreign_device_node_count: self.get(Counter::ForeignDevice),
			tape_bound_node_count: self.get(Counter::TapeBound),
			tape_capacity_node_count: self.get(Counter::TapeCapacity),
			resource_node_count: self.get(Counter::Resource),
			specialization_declined_node_count: self.get(Counter::SpecializationDeclined),
			tape_fused_node_count: self.get(Counter::TapeFused),
			specialized_fused_node_count: self.get(Counter::SpecializedFused),
			pass_count: self.get(Counter::Pass),
			structural_region_count: self.get(Counter::StructuralRegion),
			structural_node_count: self.get(Counter::StructuralNode),
			tape_region_count: self.get(Counter::TapeRegion),
			specialized_region_count: self.get(Counter::SpecializedRegion),
			jit_disabled_count: self.get(Counter::JitDisabled),
			jit_admitted_count: self.get(Counter::JitAdmitted),
			jit_reused_count: self.get(Counter::JitReused),
			jit_policy_tape_count: self.get(Counter::JitPolicyTape),
			jit_cached_negative_count: self.get(Counter::JitCachedNegative),
			jit_bound_count: self.get(Counter::JitBound),
			jit_compiler_unavailable_count: self.get(Counter::JitCompilerUnavailable),
			jit_compile_rejected_count: self.get(Counter::JitCompileRejected),
			jit_pipeline_rejected_count: self.get(Counter::JitPipelineRejected),
			jit_queued_count: self.get(Counter::JitQueued),
			jit_pending_count: self.get(Counter::JitPending),
			jit_compile_count: self.get(Counter::JitCompile),
			jit_compile_ns: self.get(Counter::JitCompileNs),
			jit_pipeline_ns: self.get(Counter::JitPipelineNs),
			dnn_applied_partition_count: self.get(Counter::DnnApplied),
			dnn_fallback_partition_count: self.get(Counter::DnnFallback),
		}
	}
}

fn duration_ns(elapsed: std::time::Duration) -> u64 {
	u64::try_from(elapsed.as_nanos()).unwrap_or(u64::MAX)
}

impl FusionStats {
	/// Return nodes offered to the elementwise fusion pass.
	///
	/// Every examined node is counted in exactly one node outcome below, so
	/// this equals the sum of the `*_node_count` outcomes other than
	/// [`FusionStats::structural_node_count`].
	pub const fn examined_node_count(self) -> u64 {
		self.examined_node_count
	}

	/// Return nodes seen while elementwise fusion was disabled or unavailable.
	pub const fn disabled_node_count(self) -> u64 {
		self.disabled_node_count
	}

	/// Return nodes outside the fusible elementwise vocabulary.
	///
	/// Includes nodes already replaced by a dedicated structural kernel.
	pub const fn not_elementwise_node_count(self) -> u64 {
		self.not_elementwise_node_count
	}

	/// Return elementwise nodes excluded by the RL fusion opt-out.
	pub const fn policy_excluded_node_count(self) -> u64 {
		self.policy_excluded_node_count
	}

	/// Return elementwise nodes whose output is aliased or multiply written.
	pub const fn hazard_node_count(self) -> u64 {
		self.hazard_node_count
	}

	/// Return elementwise nodes with no admissible elementwise neighbour.
	pub const fn isolated_node_count(self) -> u64 {
		self.isolated_node_count
	}

	/// Return elementwise nodes separated from neighbours by element count.
	pub const fn extent_boundary_node_count(self) -> u64 {
		self.extent_boundary_node_count
	}

	/// Return nodes left unfused because a gather or broadcast read needs a
	/// materialized producer.
	pub const fn cross_invocation_node_count(self) -> u64 {
		self.cross_invocation_node_count
	}

	/// Return nodes whose candidate program failed construction or validation.
	pub const fn program_rejected_node_count(self) -> u64 {
		self.program_rejected_node_count
	}

	/// Return nodes whose buffers belong to another device.
	pub const fn foreign_device_node_count(self) -> u64 {
		self.foreign_device_node_count
	}

	/// Return nodes above the tape element bound while specialization is off.
	pub const fn tape_bound_node_count(self) -> u64 {
		self.tape_bound_node_count
	}

	/// Return nodes rejected by tape register, operand, store or step capacity.
	pub const fn tape_capacity_node_count(self) -> u64 {
		self.tape_capacity_node_count
	}

	/// Return nodes left unfused because fusion storage was unavailable.
	pub const fn resource_node_count(self) -> u64 {
		self.resource_node_count
	}

	/// Return nodes above the tape element bound that specialization declined.
	pub const fn specialization_declined_node_count(self) -> u64 {
		self.specialization_declined_node_count
	}

	/// Return nodes fused into tape-interpreter dispatches.
	pub const fn tape_fused_node_count(self) -> u64 {
		self.tape_fused_node_count
	}

	/// Return nodes fused into specialized dispatches.
	pub const fn specialized_fused_node_count(self) -> u64 {
		self.specialized_fused_node_count
	}

	/// Return completed elementwise fusion passes.
	///
	/// Eager work is fused once per flush and captured work once per capture;
	/// plan replay does not run fusion.
	pub const fn pass_count(self) -> u64 {
		self.pass_count
	}

	/// Return dedicated structural replacements made before elementwise fusion.
	pub const fn structural_region_count(self) -> u64 {
		self.structural_region_count
	}

	/// Return source nodes absorbed by dedicated structural replacements.
	pub const fn structural_node_count(self) -> u64 {
		self.structural_node_count
	}

	/// Return fused tape-interpreter dispatches created.
	pub const fn tape_region_count(self) -> u64 {
		self.tape_region_count
	}

	/// Return fused specialized dispatches created.
	pub const fn specialized_region_count(self) -> u64 {
		self.specialized_region_count
	}

	/// Return specialization requests made while `OA_JIT_FUSION` was unset.
	pub const fn jit_disabled_count(self) -> u64 {
		self.jit_disabled_count
	}

	/// Return requests that admitted a new specialized pipeline.
	pub const fn jit_admitted_count(self) -> u64 {
		self.jit_admitted_count
	}

	/// Return requests that reused an admitted specialized pipeline.
	pub const fn jit_reused_count(self) -> u64 {
		self.jit_reused_count
	}

	/// Return requests where measured route policy selected the tape route.
	pub const fn jit_policy_tape_count(self) -> u64 {
		self.jit_policy_tape_count
	}

	/// Return requests for a program that already failed admission.
	pub const fn jit_cached_negative_count(self) -> u64 {
		self.jit_cached_negative_count
	}

	/// Return requests declined because the admission table is full.
	pub const fn jit_bound_count(self) -> u64 {
		self.jit_bound_count
	}

	/// Return requests declined because the runtime compiler is unavailable.
	pub const fn jit_compiler_unavailable_count(self) -> u64 {
		self.jit_compiler_unavailable_count
	}

	/// Return requests rejected by compilation, validation or ABI reflection.
	pub const fn jit_compile_rejected_count(self) -> u64 {
		self.jit_compile_rejected_count
	}

	/// Return requests rejected during pipeline preparation.
	pub const fn jit_pipeline_rejected_count(self) -> u64 {
		self.jit_pipeline_rejected_count
	}

	/// Return programs accepted onto the background compile queue.
	pub const fn jit_queued_count(self) -> u64 {
		self.jit_queued_count
	}

	/// Return requests that found a background compile still in flight.
	pub const fn jit_pending_count(self) -> u64 {
		self.jit_pending_count
	}

	/// Return runtime compiler invocations, excluding restored artifacts.
	pub const fn jit_compile_count(self) -> u64 {
		self.jit_compile_count
	}

	/// Return host wall time spent in runtime compilation, in nanoseconds.
	pub const fn jit_compile_ns(self) -> u64 {
		self.jit_compile_ns
	}

	/// Return host wall time spent creating specialized pipelines, in nanoseconds.
	pub const fn jit_pipeline_ns(self) -> u64 {
		self.jit_pipeline_ns
	}

	/// Return captured-plan DNN partitions replaced by a provider route.
	pub const fn dnn_applied_partition_count(self) -> u64 {
		self.dnn_applied_partition_count
	}

	/// Return captured-plan DNN partitions retained on their source route.
	pub const fn dnn_fallback_partition_count(self) -> u64 {
		self.dnn_fallback_partition_count
	}
}

/// Stable space-separated `key=value` record in declaration order.
impl fmt::Display for FusionStats {
	fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
		let fields = [
			("examined", self.examined_node_count),
			("disabled", self.disabled_node_count),
			("not_elementwise", self.not_elementwise_node_count),
			("policy_excluded", self.policy_excluded_node_count),
			("hazard", self.hazard_node_count),
			("isolated", self.isolated_node_count),
			("extent_boundary", self.extent_boundary_node_count),
			("cross_invocation", self.cross_invocation_node_count),
			("program_rejected", self.program_rejected_node_count),
			("foreign_device", self.foreign_device_node_count),
			("tape_bound", self.tape_bound_node_count),
			("tape_capacity", self.tape_capacity_node_count),
			("resource", self.resource_node_count),
			(
				"specialization_declined",
				self.specialization_declined_node_count,
			),
			("tape_fused", self.tape_fused_node_count),
			("specialized_fused", self.specialized_fused_node_count),
			("passes", self.pass_count),
			("structural_regions", self.structural_region_count),
			("structural_nodes", self.structural_node_count),
			("tape_regions", self.tape_region_count),
			("specialized_regions", self.specialized_region_count),
			("jit_disabled", self.jit_disabled_count),
			("jit_admitted", self.jit_admitted_count),
			("jit_reused", self.jit_reused_count),
			("jit_policy_tape", self.jit_policy_tape_count),
			("jit_cached_negative", self.jit_cached_negative_count),
			("jit_bound", self.jit_bound_count),
			(
				"jit_compiler_unavailable",
				self.jit_compiler_unavailable_count,
			),
			("jit_compile_rejected", self.jit_compile_rejected_count),
			("jit_pipeline_rejected", self.jit_pipeline_rejected_count),
			("jit_queued", self.jit_queued_count),
			("jit_pending", self.jit_pending_count),
			("jit_compiles", self.jit_compile_count),
			("jit_compile_ns", self.jit_compile_ns),
			("jit_pipeline_ns", self.jit_pipeline_ns),
			("dnn_applied", self.dnn_applied_partition_count),
			("dnn_fallback", self.dnn_fallback_partition_count),
		];
		for (index, (key, value)) in fields.iter().enumerate() {
			if index > 0 {
				formatter.write_str(" ")?;
			}
			write!(formatter, "{key}={value}")?;
		}
		Ok(())
	}
}

#[cfg(test)]
#[path = "../../../../../../test/rs/runtime/fusion_census_unit.rs"]
mod tests;