tensorlogic-compiler 0.1.0-rc.1

Compiler for transforming logic expressions into tensor computation graphs
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
//! Compilation profiling and performance tracking.
//!
//! This module provides tools for profiling the compilation process,
//! tracking performance metrics, and identifying bottlenecks.
//!
//! # Overview
//!
//! Compilation profiling helps developers:
//! - Identify slow compilation passes
//! - Track memory usage during compilation
//! - Optimize compilation performance
//! - Compare different compilation strategies
//!
//! # Features
//!
//! - **Time Tracking**: Measure time spent in each compilation phase
//! - **Memory Tracking**: Monitor memory allocations and peak usage
//! - **Pass Analysis**: Identify expensive optimization passes
//! - **Cache Statistics**: Track cache hit rates and effectiveness
//!
//! # Examples
//!
//! ```rust
//! use tensorlogic_compiler::profiling::{CompilationProfiler, ProfileConfig};
//! use tensorlogic_compiler::compile_to_einsum;
//! use tensorlogic_ir::{TLExpr, Term};
//!
//! let mut profiler = CompilationProfiler::new();
//! profiler.start_phase("compilation");
//!
//! let expr = TLExpr::pred("p", vec![Term::var("x")]);
//! let _graph = compile_to_einsum(&expr).unwrap();
//!
//! profiler.end_phase("compilation");
//!
//! let report = profiler.generate_report();
//! println!("{}", report);
//! ```

use std::collections::HashMap;
use std::time::{Duration, Instant};

/// Configuration for compilation profiling.
#[derive(Debug, Clone)]
pub struct ProfileConfig {
    /// Enable time tracking
    pub track_time: bool,
    /// Enable memory tracking
    pub track_memory: bool,
    /// Enable detailed pass-level profiling
    pub track_passes: bool,
    /// Enable cache statistics
    pub track_cache: bool,
    /// Minimum duration to report (filter noise)
    pub min_duration_ms: u64,
}

impl Default for ProfileConfig {
    fn default() -> Self {
        Self {
            track_time: true,
            track_memory: true,
            track_passes: true,
            track_cache: true,
            min_duration_ms: 1,
        }
    }
}

/// Time spent in a compilation phase.
#[derive(Debug, Clone)]
pub struct PhaseTime {
    /// Phase name
    pub name: String,
    /// Total duration
    pub duration: Duration,
    /// Number of times this phase was executed
    pub count: usize,
    /// Child phases
    pub children: Vec<PhaseTime>,
}

impl PhaseTime {
    /// Create a new phase time entry.
    pub fn new(name: String, duration: Duration) -> Self {
        Self {
            name,
            duration,
            count: 1,
            children: Vec::new(),
        }
    }

    /// Get average duration per execution.
    pub fn average_duration(&self) -> Duration {
        if self.count == 0 {
            Duration::from_secs(0)
        } else {
            self.duration / self.count as u32
        }
    }

    /// Get total time including children.
    pub fn total_time_with_children(&self) -> Duration {
        let mut total = self.duration;
        for child in &self.children {
            total += child.total_time_with_children();
        }
        total
    }
}

/// Memory usage snapshot.
#[derive(Debug, Clone, Default)]
pub struct MemorySnapshot {
    /// Timestamp of snapshot
    pub timestamp: Option<Instant>,
    /// Estimated heap usage in bytes
    pub heap_bytes: usize,
    /// Number of active allocations
    pub allocation_count: usize,
}

impl MemorySnapshot {
    /// Create a new memory snapshot.
    pub fn new() -> Self {
        Self {
            timestamp: Some(Instant::now()),
            heap_bytes: 0,
            allocation_count: 0,
        }
    }

    /// Record an allocation.
    pub fn record_allocation(&mut self, size: usize) {
        self.heap_bytes += size;
        self.allocation_count += 1;
    }

    /// Record a deallocation.
    pub fn record_deallocation(&mut self, size: usize) {
        self.heap_bytes = self.heap_bytes.saturating_sub(size);
        self.allocation_count = self.allocation_count.saturating_sub(1);
    }
}

/// Pass-level profiling information.
#[derive(Debug, Clone)]
pub struct PassProfile {
    /// Pass name
    pub name: String,
    /// Number of times executed
    pub execution_count: usize,
    /// Total time spent
    pub total_time: Duration,
    /// Number of optimizations applied
    pub optimizations_applied: usize,
    /// Memory allocated during pass
    pub memory_allocated: usize,
}

impl PassProfile {
    /// Create a new pass profile.
    pub fn new(name: String) -> Self {
        Self {
            name,
            execution_count: 0,
            total_time: Duration::from_secs(0),
            optimizations_applied: 0,
            memory_allocated: 0,
        }
    }

