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

#![allow(dead_code)]

use mlir_sys::MlirType;
use mlir_sys::mlirUnrankedTensorTypeGet;
use mlir_sys::mlirUnrankedTensorTypeGetChecked;
use mlir_sys::mlirUnrankedTensorTypeGetTypeID;

use crate::do_unsafe;
use crate::exit_code;
use crate::ir;
use crate::types;

use exit_code::ExitCode;
use exit_code::exit;
use ir::Location;
use ir::Type;
use ir::TypeID;
use types::IType;
use types::shaped::NewElementType;
use types::shaped::Shaped;

#[derive(Clone)]
pub struct UnrankedTensor(MlirType);

impl UnrankedTensor {
    pub fn new(t: &Type) -> Self {
        Self::from(do_unsafe!(mlirUnrankedTensorTypeGet(*t.get())))
    }

    pub fn new_checked(t: &Type, loc: &Location) -> Self {
        Self::from(do_unsafe!(mlirUnrankedTensorTypeGetChecked(
            *loc.get(),
            *t.get()
        )))
    }

    pub fn from_type(t: &Type) -> Self {
        if !t.is_unranked_tensor() {
            eprint!("Cannot coerce type to unranked tensor type: {}", t);
            exit(ExitCode::IRError);
        }
        Self(*t.get())
    }

    pub fn as_shaped(&self) -> Shaped {
        Shaped::from(*self.get())
    }

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

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

    pub fn get_type_id() -> TypeID {
        TypeID::from(do_unsafe!(mlirUnrankedTensorTypeGetTypeID()))
    }
}

impl From<MlirType> for UnrankedTensor {
    fn from(t: MlirType) -> Self {
        Self::from(Type::from(t))
    }
}

impl From<Type> for UnrankedTensor {
    fn from(t: Type) -> Self {
        Self::from(&t)
    }
}

impl From<&Type> for UnrankedTensor {
    fn from(t: &Type) -> Self {
        Self::from_type(t)
    }
}

impl IType for UnrankedTensor {
    fn get(&self) -> &MlirType {
        self.get()
    }

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

impl NewElementType for UnrankedTensor {
    fn new_element_type(_other: &Self, t: &Type) -> Self {
        Self::new(t)
    }
}