provekit-hla 0.1.0

High-level assembly DSL for code generation
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
use {
    crate::{
        ir::{FreshRegister, HardwareRegister, Instruction, TypedHardwareRegister, Variable},
        liveness::{Lifetime, Lifetimes},
        reification::{Index, RegisterType, ReifiedRegister},
        FreshVariable,
    },
    std::collections::{BTreeMap, BTreeSet, HashMap, HashSet, VecDeque},
};

pub type AllocatedVariable = Variable<TypedHardwareRegister>;

#[derive(Debug)]
struct RegisterAllocator {
    // Keeps track of the free hardware during allocations
    free_registers: BTreeSet<HardwareRegister>,
    // Registers that at the end should contain the outputs
    pinned:         PinnedOutputRegisters,
}

#[derive(Debug)]
struct PinnedOutputRegisters {
    // Acts as a concrete iterator to keep track of which register are free to
    // to hold output variables.
    // We use a BTreeSet as "iterator" here to also support ABI like SYS-V which
    // might use a few low numbered registers and x8.
    iter:         BTreeSet<HardwareRegister>,
    // Keep track till when the pinned output register is free to be used
    // for other variables.
    reservations: BTreeMap<HardwareRegister, (FreshRegister, usize)>,
}

impl PinnedOutputRegisters {
    fn new(iter: impl Iterator<Item = u64>) -> Self {
        Self {
            iter:         BTreeSet::from_iter(iter.map(HardwareRegister)),
            reservations: BTreeMap::new(),
        }
    }

    // Return True on successful reservation
    fn reserve_output_register(
        &mut self,
        lifetimes: &Lifetimes,
        reified_register: &ReifiedRegister<FreshRegister>,
    ) -> bool {
        match self.iter.pop_first() {
            Some(hardware_register) => {
                let lifetime = lifetimes[reified_register.reg].begin;

                self.reservations
                    .insert(hardware_register, (reified_register.reg, lifetime));
                true
            }
            None => false,
        }
    }
}

impl RegisterAllocator {
    fn new<T>(registers: T) -> Self
    where
        T: Iterator<Item = u64> + Clone,
    {
        let pool = BTreeSet::from_iter(registers.clone().map(HardwareRegister));
        RegisterAllocator {
            free_registers: pool,
            pinned:         PinnedOutputRegisters::new(registers),
        }
    }

    fn pop_first(&mut self, reg: FreshRegister, end_lifetime: usize) -> Option<HardwareRegister> {
        // Find the first register that is free and will be free for the entirety of the
        // lifetime of the fresh register.
        let reg = self
            .free_registers
            .iter()
            .find(
                // Check if the hardware register has been preassigned assigned to this fresh
                // registers Check if the hardware register can be used before it's
                // preassigned moment
                |&hardware_register| match self.pinned.reservations.get(hardware_register) {
                    Some((tp, _lifetime)) if reg == *tp => true,
                    Some((_tp, lifetime)) if end_lifetime <= *lifetime => true,
                    // Hardware register has not been preassigned
                    None => true,
                    // Hardware register was preassigned to a different fresh register and it's
                    // ownership overlaps with the lifetime of reg
                    _ => false,
                },
            )
            .copied();

        // Remove the register from the pool if found
        if let Some(hardware_register) = reg {
            self.free_registers.remove(&hardware_register);
        }

        reg
    }

    fn insert(&mut self, register: HardwareRegister) -> bool {
        self.free_registers.insert(register)
    }
}

pub fn allocate_input_variable(
    mapping: &mut RegisterMapping,
    register_bank: &mut RegisterBank,
    input_hw_registers: Vec<FreshVariable>,
    lifetimes: &Lifetimes,
) -> Vec<AllocatedVariable> {
    input_hw_registers
        .into_iter()
        .map(|variable| {
            let registers = variable
                .registers
                .into_iter()
                .map(|register| {
                    mapping
                        .get_or_allocate_register(register_bank, register, lifetimes[register.reg])
                        .to_basic_register()
                })
                .collect();
            AllocatedVariable {
                label: variable.label,
                registers,
            }
        })
        .collect()
}

