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
use super::{BlockRef, Location, Operation, OperationRef};
use crate::mlir_sys::{
mlirModuleCreateEmpty, mlirModuleCreateParse, mlirModuleDestroy, mlirModuleFromOperation,
mlirModuleGetBody, mlirModuleGetContext, mlirModuleGetOperation, MlirModule,
};
use crate::{
context::{Context, ContextRef},
string_ref::StringRef,
};
use std::marker::PhantomData;
#[derive(Debug)]
pub struct Module<'c> {
raw: MlirModule,
_context: PhantomData<&'c Context>,
}
impl<'c> Module<'c> {
pub fn new(location: Location) -> Self {
unsafe { Self::from_raw(mlirModuleCreateEmpty(location.to_raw())) }
}
pub fn parse(context: &Context, source: &str) -> Option<Self> {
unsafe {
Self::from_option_raw(mlirModuleCreateParse(
context.to_raw(),
StringRef::from(source).to_raw(),
))
}
}
pub fn as_operation(&self) -> OperationRef {
unsafe { OperationRef::from_raw(mlirModuleGetOperation(self.raw)) }
}
pub fn context(&self) -> ContextRef<'c> {
unsafe { ContextRef::from_raw(mlirModuleGetContext(self.raw)) }
}
pub fn body(&self) -> BlockRef {
unsafe { BlockRef::from_raw(mlirModuleGetBody(self.raw)) }
}
pub fn from_operation(operation: Operation) -> Option<Self> {
unsafe { Self::from_option_raw(mlirModuleFromOperation(operation.into_raw())) }
}
unsafe fn from_raw(raw: MlirModule) -> Self {
Self {
raw,
_context: Default::default(),
}
}
unsafe fn from_option_raw(raw: MlirModule) -> Option<Self> {
if raw.ptr.is_null() {
None
} else {
Some(Self::from_raw(raw))
}
}
pub(crate) const unsafe fn to_raw(&self) -> MlirModule {
self.raw
}
}
impl<'c> Drop for Module<'c> {
fn drop(&mut self) {
unsafe { mlirModuleDestroy(self.raw) };
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{
dialect::Registry,
ir::{operation, Block, Region},
utility::register_all_dialects,
};
#[test]
fn new() {
Module::new(Location::new(&Context::new(), "foo", 42, 42));
}
#[test]
fn context() {
Module::new(Location::new(&Context::new(), "foo", 42, 42)).context();
}
#[test]
fn parse() {
assert!(Module::parse(&Context::new(), "module{}").is_some());
}
#[test]
fn parse_none() {
assert!(Module::parse(&Context::new(), "module{").is_none());
}
#[test]
fn from_operation() {
let context = Context::new();
let region = Region::new();
region.append_block(Block::new(&[]));
let module = Module::from_operation(
operation::Builder::new("builtin.module", Location::unknown(&context))
.add_regions(vec![region])
.build(),
)
.unwrap();
assert!(module.as_operation().verify());
assert_eq!(module.as_operation().to_string(), "module {\n}\n")
}
#[test]
fn from_operation_fail() {
let context = Context::new();
let registry = Registry::new();
register_all_dialects(®istry);
context.append_dialect_registry(®istry);
context.get_or_load_dialect("func");
assert!(Module::from_operation(
operation::Builder::new("func.func", Location::unknown(&context)).build()
)
.is_none());
}
}