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
use arret_runtime::boxed;

use crate::codegen::alloc::{AllocAtom, BoxSource, CondPlan};
use crate::codegen::analysis::escape::{CaptureKind, Captures};
use crate::codegen::target_gen::TargetCtx;
use crate::mir::ops;

struct AllocInfo {
    output_reg: ops::RegId,
    box_size: boxed::BoxSize,
}

/// Determines if an op requires the heap to be in a consistent state before it's executed
///
/// Our `AllocAtom`s cannot span these operations
fn op_needs_heap_checkpoint(tcx: &mut TargetCtx, op: &ops::Op) -> bool {
    use crate::mir::ops::OpKind;

    match op.kind() {
        OpKind::Ret(_)
        | OpKind::RetVoid
        | OpKind::Unreachable
        | OpKind::Call(_, _)
        | OpKind::Panic(_)
        | OpKind::Int64CheckedAdd(_, _)
        | OpKind::Int64CheckedSub(_, _)
        | OpKind::Int64CheckedMul(_, _)
        | OpKind::Int64CheckedDiv(_, _)
        | OpKind::Int64CheckedRem(_, _) => true,
        OpKind::Cond(cond_op) => cond_op
            .true_ops
            .iter()
            .chain(cond_op.false_ops.iter())
            // We additionally need to make sure we don't allocate in our branches. Otherwise we
            // might need to plan an allocation of a dynamic size to cover each branch. Instead
            // just start a new atom for each branch.
            .any(|op| op_needs_heap_checkpoint(tcx, op) || op_alloc_info(tcx, op).is_some()),
        _ => false,
    }
}

/// Returns the output reg for an allocating op, or `None` otherwise
fn op_alloc_info(tcx: &mut TargetCtx, op: &ops::Op) -> Option<AllocInfo> {
    use crate::mir::ops::OpKind;

    match op.kind() {
        OpKind::AllocBoxedInt(output_reg, _) => Some(AllocInfo {
            output_reg: *output_reg,
            box_size: boxed::Int::size(),
        }),
        OpKind::AllocBoxedFloat(output_reg, _) => Some(AllocInfo {
            output_reg: *output_reg,
            box_size: boxed::Float::size(),
        }),
        OpKind::AllocBoxedChar(output_reg, _) => Some(AllocInfo {
            output_reg: *output_reg,
            box_size: boxed::Char::size(),
        }),
        OpKind::AllocBoxedSym(output_reg, _) => Some(AllocInfo {
            output_reg: *output_reg,
            box_size: boxed::Sym::size(),
        }),
        OpKind::AllocBoxedPair(output_reg, _) => Some(AllocInfo {
            output_reg: *output_reg,
            box_size: boxed::Pair::<boxed::Any>::size(),
        }),
        OpKind::AllocBoxedFunThunk(output_reg, _) => Some(AllocInfo {
            output_reg: *output_reg,
            box_size: boxed::FunThunk::size(),
        }),
        OpKind::AllocBoxedRecord(output_reg, box_record_op) => {
            let record_storage = tcx
                .target_record_struct(&box_record_op.record_struct)
                .record_storage;

            Some(AllocInfo {
                output_reg: *output_reg,
                box_size: record_storage.box_size(),
            })
        }
        _ => None,
    }
}

pub fn plan_allocs<'op>(
    tcx: &mut TargetCtx,
    captures: &Captures,
    ops: &'op [ops::Op],
) -> Vec<AllocAtom<'op>> {
    use std::mem;

    let mut atoms = vec![];
    let mut current_atom = AllocAtom::new(&ops[0..]);

    for (i, op) in ops.iter().enumerate() {
        let checkpointing_op = op_needs_heap_checkpoint(tcx, op);

        if checkpointing_op && !current_atom.is_empty() {
            atoms.push(mem::replace(&mut current_atom, AllocAtom::new(&ops[i..])));
        }

        if let ops::OpKind::Cond(ops::CondOp {
            true_ops,
            false_ops,
            ..
        }) = op.kind()
        {
            current_atom.cond_plans.push(CondPlan {
                true_subplan: plan_allocs(tcx, captures, true_ops),
                false_subplan: plan_allocs(tcx, captures, false_ops),
            });
        } else if let Some(AllocInfo {
            output_reg,
            box_size,
        }) = op_alloc_info(tcx, op)
        {
            if captures.get(output_reg) == CaptureKind::Never {
                current_atom.box_sources.push(BoxSource::Stack);
            } else {
                current_atom.box_sources.push(BoxSource::Heap(box_size));
            }
        }

        current_atom.push_op();

        if checkpointing_op {
            atoms.push(mem::replace(
                &mut current_atom,
                AllocAtom::new(&ops[i + 1..]),
            ));
        }
    }

    if !current_atom.is_empty() {
        atoms.push(current_atom);
    }

    atoms
}