    /// Record an execution of this pass.
    pub fn record_execution(&mut self, duration: Duration, optimizations: usize) {
        self.execution_count += 1;
        self.total_time += duration;
        self.optimizations_applied += optimizations;
    }

    /// Get average time per execution.
    pub fn average_time(&self) -> Duration {
        if self.execution_count == 0 {
            Duration::from_secs(0)
        } else {
            self.total_time / self.execution_count as u32
        }
    }

    /// Get optimizations per execution.
    pub fn optimizations_per_execution(&self) -> f64 {
        if self.execution_count == 0 {
            0.0
        } else {
            self.optimizations_applied as f64 / self.execution_count as f64
        }
    }
}

/// Cache statistics.
#[derive(Debug, Clone, Default)]
pub struct CacheStats {
    /// Total cache lookups
    pub lookups: usize,
    /// Cache hits
    pub hits: usize,
    /// Cache misses
    pub misses: usize,
    /// Cache evictions
    pub evictions: usize,
}

impl CacheStats {
    /// Calculate hit rate as a percentage.
    pub fn hit_rate(&self) -> f64 {
        if self.lookups == 0 {
            0.0
        } else {
            (self.hits as f64 / self.lookups as f64) * 100.0
        }
    }

    /// Calculate miss rate as a percentage.
    pub fn miss_rate(&self) -> f64 {
        100.0 - self.hit_rate()
    }

    /// Record a cache lookup.
    pub fn record_lookup(&mut self, hit: bool) {
        self.lookups += 1;
        if hit {
            self.hits += 1;
        } else {
            self.misses += 1;
        }
    }
}

/// Main compilation profiler.
pub struct CompilationProfiler {
    config: ProfileConfig,
    phases: Vec<PhaseTime>,
    active_phases: Vec<(String, Instant)>,
    memory_snapshots: Vec<MemorySnapshot>,
    pass_profiles: HashMap<String, PassProfile>,
    cache_stats: CacheStats,
    start_time: Option<Instant>,
}

impl CompilationProfiler {
    /// Create a new profiler with default configuration.
    pub fn new() -> Self {
        Self::with_config(ProfileConfig::default())
    }

    /// Create a new profiler with custom configuration.
    pub fn with_config(config: ProfileConfig) -> Self {
        Self {
            config,
            phases: Vec::new(),
            active_phases: Vec::new(),
            memory_snapshots: Vec::new(),
            pass_profiles: HashMap::new(),
            cache_stats: CacheStats::default(),
            start_time: None,
        }
    }

    /// Start overall compilation profiling.
    pub fn start(&mut self) {
        self.start_time = Some(Instant::now());
        self.phases.clear();
        self.active_phases.clear();
    }

    /// Start profiling a compilation phase.
    pub fn start_phase(&mut self, name: &str) {
        if !self.config.track_time {
            return;
        }

        self.active_phases.push((name.to_string(), Instant::now()));
    }

    /// End profiling a compilation phase.
    pub fn end_phase(&mut self, name: &str) {
        if !self.config.track_time {
            return;
        }

        if let Some(pos) = self.active_phases.iter().rposition(|(n, _)| n == name) {
            let (phase_name, start_time) = self.active_phases.remove(pos);
            let duration = start_time.elapsed();

            if duration.as_millis() >= self.config.min_duration_ms as u128 {
                self.phases.push(PhaseTime::new(phase_name, duration));
            }
        }
    }

    /// Record a pass execution.
    pub fn record_pass(&mut self, pass_name: &str, duration: Duration, optimizations: usize) {
        if !self.config.track_passes {
            return;
        }

        let profile = self
            .pass_profiles
            .entry(pass_name.to_string())
            .or_insert_with(|| PassProfile::new(pass_name.to_string()));

        profile.record_execution(duration, optimizations);
    }

    /// Take a memory snapshot.
    pub fn snapshot_memory(&mut self) {
        if !self.config.track_memory {
            return;
        }

        self.memory_snapshots.push(MemorySnapshot::new());
    }

    /// Record a cache lookup.
    pub fn record_cache_lookup(&mut self, hit: bool) {
        if !self.config.track_cache {
            return;
        }

        self.cache_stats.record_lookup(hit);
    }

    /// Get total compilation time.
    pub fn total_time(&self) -> Option<Duration> {
        self.start_time.map(|start| start.elapsed())
    }

    /// Get peak memory usage.
    pub fn peak_memory(&self) -> usize {
        self.memory_snapshots
            .iter()
            .map(|s| s.heap_bytes)
            .max()
            .unwrap_or(0)
    }

