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
use std::fmt;
pub use self::details::{TimingToken, PassTimes, take_current, add_to_current};
macro_rules! define_passes {
{ $enum:ident, $num_passes:ident, $descriptions:ident;
$($pass:ident: $desc:expr,)+
} => {
#[allow(non_camel_case_types)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum $enum { $($pass,)+ NoPass }
const $num_passes: usize = $enum::NoPass as usize;
const $descriptions: [&str; $num_passes] = [ $($desc),+ ];
$(
#[doc=$desc]
pub fn $pass() -> TimingToken {
details::start_pass($enum::$pass)
}
)+
}
}
define_passes!{
Pass, NUM_PASSES, DESCRIPTIONS;
process_file: "Processing test file",
parse_text: "Parsing textual Cretonne IL",
wasm_translate_module: "Translate WASM module",
wasm_translate_function: "Translate WASM function",
verifier: "Verify Cretonne IL",
verify_cssa: "Verify CSSA",
verify_liveness: "Verify live ranges",
verify_locations: "Verify value locations",
verify_flags: "Verify CPU flags",
compile: "Compilation passes",
flowgraph: "Control flow graph",
domtree: "Dominator tree",
loop_analysis: "Loop analysis",
preopt: "Pre-legalization rewriting",
legalize: "Legalization",
gvn: "Global value numbering",
licm: "Loop invariant code motion",
unreachable_code: "Remove unreachable blocks",
regalloc: "Register allocation",
ra_liveness: "RA liveness analysis",
ra_cssa: "RA coalescing CSSA",
ra_spilling: "RA spilling",
ra_reload: "RA reloading",
ra_coloring: "RA coloring",
prologue_epilogue: "Prologue/epilogue insertion",
binemit: "Binary machine code emission",
layout_renumber: "Layout full renumbering",
}
impl Pass {
pub fn idx(self) -> usize {
self as usize
}
}
impl fmt::Display for Pass {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match DESCRIPTIONS.get(self.idx()) {
Some(s) => f.write_str(s),
None => f.write_str("<no pass>"),
}
}
}
mod details {
use super::{Pass, NUM_PASSES, DESCRIPTIONS};
use std::cell::{Cell, RefCell};
use std::fmt;
use std::mem;
use std::time::{Instant, Duration};
pub struct TimingToken {
start: Instant,
pass: Pass,
prev: Pass,
}
#[derive(Default)]
struct PassTime {
total: Duration,
child: Duration,
}
#[derive(Default)]
pub struct PassTimes {
pass: [PassTime; NUM_PASSES],
}
impl fmt::Display for PassTimes {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
writeln!(f, "======== ======== ==================================")?;
writeln!(f, " Total Self Pass")?;
writeln!(f, "-------- -------- ----------------------------------")?;
for (time, desc) in self.pass.iter().zip(&DESCRIPTIONS) {
if time.total == Duration::default() {
continue;
}
fn fmtdur(mut dur: Duration, f: &mut fmt::Formatter) -> fmt::Result {
dur += Duration::new(0, 500_000);
let ms = dur.subsec_nanos() / 1_000_000;
write!(f, "{:4}.{:03} ", dur.as_secs(), ms)
}
fmtdur(time.total, f)?;
if let Some(s) = time.total.checked_sub(time.child) {
fmtdur(s, f)?;
}
writeln!(f, " {}", desc)?;
}
writeln!(f, "======== ======== ==================================")
}
}
thread_local!{
static CURRENT_PASS: Cell<Pass> = Cell::new(Pass::NoPass);
static PASS_TIME: RefCell<PassTimes> = RefCell::new(Default::default());
}
pub(super) fn start_pass(pass: Pass) -> TimingToken {
let prev = CURRENT_PASS.with(|p| p.replace(pass));
dbg!("timing: Starting {}, (during {})", pass, prev);
TimingToken {
start: Instant::now(),
pass,
prev,
}
}
impl Drop for TimingToken {
fn drop(&mut self) {
let duration = self.start.elapsed();
dbg!("timing: Ending {}", self.pass);
let old_cur = CURRENT_PASS.with(|p| p.replace(self.prev));
debug_assert_eq!(self.pass, old_cur, "Timing tokens dropped out of order");
PASS_TIME.with(|rc| {
let mut table = rc.borrow_mut();
table.pass[self.pass.idx()].total += duration;
if let Some(parent) = table.pass.get_mut(self.prev.idx()) {
parent.child += duration;
}
})
}
}
pub fn take_current() -> PassTimes {
PASS_TIME.with(|rc| mem::replace(&mut *rc.borrow_mut(), Default::default()))
}
pub fn add_to_current(times: PassTimes) {
PASS_TIME.with(|rc| for (a, b) in rc.borrow_mut().pass.iter_mut().zip(
×.pass,
)
{
a.total += b.total;
a.child += b.child;
})
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn display() {
assert_eq!(Pass::NoPass.to_string(), "<no pass>");
assert_eq!(Pass::regalloc.to_string(), "Register allocation");
}
}