sway-ir 0.72.1

Sway intermediate representation.
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
use crate::{
    create_arg_demotion_pass, create_arg_pointee_mutability_tagger_pass, create_ccp_pass,
    create_const_demotion_pass, create_const_folding_pass, create_cse_pass, create_dce_pass,
    create_dom_fronts_pass, create_dominators_pass, create_escaped_symbols_pass,
    create_fn_dedup_debug_profile_pass, create_fn_dedup_release_profile_pass,
    create_fn_inline_pass, create_globals_dce_pass, create_init_aggr_lowering_pass,
    create_mem2reg_pass, create_memcpyopt_pass, create_memcpyprop_reverse_pass,
    create_misc_demotion_pass, create_module_printer_pass, create_module_verifier_pass,
    create_postorder_pass, create_ret_demotion_pass, create_simplify_cfg_pass, create_sroa_pass,
    Context, Function, IrError, Module, ARG_DEMOTION_NAME, ARG_POINTEE_MUTABILITY_TAGGER_NAME,
    CCP_NAME, CONST_DEMOTION_NAME, CONST_FOLDING_NAME, CSE_NAME, DCE_NAME,
    FN_DEDUP_DEBUG_PROFILE_NAME, FN_DEDUP_RELEASE_PROFILE_NAME, FN_INLINE_NAME, GLOBALS_DCE_NAME,
    INIT_AGGR_LOWERING_NAME, MEM2REG_NAME, MEMCPYOPT_NAME, MEMCPYPROP_REVERSE_NAME,
    MISC_DEMOTION_NAME, RET_DEMOTION_NAME, SIMPLIFY_CFG_NAME, SROA_NAME,
};
use downcast_rs::{impl_downcast, Downcast};
use rustc_hash::FxHashMap;
use std::{
    any::{type_name, TypeId},
    cell::RefCell,
    collections::{hash_map, HashSet},
    ops::DerefMut,
};

/// Result of an analysis. Specific result must be downcasted to.
pub trait AnalysisResultT: Downcast {}
impl_downcast!(AnalysisResultT);
pub type AnalysisResult = Box<dyn AnalysisResultT>;

/// Program scope over which a pass executes.
pub trait PassScope {
    fn get_arena_idx(&self) -> slotmap::DefaultKey;
}
impl PassScope for Module {
    fn get_arena_idx(&self) -> slotmap::DefaultKey {
        self.0
    }
}
impl PassScope for Function {
    fn get_arena_idx(&self) -> slotmap::DefaultKey {
        self.0
    }
}

/// Is a pass an Analysis or a Transformation over the IR?
#[derive(Clone)]
pub enum PassMutability<S: PassScope> {
    /// An analysis pass, producing an analysis result.
    Analysis(fn(&Context, analyses: &AnalysisResults, S) -> Result<AnalysisResult, IrError>),
    /// A pass over the IR that can possibly modify it.
    Transform(fn(&mut Context, analyses: &AnalysisResults, S) -> Result<bool, IrError>),
}

/// A concrete version of [PassScope].
#[derive(Clone)]
pub enum ScopedPass {
    ModulePass(PassMutability<Module>),
    FunctionPass(PassMutability<Function>),
}

/// An analysis or transformation pass.
pub struct Pass {
    /// Pass identifier.
    pub name: &'static str,
    /// A short description.
    pub descr: &'static str,
    /// Other passes that this pass depends on.
    pub deps: Vec<&'static str>,
    /// The executor.
    pub runner: ScopedPass,
}

impl Pass {
    pub fn is_analysis(&self) -> bool {
        match &self.runner {
            ScopedPass::ModulePass(pm) => matches!(pm, PassMutability::Analysis(_)),
            ScopedPass::FunctionPass(pm) => matches!(pm, PassMutability::Analysis(_)),
        }
    }

    pub fn is_transform(&self) -> bool {
        !self.is_analysis()
    }

    pub fn is_module_pass(&self) -> bool {
        matches!(self.runner, ScopedPass::ModulePass(_))
    }

    pub fn is_function_pass(&self) -> bool {
        matches!(self.runner, ScopedPass::FunctionPass(_))
    }
}

#[derive(Default)]
pub struct AnalysisResults {
    // Hash from (AnalysisResultT, (PassScope, Scope Identity)) to an actual result.
    results: FxHashMap<(TypeId, (TypeId, slotmap::DefaultKey)), AnalysisResult>,
    name_typeid_map: FxHashMap<&'static str, TypeId>,
    pub is_log_enabled: bool,
    /// Amalgamated debug log from all passes
    log_string: RefCell<String>,
}

