torsh-jit 0.1.3

JIT compilation and kernel fusion for ToRSh deep learning framework
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
//! Control flow analysis for computation graphs

use crate::graph::core::{ComputationGraph, NodeId};
use crate::graph::operations::Operation;
use crate::JitResult;
use std::collections::{HashMap, HashSet, VecDeque};

/// Control flow analysis for identifying loops, conditions, and dominance relationships
#[derive(Debug, Clone)]
pub struct ControlFlowAnalysis {
    /// Dominator tree: each node maps to its immediate dominator
    pub dominators: HashMap<NodeId, Option<NodeId>>,

    /// Dominated nodes: each node maps to the set of nodes it dominates
    pub dominated: HashMap<NodeId, HashSet<NodeId>>,

    /// Loop information
    pub loops: Vec<LoopInfo>,

    /// Conditional blocks
    pub conditionals: Vec<ConditionalInfo>,

    /// Statistics about the control flow
    pub stats: ControlFlowStats,
}

impl ControlFlowAnalysis {
    /// Create a new control flow analysis
    pub fn new() -> Self {
        Self {
            dominators: HashMap::new(),
            dominated: HashMap::new(),
            loops: Vec::new(),
            conditionals: Vec::new(),
            stats: ControlFlowStats::default(),
        }
    }

    /// Analyze a computation graph for control flow patterns
    pub fn analyze(graph: &ComputationGraph) -> JitResult<Self> {
        let mut analysis = Self::new();

        // Compute dominator tree
        analysis.compute_dominators(graph)?;

        // Detect loops
        analysis.detect_loops(graph)?;

        // Detect conditionals
        analysis.detect_conditionals(graph)?;

        // Compute statistics
        analysis.compute_statistics(graph);

        Ok(analysis)
    }

    /// Compute dominator relationships
    fn compute_dominators(&mut self, graph: &ComputationGraph) -> JitResult<()> {
        // Simple dominator computation - in practice would use more sophisticated algorithms
        let nodes: Vec<NodeId> = graph.nodes().map(|(id, _)| id).collect();

        // Initialize dominators
        for &node in &nodes {
            self.dominators.insert(node, None);
            self.dominated.insert(node, HashSet::new());
        }

        // For each node, find nodes that must be traversed to reach it from any input
        for &node in &nodes {
            let mut dominates = HashSet::new();

            // Simple approximation: a node dominates another if all paths to the second
            // node must pass through the first node
            for &other_node in &nodes {
                if node != other_node && self.dominates_node(graph, node, other_node) {
                    dominates.insert(other_node);

                    // Set immediate dominator if none exists or this is closer
                    if self
                        .dominators
                        .get(&other_node)
                        .expect("dominator entry should exist")
                        .is_none()
                    {
                        self.dominators.insert(other_node, Some(node));
                    }
                }
            }

            self.dominated.insert(node, dominates);
        }

        Ok(())
    }

    /// Check if one node dominates another (simplified check)
    fn dominates_node(&self, graph: &ComputationGraph, dominator: NodeId, node: NodeId) -> bool {
        // This is a simplified domination check
        // In practice, would use proper dominator tree algorithms

        if dominator == node {
            return true;
        }

        // Check if dominator is on all paths from inputs to node
        let inputs = &graph.inputs;
        if inputs.is_empty() {
            return false;
        }

        for &input in inputs {
            if !self.path_contains_node(graph, input, node, dominator) {
                return false;
            }
        }

        true
    }

    /// Check if a path from start to end contains a specific node
    fn path_contains_node(
        &self,
        graph: &ComputationGraph,
        start: NodeId,
        end: NodeId,
        check_node: NodeId,
    ) -> bool {
        if start == end {
            return start == check_node;
        }

        let mut visited = HashSet::new();
        let mut queue = VecDeque::new();
        queue.push_back(start);

        while let Some(current) = queue.pop_front() {
            if visited.contains(&current) {
                continue;
            }
            visited.insert(current);

            if current == end {
                return visited.contains(&check_node);
            }

            for neighbor in graph.get_node_outputs(current) {
                if !visited.contains(&neighbor) {
                    queue.push_back(neighbor);
                }
            }
        }

        false
    }

