mlir/dialects/
common.rs

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
// Copyright 2024-2025, Giordano Salvador
// SPDX-License-Identifier: BSD-3-Clause

#![allow(dead_code)]

use mlir_sys::MlirAttribute;

use std::cmp;
use std::fmt;
use std::str::FromStr;

use crate::attributes;
use crate::dialects;
use crate::exit_code;
use crate::ir;
use crate::types;

use attributes::integer::Integer as IntegerAttr;
use attributes::specialized::NamedBool;
use attributes::specialized::NamedI32DenseArray;
use attributes::specialized::NamedI64DenseArray;
use attributes::specialized::NamedInteger;
use attributes::specialized::NamedMemoryLayout;
use attributes::specialized::NamedMemorySpace;
use attributes::specialized::NamedString;
use attributes::specialized::SpecializedAttribute;
use attributes::strided_layout::StridedLayout;
use dialects::affine::Map as AffineMap;
use exit_code::ExitCode;
use exit_code::exit;
use ir::Context;
use ir::StringBacked;
use types::integer::Integer as IntegerType;

///////////////////////////////
//  Attributes
///////////////////////////////

#[derive(Clone)]
pub struct DefaultMemorySpace(MlirAttribute);

#[derive(Clone)]
pub struct Dimension(MlirAttribute);

#[derive(Clone)]
pub struct IntegerMemorySpace(MlirAttribute);

#[derive(Clone)]
pub struct MemoryLayout(MlirAttribute);

#[derive(Clone)]
pub struct NonTemporal(MlirAttribute);

#[derive(Clone)]
pub struct OperandSegmentSizes(MlirAttribute);

#[derive(Clone)]
pub struct ResultSegmentSizes(MlirAttribute);

#[derive(Clone)]
pub struct StaticOffsets(MlirAttribute);

#[derive(Clone)]
pub struct StaticSizes(MlirAttribute);

#[derive(Clone)]
pub struct StaticStrides(MlirAttribute);

#[derive(Clone)]
pub struct SymbolName(MlirAttribute);

#[derive(Clone)]
pub struct SymbolVisibility(MlirAttribute);

///////////////////////////////
//  Enums
///////////////////////////////

#[derive(Clone, Copy, Default, PartialEq)]
pub enum SymbolVisibilityKind {
    #[default]
    None,
    Private,
}

///////////////////////////////
//  Attribute Implementation
///////////////////////////////

impl DefaultMemorySpace {
    pub fn new() -> Self {
        <Self as NamedMemorySpace>::new_none()
    }

    pub fn get(&self) -> &MlirAttribute {
        &self.0
    }

    pub fn get_mut(&mut self) -> &mut MlirAttribute {
        &mut self.0
    }
}

impl IntegerMemorySpace {
    pub fn new(context: &Context, n: i64) -> Self {
        const WIDTH: usize = 64;
        let t = IntegerType::new(context, WIDTH);
        let attr = IntegerAttr::new(&t, n);
        <Self as NamedMemorySpace>::new_integer(&attr)
    }

    pub fn get(&self) -> &MlirAttribute {
        &self.0
    }

    pub fn get_mut(&mut self) -> &mut MlirAttribute {
        &mut self.0
    }
}

impl Dimension {
    pub fn new(context: &Context, n: i64) -> Self {
        const WIDTH: usize = 64;
        <Self as NamedInteger>::new(context, n, WIDTH)
    }

    pub fn get(&self) -> &MlirAttribute {
        &self.0
    }

    pub fn get_mut(&mut self) -> &mut MlirAttribute {
        &mut self.0
    }
}

impl MemoryLayout {
    pub fn get(&self) -> &MlirAttribute {
        &self.0
    }

    pub fn get_mut(&mut self) -> &mut MlirAttribute {
        &mut self.0
    }
}

impl NonTemporal {
    pub fn get(&self) -> &MlirAttribute {
        &self.0
    }

    pub fn get_mut(&mut self) -> &mut MlirAttribute {
        &mut self.0
    }
}

impl OperandSegmentSizes {
    fn get(&self) -> &MlirAttribute {
        &self.0
    }

    fn get_mut(&mut self) -> &mut MlirAttribute {
        &mut self.0
    }
}

impl ResultSegmentSizes {
    fn get(&self) -> &MlirAttribute {
        &self.0
    }

    fn get_mut(&mut self) -> &mut MlirAttribute {
        &mut self.0
    }
}

impl StaticOffsets {
    pub fn get(&self) -> &MlirAttribute {
        &self.0
    }

