Skip to main content

melior/dialect/llvm/
alloca_options.rs

1use crate::{
2    ir::{
3        attribute::{IntegerAttribute, TypeAttribute},
4        Attribute, Identifier,
5    },
6    Context,
7};
8
9const ATTRIBUTE_COUNT: usize = 3;
10
11// spell-checker: disable
12
13/// alloca options.
14#[derive(Debug, Default, Clone, Copy)]
15pub struct AllocaOptions<'c> {
16    align: Option<IntegerAttribute<'c>>,
17    elem_type: Option<TypeAttribute<'c>>,
18    inalloca: bool,
19}
20
21impl<'c> AllocaOptions<'c> {
22    /// Creates load/store options.
23    pub fn new() -> Self {
24        Self::default()
25    }
26
27    /// Sets the alignment.
28    pub fn align(mut self, align: Option<IntegerAttribute<'c>>) -> Self {
29        self.align = align;
30        self
31    }
32
33    /// Sets the elem_type, not needed if the returned pointer is not opaque.
34    pub fn elem_type(mut self, elem_type: Option<TypeAttribute<'c>>) -> Self {
35        self.elem_type = elem_type;
36        self
37    }
38
39    /// Sets the inalloca flag.
40    pub fn inalloca(mut self, inalloca: bool) -> Self {
41        self.inalloca = inalloca;
42        self
43    }
44
45    pub(super) fn into_attributes(
46        self,
47        context: &'c Context,
48    ) -> Vec<(Identifier<'c>, Attribute<'c>)> {
49        let mut attributes = Vec::with_capacity(ATTRIBUTE_COUNT);
50
51        if let Some(align) = self.align {
52            attributes.push((Identifier::new(context, "alignment"), align.into()));
53        }
54
55        if let Some(elem_type) = self.elem_type {
56            attributes.push((Identifier::new(context, "elem_type"), elem_type.into()));
57        }
58
59        if self.inalloca {
60            attributes.push((
61                Identifier::new(context, "inalloca"),
62                Attribute::unit(context),
63            ));
64        }
65
66        attributes
67    }
68}