torsh_jit/abstract_interpretation/
framework_config.rs1use super::super::domains::{AbstractDomainType, AbstractValue};
8use crate::NodeId;
9use std::collections::HashMap;
10use std::time::Duration;
11
12#[derive(Debug, Clone)]
14pub struct AbstractInterpretationConfig {
15 pub domain_type: AbstractDomainType,
17 pub max_iterations: usize,
19 pub widening_delay: usize,
21 pub enable_narrowing: bool,
23 pub enable_backward_analysis: bool,
25 pub properties: Vec<Property>,
27 pub precision_threshold: f64,
29}
30
31impl Default for AbstractInterpretationConfig {
32 fn default() -> Self {
33 Self {
34 domain_type: AbstractDomainType::Intervals,
35 max_iterations: 100,
36 widening_delay: 3,
37 enable_narrowing: true,
38 enable_backward_analysis: false,
39 properties: Vec::new(),
40 precision_threshold: 0.8,
41 }
42 }
43}
44
45#[derive(Debug, Clone)]
47pub enum Property {
48 NonNegative(NodeId),
50 Positive(NodeId),
52 BoundedValue(NodeId, f64, f64),
54 NoDivisionByZero(NodeId),
56 NoOverflow(NodeId),
58 SafetyProperty(String, NodeId),
60}
61
62#[derive(Debug, Clone)]
64pub enum SafetyCheck {
65 BoundsCheck,
67 NullCheck,
69 DivisionByZeroCheck,
71 OverflowCheck,
73 ArrayBoundsCheck,
75}
76
77#[derive(Debug, Clone)]
79pub enum SafetyCheckResult {
80 Safe,
82 Unsafe,
84 Unknown,
86}
87
88#[derive(Debug, Clone)]
90pub struct Invariant {
91 pub invariant_type: InvariantType,
93 pub description: String,
95 pub confidence: f64,
97 pub location: String,
99}
100
101#[derive(Debug, Clone)]
103pub enum InvariantType {
104 ValueRange,
106 LoopInvariant,
108 ConditionalInvariant,
110 MemorySafety,
112 NumericalProperty,
114}
115
116#[derive(Debug, Clone)]
118pub struct FunctionInvariant {
119 pub invariant_type: InvariantType,
120 pub description: String,
121 pub confidence: f64,
122 pub location: String,
123}
124
125#[derive(Debug, Clone)]
127pub struct ModuleInvariant {
128 pub invariant_type: InvariantType,
129 pub description: String,
130 pub confidence: f64,
131 pub scope: String,
132}
133
134#[derive(Debug, Clone)]
136pub struct AnalysisStatistics {
137 pub analysis_time: Duration,
139 pub fixpoint_iterations: usize,
141 pub abstract_states_computed: usize,
143 pub cache_hits: usize,
145 pub cache_misses: usize,
147}
148
149#[derive(Debug, Clone)]
151pub struct ForwardAnalysisResult {
152 pub pre_states: HashMap<NodeId, AbstractValue>,
154 pub post_states: HashMap<NodeId, AbstractValue>,
156 pub iterations: usize,
158 pub converged: bool,
160}
161
162impl ForwardAnalysisResult {
163 pub fn new() -> Self {
164 Self {
165 pre_states: HashMap::new(),
166 post_states: HashMap::new(),
167 iterations: 0,
168 converged: false,
169 }
170 }
171}
172
173impl Default for ForwardAnalysisResult {
174 fn default() -> Self {
175 Self::new()
176 }
177}
178
179#[derive(Debug, Clone)]
181pub struct BackwardAnalysisResult {
182 pub pre_states: HashMap<NodeId, AbstractValue>,
184 pub post_states: HashMap<NodeId, AbstractValue>,
186 pub iterations: usize,
188 pub converged: bool,
190}
191
192impl BackwardAnalysisResult {
193 pub fn new() -> Self {
194 Self {
195 pre_states: HashMap::new(),
196 post_states: HashMap::new(),
197 iterations: 0,
198 converged: false,
199 }
200 }
201}
202
203impl Default for BackwardAnalysisResult {
204 fn default() -> Self {
205 Self::new()
206 }
207}
208
209#[derive(Debug, Clone)]
211pub struct AbstractFunctionResult {
212 pub forward_result: FunctionForwardResult,
214 pub backward_result: Option<FunctionBackwardResult>,
216 pub invariants: Vec<FunctionInvariant>,
218}
219
220#[derive(Debug, Clone)]
222pub struct FunctionForwardResult {
223 pub converged: bool,
225 pub iterations: usize,
227 pub entry_state: Option<AbstractValue>,
229 pub exit_state: Option<AbstractValue>,
231}
232
233impl FunctionForwardResult {
234 pub fn new() -> Self {
235 Self {
236 converged: false,
237 iterations: 0,
238 entry_state: None,
239 exit_state: None,
240 }
241 }
242}
243
244impl Default for FunctionForwardResult {
245 fn default() -> Self {
246 Self::new()
247 }
248}
249
250#[derive(Debug, Clone)]
252pub struct FunctionBackwardResult {
253 pub converged: bool,
255 pub iterations: usize,
257 pub entry_state: Option<AbstractValue>,
259 pub exit_state: Option<AbstractValue>,
261}
262
263impl FunctionBackwardResult {
264 pub fn new() -> Self {
265 Self {
266 converged: false,
267 iterations: 0,
268 entry_state: None,
269 exit_state: None,
270 }
271 }
272}
273
274impl Default for FunctionBackwardResult {
275 fn default() -> Self {
276 Self::new()
277 }
278}
279
280#[derive(Debug, Clone)]
282pub struct InterproceduralAnalysisResult {
283 pub call_graph: HashMap<String, Vec<String>>,
285 pub global_invariants: Vec<Invariant>,
287}
288
289#[derive(Debug, Clone)]
291pub struct PropertyResult {
292 pub property: Property,
294 pub result: SafetyCheckResult,
296 pub confidence: f64,
298 pub details: String,
300}
301
302#[derive(Debug, Clone)]
304pub struct PrecisionAnalysis {
305 pub overall_precision: f64,
307 pub node_precision: HashMap<NodeId, f64>,
309 pub improvement_suggestions: Vec<String>,
311}
312
313#[derive(Debug, Clone)]
315pub struct PerformanceAnalysis {
316 pub complexity_score: f64,
318 pub bottlenecks: Vec<PerformanceBottleneck>,
320 pub optimization_opportunities: Vec<OptimizationOpportunity>,
322}
323
324#[derive(Debug, Clone)]
326pub struct PerformanceBottleneck {
327 pub bottleneck_type: BottleneckType,
329 pub location: NodeId,
331 pub severity: f64,
333 pub description: String,
335}
336
337#[derive(Debug, Clone)]
339pub enum BottleneckType {
340 ComputationIntensive,
342 MemoryBound,
344 LoopBottleneck,
346 CallOverhead,
348 CacheMisses,
350}
351
352#[derive(Debug, Clone)]
354pub struct OptimizationOpportunity {
355 pub optimization_type: OptimizationType,
357 pub location: NodeId,
359 pub benefit: f64,
361 pub description: String,
363}
364
365#[derive(Debug, Clone)]
367pub enum OptimizationType {
368 LoopUnrolling,
370 ConstantFolding,
372 CommonSubexpressionElimination,
374 DeadCodeElimination,
376 Vectorization,
378}
379
380#[derive(Debug, Clone)]
382pub struct ComplexityMetrics {
383 pub cyclomatic_complexity: usize,
385 pub variable_count: usize,
387 pub nesting_depth: usize,
389}
390
391#[derive(Debug, Clone)]
393pub struct AbstractAnalysisResult {
394 pub forward_result: ForwardAnalysisResult,
396 pub backward_result: Option<BackwardAnalysisResult>,
398 pub invariants: Vec<Invariant>,
400 pub property_results: Vec<PropertyResult>,
402 pub precision_analysis: PrecisionAnalysis,
404 pub performance_analysis: PerformanceAnalysis,
406 pub statistics: AnalysisStatistics,
408 pub node_values: HashMap<NodeId, AbstractValue>,
410}
411
412#[derive(Debug, Clone)]
414pub struct AbstractIrResult {
415 pub function_results: HashMap<String, AbstractFunctionResult>,
417 pub interprocedural_result: InterproceduralAnalysisResult,
419 pub module_invariants: Vec<ModuleInvariant>,
421}