Skip to main content

charon_lib/transform/simplify_output/
inline_selected_functions.rs

1use std::{collections::HashMap, mem};
2
3use crate::transform::CowBox;
4use crate::transform::{TransformCtx, ctx::UllbcPass};
5use crate::ullbc_ast::*;
6
7pub struct Transform {
8    to_inline: HashMap<FunDeclId, FunDecl>,
9}
10
11impl Transform {
12    pub fn new(ctx: &mut TransformCtx) -> CowBox<dyn UllbcPass> {
13        let panic_name = Name::from_path(names::EXPLICIT_PANIC_NAME);
14        let panic_explicit = ctx
15            .translated
16            .fun_decls
17            .iter_indexed()
18            .find(|(_, decl)| decl.item_meta.name == panic_name)
19            .map(|(id, _)| id);
20
21        let repeated_globals =
22            super::anon_const_to_call::globals_in_constant_repeats(&ctx.translated);
23
24        // Collect and remove the functions that we want to inline.
25        let to_inline = ctx
26            .translated
27            .fun_decls
28            .extract(|_, decl| {
29                decl.body.as_unstructured().is_some_and(|body| {
30                    // `panic!` generates a function item named `panic_cold_explicit` that calls
31                    // `panic_explicit`. We inline that function.
32                    let block = &body.body[START_BLOCK_ID];
33                    let is_local_panic_fn = if decl.item_meta.name.short_str()
34                        == Some("panic_cold_explicit")
35                        && matches!(body.body.len(), 2 | 3)
36                        && block.statements.is_empty()
37                        && let TerminatorKind::Call { call, .. } = &block.terminator.kind
38                        && let FnOperand::Regular(fn_ptr) = &call.func
39                        && let FnPtrKind::Fun(id) = fn_ptr.kind.as_ref()
40                        && Some(*id) == panic_explicit
41                    {
42                        body.body.iter_enumerated().all(|(id, block)| {
43                            id == START_BLOCK_ID
44                                || block.statements.is_empty()
45                                    && matches!(
46                                        block.terminator.kind,
47                                        TerminatorKind::UnwindResume
48                                            | TerminatorKind::UnwindTerminate(..)
49                                            | TerminatorKind::UndefinedBehavior
50                                    )
51                        })
52                    } else {
53                        false
54                    };
55                    // The `anon_consts_to_call` pass transformed references to anon consts into
56                    // calls to their initializers, so we only have to inline these. Keep those
57                    // that are still needed because they're inside a repeat operation.
58                    let is_inlineable_anon_const = if let FunSource::GlobalInitializer(global) =
59                        &decl.src
60                        && let Some(gdecl) = ctx.translated.global_decls.get(global.id)
61                    {
62                        matches!(gdecl.global_kind, GlobalKind::AnonConst)
63                            && !repeated_globals.contains(&global.id)
64                    } else {
65                        false
66                    };
67                    let is_vec_construction_fn = decl.item_meta.diagnostic_item.as_deref()
68                        == Some(names::BOX_ASSUME_INIT_INTO_VEC_UNSAFE);
69                    is_local_panic_fn
70                        || (is_inlineable_anon_const && ctx.options.inline_anon_consts)
71                        || (is_vec_construction_fn && ctx.options.treat_box_as_builtin)
72                })
73            })
74            .collect();
75
76        CowBox::Owned(Box::new(Transform { to_inline }))
77    }
78}
79impl UllbcPass for Transform {
80    fn should_run(&self, _options: &crate::options::TranslateOptions) -> bool {
81        !self.to_inline.is_empty()
82    }
83    fn apply_preceding_passes(&mut self, ctx: &mut TransformCtx, passes: &[CowBox<dyn UllbcPass>]) {
84        for decl in self.to_inline.values_mut() {
85            for pass in passes {
86                pass.transform_item(ctx, ItemRefMut::Fun(decl));
87            }
88        }
89    }
90    fn transform_body(&self, _ctx: &mut TransformCtx, outer_body: &mut ullbc_ast::ExprBody) {
91        for block_id in outer_body.body.indices() {
92            let Some(block) = outer_body.body.get_mut(block_id) else {
93                continue;
94            };
95            let TerminatorKind::Call {
96                call: Call { func, args, .. },
97                dest,
98                target,
99                on_unwind,
100            } = &mut block.terminator.kind
101            else {
102                continue;
103            };
104            let target = *target;
105            let on_unwind = *on_unwind;
106            let kind = block.kind;
107            let dest_place = dest.clone();
108            let args = args.clone();
109            let FnOperand::Regular(fn_ptr) = &func else {
110                continue;
111            };
112            let FnPtrKind::Fun(fun_id) = fn_ptr.kind.as_ref() else {
113                continue;
114            };
115            let Some(initializer) = self.to_inline.get(fun_id) else {
116                continue;
117            };
118            let span = initializer.item_meta.span;
119            let Some(inner_body) = initializer.body.as_unstructured() else {
120                continue;
121            };
122
123            // We inline the required body by shifting its local ids and block ids
124            // and adding its blocks to the outer body. The inner body's return
125            // local becomes a normal local that we can read from. We redirect some
126            // gotos so that the inner body is executed before the current block.
127            let mut inner_body = {
128                let mut inner_body = inner_body.clone();
129                let inner_bound = inner_body.bound_body_regions;
130
131                // Shift all the body regions in the inner body BEFORE substitution,
132                // so that we only shift the inner body's own regions.
133                inner_body.dyn_visit_mut(|r: &mut Region| {
134                    if let Region::Body(v) = r {
135                        *v += outer_body.bound_body_regions;
136                    }
137                });
138                outer_body.bound_body_regions += inner_bound;
139
140                // Now substitute generics. This may inject outer-body Region::Body
141                // IDs, which is correct since they don't need shifting.
142                inner_body.substitute(&fn_ptr.generics)
143            };
144
145            let return_local = outer_body.locals.locals.next_idx();
146            inner_body.dyn_visit_in_body_mut(|l: &mut LocalId| {
147                *l += return_local;
148            });
149            outer_body
150                .locals
151                .locals
152                .extend(mem::take(&mut inner_body.locals.locals));
153
154            // The inner body assumes the arg places are live; allocate them, and initialize the
155            // args.
156            inner_body.body[0].statements.splice(
157                0..0,
158                args.into_iter()
159                    .enumerate()
160                    .flat_map(|(i, arg)| {
161                        let arg_local = return_local + i + 1;
162                        let arg_place = outer_body.locals.place_for_var(arg_local);
163                        [
164                            StatementKind::StorageLive(arg_local),
165                            StatementKind::Assign(arg_place, Rvalue::Use(arg, WithRetag::Yes)),
166                        ]
167                    })
168                    .map(|kind| Statement::new(span, kind)),
169            );
170
171            let mut final_block = BlockData::new_goto(span, target, kind);
172
173            // The inner body will write to `return_place`, but the outer body expects the value at
174            // `dest_place`.
175            let return_place = outer_body.locals.place_for_var(return_local);
176            final_block.statements.push(Statement::new(
177                span,
178                StatementKind::Assign(
179                    dest_place,
180                    Rvalue::Use(Operand::Move(return_place), WithRetag::Yes),
181                ),
182            ));
183            let final_block = outer_body.body.push(final_block);
184
185            // Shift all block ids in the inner body and point return/unwind to where they should.
186            let start_block = outer_body.body.next_idx();
187            inner_body.visit_block_ids_mut(|b: &mut BlockId| {
188                *b += start_block;
189            });
190            inner_body
191                .body
192                .dyn_visit_in_body_mut(|t: &mut Terminator| match t.kind {
193                    TerminatorKind::Return => {
194                        t.kind = TerminatorKind::Goto {
195                            target: final_block,
196                        };
197                    }
198                    TerminatorKind::UnwindResume => {
199                        t.kind = TerminatorKind::Goto { target: on_unwind };
200                    }
201                    _ => (),
202                });
203            if kind == UnwindKind::Cleanup {
204                for block in &mut inner_body.body {
205                    block.kind = block.kind.further_unwind_kind();
206                }
207            }
208            // At the end of the current block, start evaluating the inner body.
209            outer_body.body[block_id].terminator.kind = TerminatorKind::Goto {
210                target: start_block,
211            };
212            // Add the blocks for the inner body.
213            outer_body.body.extend(inner_body.body);
214        }
215    }
216}