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
//! Assembly inspection utilities for verifying zero-cost abstractions
//!
//! This module provides tools and documentation for inspecting generated assembly
//! to verify that Rust's zero-cost abstractions are working as expected.
//!
//! # Assembly Inspection Guide
//!
//! This module helps you verify zero-cost abstractions by inspecting generated assembly.
//!
//! ## Generating Assembly Output
//!
//! ### Method 1: Using cargo-asm (Recommended)
//!
//! ```bash
//! # Install cargo-asm
//! cargo install cargo-asm
//!
//! # Generate assembly for a specific function
//! cargo asm --release celers::dev_utils::TaskBuilder::new
//!
//! # Generate assembly with Intel syntax (easier to read)
//! cargo asm --release --intel celers::dev_utils::TaskBuilder::new
//! ```
//!
//! ### Method 2: Using rustc directly
//!
//! ```bash
//! # Generate assembly for the entire crate
//! cargo rustc --release -- --emit asm
//!
//! # Output will be in target/release/deps/*.s
//! ```
//!
//! ### Method 3: Using Compiler Explorer (Godbolt)
//!
//! Visit <https://rust.godbolt.org/> and paste your code to see assembly interactively.
//!
//! ## What to Look For
//!
//! ### 1. Function Inlining
//!
//! Zero-cost abstractions should be inlined. Look for:
//! - Direct instructions instead of `call` instructions
//! - No function preamble/epilogue for simple wrappers
//!
//! Example of good inlining:
//! ```asm
//! # Simple arithmetic directly in caller
//! add rdi, rsi
//! mov rax, rdi
//! ```
//!
//! Example of poor inlining (overhead):
//! ```asm
//! # Function call overhead
//! call my_wrapper_function
//! push rbp
//! mov rbp, rsp
//! ```
//!
//! ### 2. Dead Code Elimination
//!
//! Unused code should be completely removed:
//! - No assembly generated for unused branches
//! - Constant propagation should eliminate runtime checks
//!
//! ### 3. Iterator Optimization
//!
//! Iterator chains should compile to simple loops:
//! ```asm
//! # Should see a tight loop, not iterator machinery
//! .LBB0_1:
//! add rax, 1
//! cmp rax, rcx
//! jne .LBB0_1
//! ```
//!
//! ### 4. Monomorphization
//!
//! Generic functions should be specialized:
//! - No dynamic dispatch (vtable calls) unless using trait objects
//! - Type parameters resolved at compile time
//!
//! ## Example Commands for CeleRS
//!
//! ```bash
//! # Verify TaskBuilder is zero-cost
//! cargo asm --release --intel celers::dev_utils::TaskBuilder::build
//!
//! # Verify chain workflow construction
//! cargo asm --release --intel celers::canvas::Chain::new
//!
//! # Compare debug vs release
//! cargo asm celers::dev_utils::TaskBuilder::build > debug.asm
//! cargo asm --release celers::dev_utils::TaskBuilder::build > release.asm
//! diff debug.asm release.asm
//! ```
//!
//! ## Automated Verification
//!
//! Use these helper functions to automate assembly inspection:
use Command;
/// Generate assembly output for a specific function
///
/// # Example
///
/// ```rust,no_run
/// # #[cfg(feature = "dev-utils")]
/// # {
/// use celers::assembly_inspection::generate_asm;
///
/// let asm = generate_asm("celers::dev_utils::TaskBuilder::new", true).unwrap();
/// println!("Assembly:\n{}", asm);
/// # }
/// ```
/// Check if a function is properly inlined by looking for call instructions
///
/// Returns true if the function appears to be inlined (no call instructions found)
/// Count the number of instructions in the assembly
///
/// Useful for comparing optimization levels
/// Generate a performance report comparing debug and release assembly