Skip to main content

melior/dialect/
handle.rs

1use super::DialectRegistry;
2use crate::{context::Context, dialect::Dialect, string_ref::StringRef};
3use mlir_sys::{
4    mlirDialectHandleGetNamespace, mlirDialectHandleInsertDialect, mlirDialectHandleLoadDialect,
5    mlirDialectHandleRegisterDialect, mlirGetDialectHandle__async__, mlirGetDialectHandle__cf__,
6    mlirGetDialectHandle__func__, mlirGetDialectHandle__gpu__, mlirGetDialectHandle__linalg__,
7    mlirGetDialectHandle__llvm__, mlirGetDialectHandle__pdl__, mlirGetDialectHandle__quant__,
8    mlirGetDialectHandle__scf__, mlirGetDialectHandle__shape__,
9    mlirGetDialectHandle__sparse_tensor__, mlirGetDialectHandle__tensor__, MlirDialectHandle,
10};
11
12/// A dialect handle.
13#[derive(Clone, Copy, Debug)]
14pub struct DialectHandle {
15    raw: MlirDialectHandle,
16}
17
18impl DialectHandle {
19    /// Creates a `async` dialect handle.
20    pub fn r#async() -> Self {
21        unsafe { Self::from_raw(mlirGetDialectHandle__async__()) }
22    }
23
24    /// Creates a `cf` dialect handle.
25    pub fn cf() -> Self {
26        unsafe { Self::from_raw(mlirGetDialectHandle__cf__()) }
27    }
28
29    /// Creates a `func` dialect handle.
30    pub fn func() -> Self {
31        unsafe { Self::from_raw(mlirGetDialectHandle__func__()) }
32    }
33
34    /// Creates a `gpu` dialect handle.
35    pub fn gpu() -> Self {
36        unsafe { Self::from_raw(mlirGetDialectHandle__gpu__()) }
37    }
38
39    /// Creates a `linalg` dialect handle.
40    pub fn linalg() -> Self {
41        unsafe { Self::from_raw(mlirGetDialectHandle__linalg__()) }
42    }
43
44    /// Creates a `llvm` dialect handle.
45    pub fn llvm() -> Self {
46        unsafe { Self::from_raw(mlirGetDialectHandle__llvm__()) }
47    }
48
49    /// Creates a `pdl` dialect handle.
50    pub fn pdl() -> Self {
51        unsafe { Self::from_raw(mlirGetDialectHandle__pdl__()) }
52    }
53
54    /// Creates a `quant` dialect handle.
55    pub fn quant() -> Self {
56        unsafe { Self::from_raw(mlirGetDialectHandle__quant__()) }
57    }
58
59    /// Creates a `scf` dialect handle.
60    pub fn scf() -> Self {
61        unsafe { Self::from_raw(mlirGetDialectHandle__scf__()) }
62    }
63
64    /// Creates a `shape` dialect handle.
65    pub fn shape() -> Self {
66        unsafe { Self::from_raw(mlirGetDialectHandle__shape__()) }
67    }
68
69    /// Creates a `sparse_tensor` dialect handle.
70    pub fn sparse_tensor() -> Self {
71        unsafe { Self::from_raw(mlirGetDialectHandle__sparse_tensor__()) }
72    }
73
74    /// Creates a `tensor` dialect handle.
75    pub fn tensor() -> Self {
76        unsafe { Self::from_raw(mlirGetDialectHandle__tensor__()) }
77    }
78
79    /// Returns a namespace.
80    pub fn namespace(&self) -> StringRef {
81        unsafe { StringRef::from_raw(mlirDialectHandleGetNamespace(self.raw)) }
82    }
83
84    /// Inserts a dialect into a dialect registry.
85    pub fn insert_dialect(&self, registry: &DialectRegistry) {
86        unsafe { mlirDialectHandleInsertDialect(self.raw, registry.to_raw()) }
87    }
88
89    /// Loads a dialect into a context.
90    pub fn load_dialect<'c>(&self, context: &'c Context) -> Dialect<'c> {
91        unsafe { Dialect::from_raw(mlirDialectHandleLoadDialect(self.raw, context.to_raw())) }
92    }
93
94    /// Registers a dialect into a context.
95    pub fn register_dialect(&self, context: &Context) {
96        unsafe { mlirDialectHandleRegisterDialect(self.raw, context.to_raw()) }
97    }
98
99    /// Creates a dialect handle from a raw object.
100    ///
101    /// # Safety
102    ///
103    /// A raw object must be valid.
104    pub const unsafe fn from_raw(handle: MlirDialectHandle) -> Self {
105        Self { raw: handle }
106    }
107
108    /// Converts a dialect handle into a raw object.
109    pub const fn to_raw(self) -> MlirDialectHandle {
110        self.raw
111    }
112}
113
114#[cfg(test)]
115mod tests {
116    use super::*;
117
118    #[test]
119    fn func() {
120        DialectHandle::func();
121    }
122
123    #[test]
124    fn llvm() {
125        DialectHandle::llvm();
126    }
127
128    #[test]
129    fn namespace() {
130        DialectHandle::func().namespace();
131    }
132
133    #[test]
134    fn insert_dialect() {
135        let registry = DialectRegistry::new();
136
137        DialectHandle::func().insert_dialect(&registry);
138    }
139
140    #[test]
141    fn load_dialect() {
142        let context = Context::new();
143
144        DialectHandle::func().load_dialect(&context);
145    }
146
147    #[test]
148    fn register_dialect() {
149        let context = Context::new();
150
151        DialectHandle::func().register_dialect(&context);
152    }
153}