Skip to main content

tenferro_runtime/program/
value.rs

1use std::fmt;
2use std::sync::atomic::{AtomicU64, Ordering};
3
4static NEXT_BUILDER_NONCE: AtomicU64 = AtomicU64::new(1);
5
6#[derive(Clone, Copy, PartialEq, Eq, Hash)]
7pub(crate) struct ProgramBuilderNonce(u64);
8
9impl ProgramBuilderNonce {
10    pub(crate) fn fresh() -> Self {
11        loop {
12            let nonce = NEXT_BUILDER_NONCE.fetch_add(1, Ordering::Relaxed);
13            if nonce != 0 {
14                return Self(nonce);
15            }
16        }
17    }
18}
19
20/// An opaque SSA value owned by one semantic-program builder.
21#[derive(Clone, Copy, PartialEq, Eq, Hash)]
22pub struct ProgramValue {
23    pub(crate) slot: u32,
24    pub(crate) owner: ProgramBuilderNonce,
25}
26
27impl ProgramValue {
28    pub(crate) fn new(slot: u32, owner: ProgramBuilderNonce) -> Self {
29        Self { slot, owner }
30    }
31}
32
33impl fmt::Debug for ProgramValue {
34    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
35        formatter.write_str("ProgramValue(<opaque>)")
36    }
37}
38
39/// Opaque lookup key for one frozen tensor binding.
40#[derive(Clone, Copy, PartialEq, Eq, Hash)]
41pub struct BindingKey {
42    pub(crate) slot: u32,
43    pub(crate) owner: ProgramBuilderNonce,
44}
45
46impl BindingKey {
47    pub(crate) const fn new(slot: u32, owner: ProgramBuilderNonce) -> Self {
48        Self { slot, owner }
49    }
50}
51
52impl fmt::Debug for BindingKey {
53    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
54        formatter.write_str("BindingKey(<opaque>)")
55    }
56}