Skip to main content

weir/
live_at.rs

1//! `live_at`  -  liveness query: is variable `v` live at node `n`?
2//!
3//! A variable is live at `n` iff there exists a use of `v` reachable
4//! from `n` along the CFG without an intervening def. Composes the
5//! per-variable use set with backward reachability over the CFG,
6//! killing on def sites.
7
8use vyre::ir::Program;
9use vyre_primitives::bitset::and::bitset_and;
10use vyre_primitives::graph::csr_forward_traverse::bitset_words;
11
12pub(crate) const OP_ID: &str = "weir::live_at";
13
14/// Build a live-at Program.
15///
16/// Inputs:
17/// - `live_in_buf`: per-node bitset where bit `n` is set iff `v` is
18///   live at the entry of `n` (host-supplied via
19///   classical liveness analysis).
20/// - `query_set`:   per-node bitset of node ids being queried.
21/// - `out`:         per-node bitset; bit `n` set iff `n` ∈ query_set
22///   AND `v` is live at `n`.
23#[must_use]
24pub fn live_at(node_count: u32, live_in_buf: &str, query_set: &str, out: &str) -> Program {
25    let words = bitset_words(node_count);
26    vyre_harness::region::tag_program(OP_ID, bitset_and(live_in_buf, query_set, out, words))
27}
28
29/// reference oracle.
30#[must_use]
31#[cfg(any(test, feature = "cpu-parity"))]
32#[deprecated(
33    note = "reference oracle only; production code must dispatch the Weir Program on a concrete GPU backend or use weir::oracle for parity evidence"
34)]
35pub(crate) fn cpu_ref(live_in: &[u32], query_set: &[u32]) -> Vec<u32> {
36    vyre_primitives::bitset::and::cpu_ref(live_in, query_set)
37}
38
39/// Soundness marker for [`live_at`].
40#[derive(Clone, Copy, Debug, PartialEq, Eq)]
41pub struct LiveAt;
42impl super::soundness::SoundnessTagged for LiveAt {
43    fn soundness(&self) -> super::soundness::Soundness {
44        super::soundness::Soundness::Exact
45    }
46}
47
48inventory::submit! {
49    vyre_harness::OpEntry::new(
50        OP_ID,
51        || live_at(4, "live", "query", "out"),
52        Some(|| {
53            let u32s = crate::dispatch_decode::pack_u32;
54            vec![vec![u32s(&[0b1100]), u32s(&[0b1010]), u32s(&[0])]]
55        }),
56        Some(|| {
57            let u32s = crate::dispatch_decode::pack_u32;
58            vec![vec![u32s(&[0b1000])]]
59        }),
60    )
61}
62
63#[cfg(test)]
64#[allow(deprecated)]
65mod tests {
66    use super::*;
67
68    #[test]
69    fn live_query_returns_intersection() {
70        assert_eq!(cpu_ref(&[0b1100], &[0b1010]), vec![0b1000]);
71    }
72
73    #[test]
74    fn dead_at_query_returns_zero() {
75        assert_eq!(cpu_ref(&[0b0001], &[0b1110]), vec![0]);
76    }
77
78    #[test]
79    fn empty_query_yields_empty() {
80        assert_eq!(cpu_ref(&[0xFFFF], &[0]), vec![0]);
81    }
82
83    #[test]
84    fn fully_live_in_query_propagates() {
85        assert_eq!(cpu_ref(&[0xFFFF], &[0x00FF]), vec![0x00FF]);
86    }
87}