impl AnalysisResults {
    pub fn push_log(&self, log: impl AsRef<str>) {
        if self.is_log_enabled {
            self.log_string.borrow_mut().push_str(log.as_ref());
        }
    }

    /// Get the results of an analysis.
    /// Example analyses.get_analysis_result::<DomTreeAnalysis>(foo).
    pub fn get_analysis_result<T: AnalysisResultT, S: PassScope + 'static>(&self, scope: S) -> &T {
        self.results
            .get(&(
                TypeId::of::<T>(),
                (TypeId::of::<S>(), scope.get_arena_idx()),
            ))
            .unwrap_or_else(|| {
                panic!(
                    "Internal error. Analysis result {} unavailable for {} with idx {:?}",
                    type_name::<T>(),
                    type_name::<S>(),
                    scope.get_arena_idx()
                )
            })
            .downcast_ref()
            .expect("AnalysisResult: Incorrect type")
    }

    /// Is an analysis result available at the given scope?
    fn is_analysis_result_available<S: PassScope + 'static>(
        &self,
        name: &'static str,
        scope: S,
    ) -> bool {
        self.name_typeid_map
            .get(name)
            .and_then(|result_typeid| {
                self.results
                    .get(&(*result_typeid, (TypeId::of::<S>(), scope.get_arena_idx())))
            })
            .is_some()
    }

    /// Add a new result.
    fn add_result<S: PassScope + 'static>(
        &mut self,
        name: &'static str,
        scope: S,
        result: AnalysisResult,
    ) {
        let result_typeid = (*result).type_id();
        self.results.insert(
            (result_typeid, (TypeId::of::<S>(), scope.get_arena_idx())),
            result,
        );
        self.name_typeid_map.insert(name, result_typeid);
    }

    /// Invalidate all results at a given scope.
    fn invalidate_all_results_at_scope<S: PassScope + 'static>(&mut self, scope: S) {
        self.results
            .retain(|(_result_typeid, (scope_typeid, scope_idx)), _v| {
                (*scope_typeid, *scope_idx) != (TypeId::of::<S>(), scope.get_arena_idx())
            });
    }
}

/// Options when running the `PassManager`.
///
/// # Printing Options
///
/// Note that states of IR can always be printed by injecting the module printer pass
/// and just running the passes. That approach however offers less control over the
/// printing. E.g., requiring the printing to happen only if the previous passes
/// modified the IR cannot be done by simply injecting a module printer.
#[derive(Debug)]
pub struct Options {
    pub print_initial: bool,
    pub print_final: bool,
    pub print_modified_only: bool,
    pub print_metadata: bool,
    pub print_passes: HashSet<String>,
    pub force_verify_ir: bool,
    pub rounds: usize,
    pub log: bool,
}

impl Default for Options {
    fn default() -> Self {
        Self {
            print_initial: false,
            print_final: false,
            print_modified_only: false,
            print_metadata: false,
            print_passes: HashSet::default(),
            force_verify_ir: false,
            rounds: 2,
            log: false,
        }
    }
}

#[derive(Default)]
pub struct PassManager {
    passes: FxHashMap<&'static str, Pass>,
    analyses: AnalysisResults,
}

impl PassManager {
    pub const OPTIMIZATION_PASSES: [&'static str; 19] = [
        ARG_DEMOTION_NAME,
        ARG_POINTEE_MUTABILITY_TAGGER_NAME,
        CCP_NAME,
        CONST_DEMOTION_NAME,
        CONST_FOLDING_NAME,
        CSE_NAME,
        DCE_NAME,
        FN_DEDUP_DEBUG_PROFILE_NAME,
        FN_DEDUP_RELEASE_PROFILE_NAME,
        FN_INLINE_NAME,
        GLOBALS_DCE_NAME,
        INIT_AGGR_LOWERING_NAME,
        MEM2REG_NAME,
        MEMCPYOPT_NAME,
        MEMCPYPROP_REVERSE_NAME,
        MISC_DEMOTION_NAME,
        RET_DEMOTION_NAME,
        SIMPLIFY_CFG_NAME,
        SROA_NAME,
    ];