/// Pins a fresh register to a specific hardware register.
///
/// This function assigns a specific hardware register to a fresh register,
/// ensuring that register allocation will use the specified hardware register.
///
/// # Arguments
///
/// * `register_bank` - RegisterBank to pin the register in
/// * `lifetimes` - Register lifetimes for allocation planning. It will use the
///   begin value.
pub fn reserve_output_variable(
    register_bank: &mut RegisterBank,
    lifetimes: &Lifetimes,
    variable: &FreshVariable,
) {
    let pool = register_bank.get_register_pool(variable.registers[0].r#type);
    for reified_register in &variable.registers {
        if !pool
            .pinned
            .reserve_output_register(lifetimes, reified_register)
        {
            panic!(
                "Ran out of registers to reserve {}! Reduce the number of outputs.",
                variable.label
            )
        }
    }
}

/// Manages pools of hardware registers for allocation.
///
/// RegisterBank maintains separate pools for general-purpose registers and
/// vector registers. It handles allocation and deallocation of hardware
/// registers.
#[derive(Debug)]
pub struct RegisterBank {
    general_purpose: RegisterAllocator,
    vector:          RegisterAllocator,
}

impl Default for RegisterBank {
    fn default() -> Self {
        Self::new()
    }
}

impl RegisterBank {
    /// Creates a new RegisterBank with default register pools.
    ///
    /// # Returns
    ///
    /// A new RegisterBank with general-purpose and vector register pools.
    /// Certain registers are excluded:
    /// - Register 18 (reserved by OS)
    /// - Register 19 (reserved by LLVM)
    /// - Register 30 (reserved for link register)
    /// - Register 31 (reserved for stack pointer)
    pub fn new() -> Self {
        Self {
            general_purpose: RegisterAllocator::new((0..=17).chain(20..29)),
            vector:          RegisterAllocator::new(0..=31),
        }
    }

    /// Gets the appropriate register pool based on register type.
    ///
    /// # Arguments
    ///
    /// * `r#type` - The register type (X, V, or D)
    ///
    /// # Returns
    ///
    /// A mutable reference to the corresponding register pool.
    fn get_register_pool(&mut self, r#type: RegisterType) -> &mut RegisterAllocator {
        match r#type {
            RegisterType::X => &mut self.general_purpose,
            RegisterType::V | RegisterType::D => &mut self.vector,
        }
    }

    /// Allocates a hardware register for a fresh register.
    ///
    /// # Arguments
    ///
    /// * `tp` - The fresh register to allocate for
    /// * `end_lifetime` - The instruction index after which the register is no
    ///   longer needed
    ///
    /// # Returns
    ///
    /// An Option containing the allocated hardware register, or None if
    /// allocation failed.
    fn pop_first(
        &mut self,
        reified_register: ReifiedRegister<FreshRegister>,
        end_lifetime: usize,
    ) -> Option<ReifiedRegister<HardwareRegister>> {
        let hw_reg = self
            .get_register_pool(reified_register.r#type)
            .pop_first(reified_register.reg, end_lifetime);

        hw_reg.map(|reg| reified_register.into_hardware(reg))
    }

    /// Returns a hardware register back to the register pool.
    ///
    /// # Returns
    ///
    /// `true` if the register was added to the pool, `false` if it was already
    /// in the pool.
    fn insert(&mut self, register: TypedHardwareRegister) -> bool {
        match register {
            TypedHardwareRegister::General(hardware_register) => {
                self.general_purpose.insert(hardware_register)
            }
            TypedHardwareRegister::Vector(hardware_register) => {
                self.vector.insert(hardware_register)
            }
        }
    }
}

/// Maps fresh registers to their assigned hardware registers.
///
/// RegisterMapping maintains the mapping between virtual registers
/// (FreshRegisters) and their corresponding physical hardware registers
/// _during_ register allocation. It represents the active set of allocations.
#[derive(Debug, Default)]
pub struct RegisterMapping {
    mapping: HashMap<FreshRegister, TypedHardwareRegister>,
}

impl RegisterMapping {
    /// Creates a new empty RegisterMapping.
    pub fn new() -> Self {
        Self {
            mapping: HashMap::with_capacity(100),
        }
    }

    /// Returns the number of registers currently allocated.
    pub fn allocated(&self) -> usize {
        self.mapping.len()
    }

    /// Gets the physical register for an operand.
    ///
    /// # Arguments
    ///
    /// * `fresh` - The reified fresh register to look up
    ///
    /// # Returns
    ///
    /// The corresponding hardware register.
    ///
    /// # Panics
    ///
    /// Panics if the register has not been assigned yet.
    fn get_register(
        &self,
        fresh: ReifiedRegister<FreshRegister>,
    ) -> ReifiedRegister<HardwareRegister> {
        match self.mapping.get(&fresh.reg) {
            Some(reg) => fresh.into_hardware(reg.reg()),
            None => panic!(
                "Internal error: {fresh:?} has not been assigned yet. This should not be possible."
            ),
        }
    }

    /// Gets or allocates a register.
    ///
    /// If the register is already mapped, returns the existing mapping.
    /// Otherwise, allocates a new hardware register.
    ///
    /// # Arguments
    ///
    /// * `register_bank` - The register bank to allocate from
    /// * `typed_register` - The fresh register to get or allocate
    /// * `end_lifetime` - The instruction index after which the register is no
    ///   longer needed
    ///
    /// # Returns
    ///
    /// The corresponding hardware register.
    ///
    /// # Panics
    ///
    /// Panics if register allocation fails.
    pub fn get_or_allocate_register(
        &mut self,
        register_bank: &mut RegisterBank,
        typed_register: ReifiedRegister<FreshRegister>,
        lifetime: Lifetime,
    ) -> ReifiedRegister<HardwareRegister> {
        // Either return existing mapping or create new one
        match self.mapping.get(&typed_register.reg) {
            Some(reg) => typed_register.into_hardware(reg.reg()),
            None => {
                let hardware_reified_register = register_bank
                    .pop_first(typed_register, lifetime.end)
                    .unwrap_or_else(|| {
                        panic!(
                            "All register are in use. HLA does not support spilling to stack for \
                             performance reasons. Reduce the number of registers simultaneously \
                             in use."
                        )
                    });

                self.mapping.insert(
                    typed_register.reg,
                    hardware_reified_register.to_basic_register(),
                );
                hardware_reified_register
            }
        }
    }

    /// Frees a register, returning it to the register bank.
    ///
    /// # Returns
    ///
    /// `true` if the register was freed, `false` otherwise.
    fn free_register(&mut self, register_bank: &mut RegisterBank, fresh: FreshRegister) -> bool {
        if let Some(reg) = self.mapping.remove(&fresh) {
            let result = register_bank.insert(reg);
            assert!(
                result,
                "hardware:{reg:?} is assigned to more than one fresh register."
            );
            result
        } else {
            panic!(
                "Trying to free a fresh register that has not been assigned a hardware register: \
                 {fresh}"
            )
        }
    }

    pub fn get_allocated_variable(&mut self, variable: &FreshVariable) -> AllocatedVariable {
        AllocatedVariable {
            label:     variable.label.clone(),
            registers: variable
                .registers
                .iter()
                .map(|register| {
                    {
                        self.mapping
                            .get(&register.reg)
                            .map(|hw_reg| {
                                ReifiedRegister {
                                    reg:    hw_reg.reg(),
                                    r#type: register.r#type,
                                    idx:    Index::None,
                                }
                                .to_basic_register()
                            })
                            .unwrap_or_else(|| {
                                panic!("Internal error: {} has not been allocated.", variable.label)
                            })
                    }
                })
                .collect(),
        }
    }
}

/// Allocates hardware registers for a sequence of instructions.
///
/// This function transforms instructions using fresh registers into
/// instructions using hardware registers, performing register allocation based
/// on the results of liveness analysis.
///
/// # Arguments
///
/// * `mapping` - The register mapping to use and update
/// * `register_bank` - The register bank to allocate from
/// * `instructions` - The instruction sequence using fresh registers
/// * `releases` - The registers to release after each instruction
/// * `lifetimes` - The lifetime information for each register
///
/// # Returns
///
/// A new sequence of instructions using hardware registers.
///
/// # Panics
///
/// Panics if the instructions and releases collections have different lengths.
pub fn hardware_register_allocation(
    mapping: &mut RegisterMapping,
    register_bank: &mut RegisterBank,
    instructions: Vec<Instruction<FreshRegister>>,
    releases: VecDeque<HashSet<FreshRegister>>,
    lifetimes: Lifetimes,
) -> Vec<Instruction<HardwareRegister>> {
    assert_eq!(
        instructions.len(),
        releases.len(),
        "The instructions and release collections need to be the same length"
    );

    instructions
        .into_iter()
        .zip(releases)
        .map(|(instruction, release)| {
            // Map operands to hardware registers
            let src = instruction
                .operands
                .into_iter()
                .map(|s| mapping.get_register(s))
                .collect();

            // Free registers that are no longer needed
            release.into_iter().for_each(|fresh| {
                mapping.free_register(register_bank, fresh);
            });

            // Allocate result registers
            let dest = instruction
                .results
                .into_iter()
                .map(|d| {
                    let idx = d.reg;
                    mapping.get_or_allocate_register(register_bank, d, lifetimes[idx])
                })
                .collect();

            // Construct the hardware instruction
            Instruction {
                opcode:    instruction.opcode,
                results:   dest,
                operands:  src,
                modifiers: instruction.modifiers,
            }
        })
        .collect()
}