calyx_opt/
pass_manager.rs

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
//! Define the PassManager structure that is used to construct and run pass
//! passes.
use crate::traversal;
use calyx_ir as ir;
use calyx_utils::{Error, MultiError};
use std::collections::{HashMap, HashSet};
use std::fmt::Write as _;
use std::time::Instant;

pub type PassResult<T> = std::result::Result<T, MultiError>;

/// Top-level type for all passes that transform an [ir::Context]
pub type PassClosure = Box<dyn Fn(&mut ir::Context) -> PassResult<()>>;

/// Structure that tracks all registered passes for the compiler.
#[derive(Default)]
pub struct PassManager {
    /// All registered passes
    passes: HashMap<String, PassClosure>,
    /// Tracks alias for groups of passes that run together.
    aliases: HashMap<String, Vec<String>>,
    // Track the help information for passes
    help: HashMap<String, String>,
}

impl PassManager {
    /// Register a new Calyx pass and return an error if another pass with the
    /// same name has already been registered.
    ///
    /// ## Example
    /// ```rust
    /// let pm = PassManager::default();
    /// pm.register_pass::<WellFormed>()?;
    /// ```
    pub fn register_pass<Pass>(&mut self) -> PassResult<()>
    where
        Pass:
            traversal::Visitor + traversal::ConstructVisitor + traversal::Named,
    {
        self.register_generic_pass::<Pass>(Box::new(|ir| {
            Pass::do_pass_default(ir)?;
            Ok(())
        }))
    }

    /// Registers a diagnostic pass as a normal pass. If there is an error,
    /// this will report the first error gathered by the pass.
    pub fn register_diagnostic<Pass>(&mut self) -> PassResult<()>
    where
        Pass: traversal::Visitor
            + traversal::ConstructVisitor
            + traversal::Named
            + traversal::DiagnosticPass,
    {
        self.register_generic_pass::<Pass>(Box::new(|ir| {
            let mut visitor = Pass::from(ir)?;
            visitor.do_pass(ir)?;

            let errors: Vec<_> =
                visitor.diagnostics().errors_iter().cloned().collect();
            if !errors.is_empty() {
                Err(MultiError::from(errors))
            } else {
                // only show warnings, if there are no errors
                visitor.diagnostics().warning_iter().for_each(
                    |warning| log::warn!(target: Pass::name(), "{warning:?}"),
                );
                Ok(())
            }
        }))
    }

    fn register_generic_pass<Pass>(
        &mut self,
        pass_closure: PassClosure,
    ) -> PassResult<()>
    where
        Pass:
            traversal::Visitor + traversal::ConstructVisitor + traversal::Named,
    {
        let name = Pass::name().to_string();
        if self.passes.contains_key(&name) {
            return Err(Error::misc(format!(
                "Pass with name '{}' is already registered.",
                name
            ))
            .into());
        }
        self.passes.insert(name.clone(), pass_closure);
        let mut help = format!("- {}: {}", name, Pass::description());
        for opt in Pass::opts() {
            write!(
                &mut help,
                "\n  * {}: {} (default: {})",
                opt.name(),
                opt.description(),
                opt.default()
            )
            .unwrap();
        }
        self.help.insert(name, help);
        Ok(())
    }

    /// Adds a new alias for groups of passes. An alias is a list of strings
    /// that represent valid pass names OR an alias.
    /// The passes and aliases are executed in the order of specification.
    pub fn add_alias(
        &mut self,
        name: String,
        passes: Vec<String>,
    ) -> PassResult<()> {
        if self.aliases.contains_key(&name) {
            return Err(Error::misc(format!(
                "Alias with name '{}'  already registered.",
                name
            ))
            .into());
        }
        // Expand any aliases used in defining this alias.
        let all_passes = passes
            .into_iter()
            .flat_map(|pass| {
                if self.aliases.contains_key(&pass) {
                    self.aliases[&pass].clone()
                } else if self.passes.contains_key(&pass) {
                    vec![pass]
                } else {
                    panic!("No pass or alias named: {}", pass)
                }
            })
            .collect();
        self.aliases.insert(name, all_passes);
        Ok(())
    }

    /// Return the help string for a specific pass.
    pub fn specific_help(&self, pass: &str) -> Option<String> {
        self.help.get(pass).cloned().or_else(|| {
            self.aliases.get(pass).map(|passes| {
                let pass_str = passes
                    .iter()
                    .map(|p| format!("- {p}"))
                    .collect::<Vec<String>>()
                    .join("\n");
                format!("`{pass}' is an alias for pass pipeline:\n{}", pass_str)
            })
        })
    }

    /// Return a string representation to show all available passes and aliases.
    /// Appropriate for help text.
    pub fn complete_help(&self) -> String {
        let mut ret = String::with_capacity(1000);

        // Push all passes.
        let mut pass_names = self.passes.keys().collect::<Vec<_>>();
        pass_names.sort();
        ret.push_str("Passes:\n");
        pass_names.iter().for_each(|&pass| {
            writeln!(ret, "{}", self.help[pass]).unwrap();
        });

        // Push all aliases
        let mut aliases = self.aliases.iter().collect::<Vec<_>>();
        aliases.sort_by(|kv1, kv2| kv1.0.cmp(kv2.0));
        ret.push_str("\nAliases:\n");
        aliases.iter().for_each(|(alias, passes)| {
            let pass_str = passes
                .iter()
                .map(|p| p.to_string())
                .collect::<Vec<String>>()
                .join(", ");
            writeln!(ret, "- {}: {}", alias, pass_str).unwrap();
        });
        ret
    }