    /// Register a pass. Should be called only once for each pass.
    pub fn register(&mut self, pass: Pass) -> &'static str {
        for dep in &pass.deps {
            if let Some(dep_t) = self.lookup_registered_pass(dep) {
                if dep_t.is_transform() {
                    panic!(
                        "Pass {} cannot depend on a transformation pass {}",
                        pass.name, dep
                    );
                }
                if pass.is_function_pass() && dep_t.is_module_pass() {
                    panic!(
                        "Function pass {} cannot depend on module pass {}",
                        pass.name, dep
                    );
                }
            } else {
                panic!(
                    "Pass {} depends on a (yet) unregistered pass {}",
                    pass.name, dep
                );
            }
        }
        let pass_name = pass.name;
        match self.passes.entry(pass.name) {
            hash_map::Entry::Occupied(_) => {
                panic!("Trying to register an already registered pass");
            }
            hash_map::Entry::Vacant(entry) => {
                entry.insert(pass);
            }
        }
        pass_name
    }

    fn actually_run(&mut self, ir: &mut Context, pass: &'static str) -> Result<bool, IrError> {
        let mut modified = false;

        fn run_module_pass(
            pm: &mut PassManager,
            ir: &mut Context,
            pass: &'static str,
            module: Module,
        ) -> Result<bool, IrError> {
            let mut modified = false;
            let pass_t = pm.passes.get(pass).expect("Unregistered pass");
            for dep in pass_t.deps.clone() {
                let dep_t = pm.passes.get(dep).expect("Unregistered dependent pass");
                // If pass registration allows transformations as dependents, we could remove this I guess.
                assert!(dep_t.is_analysis());
                match dep_t.runner {
                    ScopedPass::ModulePass(_) => {
                        if !pm.analyses.is_analysis_result_available(dep, module) {
                            run_module_pass(pm, ir, dep, module)?;
                        }
                    }
                    ScopedPass::FunctionPass(_) => {
                        for f in module.function_iter(ir) {
                            if !pm.analyses.is_analysis_result_available(dep, f) {
                                run_function_pass(pm, ir, dep, f)?;
                            }
                        }
                    }
                }
            }

            // Get the pass again to satisfy the borrow checker.
            let pass_t = pm.passes.get(pass).expect("Unregistered pass");
            let ScopedPass::ModulePass(mp) = pass_t.runner.clone() else {
                panic!("Expected a module pass");
            };
            match mp {
                PassMutability::Analysis(analysis) => {
                    let result = analysis(ir, &pm.analyses, module)?;
                    pm.analyses.add_result(pass, module, result);
                }
                PassMutability::Transform(transform) => {
                    if transform(ir, &pm.analyses, module)? {
                        pm.analyses.invalidate_all_results_at_scope(module);
                        for f in module.function_iter(ir) {
                            pm.analyses.invalidate_all_results_at_scope(f);
                        }
                        modified = true;
                    }
                }
            }

            Ok(modified)
        }

        fn run_function_pass(
            pm: &mut PassManager,
            ir: &mut Context,
            pass: &'static str,
            function: Function,
        ) -> Result<bool, IrError> {
            let mut modified = false;
            let pass_t = pm.passes.get(pass).expect("Unregistered pass");
            for dep in pass_t.deps.clone() {
                let dep_t = pm.passes.get(dep).expect("Unregistered dependent pass");
                // If pass registration allows transformations as dependents, we could remove this I guess.
                assert!(dep_t.is_analysis());
                match dep_t.runner {
                    ScopedPass::ModulePass(_) => {
                        panic!("Function pass {pass} cannot depend on module pass {dep}")
                    }
                    ScopedPass::FunctionPass(_) => {
                        if !pm.analyses.is_analysis_result_available(dep, function) {
                            run_function_pass(pm, ir, dep, function)?;
                        };
                    }
                }
            }

            // Get the pass again to satisfy the borrow checker.
            let pass_t = pm.passes.get(pass).expect("Unregistered pass");
            let ScopedPass::FunctionPass(fp) = pass_t.runner.clone() else {
                panic!("Expected a function pass");
            };
            match fp {
                PassMutability::Analysis(analysis) => {
                    let result = analysis(ir, &pm.analyses, function)?;
                    pm.analyses.add_result(pass, function, result);
                }
                PassMutability::Transform(transform) => {
                    if transform(ir, &pm.analyses, function)? {
                        pm.analyses.invalidate_all_results_at_scope(function);
                        modified = true;
                    }
                }
            }

            Ok(modified)
        }

        for m in ir.module_iter() {
            let pass_t = self.passes.get(pass).expect("Unregistered pass");
            let pass_runner = pass_t.runner.clone();
            match pass_runner {
                ScopedPass::ModulePass(_) => {
                    modified |= run_module_pass(self, ir, pass, m)?;
                }
                ScopedPass::FunctionPass(_) => {
                    for f in m.function_iter(ir) {
                        modified |= run_function_pass(self, ir, pass, f)?;
                    }
                }
            }
        }
        Ok(modified)
    }

    /// Run the `passes` and return true if the `passes` modify the initial `ir`.
    /// The IR states are printed according to the options provided and verified.
    pub fn run(
        &mut self,
        ir: &mut Context,
        passes: &PassGroup,
        options: &Options,
    ) -> Result<bool, IrError> {
        if options.print_initial {
            print_initial_or_final_ir(ir, "Initial", options.print_metadata);
        }

        self.analyses.is_log_enabled = options.log;
        self.analyses.log_string.borrow_mut().clear();

        // Verify before we start
        ir.verify()?;

        let mut global_modified = false;

        for _ in 0..options.rounds {
            let mut iter_modified = false;

            for pass in passes.flatten_pass_group() {
                // Save IR before optimisation only when forcing verification
                let ir_before = if options.force_verify_ir {
                    ir.to_string()
                } else {
                    String::new()
                };

                // run the pass
                let modified = self.actually_run(ir, pass)?;

                // Save IR after optimisation only when forcing verification
                let ir_after = if options.force_verify_ir {
                    ir.to_string()
                } else {
                    String::new()
                };

                iter_modified |= modified;

                if options.print_passes.contains(pass) && (!options.print_modified_only || modified)
                {
                    print_ir_after_pass(
                        ir,
                        self.lookup_registered_pass(pass).unwrap(),
                        options.print_metadata,
                    );
                }

                ir.verify()?;

                if options.force_verify_ir {
                    // Verify pass correctly return modified
                    let ir_modified = ir_before != ir_after;
                    if modified != ir_modified {
                        return Err(IrError::InvalidPassModified {
                            pass: pass.to_string(),
                            returned: modified,
                            comparison: ir_modified,
                        });
                    }
                }
            }

            global_modified |= iter_modified;
            if !iter_modified {
                break;
            }
        }

        if options.print_final {
            print_initial_or_final_ir(ir, "Final", options.print_metadata);
        }

        Ok(global_modified)
    }

    /// Get reference to a registered pass.
    pub fn lookup_registered_pass(&self, name: &str) -> Option<&Pass> {
        self.passes.get(name)
    }

    pub fn take_log(&self) -> String {
        let mut log = self.analyses.log_string.borrow_mut();
        std::mem::take(log.deref_mut())
    }

    pub fn help_text(&self) -> String {
        let summary = self
            .passes
            .iter()
            .map(|(name, pass)| format!("  {name:16} - {}", pass.descr))
            .collect::<Vec<_>>()
            .join("\n");

        format!("Valid pass names are:\n\n{summary}",)
    }
}

