Skip to main content

vyre_foundation/runtime/
cpu_op.rs

1//! CPU reference execution contract for operation types.
2
3use crate::ir_inner::model::program::Program;
4pub use vyre_spec::CpuFn;
5
6/// CPU reference implementation for an operation.
7pub trait CpuOp {
8    /// Execute one flat byte payload and append the byte output to `output`.
9    fn cpu(input: &[u8], output: &mut Vec<u8>);
10}
11
12/// Marker trait for Category A operations with an executable IR program.
13pub trait CategoryAOp {
14    /// Build the canonical Category A IR program.
15    fn program() -> Program;
16}
17
18/// Failing CPU adapter for intrinsics whose existing reference accepts structured buffers.
19///
20/// This is the explicit reference-oracle sentinel for Category C ops whose
21/// typed CPU reference is intentionally not exposed through the flat ABI. The
22/// function clears the output buffer and returns no flat result. Runtime
23/// dispatchers must reject this sentinel through [`is_cpu_reference_sentinel`]
24/// before invocation so callers cannot consume an empty byte vector as a valid
25/// CPU reference result.
26///
27/// Each op can register its own CPU ref via `vyre-reference`, and
28/// `DialectRegistry::get_lowering(ReferenceBackend)` dispatches to it
29/// directly rather than going through this sentinel.
30///
31/// AUDIT_2026-05-23: Deprecated - CPU sentinels are fallback holes.
32/// Category C ops must implement typed GPU lowerings instead.
33#[deprecated(
34    note = "structured_intrinsic_cpu is a non-executable fallback sentinel. Implement typed GPU lowering for the op."
35)]
36pub fn structured_intrinsic_cpu(input: &[u8], output: &mut Vec<u8>) {
37    let _ = input;
38    output.clear();
39}
40
41/// True when [`structured_intrinsic_cpu`] is set as an op's CPU lowering.
42///
43/// Conformance tooling uses this to flag operations that still expose only the
44/// structured-reference sentinel, so parity status is recorded explicitly
45/// instead of pretending a flat CPU adapter exists.
46#[must_use]
47pub fn is_cpu_reference_sentinel(f: CpuFn) -> bool {
48    #[allow(deprecated)]
49    std::ptr::fn_addr_eq(f, structured_intrinsic_cpu as CpuFn)
50}
51
52/// Compatibility wrapper for older conformance tooling.
53#[deprecated(
54    note = "use is_cpu_reference_sentinel; CPU reference sentinels are explicit oracles, not runtime fallbacks"
55)]
56#[must_use]
57pub fn is_fallback_cpu_ref(f: CpuFn) -> bool {
58    is_cpu_reference_sentinel(f)
59}
60
61#[cfg(test)]
62#[allow(deprecated)]
63mod tests {
64    use super::*;
65
66    #[test]
67    fn is_cpu_reference_sentinel_detects_structured_intrinsic() {
68        assert!(is_cpu_reference_sentinel(structured_intrinsic_cpu));
69    }
70
71    #[test]
72    fn is_cpu_reference_sentinel_rejects_other_fn() {
73        #[allow(clippy::ptr_arg)] // Must match `CpuFn` (`&mut Vec<u8>`), not `&mut [u8]`.
74        fn custom_cpu(_input: &[u8], _output: &mut Vec<u8>) {}
75        assert!(!is_cpu_reference_sentinel(custom_cpu));
76    }
77
78    #[test]
79    fn structured_intrinsic_clears_output_without_flat_result() {
80        let mut output = vec![1, 2, 3];
81        structured_intrinsic_cpu(b"input", &mut output);
82        assert!(output.is_empty());
83    }
84}