mlir/dialects/
cf.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
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
// Copyright 2025, Giordano Salvador
// SPDX-License-Identifier: BSD-3-Clause

#![allow(dead_code)]

use mlir_sys::MlirAttribute;
use mlir_sys::MlirOperation;

use std::fmt;
use std::iter;

use crate::attributes;
use crate::dialects;
use crate::effects;
use crate::exit_code;
use crate::interfaces;
use crate::ir;
use crate::traits;
use crate::types;

use attributes::IAttributeNamed;
use attributes::specialized::NamedI32DenseArray;
use attributes::specialized::NamedIntegerDenseElements;
use attributes::specialized::NamedString;
use attributes::specialized::SpecializedAttribute;
use dialects::IOp;
use dialects::IOperation;
use dialects::OpRef;
use dialects::common::OperandSegmentSizes;
use effects::MEFF_NO_MEMORY_EFFECT;
use effects::MemoryEffectList;
use exit_code::ExitCode;
use exit_code::exit;
use interfaces::Interface;
use interfaces::MemoryEffectOpInterface;
use ir::Block;
use ir::Context;
use ir::Dialect;
use ir::Location;
use ir::OperationState;
use ir::ShapeImpl;
use ir::StringBacked;
use ir::StringRef;
use ir::Value;
use traits::Trait;
use types::IType;
use types::integer::Integer as IntegerType;
use types::vector::Vector;

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

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

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

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

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

#[repr(C)]
#[derive(Clone, Copy, Eq, Hash, PartialEq)]
pub enum Op {
    Assert,
    Br,
    CondBr,
    Switch,
}

///////////////////////////////
//  Operations
///////////////////////////////

#[derive(Clone)]
pub struct Assert(MlirOperation);

#[derive(Clone)]
pub struct Branch(MlirOperation);

#[derive(Clone)]
pub struct CondBranch(MlirOperation);

#[derive(Clone)]
pub struct Switch(MlirOperation);

///////////////////////////////
//  Attribute Implementations
///////////////////////////////

impl CaseValues {
    pub fn new(t: &IntegerType, elements: &[i64]) -> Self {
        let s = ShapeImpl::from(vec![elements.len() as i64]);
        let t_vec = Vector::new(&s, &t.as_type()).as_shaped();
        <Self as NamedIntegerDenseElements>::new(&t_vec, elements)
    }

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

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

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

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

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

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

///////////////////////////////
//  Enum Implementations
///////////////////////////////

impl Op {
    pub fn get_name(&self) -> &'static str {
        match self {
            Op::Assert => "assert",
            Op::Br => "br",
            Op::CondBr => "cond_br",
            Op::Switch => "switch",
        }
    }
}

///////////////////////////////
//  Operation Implementations
///////////////////////////////

impl Assert {
    pub fn new(context: &Context, cond: &Value, msg: &StringRef, loc: &Location) -> Self {
        if !cond.get_type().is_bool() {
            eprintln!(
                "Expected bool type (1-bit signless integer) for condition operand of \
                assert operation"
            );
            exit(ExitCode::DialectError);
        }
        let dialect = context.get_dialect_cf();
        let name = dialect.get_op_name(&Op::Assert);
        let attr = Message::new(context, msg).as_named_attribute();
        let mut op_state = OperationState::new(&name.as_string_ref(), loc);
        op_state.add_attributes(&[attr]);
        op_state.add_operands(&[cond.clone()]);
        Self::from(*op_state.create_operation().get_mut())
    }

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