    pub fn get_mut(&mut self) -> &mut MlirAttribute {
        &mut self.0
    }
}

impl StaticSizes {
    pub fn get(&self) -> &MlirAttribute {
        &self.0
    }

    pub fn get_mut(&mut self) -> &mut MlirAttribute {
        &mut self.0
    }
}

impl StaticStrides {
    pub fn get(&self) -> &MlirAttribute {
        &self.0
    }

    pub fn get_mut(&mut self) -> &mut MlirAttribute {
        &mut self.0
    }
}

impl SymbolName {
    pub fn get(&self) -> &MlirAttribute {
        &self.0
    }

    pub fn get_mut(&mut self) -> &mut MlirAttribute {
        &mut self.0
    }
}

impl SymbolVisibility {
    pub fn new(context: &Context, k: SymbolVisibilityKind) -> Option<Self> {
        match k {
            SymbolVisibilityKind::None => None,
            SymbolVisibilityKind::Private => {
                let s = StringBacked::from(k.to_string());
                Some(<Self as NamedString>::new(context, &s.as_string_ref()))
            }
        }
    }

    pub fn get(&self) -> &MlirAttribute {
        &self.0
    }

    pub fn get_mut(&mut self) -> &mut MlirAttribute {
        &mut self.0
    }
}

///////////////////////////////
//  Trait Implementation
///////////////////////////////

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

SpecializedAttribute!("memorySpace" = impl NamedMemorySpace for DefaultMemorySpace {
    fn from_checked(attr: MlirAttribute) -> Self {
        let attr_ = Self::from(attr);
        if !attr_.is_none() {
            eprintln!("Expected default memory space to be none memory space type");
            exit(ExitCode::DialectError);
        }
        attr_
    }
});

impl cmp::PartialEq for DefaultMemorySpace {
    fn eq(&self, rhs: &Self) -> bool {
        <Self as NamedMemorySpace>::eq(self, rhs)
    }
}

SpecializedAttribute!("memorySpace" = impl NamedMemorySpace for IntegerMemorySpace {
    fn from_checked(attr: MlirAttribute) -> Self {
        let attr_ = Self::from(attr);
        if !attr_.is_integer() {
            eprintln!("Expected integer memory space to be integer memory space type");
            exit(ExitCode::DialectError);
        }
        attr_
    }
});

impl cmp::PartialEq for IntegerMemorySpace {
    fn eq(&self, rhs: &Self) -> bool {
        <Self as NamedMemorySpace>::eq(self, rhs)
    }
}

SpecializedAttribute!("dim" = impl NamedInteger for Dimension {});

impl From<AffineMap> for MemoryLayout {
    fn from(map: AffineMap) -> Self {
        Self::from(&map)
    }
}

impl From<&AffineMap> for MemoryLayout {
    fn from(map: &AffineMap) -> Self {
        Self::new_affine_map(map)
    }
}

impl From<StridedLayout> for MemoryLayout {
    fn from(layout: StridedLayout) -> Self {
        Self::from(&layout)
    }
}

impl From<&StridedLayout> for MemoryLayout {
    fn from(layout: &StridedLayout) -> Self {
        Self::new_strided_layout(layout)
    }
}

SpecializedAttribute!("layout" = impl NamedMemoryLayout for MemoryLayout {});

SpecializedAttribute!("nontemporal" = impl NamedBool for NonTemporal {});

SpecializedAttribute!("operandSegmentSizes" = impl NamedI32DenseArray for OperandSegmentSizes {});

SpecializedAttribute!("resultSegmentSizes" = impl NamedI32DenseArray for ResultSegmentSizes {});

SpecializedAttribute!("static_offsets" = impl NamedI64DenseArray for StaticOffsets {});

SpecializedAttribute!("static_sizes" = impl NamedI64DenseArray for StaticSizes {});

SpecializedAttribute!("static_strides" = impl NamedI64DenseArray for StaticStrides {});

SpecializedAttribute!("sym_name" = impl NamedString for SymbolName {});

impl FromStr for SymbolVisibilityKind {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "" => Ok(SymbolVisibilityKind::None),
            "private" => Ok(SymbolVisibilityKind::Private),
            _ => Err(format!("Invalid symbol visibility kind: {}", s)),
        }
    }
}

SpecializedAttribute!("sym_visibility" = impl NamedString for SymbolVisibility {});

///////////////////////////////
//  Display
///////////////////////////////

impl fmt::Display for SymbolVisibilityKind {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", match self {
            SymbolVisibilityKind::None => "none",
            SymbolVisibilityKind::Private => "private",
        })
    }
}