Skip to main content

logicaffeine_compile/codegen_sva/
sva_vacuity.rs

1//! SVA Nonvacuous Evaluation Tracking (IEEE 16.14.8)
2//!
3//! Implements the 33 rules (a-ag) for determining when an assertion
4//! evaluation is nonvacuous. A vacuous success means the antecedent
5//! was never triggered — the property never actually tested the design.
6//! If ALL evaluation attempts are vacuous, the assertion is dead.
7
8use super::sva_model::SvaExpr;
9
10/// Result of vacuity analysis for a property.
11#[derive(Debug, Clone, PartialEq)]
12pub enum VacuityStatus {
13    /// The evaluation is definitely nonvacuous
14    Nonvacuous,
15    /// The evaluation is vacuous (property never tested)
16    Vacuous,
17    /// Cannot determine statically — depends on runtime values
18    Unknown,
19}
20
21/// Analyze whether an SVA property evaluation can be nonvacuous.
22///
23/// Returns `Nonvacuous` if there exists at least one evaluation attempt
24/// where the property is exercised (antecedent triggered, etc.).
25/// Returns `Vacuous` if the property can NEVER be exercised.
26/// Returns `Unknown` for runtime-dependent cases.
27///
28/// IEEE 16.14.8 rules a-ag.
29pub fn analyze_vacuity(expr: &SvaExpr) -> VacuityStatus {
30    match expr {
31        // Rule (a): A sequence is always nonvacuous
32        SvaExpr::Delay { .. }
33        | SvaExpr::Repetition { .. }
34        | SvaExpr::GotoRepetition { .. }
35        | SvaExpr::NonConsecRepetition { .. } => VacuityStatus::Nonvacuous,
36
37        // Rule (b): strong(seq) is always nonvacuous
38        SvaExpr::Strong(_) => VacuityStatus::Nonvacuous,
39
40        // Rule (c): weak(seq) is always nonvacuous
41        SvaExpr::Weak(_) => VacuityStatus::Nonvacuous,
42
43        // Rule (d): not p is nonvacuous iff p is nonvacuous
44        SvaExpr::PropertyNot(inner) => analyze_vacuity(inner),
45
46        // Rule (e): p or q is nonvacuous if either is
47        SvaExpr::Or(l, r) | SvaExpr::SequenceOr(l, r) | SvaExpr::PropertyIff(l, r) => {
48            match (analyze_vacuity(l), analyze_vacuity(r)) {
49                (VacuityStatus::Nonvacuous, _) | (_, VacuityStatus::Nonvacuous) => {
50                    VacuityStatus::Nonvacuous
51                }
52                (VacuityStatus::Vacuous, VacuityStatus::Vacuous) => VacuityStatus::Vacuous,
53                _ => VacuityStatus::Unknown,
54            }
55        }
56
57        // Rule (f): p and q is nonvacuous if either is
58        SvaExpr::And(l, r) | SvaExpr::SequenceAnd(l, r) => {
59            match (analyze_vacuity(l), analyze_vacuity(r)) {
60                (VacuityStatus::Nonvacuous, _) | (_, VacuityStatus::Nonvacuous) => {
61                    VacuityStatus::Nonvacuous
62                }
63                (VacuityStatus::Vacuous, VacuityStatus::Vacuous) => VacuityStatus::Vacuous,
64                _ => VacuityStatus::Unknown,
65            }
66        }
67
68        // Rule (g): if(cond) p else q — nonvacuous if either branch can be
69        SvaExpr::IfElse {
70            then_expr,
71            else_expr,
72            ..
73        } => match (analyze_vacuity(then_expr), analyze_vacuity(else_expr)) {
74            (VacuityStatus::Nonvacuous, _) | (_, VacuityStatus::Nonvacuous) => {
75                VacuityStatus::Nonvacuous
76            }
77            _ => VacuityStatus::Unknown,
78        },
79
80        // Rule (h): seq |-> prop is nonvacuous when seq has an endpoint match
81        // Rule (i): seq |=> prop is nonvacuous when seq has a match point
82        SvaExpr::Implication { antecedent, .. } => {
83            // The antecedent must be able to match for the property to be nonvacuous
84            match analyze_vacuity(antecedent) {
85                VacuityStatus::Nonvacuous => VacuityStatus::Unknown, // depends on runtime
86                VacuityStatus::Vacuous => VacuityStatus::Vacuous,
87                VacuityStatus::Unknown => VacuityStatus::Unknown,
88            }
89        }
90
91        // Rule (j-k): followed-by operators
92        SvaExpr::FollowedBy { antecedent, .. } => match analyze_vacuity(antecedent) {
93            VacuityStatus::Nonvacuous => VacuityStatus::Unknown,
94            VacuityStatus::Vacuous => VacuityStatus::Vacuous,
95            VacuityStatus::Unknown => VacuityStatus::Unknown,
96        },
97
98        // Rule (l-n): nexttime/s_nexttime — nonvacuous if next tick exists and body nonvacuous
99        SvaExpr::Nexttime(inner, _) | SvaExpr::SNexttime(inner, _) => analyze_vacuity(inner),
100
101        // Rule (p): always p — nonvacuous when p is nonvacuous at some tick
102        SvaExpr::Always(inner) | SvaExpr::SAlways(inner) => analyze_vacuity(inner),
103
104        // Rule (q-r): always [m:n] p — nonvacuous at some tick in range
105        SvaExpr::AlwaysBounded { body, .. } | SvaExpr::SAlwaysBounded { body, .. } => {
106            analyze_vacuity(body)
107        }
108
109        // Rule (s): s_eventually p — nonvacuous if p holds at some tick
110        SvaExpr::SEventually(inner) => analyze_vacuity(inner),
111
112        // Rule (u): eventually [m:n] p — nonvacuous
113        SvaExpr::EventuallyBounded { body, .. } | SvaExpr::SEventuallyBounded { body, .. } => {
114            analyze_vacuity(body)
115        }
116
117        // Rule (v): p until q — nonvacuous
118        SvaExpr::Until { .. } => VacuityStatus::Nonvacuous,
119
120        // Rule (z): p implies q — nonvacuous when p is true
121        SvaExpr::PropertyImplies(lhs, _) => {
122            match analyze_vacuity(lhs) {
123                VacuityStatus::Nonvacuous => VacuityStatus::Unknown, // depends on p being true
124                VacuityStatus::Vacuous => VacuityStatus::Vacuous,
125                VacuityStatus::Unknown => VacuityStatus::Unknown,
126            }
127        }
128
129        // Rule (ag): disable iff (rst) p — nonvacuous when rst is not always active
130        SvaExpr::DisableIff { body, .. } => analyze_vacuity(body),
131
132        // PropertyCase: vacuity depends on whether the case expression matches
133        // any item at runtime. Without a default, unmatched cases are vacuously true.
134        // This is runtime-dependent, so return Unknown.
135        SvaExpr::PropertyCase { default, .. } => {
136            if default.is_some() {
137                // With a default, some branch always fires
138                VacuityStatus::Nonvacuous
139            } else {
140                // Without a default, whether any case matches is runtime-dependent
141                VacuityStatus::Unknown
142            }
143        }
144
145        // Atomic signals, constants, system functions — nonvacuous (they always evaluate)
146        SvaExpr::Signal(_)
147        | SvaExpr::Const(_, _)
148        | SvaExpr::Rose(_)
149        | SvaExpr::Fell(_)
150        | SvaExpr::Past(_, _)
151        | SvaExpr::Stable(_)
152        | SvaExpr::Changed(_)
153        | SvaExpr::Not(_)
154        | SvaExpr::Eq(_, _)
155        | SvaExpr::NotEq(_, _)
156        | SvaExpr::LessThan(_, _)
157        | SvaExpr::GreaterThan(_, _)
158        | SvaExpr::LessEqual(_, _)
159        | SvaExpr::GreaterEqual(_, _)
160        | SvaExpr::Ternary { .. }
161        | SvaExpr::OneHot0(_)
162        | SvaExpr::OneHot(_)
163        | SvaExpr::CountOnes(_)
164        | SvaExpr::IsUnknown(_)
165        | SvaExpr::Sampled(_)
166        | SvaExpr::Bits(_)
167        | SvaExpr::Clog2(_)
168        | SvaExpr::CountBits(_, _)
169        | SvaExpr::IsUnbounded(_)
170        | SvaExpr::AcceptOn { .. }
171        | SvaExpr::RejectOn { .. }
172        | SvaExpr::SyncAcceptOn { .. }
173        | SvaExpr::SyncRejectOn { .. }
174        | SvaExpr::FirstMatch(_)
175        | SvaExpr::Throughout { .. }
176        | SvaExpr::Within { .. }
177        | SvaExpr::Intersect { .. }
178        | SvaExpr::BitAnd(_, _)
179        | SvaExpr::BitOr(_, _)
180        | SvaExpr::BitXor(_, _)
181        | SvaExpr::BitNot(_)
182        | SvaExpr::ReductionAnd(_)
183        | SvaExpr::ReductionOr(_)
184        | SvaExpr::ReductionXor(_)
185        | SvaExpr::BitSelect { .. }
186        | SvaExpr::PartSelect { .. }
187        | SvaExpr::Concat(_)
188        | SvaExpr::ConstCast(_)
189        | SvaExpr::FieldAccess { .. }
190        | SvaExpr::EnumLiteral { .. }
191        | SvaExpr::Triggered(_)
192        | SvaExpr::Matched(_)
193        | SvaExpr::ImmediateAssert { .. }
194        | SvaExpr::SequenceAction { .. }
195        | SvaExpr::Clocked { .. }
196        | SvaExpr::LocalVar(_)
197        | SvaExpr::ArrayMap { .. }
198        | SvaExpr::TypeThis
199        | SvaExpr::RealConst(_) => VacuityStatus::Nonvacuous,
200    }
201}
202
203/// Check if ALL evaluation attempts of a property are vacuous.
204/// If so, the assertion is dead and provides no verification value.
205pub fn is_dead_assertion(expr: &SvaExpr) -> bool {
206    matches!(analyze_vacuity(expr), VacuityStatus::Vacuous)
207}