aprender-explain 0.70.1

PTX/SIMD/wgpu visualization and tracing CLI for Trueno
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
//! Sidebar widget rendering for the TUI dashboard.
//!
//! Contains render functions for registers, memory, roofline, bug hunting,
//! and muda (waste) warning panels using presentar canvas drawing.

use presentar_core::{Canvas, Color, Point, TextStyle};
use presentar_terminal::direct::DirectTerminalCanvas;

use crate::analyzer::MudaType;
use crate::ptx::BugSeverity;

use super::TuiApp;

/// Color constants for sidebar rendering.
const COLOR_GREEN: Color = Color {
    r: 0.3,
    g: 1.0,
    b: 0.3,
    a: 1.0,
};
const COLOR_YELLOW: Color = Color {
    r: 1.0,
    g: 1.0,
    b: 0.3,
    a: 1.0,
};
const COLOR_RED: Color = Color {
    r: 1.0,
    g: 0.3,
    b: 0.3,
    a: 1.0,
};
const COLOR_BLUE: Color = Color {
    r: 0.3,
    g: 0.5,
    b: 1.0,
    a: 1.0,
};
const COLOR_TEXT: Color = Color {
    r: 0.8,
    g: 0.8,
    b: 0.8,
    a: 1.0,
};

/// Draw a box border top line: `┌─ Title ───┐`
fn draw_box_top(
    canvas: &mut DirectTerminalCanvas<'_>,
    title: &str,
    x: f32,
    y: f32,
    width: u16,
    color: Color,
) {
    let style = TextStyle {
        color,
        ..Default::default()
    };
    let inner = (width as usize).saturating_sub(2);
    let title_part = format!("┌─ {} ", title);
    let fill_len = inner.saturating_sub(title_part.len().saturating_sub(1));
    let line = format!("{}{}┐", title_part, "─".repeat(fill_len));
    canvas.draw_text(&line, Point::new(x, y), &style);
}

/// Draw a box border bottom line: `└───────────┘`
fn draw_box_bottom(
    canvas: &mut DirectTerminalCanvas<'_>,
    x: f32,
    y: f32,
    width: u16,
    color: Color,
) {
    let style = TextStyle {
        color,
        ..Default::default()
    };
    let inner = (width as usize).saturating_sub(2);
    let line = format!("└{}┘", "─".repeat(inner));
    canvas.draw_text(&line, Point::new(x, y), &style);
}

/// Draw a bordered text line: `│ text...   │` padded to width.
fn draw_box_line(
    canvas: &mut DirectTerminalCanvas<'_>,
    text: &str,
    x: f32,
    y: f32,
    width: u16,
    text_color: Color,
    border_color: Color,
) {
    let border_style = TextStyle {
        color: border_color,
        ..Default::default()
    };
    let text_style = TextStyle {
        color: text_color,
        ..Default::default()
    };
    let inner = (width as usize).saturating_sub(4); // "│ " + " │"
    let padded: String = if text.len() > inner {
        text.chars().take(inner).collect()
    } else {
        format!("{:<width$}", text, width = inner)
    };
    canvas.draw_text("│ ", Point::new(x, y), &border_style);
    canvas.draw_text(&padded, Point::new(x + 2.0, y), &text_style);
    canvas.draw_text(" │", Point::new(x + 2.0 + inner as f32, y), &border_style);
}

/// Render the full sidebar onto the canvas.
pub(super) fn render_sidebar(
    canvas: &mut DirectTerminalCanvas<'_>,
    app: &TuiApp,
    x: f32,
    y: f32,
    width: u16,
    height: u16,
) {
    // Section heights (matching original layout constraints)
    let reg_h: u16 = 8;
    let mem_h: u16 = 6;
    let roof_h: u16 = 5;
    let bug_h: u16 = 6;
    let warn_h: u16 = height.saturating_sub(reg_h + mem_h + roof_h + bug_h);

    let mut cy = y;

    render_register_panel(canvas, app, x, cy, width, reg_h);
    cy += f32::from(reg_h);

    render_memory_panel(canvas, app, x, cy, width, mem_h);
    cy += f32::from(mem_h);

    render_roofline_panel(canvas, app, x, cy, width, roof_h);
    cy += f32::from(roof_h);

    render_bugs_panel(canvas, app, x, cy, width, bug_h);
    cy += f32::from(bug_h);

    render_warnings_panel(canvas, app, x, cy, width, warn_h);
}

