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#[derive(Clone, Copy, Debug)]
14pub struct DialectHandle {
15 raw: MlirDialectHandle,
16}
17
18impl DialectHandle {
19 pub fn r#async() -> Self {
21 unsafe { Self::from_raw(mlirGetDialectHandle__async__()) }
22 }
23
24 pub fn cf() -> Self {
26 unsafe { Self::from_raw(mlirGetDialectHandle__cf__()) }
27 }
28
29 pub fn func() -> Self {
31 unsafe { Self::from_raw(mlirGetDialectHandle__func__()) }
32 }
33
34 pub fn gpu() -> Self {
36 unsafe { Self::from_raw(mlirGetDialectHandle__gpu__()) }
37 }
38
39 pub fn linalg() -> Self {
41 unsafe { Self::from_raw(mlirGetDialectHandle__linalg__()) }
42 }
43
44 pub fn llvm() -> Self {
46 unsafe { Self::from_raw(mlirGetDialectHandle__llvm__()) }
47 }
48
49 pub fn pdl() -> Self {
51 unsafe { Self::from_raw(mlirGetDialectHandle__pdl__()) }
52 }
53
54 pub fn quant() -> Self {
56 unsafe { Self::from_raw(mlirGetDialectHandle__quant__()) }
57 }
58
59 pub fn scf() -> Self {
61 unsafe { Self::from_raw(mlirGetDialectHandle__scf__()) }
62 }
63
64 pub fn shape() -> Self {
66 unsafe { Self::from_raw(mlirGetDialectHandle__shape__()) }
67 }
68
69 pub fn sparse_tensor() -> Self {
71 unsafe { Self::from_raw(mlirGetDialectHandle__sparse_tensor__()) }
72 }
73
74 pub fn tensor() -> Self {
76 unsafe { Self::from_raw(mlirGetDialectHandle__tensor__()) }
77 }
78
79 pub fn namespace(&self) -> StringRef {
81 unsafe { StringRef::from_raw(mlirDialectHandleGetNamespace(self.raw)) }
82 }
83
84 pub fn insert_dialect(&self, registry: &DialectRegistry) {
86 unsafe { mlirDialectHandleInsertDialect(self.raw, registry.to_raw()) }
87 }
88
89 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 pub fn register_dialect(&self, context: &Context) {
96 unsafe { mlirDialectHandleRegisterDialect(self.raw, context.to_raw()) }
97 }
98
99 pub const unsafe fn from_raw(handle: MlirDialectHandle) -> Self {
105 Self { raw: handle }
106 }
107
108 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(®istry);
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}