// Empty IRs are result of compiling dependencies. We don't want to print those.
fn ir_is_empty(ir: &Context) -> bool {
    ir.functions.is_empty()
        && ir.blocks.is_empty()
        && ir.values.is_empty()
        && ir.local_vars.is_empty()
}

fn print_ir_after_pass(ir: &Context, pass: &Pass, print_metadata: bool) {
    if !ir_is_empty(ir) {
        println!("// IR: [{}] {}", pass.name, pass.descr);
        println!(
            "{}",
            crate::printer::to_string_with_metadata(ir, print_metadata)
        );
    }
}

fn print_initial_or_final_ir(ir: &Context, initial_or_final: &'static str, print_metadata: bool) {
    if !ir_is_empty(ir) {
        println!("// IR: {initial_or_final}");
        println!(
            "{}",
            crate::printer::to_string_with_metadata(ir, print_metadata)
        );
    }
}

/// A group of passes.
/// Can contain sub-groups.
#[derive(Default)]
pub struct PassGroup(Vec<PassOrGroup>);

/// An individual pass, or a group (with possible subgroup) of passes.
pub enum PassOrGroup {
    Pass(&'static str),
    Group(PassGroup),
}

impl PassGroup {
    // Flatten a group of passes into an ordered list.
    fn flatten_pass_group(&self) -> Vec<&'static str> {
        let mut output = Vec::<&str>::new();
        fn inner(output: &mut Vec<&str>, input: &PassGroup) {
            for pass_or_group in &input.0 {
                match pass_or_group {
                    PassOrGroup::Pass(pass) => output.push(pass),
                    PassOrGroup::Group(pg) => inner(output, pg),
                }
            }
        }
        inner(&mut output, self);
        output
    }