fn render_register_panel(
    canvas: &mut DirectTerminalCanvas<'_>,
    app: &TuiApp,
    x: f32,
    y: f32,
    width: u16,
    _height: u16,
) {
    let regs = &app.report.registers;
    let total = regs.total();
    let occupancy = app.report.estimated_occupancy;

    let status_color = if total < 64 {
        COLOR_GREEN
    } else if total < 128 {
        COLOR_YELLOW
    } else {
        COLOR_RED
    };

    draw_box_top(canvas, "Registers", x, y, width, status_color);
    draw_box_line(
        canvas,
        &format!(".f32: {:3} / 255", regs.f32_regs),
        x,
        y + 1.0,
        width,
        COLOR_TEXT,
        status_color,
    );
    draw_box_line(
        canvas,
        &format!(".b32: {:3} / 255", regs.b32_regs),
        x,
        y + 2.0,
        width,
        COLOR_TEXT,
        status_color,
    );
    draw_box_line(
        canvas,
        &format!(".b64: {:3} / 255", regs.b64_regs),
        x,
        y + 3.0,
        width,
        COLOR_TEXT,
        status_color,
    );
    draw_box_line(
        canvas,
        &format!(".pred: {:2} / 8", regs.pred_regs),
        x,
        y + 4.0,
        width,
        COLOR_TEXT,
        status_color,
    );

    // Total + occupancy line with status color for the value
    let occ_text = format!("Total: {} -> {:.0}% occ", total, occupancy * 100.0);
    draw_box_line(
        canvas,
        &occ_text,
        x,
        y + 5.0,
        width,
        status_color,
        status_color,
    );

    // Empty line + bottom border
    draw_box_line(canvas, "", x, y + 6.0, width, COLOR_TEXT, status_color);
    draw_box_bottom(canvas, x, y + 7.0, width, status_color);
}

fn render_memory_panel(
    canvas: &mut DirectTerminalCanvas<'_>,
    app: &TuiApp,
    x: f32,
    y: f32,
    width: u16,
    _height: u16,
) {
    let mem = &app.report.memory;
    let coal_pct = mem.coalesced_ratio * 100.0;

    let status_color = if coal_pct >= 90.0 {
        COLOR_GREEN
    } else if coal_pct >= 70.0 {
        COLOR_YELLOW
    } else {
        COLOR_RED
    };

    draw_box_top(canvas, "Memory", x, y, width, status_color);
    draw_box_line(
        canvas,
        &format!("Global ld: {}", mem.global_loads),
        x,
        y + 1.0,
        width,
        COLOR_TEXT,
        status_color,
    );
    draw_box_line(
        canvas,
        &format!("Global st: {}", mem.global_stores),
        x,
        y + 2.0,
        width,
        COLOR_TEXT,
        status_color,
    );

    let coal_text = format!("Coalesced: {:.1}%", coal_pct);
    draw_box_line(
        canvas,
        &coal_text,
        x,
        y + 3.0,
        width,
        status_color,
        status_color,
    );

    draw_box_line(canvas, "", x, y + 4.0, width, COLOR_TEXT, status_color);
    draw_box_bottom(canvas, x, y + 5.0, width, status_color);
}

fn render_roofline_panel(
    canvas: &mut DirectTerminalCanvas<'_>,
    app: &TuiApp,
    x: f32,
    y: f32,
    width: u16,
    _height: u16,
) {
    let roof = &app.report.roofline;

    let (bound_text, bound_color) = if roof.memory_bound {
        ("Memory-bound", COLOR_YELLOW)
    } else {
        ("Compute-bound", COLOR_GREEN)
    };

    draw_box_top(canvas, "Roofline", x, y, width, COLOR_BLUE);
    draw_box_line(
        canvas,
        &format!("AI: {:.2} FLOP/B", roof.arithmetic_intensity),
        x,
        y + 1.0,
        width,
        COLOR_TEXT,
        COLOR_BLUE,
    );
    draw_box_line(
        canvas,
        &format!("Bottleneck: {}", bound_text),
        x,
        y + 2.0,
        width,
        bound_color,
        COLOR_BLUE,
    );
    draw_box_line(canvas, "", x, y + 3.0, width, COLOR_TEXT, COLOR_BLUE);
    draw_box_bottom(canvas, x, y + 4.0, width, COLOR_BLUE);
}