    /// Get the slowest compilation phase.
    pub fn slowest_phase(&self) -> Option<&PhaseTime> {
        self.phases.iter().max_by_key(|p| p.duration)
    }

    /// Get the most expensive pass (by total time).
    pub fn most_expensive_pass(&self) -> Option<&PassProfile> {
        self.pass_profiles.values().max_by_key(|p| p.total_time)
    }

    /// Generate a human-readable profiling report.
    pub fn generate_report(&self) -> String {
        let mut report = String::new();

        report.push_str("=== Compilation Profiling Report ===\n\n");

        // Overall stats
        if let Some(total) = self.total_time() {
            report.push_str(&format!("Total Time: {:.2?}\n", total));
        }

        if self.config.track_memory {
            report.push_str(&format!("Peak Memory: {} bytes\n", self.peak_memory()));
        }

        report.push('\n');

        // Phase breakdown
        if self.config.track_time && !self.phases.is_empty() {
            report.push_str("=== Phase Breakdown ===\n");
            for phase in &self.phases {
                report.push_str(&format!(
                    "  {}: {:.2?} ({} times, avg: {:.2?})\n",
                    phase.name,
                    phase.duration,
                    phase.count,
                    phase.average_duration()
                ));
            }
            report.push('\n');
        }

        // Pass profiles
        if self.config.track_passes && !self.pass_profiles.is_empty() {
            report.push_str("=== Optimization Passes ===\n");
            let mut passes: Vec<_> = self.pass_profiles.values().collect();
            passes.sort_by_key(|p| std::cmp::Reverse(p.total_time));

            for pass in passes.iter().take(10) {
                report.push_str(&format!(
                    "  {}: {:.2?} ({} execs, {:.1} opts/exec)\n",
                    pass.name,
                    pass.total_time,
                    pass.execution_count,
                    pass.optimizations_per_execution()
                ));
            }
            report.push('\n');
        }

        // Cache statistics
        if self.config.track_cache && self.cache_stats.lookups > 0 {
            report.push_str("=== Cache Statistics ===\n");
            report.push_str(&format!("  Lookups: {}\n", self.cache_stats.lookups));
            report.push_str(&format!("  Hits: {}\n", self.cache_stats.hits));
            report.push_str(&format!("  Misses: {}\n", self.cache_stats.misses));
            report.push_str(&format!(
                "  Hit Rate: {:.1}%\n",
                self.cache_stats.hit_rate()
            ));
            report.push('\n');
        }

        // Recommendations
        if let Some(slowest) = self.slowest_phase() {
            report.push_str("=== Recommendations ===\n");
            report.push_str(&format!(
                "  Slowest phase: {} ({:.2?})\n",
                slowest.name, slowest.duration
            ));

            if let Some(expensive_pass) = self.most_expensive_pass() {
                report.push_str(&format!(
                    "  Most expensive pass: {} ({:.2?})\n",
                    expensive_pass.name, expensive_pass.total_time
                ));
            }

            if self.config.track_cache && self.cache_stats.hit_rate() < 50.0 {
                report.push_str("  Consider increasing cache size (low hit rate)\n");
            }
        }

        report
    }

    /// Generate JSON profiling report.
    pub fn generate_json_report(&self) -> String {
        // Simple JSON serialization
        let mut json = String::from("{\n");

        if let Some(total) = self.total_time() {
            json.push_str(&format!("  \"total_time_ms\": {},\n", total.as_millis()));
        }

        json.push_str(&format!(
            "  \"peak_memory_bytes\": {},\n",
            self.peak_memory()
        ));

        // Phases
        json.push_str("  \"phases\": [\n");
        for (i, phase) in self.phases.iter().enumerate() {
            json.push_str(&format!(
                "    {{\"name\": \"{}\", \"duration_ms\": {}, \"count\": {}}}",
                phase.name,
                phase.duration.as_millis(),
                phase.count
            ));
            if i < self.phases.len() - 1 {
                json.push(',');
            }
            json.push('\n');
        }
        json.push_str("  ],\n");

        // Cache stats
        json.push_str("  \"cache\": {\n");
        json.push_str(&format!("    \"lookups\": {},\n", self.cache_stats.lookups));
        json.push_str(&format!("    \"hits\": {},\n", self.cache_stats.hits));
        json.push_str(&format!(
            "    \"hit_rate\": {:.2}\n",
            self.cache_stats.hit_rate()
        ));
        json.push_str("  }\n");

        json.push_str("}\n");
        json
    }
}

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