#[cfg(test)]
mod test {
    use super::*;

    /// Plans allocations assuming the native data layout
    fn plan_native_allocs(ops: &[ops::Op]) -> Vec<AllocAtom<'_>> {
        use llvm_sys::target_machine::*;

        use crate::codegen::target_machine::create_target_machine;
        use crate::codegen::test::initialise_test_llvm;

        initialise_test_llvm();

        let target_machine = create_target_machine(
            None,
            LLVMRelocMode::LLVMRelocDynamicNoPic,
            LLVMCodeModel::LLVMCodeModelDefault,
        );

        let mut tcx = TargetCtx::new(target_machine, false);
        let atoms = plan_allocs(&mut tcx, &Captures::new(), ops);

        unsafe {
            LLVMDisposeTargetMachine(target_machine);
        }

        atoms
    }

    #[test]
    fn empty_ops() {
        let actual_atoms = plan_native_allocs(&[]);
        assert_eq!(0, actual_atoms.len());
    }

    #[test]
    fn condless_allocs() {
        let reg1 = ops::RegId::alloc();
        let reg2 = ops::RegId::alloc();
        let reg3 = ops::RegId::alloc();
        let reg4 = ops::RegId::alloc();

        let input_ops = [
            ops::OpKind::AllocBoxedInt(reg1, reg1).into(),
            ops::OpKind::ConstBoxedTrue(reg2, ()).into(),
            ops::OpKind::RetVoid.into(),
            ops::OpKind::AllocBoxedInt(reg3, reg3).into(),
            ops::OpKind::AllocBoxedInt(reg4, reg4).into(),
        ];

        let expected_atoms = vec![
            AllocAtom {
                box_sources: vec![BoxSource::Stack],
                cond_plans: vec![],
                ops_base: &input_ops[0..],
                ops_count: 2,
            },
            AllocAtom {
                box_sources: vec![],
                cond_plans: vec![],
                ops_base: &input_ops[2..],
                ops_count: 1,
            },
            AllocAtom {
                box_sources: vec![BoxSource::Stack, BoxSource::Stack],
                cond_plans: vec![],
                ops_base: &input_ops[3..],
                ops_count: 2,
            },
        ];

        let actual_atoms = plan_native_allocs(&input_ops);

        assert_eq!(expected_atoms, actual_atoms);
    }

    #[test]
    fn non_allocating_cond() {
        let output_reg = ops::RegId::alloc();
        let true_result_reg = ops::RegId::alloc();
        let false_result_reg = ops::RegId::alloc();

        let test_reg = ops::RegId::alloc();

        let true_ops = Box::new([ops::OpKind::ConstBoxedNil(true_result_reg, ()).into()]);
        let false_ops = Box::new([ops::OpKind::ConstBoxedNil(false_result_reg, ()).into()]);

        let input_ops = [
            ops::OpKind::AllocBoxedInt(test_reg, test_reg).into(),
            ops::OpKind::Cond(ops::CondOp {
                reg_phi: Some(ops::RegPhi {
                    output_reg,
                    true_result_reg,
                    false_result_reg,
                }),
                test_reg,
                true_ops,
                false_ops,
            })
            .into(),
        ];

        let actual_atoms = plan_native_allocs(&input_ops);
        // We should place the `AllocBoxedInt` and `Cond` in the same atom
        assert_eq!(1, actual_atoms.len());
    }

    #[test]
    fn allocating_cond() {
        let output_reg = ops::RegId::alloc();
        let test_reg = ops::RegId::alloc();
        let true_result_reg = ops::RegId::alloc();
        let false_result_reg = ops::RegId::alloc();

        let true_ops = Box::new([ops::OpKind::ConstBoxedNil(true_result_reg, ()).into()]);
        let false_ops =
            Box::new([ops::OpKind::AllocBoxedInt(false_result_reg, false_result_reg).into()]);

        let input_ops = [
            ops::OpKind::AllocBoxedInt(test_reg, test_reg).into(),
            ops::OpKind::Cond(ops::CondOp {
                reg_phi: Some(ops::RegPhi {
                    output_reg,
                    true_result_reg,
                    false_result_reg,
                }),
                test_reg,
                true_ops,
                false_ops,
            })
            .into(),
        ];

        let actual_atoms = plan_native_allocs(&input_ops);
        // We should place the `AllocBoxedInt` and `Cond` in different atoms
        assert_eq!(2, actual_atoms.len());
    }
}