fn render_bugs_panel(
    canvas: &mut DirectTerminalCanvas<'_>,
    app: &TuiApp,
    x: f32,
    y: f32,
    width: u16,
    _height: u16,
) {
    let bug_report = &app.bug_report;
    let critical = bug_report.count_by_severity(BugSeverity::Critical);
    let high = bug_report.count_by_severity(BugSeverity::High);
    let medium = bug_report.count_by_severity(BugSeverity::Medium);

    let status_color = if critical > 0 {
        COLOR_RED
    } else if high > 0 {
        COLOR_YELLOW
    } else {
        COLOR_GREEN
    };

    draw_box_top(canvas, "Bug Hunt", x, y, width, status_color);

    if bug_report.bugs.is_empty() {
        draw_box_line(
            canvas,
            "No bugs detected",
            x,
            y + 1.0,
            width,
            COLOR_GREEN,
            status_color,
        );
        draw_box_line(canvas, "", x, y + 2.0, width, COLOR_TEXT, status_color);
        draw_box_line(canvas, "", x, y + 3.0, width, COLOR_TEXT, status_color);
    } else {
        let c_color = if critical > 0 { COLOR_RED } else { COLOR_GREEN };
        let h_color = if high > 0 { COLOR_YELLOW } else { COLOR_GREEN };
        let m_color = if medium > 0 { COLOR_BLUE } else { COLOR_GREEN };

        draw_box_line(
            canvas,
            &format!("P0 Critical: {}", critical),
            x,
            y + 1.0,
            width,
            c_color,
            status_color,
        );
        draw_box_line(
            canvas,
            &format!("P1 High: {}", high),
            x,
            y + 2.0,
            width,
            h_color,
            status_color,
        );
        draw_box_line(
            canvas,
            &format!("P2 Medium: {}", medium),
            x,
            y + 3.0,
            width,
            m_color,
            status_color,
        );
    }

    draw_box_line(canvas, "", x, y + 4.0, width, COLOR_TEXT, status_color);
    draw_box_bottom(canvas, x, y + 5.0, width, status_color);
}

fn render_warnings_panel(
    canvas: &mut DirectTerminalCanvas<'_>,
    app: &TuiApp,
    x: f32,
    y: f32,
    width: u16,
    height: u16,
) {
    let border_color = if app.report.warnings.is_empty() {
        COLOR_GREEN
    } else {
        COLOR_YELLOW
    };

    draw_box_top(canvas, "Muda (Waste)", x, y, width, border_color);

    if app.report.warnings.is_empty() {
        draw_box_line(
            canvas,
            "No Muda detected",
            x,
            y + 1.0,
            width,
            COLOR_GREEN,
            border_color,
        );
        // Fill remaining lines
        for row in 2..height.saturating_sub(1) {
            draw_box_line(
                canvas,
                "",
                x,
                y + f32::from(row),
                width,
                COLOR_TEXT,
                border_color,
            );
        }
    } else {
        let max_items = (height as usize).saturating_sub(2);
        for (i, w) in app.report.warnings.iter().take(max_items).enumerate() {
            let icon = match w.muda_type {
                MudaType::Transport => "! ",
                MudaType::Waiting => "~ ",
                MudaType::Overprocessing => "@ ",
            };
            let text = format!("{}{}", icon, w.description);
            draw_box_line(
                canvas,
                &text,
                x,
                y + 1.0 + i as f32,
                width,
                COLOR_YELLOW,
                border_color,
            );
        }
        // Fill remaining lines
        let used = app.report.warnings.len().min(max_items);
        for row in (used + 1)..height.saturating_sub(1) as usize {
            draw_box_line(
                canvas,
                "",
                x,
                y + row as f32,
                width,
                COLOR_TEXT,
                border_color,
            );
        }
    }

    draw_box_bottom(canvas, x, y + f32::from(height - 1), width, border_color);
}