    /// Append a pass to this group.
    pub fn append_pass(&mut self, pass: &'static str) {
        self.0.push(PassOrGroup::Pass(pass));
    }

    /// Append a pass group.
    pub fn append_group(&mut self, group: PassGroup) {
        self.0.push(PassOrGroup::Group(group));
    }
}

/// A convenience utility to register known passes.
pub fn register_known_passes(pm: &mut PassManager) {
    // Analysis passes.
    pm.register(create_postorder_pass());
    pm.register(create_dominators_pass());
    pm.register(create_dom_fronts_pass());
    pm.register(create_escaped_symbols_pass());
    pm.register(create_module_printer_pass());
    pm.register(create_module_verifier_pass());

    // Lowering passes.
    pm.register(create_init_aggr_lowering_pass());

    // Optimization passes.
    pm.register(create_arg_pointee_mutability_tagger_pass());
    pm.register(create_fn_dedup_release_profile_pass());
    pm.register(create_fn_dedup_debug_profile_pass());
    pm.register(create_mem2reg_pass());
    pm.register(create_sroa_pass());
    pm.register(create_fn_inline_pass());
    pm.register(create_const_folding_pass());
    pm.register(create_ccp_pass());
    pm.register(create_simplify_cfg_pass());
    pm.register(create_globals_dce_pass());
    pm.register(create_dce_pass());
    pm.register(create_cse_pass());
    pm.register(create_arg_demotion_pass());
    pm.register(create_const_demotion_pass());
    pm.register(create_ret_demotion_pass());
    pm.register(create_misc_demotion_pass());
    pm.register(create_memcpyopt_pass());
    pm.register(create_memcpyprop_reverse_pass());
}

pub fn create_o1_pass_group() -> PassGroup {
    let mut o1 = PassGroup::default();
    o1.append_pass(MEM2REG_NAME);
    o1.append_pass(FN_DEDUP_RELEASE_PROFILE_NAME);
    o1.append_pass(FN_INLINE_NAME);
    o1.append_pass(ARG_POINTEE_MUTABILITY_TAGGER_NAME);
    o1.append_pass(SIMPLIFY_CFG_NAME);
    o1.append_pass(GLOBALS_DCE_NAME);
    o1.append_pass(DCE_NAME);
    o1.append_pass(FN_INLINE_NAME);
    o1.append_pass(ARG_POINTEE_MUTABILITY_TAGGER_NAME);
    o1.append_pass(CCP_NAME);
    o1.append_pass(CONST_FOLDING_NAME);
    o1.append_pass(SIMPLIFY_CFG_NAME);
    o1.append_pass(CSE_NAME);
    o1.append_pass(CONST_FOLDING_NAME);
    o1.append_pass(SIMPLIFY_CFG_NAME);
    o1.append_pass(GLOBALS_DCE_NAME);
    o1.append_pass(DCE_NAME);
    o1.append_pass(FN_DEDUP_RELEASE_PROFILE_NAME);

    o1
}

/// Utility to insert a pass after every pass in the given group `pg`.
/// It preserves the `pg` group's structure. This means if `pg` has subgroups
/// and those have subgroups, the resulting [PassGroup] will have the
/// same subgroups, but with the `pass` inserted after every pass in every
/// subgroup, as well as all passes outside of any groups.
pub fn insert_after_each(pg: PassGroup, pass: &'static str) -> PassGroup {
    fn insert_after_each_rec(pg: PassGroup, pass: &'static str) -> Vec<PassOrGroup> {
        pg.0.into_iter()
            .flat_map(|p_o_g| match p_o_g {
                PassOrGroup::Group(group) => vec![PassOrGroup::Group(PassGroup(
                    insert_after_each_rec(group, pass),
                ))],
                PassOrGroup::Pass(_) => vec![p_o_g, PassOrGroup::Pass(pass)],
            })
            .collect()
    }

    PassGroup(insert_after_each_rec(pg, pass))
}