Skip to main content

melior/ir/type/
type_like.rs

1use super::TypeId;
2use crate::{context::ContextRef, dialect::Dialect};
3use mlir_sys::{mlirTypeDump, mlirTypeGetContext, mlirTypeGetDialect, mlirTypeGetTypeID, MlirType};
4
5/// Trait for type-like types.
6pub trait TypeLike<'c> {
7    /// Converts a type into a raw object.
8    fn to_raw(&self) -> MlirType;
9
10    /// Returns a context.
11    fn context(&self) -> ContextRef<'c> {
12        unsafe { ContextRef::from_raw(mlirTypeGetContext(self.to_raw())) }
13    }
14
15    /// Returns an ID.
16    fn id(&self) -> TypeId<'c> {
17        unsafe { TypeId::from_raw(mlirTypeGetTypeID(self.to_raw())) }
18    }
19
20    /// Returns a dialect.
21    fn dialect(&self) -> Dialect<'c> {
22        unsafe { Dialect::from_raw(mlirTypeGetDialect(self.to_raw())) }
23    }
24
25    /// Dumps a type.
26    fn dump(&self) {
27        unsafe { mlirTypeDump(self.to_raw()) }
28    }
29
30    melior_macro::type_check_functions!(
31        // spell-checker: disable
32        mlirTypeIsAAnyQuantizedType,
33        mlirTypeIsABF16,
34        mlirTypeIsACalibratedQuantizedType,
35        mlirTypeIsAComplex,
36        mlirTypeIsAF16,
37        mlirTypeIsAF32,
38        mlirTypeIsAF64,
39        mlirTypeIsAFloat,
40        mlirTypeIsAFloat8E4M3,
41        mlirTypeIsAFloat8E4M3B11FNUZ,
42        mlirTypeIsAFloat8E4M3FN,
43        mlirTypeIsAFloat8E4M3FNUZ,
44        mlirTypeIsAFloat8E5M2,
45        mlirTypeIsAFloat8E5M2FNUZ,
46        mlirTypeIsAFunction,
47        mlirTypeIsAGPUAsyncTokenType,
48        mlirTypeIsAIndex,
49        mlirTypeIsAInteger,
50        mlirTypeIsALLVMPointerType,
51        mlirTypeIsALLVMStructType,
52        mlirTypeIsAMemRef,
53        mlirTypeIsANone,
54        mlirTypeIsANVGPUTensorMapDescriptorType,
55        mlirTypeIsAOpaque,
56        mlirTypeIsAPDLAttributeType,
57        mlirTypeIsAPDLOperationType,
58        mlirTypeIsAPDLRangeType,
59        mlirTypeIsAPDLType,
60        mlirTypeIsAPDLTypeType,
61        mlirTypeIsAPDLValueType,
62        mlirTypeIsAQuantizedType,
63        mlirTypeIsARankedTensor,
64        mlirTypeIsAShaped,
65        mlirTypeIsATensor,
66        mlirTypeIsATF32,
67        mlirTypeIsATransformAnyOpType,
68        mlirTypeIsATransformAnyParamType,
69        mlirTypeIsATransformAnyValueType,
70        mlirTypeIsATransformOperationType,
71        mlirTypeIsATransformParamType,
72        mlirTypeIsATuple,
73        mlirTypeIsAUniformQuantizedPerAxisType,
74        mlirTypeIsAUniformQuantizedType,
75        mlirTypeIsAUnrankedMemRef,
76        mlirTypeIsAUnrankedTensor,
77        mlirTypeIsAVector,
78        // spell-checker: enable
79    );
80}
81
82#[cfg(test)]
83mod tests {
84    use super::*;
85    use crate::{
86        ir::{
87            r#type::{FunctionType, IntegerType},
88            Type,
89        },
90        Context,
91    };
92
93    #[test]
94    fn context() {
95        Type::parse(&Context::new(), "i8").unwrap().context();
96    }
97
98    #[test]
99    fn id() {
100        let context = Context::new();
101
102        assert_eq!(Type::index(&context).id(), Type::index(&context).id());
103    }
104
105    #[test]
106    fn dialect() {
107        let context = Context::new();
108
109        assert_eq!(
110            Type::index(&context).dialect().namespace().unwrap(),
111            "builtin"
112        );
113    }
114
115    #[test]
116    fn is_integer() {
117        let context = Context::new();
118
119        assert!(IntegerType::new(&context, 64).is_integer());
120    }
121
122    #[test]
123    fn is_index() {
124        let context = Context::new();
125
126        assert!(Type::index(&context).is_index());
127    }
128
129    #[test]
130    fn is_bfloat16() {
131        let context = Context::new();
132
133        assert!(FunctionType::new(&context, &[], &[]).is_function());
134    }
135
136    #[test]
137    fn is_function() {
138        let context = Context::new();
139
140        assert!(FunctionType::new(&context, &[], &[]).is_function());
141    }
142
143    #[test]
144    fn is_vector() {
145        let context = Context::new();
146
147        assert!(Type::vector(&[42], Type::index(&context)).is_vector());
148    }
149
150    #[test]
151    fn dump() {
152        Type::index(&Context::new()).dump();
153    }
154}