    pub fn get_message(&self) -> Message {
        let attr_name = StringBacked::from(Message::get_name());
        let attr = self
            .as_operation()
            .get_attribute_inherent(&attr_name.as_string_ref());
        Message::from(*attr.get())
    }

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

impl Branch {
    pub fn new(context: &Context, args: &[Value], succ: &Block, loc: &Location) -> Self {
        let n_args = args.len() as isize;
        let n_succ = succ.num_args();
        if n_succ != n_args {
            eprintln!(
                "Expected matching number of successor block arguments ({}) and \
                operands ({}) of branch operation",
                n_succ, n_args,
            );
            exit(ExitCode::DialectError);
        }
        if args
            .iter()
            .enumerate()
            .any(|(i, v)| v.get_type() != succ.get_arg(i as isize).get_type())
        {
            eprintln!(
                "Expected matching types for successor block argument and operands of \
                branch operation"
            );
            exit(ExitCode::DialectError);
        }
        let dialect = context.get_dialect_cf();
        let name = dialect.get_op_name(&Op::Br);
        let mut op_state = OperationState::new(&name.as_string_ref(), loc);
        op_state.add_operands(args);
        op_state.add_successors(&[succ.clone()]);
        Self::from(*op_state.create_operation().get_mut())
    }

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

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

    pub fn get_successor(&self) -> Block {
        self.as_operation().get_successor(0)
    }
}

impl CondBranch {
    pub fn new(
        context: &Context,
        cond: &Value,
        args_true: &[Value],
        args_false: &[Value],
        succ_true: &Block,
        succ_false: &Block,
        loc: &Location,
    ) -> Self {
        if !cond.get_type().is_bool() {
            eprintln!(
                "Expected bool type (1-bit signless integer) for condition operand of \
                cond branch operation"
            );
            exit(ExitCode::DialectError);
        }
        let n_args_true = args_true.len() as isize;
        let n_args_false = args_false.len() as isize;
        let n_succ_true = succ_true.num_args();
        let n_succ_false = succ_false.num_args();
        if n_succ_true != n_args_true {
            eprintln!(
                "Expected matching number of true successor block arguments ({}) and \
                operands ({}) of cond branch operation",
                n_succ_true, n_args_true,
            );
            exit(ExitCode::DialectError);
        }
        if n_succ_false != n_args_false {
            eprintln!(
                "Expected matching number of false successor block arguments ({}) and \
                operands ({}) of cond branch operation",
                n_succ_true, n_args_true,
            );
            exit(ExitCode::DialectError);
        }
        if args_true
            .iter()
            .enumerate()
            .any(|(i, v)| v.get_type() != succ_true.get_arg(i as isize).get_type())
        {
            eprintln!(
                "Expected matching types for true successor block arguments and operands of \
                cond branch operation"
            );
            exit(ExitCode::DialectError);
        }
        if args_false
            .iter()
            .enumerate()
            .any(|(i, v)| v.get_type() != succ_false.get_arg(i as isize).get_type())
        {
            eprintln!(
                "Expected matching types for false successor block arguments and operands of \
                cond branch operation"
            );
            exit(ExitCode::DialectError);
        }
        let dialect = context.get_dialect_cf();
        let name = dialect.get_op_name(&Op::CondBr);
        let attr_opseg = OperandSegmentSizes::new(context, &[
            1,
            args_true.len() as i32,
            args_false.len() as i32,
        ])
        .as_named_attribute();
        let mut operands: Vec<Value> = vec![cond.clone()];
        operands.append(&mut args_true.to_vec());
        operands.append(&mut args_false.to_vec());
        let mut op_state = OperationState::new(&name.as_string_ref(), loc);
        op_state.add_attributes(&[attr_opseg]);
        op_state.add_operands(&operands);
        op_state.add_successors(&[succ_true.clone(), succ_false.clone()]);
        Self::from(*op_state.create_operation().get_mut())
    }

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

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

    pub fn get_successor_false(&self) -> Block {
        self.as_operation().get_successor(1)
    }

