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
use crate::{
ir::Location,
logical_result::LogicalResult,
mlir_sys::{
mlirContextAttachDiagnosticHandler, mlirContextDetachDiagnosticHandler,
mlirDiagnosticGetLocation, mlirDiagnosticGetNote, mlirDiagnosticGetNumNotes,
mlirDiagnosticGetSeverity, mlirDiagnosticPrint, MlirDiagnostic, MlirDiagnosticHandlerID,
MlirDiagnosticSeverity_MlirDiagnosticError, MlirDiagnosticSeverity_MlirDiagnosticNote,
MlirDiagnosticSeverity_MlirDiagnosticRemark, MlirDiagnosticSeverity_MlirDiagnosticWarning,
MlirLogicalResult,
},
utility::print_callback,
Context,
};
use std::{ffi::c_void, fmt, marker::PhantomData};
#[derive(Debug)]
pub enum DiagnosticSeverity {
Error,
Warning,
Note,
Remark,
}
#[derive(Debug)]
pub struct Diagnostic<'a> {
raw: MlirDiagnostic,
phantom: PhantomData<&'a ()>,
}
impl<'a> Diagnostic<'a> {
pub fn location(&self) -> Location {
unsafe { Location::from_raw(mlirDiagnosticGetLocation(self.raw)) }
}
pub fn severity(&self) -> DiagnosticSeverity {
#[allow(non_upper_case_globals)]
match unsafe { mlirDiagnosticGetSeverity(self.raw) } {
MlirDiagnosticSeverity_MlirDiagnosticError => DiagnosticSeverity::Error,
MlirDiagnosticSeverity_MlirDiagnosticWarning => DiagnosticSeverity::Warning,
MlirDiagnosticSeverity_MlirDiagnosticNote => DiagnosticSeverity::Note,
MlirDiagnosticSeverity_MlirDiagnosticRemark => DiagnosticSeverity::Remark,
_ => unreachable!(),
}
}
pub fn note_count(&self) -> usize {
unsafe { mlirDiagnosticGetNumNotes(self.raw) as usize }
}
pub fn note(&self, index: usize) -> Self {
unsafe { Self::from_raw(mlirDiagnosticGetNote(self.raw, index as isize)) }
}
pub(crate) unsafe fn from_raw(raw: MlirDiagnostic) -> Self {
Self {
raw,
phantom: Default::default(),
}
}
}
impl<'a> fmt::Display for Diagnostic<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut data = (f, Ok(()));
unsafe {
mlirDiagnosticPrint(
self.raw,
Some(print_callback),
&mut data as *mut _ as *mut c_void,
);
}
data.1
}
}
#[derive(Debug)]
pub struct DiagnosticHandler {
raw: MlirDiagnosticHandlerID,
}
impl Context {
pub fn attach_diagnostic_handler<F>(&self, handler: F) -> DiagnosticHandler
where
F: FnMut(Diagnostic) -> LogicalResult,
{
let handler = Box::new(handler);
let handler = Box::into_raw(handler);
let handler = unsafe {
mlirContextAttachDiagnosticHandler(
self.to_raw(),
Some(_mlir_cb_invoke::<F>),
handler as *mut c_void,
Some(_mlir_cb_detach::<F>),
)
};
DiagnosticHandler { raw: handler }
}
pub fn detach_diagnostic_handler<F>(&self, handler: DiagnosticHandler) {
unsafe {
mlirContextDetachDiagnosticHandler(self.to_raw(), handler.raw);
}
}
}
unsafe extern "C" fn _mlir_cb_invoke<F>(
diagnostic: MlirDiagnostic,
user_data: *mut c_void,
) -> MlirLogicalResult
where
F: FnMut(Diagnostic) -> LogicalResult,
{
let diagnostic = Diagnostic::from_raw(diagnostic);
let handler: &mut F = &mut *(user_data as *mut F);
handler(diagnostic).to_raw()
}
unsafe extern "C" fn _mlir_cb_detach<F>(user_data: *mut c_void)
where
F: FnMut(Diagnostic) -> LogicalResult,
{
drop(Box::from_raw(user_data as *mut F));
}