    /// Attempts to resolve the alias name. If there is no alias with this name,
    /// assumes that this is a pass instead.
    fn resolve_alias(&self, maybe_alias: &str) -> Vec<String> {
        self.aliases
            .get(maybe_alias)
            .cloned()
            .unwrap_or_else(|| vec![maybe_alias.to_string()])
    }

    /// Creates a plan using an inclusion and exclusion list which might contain
    /// aliases.
    fn create_plan(
        &self,
        incls: &[String],
        excls: &[String],
        insns: &[String],
    ) -> PassResult<(Vec<String>, HashSet<String>)> {
        let mut insertions = insns
            .iter()
            .filter_map(|str| match str.split_once(':') {
                Some((before, after)) => {
                    Some((before.to_string(), after.to_string()))
                }
                None => {
                    log::warn!("No ':' in {str}. Ignoring this option.");
                    None
                }
            })
            .collect::<Vec<_>>();
        // Incls and excls can have aliases in them. Resolve them.
        let mut passes = incls
            .iter()
            .flat_map(|maybe_alias| self.resolve_alias(maybe_alias))
            .collect::<Vec<_>>();

        let excl_set = excls
            .iter()
            .flat_map(|maybe_alias| self.resolve_alias(maybe_alias))
            .collect::<HashSet<String>>();

        // Validate that names of passes in incl and excl sets are known
        passes.iter().chain(excl_set.iter().chain(insertions.iter().flat_map(|(pass1, pass2)| vec![pass1, pass2]))).try_for_each(|pass| {
            if !self.passes.contains_key(pass) {
                Err(Error::misc(format!(
                    "Unknown pass: {pass}. Run compiler with pass-help subcommand to view registered passes."
                )))
            } else {
                Ok(())
            }
        })?;

        // Remove passes from `insertions` that are not slated to run.
        insertions.retain(|(pass1, pass2)|
            if !passes.contains(pass1) || excl_set.contains(pass1) {
                log::warn!("Pass {pass1} is not slated to run. Reordering will have no effect.");
                false
            }
            else if !passes.contains(pass2) || excl_set.contains(pass2) {
                log::warn!("Pass {pass2} is not slated to run. Reordering will have no effect.");
                false
            }
            else {
                true
            }
        );

        // Perform re-insertion.
        // Insert `after` right after `before`. If `after` already appears after
        // before, do nothing.
        for (before, after) in insertions {
            let before_idx =
                passes.iter().position(|pass| *pass == before).unwrap();
            let after_idx =
                passes.iter().position(|pass| *pass == after).unwrap();
            // Only need to perform re-insertion if it is actually out of order.
            if before_idx > after_idx {
                passes.insert(before_idx + 1, after);
                passes.remove(after_idx);
            }
        }

        Ok((passes, excl_set))
    }

    /// Executes a given "plan" constructed using the incl and excl lists.
    /// ord is a relative ordering that should be enforced.
    pub fn execute_plan(
        &self,
        ctx: &mut ir::Context,
        incl: &[String],
        excl: &[String],
        insn: &[String],
        dump_ir: bool,
    ) -> PassResult<()> {
        let (passes, excl_set) = self.create_plan(incl, excl, insn)?;

        for name in passes {
            // Pass is known to exist because create_plan validates the
            // names of passes.
            let pass = &self.passes[&name];

            // Conditional compilation for WASM target because Instant::now
            // is not supported.
            if cfg!(not(target_family = "wasm")) {
                if !excl_set.contains(&name) {
                    let start = Instant::now();
                    pass(ctx)?;
                    if dump_ir {
                        ir::Printer::write_context(
                            ctx,
                            true,
                            &mut std::io::stdout(),
                        )?;
                    }
                    let elapsed = start.elapsed();
                    // Warn if pass takes more than 3 seconds.
                    if elapsed.as_secs() > 5 {
                        log::warn!("{name}: {}ms", elapsed.as_millis());
                    } else {
                        log::info!("{name}: {}ms", start.elapsed().as_millis());
                    }
                } else {
                    log::info!("{name}: Ignored")
                }
            } else if !excl_set.contains(&name) {
                pass(ctx)?;
            }
        }

        Ok(())
    }
}

/// Simple macro to register an alias with a pass manager.
///
/// ## Example
/// ```
/// let pm = PassManager::default();
/// // Register passes WellFormed, Papercut, and Canonicalize.
/// register_alias!(pm, "validate", [WellFormed, Papercut, Canonicalize]);
/// ```
#[macro_export]
macro_rules! register_alias {
    (@unwrap_name $pass:ident) => {
        $pass::name().to_string()
    };

    (@unwrap_name $pass:literal) => {
        $pass.to_string()
    };

    ($manager:expr, $alias:literal, [ $($pass:tt),* $(,)? ]) => {
        $manager.add_alias($alias.to_string(), vec![
            $(register_alias!(@unwrap_name $pass)),*
        ])?;
    };
}