#[cfg(test)]
mod tests {
    use super::*;
    use std::thread;

    #[test]
    fn test_profiler_basic() {
        let mut profiler = CompilationProfiler::new();
        profiler.start();

        profiler.start_phase("test_phase");
        thread::sleep(Duration::from_millis(10));
        profiler.end_phase("test_phase");

        assert!(!profiler.phases.is_empty());
    }

    #[test]
    fn test_phase_time() {
        let phase = PhaseTime::new("test".to_string(), Duration::from_secs(1));
        assert_eq!(phase.name, "test");
        assert_eq!(phase.count, 1);
        assert_eq!(phase.average_duration(), Duration::from_secs(1));
    }

    #[test]
    fn test_memory_snapshot() {
        let mut snapshot = MemorySnapshot::new();
        snapshot.record_allocation(1000);
        snapshot.record_allocation(500);

        assert_eq!(snapshot.heap_bytes, 1500);
        assert_eq!(snapshot.allocation_count, 2);

        snapshot.record_deallocation(500);
        assert_eq!(snapshot.heap_bytes, 1000);
        assert_eq!(snapshot.allocation_count, 1);
    }

    #[test]
    fn test_pass_profile() {
        let mut profile = PassProfile::new("constant_folding".to_string());
        profile.record_execution(Duration::from_millis(10), 5);
        profile.record_execution(Duration::from_millis(15), 3);

        assert_eq!(profile.execution_count, 2);
        assert_eq!(profile.optimizations_applied, 8);
        assert!(profile.average_time().as_millis() >= 10);
        assert_eq!(profile.optimizations_per_execution(), 4.0);
    }

    #[test]
    fn test_cache_stats() {
        let mut stats = CacheStats::default();
        stats.record_lookup(true); // hit
        stats.record_lookup(true); // hit
        stats.record_lookup(false); // miss

        assert_eq!(stats.lookups, 3);
        assert_eq!(stats.hits, 2);
        assert_eq!(stats.misses, 1);
        assert!((stats.hit_rate() - 66.67).abs() < 0.1);
    }

    #[test]
    fn test_generate_report() {
        let mut profiler = CompilationProfiler::new();
        profiler.start();
        profiler.start_phase("compilation");
        thread::sleep(Duration::from_millis(10));
        profiler.end_phase("compilation");

        let report = profiler.generate_report();
        assert!(report.contains("Compilation Profiling Report"));
        assert!(report.contains("Total Time"));
    }

    #[test]
    fn test_slowest_phase() {
        let mut profiler = CompilationProfiler::new();
        profiler.start();

        profiler.start_phase("fast");
        thread::sleep(Duration::from_millis(5));
        profiler.end_phase("fast");

        profiler.start_phase("slow");
        thread::sleep(Duration::from_millis(20));
        profiler.end_phase("slow");

        let slowest = profiler.slowest_phase().unwrap();
        assert_eq!(slowest.name, "slow");
    }

    #[test]
    fn test_most_expensive_pass() {
        let mut profiler = CompilationProfiler::new();
        profiler.record_pass("pass1", Duration::from_millis(10), 5);
        profiler.record_pass("pass2", Duration::from_millis(50), 10);

        let expensive = profiler.most_expensive_pass().unwrap();
        assert_eq!(expensive.name, "pass2");
    }

    #[test]
    fn test_json_report() {
        let mut profiler = CompilationProfiler::new();
        profiler.start();
        profiler.record_cache_lookup(true);
        profiler.record_cache_lookup(false);

        let json = profiler.generate_json_report();
        assert!(json.contains("total_time_ms"));
        assert!(json.contains("cache"));
        assert!(json.contains("hit_rate"));
    }

    #[test]
    fn test_config_filtering() {
        let config = ProfileConfig {
            track_time: true,
            track_memory: false,
            track_passes: true,
            track_cache: false,
            min_duration_ms: 100,
        };

        let mut profiler = CompilationProfiler::with_config(config);
        profiler.start();

        // Short phase should be filtered out
        profiler.start_phase("short");
        thread::sleep(Duration::from_millis(1));
        profiler.end_phase("short");

        assert!(profiler.phases.is_empty());
    }

    #[test]
    fn test_nested_phases() {
        let mut profiler = CompilationProfiler::new();
        profiler.start();

        profiler.start_phase("outer");
        thread::sleep(Duration::from_millis(5));

        profiler.start_phase("inner");
        thread::sleep(Duration::from_millis(5));
        profiler.end_phase("inner");

        profiler.end_phase("outer");

        assert_eq!(profiler.phases.len(), 2);
    }
}