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
use crate::mlir_sys::{
mlirParsePassPipeline, mlirRegisterAllDialects, mlirRegisterAllLLVMTranslations,
mlirRegisterAllPasses, MlirStringRef,
};
use crate::{
context::Context, dialect, logical_result::LogicalResult, pass, string_ref::StringRef, Error,
};
use std::{
ffi::c_void,
fmt::{self, Formatter},
sync::Once,
};
pub fn register_all_dialects(registry: &dialect::Registry) {
unsafe { mlirRegisterAllDialects(registry.to_raw()) }
}
pub fn register_all_llvm_translations(context: &Context) {
unsafe { mlirRegisterAllLLVMTranslations(context.to_raw()) }
}
pub fn register_all_passes() {
static ONCE: Once = Once::new();
ONCE.call_once(|| unsafe { mlirRegisterAllPasses() });
}
pub fn parse_pass_pipeline(manager: pass::OperationManager, source: &str) -> Result<(), Error> {
let mut error_msg = String::new();
let result = LogicalResult::from_raw(unsafe {
mlirParsePassPipeline(
manager.to_raw(),
StringRef::from(source).to_raw(),
Some(error_callback),
&mut error_msg as *mut _ as *mut c_void,
)
});
if result.is_success() {
Ok(())
} else {
Err(Error::ParsePassPipeline(error_msg))
}
}
pub(crate) unsafe fn into_raw_array<T>(mut xs: Vec<T>) -> *mut T {
xs.shrink_to_fit();
xs.leak().as_mut_ptr()
}
pub(crate) unsafe extern "C" fn print_callback(string: MlirStringRef, data: *mut c_void) {
let (formatter, result) = &mut *(data as *mut (&mut Formatter, fmt::Result));
if result.is_err() {
return;
}
*result = (|| -> fmt::Result {
write!(
formatter,
"{}",
StringRef::from_raw(string)
.as_str()
.map_err(|_| fmt::Error)?
)
})();
}
pub(crate) unsafe extern "C" fn print_callback_v2(string: MlirStringRef, data: *mut c_void) {
let data = &mut *(data as *mut String);
let debug = StringRef::from_raw(string);
let debug_str = debug.as_str().unwrap();
data.push_str(debug_str);
}
pub(crate) unsafe extern "C" fn print_debug_callback(string: MlirStringRef, data: *mut c_void) {
let data = &mut *(data as *mut String);
let debug = StringRef::from_raw(string);
let debug_str = debug.as_str().unwrap();
data.push_str(debug_str);
}
pub(crate) unsafe extern "C" fn error_callback(string: MlirStringRef, data: *mut c_void) {
let data = &mut *(data as *mut String);
let err = StringRef::from_raw(string);
let error = err.as_str().unwrap();
data.push_str(error);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn register_dialects() {
let registry = dialect::Registry::new();
register_all_dialects(®istry);
}
#[test]
fn register_dialects_twice() {
let registry = dialect::Registry::new();
register_all_dialects(®istry);
register_all_dialects(®istry);
}
#[test]
fn register_llvm_translations() {
let context = Context::new();
register_all_llvm_translations(&context);
}
#[test]
fn register_llvm_translations_twice() {
let context = Context::new();
register_all_llvm_translations(&context);
register_all_llvm_translations(&context);
}
#[test]
fn register_passes() {
register_all_passes();
}
#[test]
fn register_passes_twice() {
register_all_passes();
register_all_passes();
}
#[test]
fn register_passes_many_times() {
for _ in 0..1000 {
register_all_passes();
}
}
}