    pub fn get_successor_true(&self) -> Block {
        self.as_operation().get_successor(0)
    }
}

impl Switch {
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        context: &Context,
        flag: &Value,
        cases: &[i64],
        args_default: &[Value],
        args_cases: &[&[Value]],
        succ_default: &Block,
        succ_cases: &[Block],
        loc: &Location,
    ) -> Self {
        let t = flag.get_type();
        if !t.is_integer() {
            eprintln!("Expected integer type for flag operand of switch operation");
            exit(ExitCode::DialectError);
        }
        let case_opseg: Vec<i32> = args_cases.iter().map(|args| args.len() as i32).collect();
        let n_block_args: Vec<i32> = succ_cases.iter().map(|b| b.num_args() as i32).collect();
        if iter::zip(case_opseg.iter(), n_block_args.iter()).any(|(n_c, n_b)| n_c != n_b) {
            eprintln!(
                "Expected matching number of successor block arguments and case arguments \
                for switch operation"
            );
            exit(ExitCode::DialectError);
        }
        for (j, arg) in args_default.iter().enumerate() {
            if arg.get_type() != succ_default.get_arg(j as isize).get_type() {
                eprintln!(
                    "Expected matching types for operands and block argument {} for \
                    default case of switch operation",
                    j,
                );
                exit(ExitCode::DialectError);
            }
        }
        for (i, (args, block)) in iter::zip(args_cases.iter(), succ_cases.iter()).enumerate() {
            for (j, arg) in args.iter().enumerate() {
                if arg.get_type() != block.get_arg(j as isize).get_type() {
                    eprintln!(
                        "Expected matching types for operands and block argument {} for \
                        case {} of switch operation",
                        j, i,
                    );
                    exit(ExitCode::DialectError);
                }
            }
        }
        let dialect = context.get_dialect_cf();
        let name = dialect.get_op_name(&Op::Switch);
        let n_args_cases = case_opseg.iter().sum();
        let attr_opseg =
            OperandSegmentSizes::new(context, &[1, args_default.len() as i32, n_args_cases])
                .as_named_attribute();
        let attr_case_opseg = CaseOperandSegments::new(context, &case_opseg).as_named_attribute();
        let attr_case_values = CaseValues::new(&IntegerType::from(t), cases).as_named_attribute();
        let mut operands: Vec<Value> = vec![flag.clone()];
        operands.append(&mut args_default.to_vec());
        args_cases
            .iter()
            .for_each(|args| operands.append(&mut args.to_vec()));
        let mut successors: Vec<Block> = vec![succ_default.clone()];
        successors.append(&mut succ_cases.to_vec());
        let mut op_state = OperationState::new(&name.as_string_ref(), loc);
        op_state.add_attributes(&[attr_opseg, attr_case_opseg, attr_case_values]);
        op_state.add_operands(&operands);
        op_state.add_successors(&successors);
        Self::from(*op_state.create_operation().get_mut())
    }

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

    pub fn get_case_operand_segments(&self) -> CaseOperandSegments {
        let attr_name = StringBacked::from(CaseOperandSegments::get_name());
        let attr = self
            .as_operation()
            .get_attribute_inherent(&attr_name.as_string_ref());
        CaseOperandSegments::from(*attr.get())
    }

    pub fn get_case_values(&self) -> CaseValues {
        let attr_name = StringBacked::from(CaseValues::get_name());
        let attr = self
            .as_operation()
            .get_attribute_inherent(&attr_name.as_string_ref());
        CaseValues::from(*attr.get())
    }

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

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

impl From<MlirOperation> for Assert {
    fn from(op: MlirOperation) -> Self {
        Self(op)
    }
}

impl IOperation for Assert {
    fn get(&self) -> &MlirOperation {
        self.get()
    }

    fn get_dialect(&self) -> Dialect {
        self.as_operation().get_context().get_dialect_cf()
    }

    fn get_effects(&self) -> MemoryEffectList {
        &[]
    }

    fn get_interfaces(&self) -> &'static [Interface] {
        &[Interface::MemoryEffect(
            MemoryEffectOpInterface::UndefinedMemoryEffect,
        )]
    }

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

    fn get_name(&self) -> &'static str {
        Op::Assert.get_name()
    }

    fn get_op(&self) -> OpRef {
        &Op::Assert
    }

    fn get_traits(&self) -> &'static [Trait] {
        &[]
    }
}