    /// Detect loop structures in the graph
    fn detect_loops(&mut self, graph: &ComputationGraph) -> JitResult<()> {
        let nodes: Vec<NodeId> = graph.nodes().map(|(id, _)| id).collect();

        for &node in &nodes {
            if let Some(node_data) = graph.get_node(node) {
                match &node_data.operation {
                    Operation::While(while_info) => {
                        let loop_info = LoopInfo {
                            header: node,
                            condition: while_info.condition,
                            body_nodes: self.find_loop_body_nodes(graph, while_info.body),
                            loop_type: LoopType::While,
                            max_iterations: while_info.max_iterations,
                        };
                        self.loops.push(loop_info);
                    }
                    Operation::For(for_info) => {
                        let loop_info = LoopInfo {
                            header: node,
                            condition: for_info.start, // Simplified
                            body_nodes: self.find_loop_body_nodes(graph, for_info.body),
                            loop_type: LoopType::For,
                            max_iterations: None, // Could be computed from for loop bounds
                        };
                        self.loops.push(loop_info);
                    }
                    _ => {}
                }
            }
        }

        Ok(())
    }

    /// Find all nodes that belong to a loop body
    fn find_loop_body_nodes(
        &self,
        graph: &ComputationGraph,
        body_start: NodeId,
    ) -> HashSet<NodeId> {
        let mut body_nodes = HashSet::new();
        let mut queue = VecDeque::new();
        queue.push_back(body_start);

        while let Some(node) = queue.pop_front() {
            if body_nodes.contains(&node) {
                continue;
            }
            body_nodes.insert(node);

            // Add successors that are part of the loop body
            for successor in graph.get_node_outputs(node) {
                if let Some(successor_data) = graph.get_node(successor) {
                    match &successor_data.operation {
                        Operation::Break | Operation::Continue => {
                            // Don't traverse beyond loop control statements
                            body_nodes.insert(successor);
                        }
                        _ => {
                            if !body_nodes.contains(&successor) {
                                queue.push_back(successor);
                            }
                        }
                    }
                }
            }
        }

        body_nodes
    }

    /// Detect conditional structures in the graph
    fn detect_conditionals(&mut self, graph: &ComputationGraph) -> JitResult<()> {
        let nodes: Vec<NodeId> = graph.nodes().map(|(id, _)| id).collect();

        for &node in &nodes {
            if let Some(node_data) = graph.get_node(node) {
                if let Operation::If(if_info) = &node_data.operation {
                    let then_nodes = self.find_branch_nodes(graph, if_info.then_block);
                    let else_nodes = if let Some(else_block) = if_info.else_block {
                        self.find_branch_nodes(graph, else_block)
                    } else {
                        HashSet::new()
                    };

                    let conditional_info = ConditionalInfo {
                        condition_node: if_info.condition,
                        then_nodes,
                        else_nodes,
                        merge_point: if_info.merge_point,
                    };
                    self.conditionals.push(conditional_info);
                }
            }
        }

        Ok(())
    }

    /// Find all nodes that belong to a conditional branch
    fn find_branch_nodes(&self, graph: &ComputationGraph, branch_start: NodeId) -> HashSet<NodeId> {
        let mut branch_nodes = HashSet::new();
        let mut queue = VecDeque::new();
        queue.push_back(branch_start);

        while let Some(node) = queue.pop_front() {
            if branch_nodes.contains(&node) {
                continue;
            }
            branch_nodes.insert(node);

            // Add successors until we reach a merge point or loop back
            for successor in graph.get_node_outputs(node) {
                if let Some(successor_data) = graph.get_node(successor) {
                    match &successor_data.operation {
                        Operation::Merge(_) => {
                            // Stop at merge points
                            break;
                        }
                        _ => {
                            if !branch_nodes.contains(&successor) {
                                queue.push_back(successor);
                            }
                        }
                    }
                }
            }
        }

        branch_nodes
    }

    /// Compute control flow statistics
    fn compute_statistics(&mut self, graph: &ComputationGraph) {
        let mut loop_count = 0;
        let mut conditional_count = 0;
        let mut block_count = 0;

        for (_, node) in graph.nodes() {
            match &node.operation {
                Operation::While(_) | Operation::For(_) => loop_count += 1,
                Operation::If(_) => conditional_count += 1,
                Operation::Block(_) => block_count += 1,
                _ => {}
            }
        }

        self.stats = ControlFlowStats {
            total_nodes: graph.node_count(),
            loop_count,
            conditional_count,
            block_count,
            max_loop_depth: self.compute_max_loop_depth(),
            max_conditional_depth: self.compute_max_conditional_depth(),
        };
    }

    /// Compute maximum loop nesting depth
    fn compute_max_loop_depth(&self) -> usize {
        // Simplified computation - would need more sophisticated analysis for nested loops
        if self.loops.is_empty() {
            0
        } else {
            1 // For now, assume max depth of 1
        }
    }

    /// Compute maximum conditional nesting depth
    fn compute_max_conditional_depth(&self) -> usize {
        // Simplified computation - would need more sophisticated analysis for nested conditionals
        if self.conditionals.is_empty() {
            0
        } else {
            1 // For now, assume max depth of 1
        }
    }

    /// Check if a node is inside a loop
    pub fn is_in_loop(&self, node: NodeId) -> bool {
        self.loops
            .iter()
            .any(|loop_info| loop_info.body_nodes.contains(&node))
    }

    /// Check if a node is inside a conditional branch
    pub fn is_in_conditional(&self, node: NodeId) -> bool {
        self.conditionals.iter().any(|cond_info| {
            cond_info.then_nodes.contains(&node) || cond_info.else_nodes.contains(&node)
        })
    }

    /// Get the loop that contains a given node
    pub fn containing_loop(&self, node: NodeId) -> Option<&LoopInfo> {
        self.loops
            .iter()
            .find(|loop_info| loop_info.body_nodes.contains(&node))
    }

    /// Get the conditional that contains a given node
    pub fn containing_conditional(&self, node: NodeId) -> Option<&ConditionalInfo> {
        self.conditionals.iter().find(|cond_info| {
            cond_info.then_nodes.contains(&node) || cond_info.else_nodes.contains(&node)
        })
    }
}

impl Default for ControlFlowAnalysis {
    fn default() -> Self {
        Self::new()
    }
}

/// Information about a loop in the control flow
#[derive(Debug, Clone)]
pub struct LoopInfo {
    /// Header node of the loop
    pub header: NodeId,
    /// Condition node
    pub condition: NodeId,
    /// Nodes that are part of the loop body
    pub body_nodes: HashSet<NodeId>,
    /// Type of loop
    pub loop_type: LoopType,
    /// Maximum number of iterations (if known)
    pub max_iterations: Option<usize>,
}

/// Types of loops
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum LoopType {
    While,
    For,
    DoWhile,
}

/// Information about a conditional structure
#[derive(Debug, Clone)]
pub struct ConditionalInfo {
    /// The condition node
    pub condition_node: NodeId,
    /// Nodes in the 'then' branch
    pub then_nodes: HashSet<NodeId>,
    /// Nodes in the 'else' branch (if any)
    pub else_nodes: HashSet<NodeId>,
    /// Merge point where branches reconverge
    pub merge_point: Option<NodeId>,
}

/// Statistics about control flow in the graph
#[derive(Debug, Clone, Default)]
pub struct ControlFlowStats {
    /// Total number of nodes in the graph
    pub total_nodes: usize,
    /// Number of loops
    pub loop_count: usize,
    /// Number of conditionals
    pub conditional_count: usize,
    /// Number of block operations
    pub block_count: usize,
    /// Maximum loop nesting depth
    pub max_loop_depth: usize,
    /// Maximum conditional nesting depth
    pub max_conditional_depth: usize,
}