impl From<MlirOperation> for Branch {
    fn from(op: MlirOperation) -> Self {
        Self(op)
    }
}

impl IOperation for Branch {
    fn get(&self) -> &MlirOperation {
        self.get()
    }

    fn get_dialect(&self) -> Dialect {
        self.as_operation().get_context().get_dialect_cf()
    }

    fn get_effects(&self) -> MemoryEffectList {
        &[MEFF_NO_MEMORY_EFFECT]
    }

    fn get_interfaces(&self) -> &'static [Interface] {
        &[
            Interface::BranchOpInterface,
            Interface::ConditionallySpeculatable,
            Interface::MemoryEffect(MemoryEffectOpInterface::NoMemoryEffect),
        ]
    }

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

    fn get_name(&self) -> &'static str {
        Op::Br.get_name()
    }

    fn get_op(&self) -> OpRef {
        &Op::Br
    }

    fn get_traits(&self) -> &'static [Trait] {
        &[Trait::AlwaysSpeculatableImplTrait, Trait::Terminator]
    }
}

SpecializedAttribute!("case_values" = impl NamedIntegerDenseElements for CaseValues {});

SpecializedAttribute!("case_operand_segments" = impl NamedI32DenseArray for CaseOperandSegments {});

impl From<MlirOperation> for CondBranch {
    fn from(op: MlirOperation) -> Self {
        Self(op)
    }
}

impl IOperation for CondBranch {
    fn get(&self) -> &MlirOperation {
        self.get()
    }

    fn get_dialect(&self) -> Dialect {
        self.as_operation().get_context().get_dialect_cf()
    }

    fn get_effects(&self) -> MemoryEffectList {
        &[MEFF_NO_MEMORY_EFFECT]
    }

    fn get_interfaces(&self) -> &'static [Interface] {
        &[
            Interface::BranchOpInterface,
            Interface::ConditionallySpeculatable,
            Interface::MemoryEffect(MemoryEffectOpInterface::NoMemoryEffect),
        ]
    }

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

    fn get_name(&self) -> &'static str {
        Op::CondBr.get_name()
    }

    fn get_op(&self) -> OpRef {
        &Op::CondBr
    }

    fn get_traits(&self) -> &'static [Trait] {
        &[
            Trait::AlwaysSpeculatableImplTrait,
            Trait::AttrSizedOperandSegments,
            Trait::Terminator,
        ]
    }
}

SpecializedAttribute!("msg" = impl NamedString for Message {});

impl IOp for Op {
    fn get_name(&self) -> &'static str {
        self.get_name()
    }
}

impl From<MlirOperation> for Switch {
    fn from(op: MlirOperation) -> Self {
        Self(op)
    }
}

impl IOperation for Switch {
    fn get(&self) -> &MlirOperation {
        self.get()
    }

    fn get_dialect(&self) -> Dialect {
        self.as_operation().get_context().get_dialect_cf()
    }

    fn get_effects(&self) -> MemoryEffectList {
        &[MEFF_NO_MEMORY_EFFECT]
    }

    fn get_interfaces(&self) -> &'static [Interface] {
        &[
            Interface::BranchOpInterface,
            Interface::ConditionallySpeculatable,
            Interface::MemoryEffect(MemoryEffectOpInterface::NoMemoryEffect),
        ]
    }

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

    fn get_name(&self) -> &'static str {
        Op::Switch.get_name()
    }

    fn get_op(&self) -> OpRef {
        &Op::Switch
    }

    fn get_traits(&self) -> &'static [Trait] {
        &[
            Trait::AlwaysSpeculatableImplTrait,
            Trait::AttrSizedOperandSegments,
            Trait::Terminator,
        ]
    }
}

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

impl fmt::Display for Op {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", match self {
            Op::Assert => "AssertOp",
            Op::Br => "BranchOp",
            Op::CondBr => "CondBranchOp",
            Op::Switch => "SwitchOp",
        })
    }
}