xrack-mlir 0.5.0

XRack mlir support
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
use crate::ffi;
use std::{
    collections::BTreeSet,
    ffi::c_void,
    fmt,
    hash::{Hash, Hasher},
    marker::PhantomData,
};
use xrack_syntax::ast::{Parameter, Span, Type};

struct Context(ffi::MlirContext);

impl Context {
    fn new() -> Self {
        // Diagnostics are collected synchronously on the calling thread.
        let context = Self(unsafe { ffi::mlirContextCreateWithThreading(false) });
        unsafe { ffi::mlirDialectHandleLoadDialect(ffi::mlirGetDialectHandle__xr__(), context.0) };
        context
    }
}

impl Drop for Context {
    fn drop(&mut self) {
        // The module is destroyed before its context.
        unsafe { ffi::mlirContextDestroy(self.0) };
    }
}

struct PassManager(ffi::MlirPassManager);

impl Drop for PassManager {
    fn drop(&mut self) {
        // The manager is scoped to the call that borrows its context.
        unsafe { ffi::mlirPassManagerDestroy(self.0) };
    }
}

/// An owned MLIR module. Read-only handles borrow it and cannot survive a pass run.
pub struct Module {
    raw: ffi::MlirModule,
    context: Context,
    visual_cache_plan: bool,
}

impl fmt::Debug for Module {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Module").finish_non_exhaustive()
    }
}

impl Drop for Module {
    fn drop(&mut self) {
        // The module is owned by Rust. Its context remains alive here.
        unsafe { ffi::mlirModuleDestroy(self.raw) };
    }
}

impl Module {
    pub(crate) fn empty() -> Self {
        let context = Context::new();
        let raw = unsafe { ffi::mlirModuleCreateEmpty(ffi::mlirLocationUnknownGet(context.0)) };
        Self {
            raw,
            context,
            visual_cache_plan: false,
        }
    }

    pub(crate) fn builder_handles(&mut self) -> (ffi::MlirContext, ffi::MlirBlock) {
        (self.context.0, unsafe { ffi::mlirModuleGetBody(self.raw) })
    }

    /// Parse and verify XR IR without running its optimization pipeline.
    pub fn parse(source: &str) -> Result<Self, String> {
        let context = Context::new();
        let raw = capture_diagnostics(&context, || {
            // The parser borrows source only for the duration of the call.
            let raw = unsafe { ffi::mlirModuleCreateParse(context.0, string_ref(source)) };
            if raw.ptr.is_null() {
                return Err("failed to parse MLIR module");
            }
            Ok(raw)
        })?;
        let mut module = Self {
            raw,
            context,
            visual_cache_plan: false,
        };
        module.verify()?;
        module.assign_anonymous_element_keys();
        Ok(module)
    }

    fn assign_anonymous_element_keys(&mut self) {
        fn collect<'m>(
            parent: OperationRef<'m>,
            used: &mut BTreeSet<String>,
            anonymous: &mut Vec<(OperationRef<'m>, bool)>,
        ) {
            for op in parent.children() {
                if !matches!(op.name(), "xr.element" | "xr.instance") {
                    continue;
                }
                let key = op.attribute("key").or_else(|| op.attribute("id"));
                if let Some(key) = key.filter(|key| !key.is_empty()) {
                    used.insert(key.to_owned());
                } else {
                    anonymous.push((op, parent.name() == "xr.component"));
                }
                collect(op, used, anonymous);
            }
        }

        for component in self.operations().filter(|op| op.name() == "xr.component") {
            let mut used = BTreeSet::from(["root".to_owned()]);
            let mut anonymous = Vec::new();
            collect(component, &mut used, &mut anonymous);
            let mut candidates = (0..).map(|index| format!("__xr_anon_{index}"));
            for (op, is_root) in anonymous {
                let key = if is_root {
                    "root".into()
                } else {
                    candidates
                        .find(|candidate| !used.contains(candidate))
                        .expect("unbounded keys")
                };
                let selector = op
                    .children()
                    .find(|child| child.name() == "xr.state_select");
                // The key and the local selector's owner are verified together.
                // Both attributes belong to the module and are updated before it is shared.
                unsafe {
                    let value = ffi::mlirStringAttrGet(self.context.0, string_ref(&key));
                    ffi::mlirOperationSetInherentAttributeByName(op.raw, string_ref("key"), value);
                    if let Some(selector) = selector {
                        ffi::mlirOperationSetInherentAttributeByName(
                            selector.raw,
                            string_ref("owner"),
                            value,
                        );
                    }
                }
            }
        }
    }

    /// Verify the ODS operations and XR's module-wide constraints.
    pub fn verify(&self) -> Result<(), String> {
        capture_diagnostics(&self.context, || {
            let op = unsafe { ffi::mlirModuleGetOperation(self.raw) };
            if !unsafe { ffi::mlirOperationVerify(op) }
                || !unsafe { ffi::xrackMlirVerifyModule(self.raw) }
            {
                return Err("XR module verification failed");
            }
            Ok(())
        })
    }

    /// Consume the module before running mutating passes, invalidating old borrows.
    pub fn optimize(mut self) -> Result<Self, String> {
        self.verify()?;
        capture_diagnostics(&self.context, || {
            let manager = PassManager(unsafe { ffi::mlirPassManagerCreate(self.context.0) });
            // The operation, manager and dialect all belong to this context.
            unsafe {
                ffi::mlirPassManagerEnableVerifier(manager.0, true);
                ffi::xrackMlirAddPipeline(manager.0);
                let result =
                    ffi::mlirPassManagerRunOnOp(manager.0, ffi::mlirModuleGetOperation(self.raw));
                // Mirrors mlirLogicalResultIsSuccess, a static inline C helper.
                if result.value == 0 {
                    return Err("XR optimization pipeline failed");
                }
                if !ffi::xrackMlirVerifyModule(self.raw) {
                    return Err("optimized XR module verification failed");
                }
            }
            Ok(())
        })?;
        self.visual_cache_plan = true;
        Ok(self)
    }

    /// Only use visual annotations created by this module's optimization pipeline.
    pub fn has_visual_cache_plan(&self) -> bool {
        self.visual_cache_plan
    }

    pub fn operations(&self) -> impl Iterator<Item = OperationRef<'_>> {
        // The module body always exists and is owned by the module.
        BlockRef {
            raw: unsafe { ffi::mlirModuleGetBody(self.raw) },
            owner: PhantomData,
        }
        .operations()
    }

    pub fn to_mlir(&self) -> String {
        let mut output = Vec::new();
        // Printing invokes the callback synchronously and does not mutate IR.
        unsafe {
            let flags = ffi::mlirOpPrintingFlagsCreate();
            ffi::mlirOpPrintingFlagsEnableDebugInfo(flags, true, false);
            ffi::mlirOperationPrintWithFlags(
                ffi::mlirModuleGetOperation(self.raw),
                flags,
                Some(append_bytes),
                data(&mut output),
            );
            ffi::mlirOpPrintingFlagsDestroy(flags);
        }
        String::from_utf8_lossy(&output).into_owned()
    }
}

pub fn process(source: &str) -> Result<Module, String> {
    Module::parse(source)?.optimize()
}

/// Decode the finite floating-point spellings accepted by the XR dialect.
pub fn parse_float_literal(spelling: &str) -> Option<f64> {
    let mut value = 0.0;
    let parsed = unsafe { ffi::xrackMlirParseFloatLiteral(string_ref(spelling), &mut value) };
    parsed.then_some(value)
}

pub(crate) fn string_ref(text: &str) -> ffi::MlirStringRef {
    ffi::MlirStringRef {
        data: text.as_ptr().cast(),
        length: text.len(),
    }
}

// Caller supplies the lifetime of the native owner of this string.
unsafe fn bytes<'a>(text: ffi::MlirStringRef) -> &'a [u8] {
    if text.length == 0 {
        return &[];
    }
    // A nonempty MlirStringRef points to initialized bytes owned by MLIR.
    unsafe { std::slice::from_raw_parts(text.data.cast(), text.length) }
}

fn data(output: &mut Vec<u8>) -> *mut c_void {
    (output as *mut Vec<u8>).cast()
}

unsafe extern "C" fn append_bytes(text: ffi::MlirStringRef, output: *mut c_void) {
    // The caller supplies an exclusive, live buffer for this synchronous callback.
    unsafe { &mut *output.cast::<Vec<u8>>() }.extend_from_slice(unsafe { bytes(text) });
}

unsafe extern "C" fn collect_diagnostic(
    diag: ffi::MlirDiagnostic,
    output: *mut c_void,
) -> ffi::MlirLogicalResult {
    // Printing consumes the diagnostic before the handler returns.
    unsafe { ffi::mlirDiagnosticPrint(diag, Some(append_bytes), output) };
    unsafe { &mut *output.cast::<Vec<u8>>() }.push(b'\n');
    // Mirrors the inline mlirLogicalResultSuccess helper.
    ffi::MlirLogicalResult { value: 1 }
}

fn capture_diagnostics<T>(
    context: &Context,
    call: impl FnOnce() -> Result<T, &'static str>,
) -> Result<T, String> {
    struct Attachment<'a> {
        context: &'a Context,
        id: ffi::MlirDiagnosticHandlerID,
        buffer: *mut Vec<u8>,
    }
    impl Drop for Attachment<'_> {
        fn drop(&mut self) {
            // Detach before releasing the callback's user data.
            unsafe {
                ffi::mlirContextDetachDiagnosticHandler(self.context.0, self.id);
                drop(Box::from_raw(self.buffer));
            }
        }
    }
    let buffer = Box::into_raw(Box::new(Vec::<u8>::new()));
    // The attachment owns this allocation until the callback is detached.
    let id = unsafe {
        ffi::mlirContextAttachDiagnosticHandler(
            context.0,
            Some(collect_diagnostic),
            buffer.cast(),
            None,
        )
    };
    let attachment = Attachment {
        context,
        id,
        buffer,
    };
    call().map_err(|fallback| {
        // The synchronous operation and all its diagnostic callbacks have returned.
        let diagnostics = unsafe { &*attachment.buffer };
        if diagnostics.is_empty() {
            fallback.to_owned()
        } else {
            String::from_utf8_lossy(diagnostics).into_owned()
        }
    })
}

#[derive(Clone, Copy, Debug)]
pub struct OperationRef<'m> {
    raw: ffi::MlirOperation,
    owner: PhantomData<&'m Module>,
}

impl<'m> OperationRef<'m> {
    fn from_raw(raw: ffi::MlirOperation) -> Option<Self> {
        (!raw.ptr.is_null()).then_some(Self {
            raw,
            owner: PhantomData,
        })
    }

    pub fn name(self) -> &'m str {
        // Registered operation identifiers are valid UTF-8 and live in the context.
        let name = unsafe { ffi::mlirIdentifierStr(ffi::mlirOperationGetName(self.raw)) };
        std::str::from_utf8(unsafe { bytes(name) }).expect("MLIR operation name is UTF-8")
    }

    pub fn attribute(self, name: &str) -> Option<&'m str> {
        // String and symbol reference attributes are immutable context-owned values.
        let attr = unsafe { ffi::mlirOperationGetAttributeByName(self.raw, string_ref(name)) };
        if attr.ptr.is_null() {
            return None;
        }
        let text = unsafe {
            if ffi::mlirAttributeIsAString(attr) {
                ffi::mlirStringAttrGetValue(attr)
            } else if ffi::mlirAttributeIsAFlatSymbolRef(attr) {
                ffi::mlirFlatSymbolRefAttrGetValue(attr)
            } else {
                return None;
            }
        };
        std::str::from_utf8(unsafe { bytes(text) }).ok()
    }

    pub fn attr(self, name: &str) -> Result<&'m str, String> {
        self.attribute(name)
            .ok_or_else(|| format!("{} missing or invalid `{name}`", self.name()))
    }

    /// Decode the signature stored by an XR callback or event handler.
    pub fn parameters(self) -> Result<Vec<Parameter>, String> {
        serde_json::from_str(self.attr("parameters")?).map_err(|error| error.to_string())
    }

    pub fn string_array_attribute(self, name: &str) -> Option<Vec<&'m str>> {
        // The array and its strings are immutable and owned by the module context.
        let attr = unsafe { ffi::mlirOperationGetAttributeByName(self.raw, string_ref(name)) };
        if attr.ptr.is_null() || !unsafe { ffi::mlirAttributeIsAArray(attr) } {
            return None;
        }
        (0..unsafe { ffi::mlirArrayAttrGetNumElements(attr) })
            .map(|index| {
                let element = unsafe { ffi::mlirArrayAttrGetElement(attr, index) };
                if !unsafe { ffi::mlirAttributeIsAString(element) } {
                    return None;
                }
                let value = unsafe { ffi::mlirStringAttrGetValue(element) };
                std::str::from_utf8(unsafe { bytes(value) }).ok()
            })
            .collect()
    }

    pub fn integer_attribute(self, name: &str) -> Option<i64> {
        // Only read the value of a valid, context-owned integer attribute.
        let attr = unsafe { ffi::mlirOperationGetAttributeByName(self.raw, string_ref(name)) };
        if attr.ptr.is_null() || !unsafe { ffi::mlirAttributeIsAInteger(attr) } {
            return None;
        }
        let ty = unsafe { ffi::mlirAttributeGetType(attr) };
        if !unsafe { ffi::mlirTypeIsAInteger(ty) }
            || unsafe { ffi::mlirIntegerTypeGetWidth(ty) } > 64
        {
            return None;
        }
        Some(unsafe { ffi::mlirIntegerAttrGetValueInt(attr) })
    }

    pub fn i64_array_attribute(self, name: &str) -> Option<Vec<i64>> {
        let attr = unsafe { ffi::mlirOperationGetAttributeByName(self.raw, string_ref(name)) };
        if attr.ptr.is_null() || !unsafe { ffi::mlirAttributeIsADenseI64Array(attr) } {
            return None;
        }
        Some(
            (0..unsafe { ffi::mlirDenseArrayGetNumElements(attr) })
                .map(|index| unsafe { ffi::mlirDenseI64ArrayGetElement(attr, index) })
                .collect(),
        )
    }

    pub fn f64_array_attribute<const N: usize>(self, name: &str) -> Option<[f64; N]> {
        // Check the element type and length before reading any values.
        let attr = unsafe { ffi::mlirOperationGetAttributeByName(self.raw, string_ref(name)) };
        if attr.ptr.is_null()
            || !unsafe { ffi::mlirAttributeIsADenseF64Array(attr) }
            || unsafe { ffi::mlirDenseArrayGetNumElements(attr) } != N as isize
        {
            return None;
        }
        Some(std::array::from_fn(|index| unsafe {
            ffi::mlirDenseF64ArrayGetElement(attr, index as isize)
        }))
    }

    pub fn operand_count(self) -> usize {
        unsafe { ffi::mlirOperationGetNumOperands(self.raw) as usize }
    }

    pub fn operand(self, index: usize) -> Option<ValueRef<'m>> {
        (index < self.operand_count()).then(|| ValueRef {
            raw: unsafe { ffi::mlirOperationGetOperand(self.raw, index as isize) },
            owner: PhantomData,
        })
    }

    pub fn result_count(self) -> usize {
        unsafe { ffi::mlirOperationGetNumResults(self.raw) as usize }
    }

    pub fn result(self, index: usize) -> Option<ValueRef<'m>> {
        (index < self.result_count()).then(|| ValueRef {
            raw: unsafe { ffi::mlirOperationGetResult(self.raw, index as isize) },
            owner: PhantomData,
        })
    }

    pub fn regions(self) -> impl Iterator<Item = RegionRef<'m>> {
        let count = unsafe { ffi::mlirOperationGetNumRegions(self.raw) };
        (0..count).map(move |index| RegionRef {
            raw: unsafe { ffi::mlirOperationGetRegion(self.raw, index) },
            owner: PhantomData,
        })
    }

    /// Visit immediate children in region, block and operation order. Use
    /// `regions()` when the distinction between regions matters to the caller.
    pub fn children(self) -> impl Iterator<Item = OperationRef<'m>> {
        self.regions()
            .flat_map(RegionRef::blocks)
            .flat_map(BlockRef::operations)
    }

    pub fn span(self) -> Span {
        // The location belongs to the context. Only access its coordinates for this kind.
        unsafe {
            let location = ffi::mlirOperationGetLocation(self.raw);
            if ffi::mlirLocationIsAFileLineColRange(location) {
                return Span {
                    line: ffi::mlirLocationFileLineColRangeGetStartLine(location) as usize,
                    column: ffi::mlirLocationFileLineColRangeGetStartColumn(location) as usize,
                    source: 0,
                };
            }
        }
        Span {
            line: 1,
            column: 1,
            source: 0,
        }
    }

    pub fn source_file(self) -> Option<&'m str> {
        // File locations and their names are immutable and owned by the module context.
        let name = unsafe {
            let location = ffi::mlirOperationGetLocation(self.raw);
            if !ffi::mlirLocationIsAFileLineColRange(location) {
                return None;
            }
            ffi::mlirIdentifierStr(ffi::mlirLocationFileLineColRangeGetFilename(location))
        };
        std::str::from_utf8(unsafe { bytes(name) }).ok()
    }
}

#[derive(Clone, Copy, Debug)]
pub struct ValueRef<'m> {
    raw: ffi::MlirValue,
    owner: PhantomData<&'m Module>,
}

impl PartialEq for ValueRef<'_> {
    fn eq(&self, other: &Self) -> bool {
        self.raw.ptr == other.raw.ptr
    }
}
impl Eq for ValueRef<'_> {}
impl Hash for ValueRef<'_> {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.raw.ptr.hash(state);
    }
}

impl<'m> ValueRef<'m> {
    pub(crate) fn uses(self) -> impl Iterator<Item = (OperationRef<'m>, usize)> {
        // Uses are borrowed from the module. Its operation graph cannot be mutated
        // while these handles are available.
        let first = unsafe { ffi::mlirValueGetFirstUse(self.raw) };
        std::iter::successors((!first.ptr.is_null()).then_some(first), |usage| {
            let next = unsafe { ffi::mlirOpOperandGetNextUse(*usage) };
            (!next.ptr.is_null()).then_some(next)
        })
        .map(|usage| unsafe {
            let owner = ffi::mlirOpOperandGetOwner(usage);
            let index = ffi::mlirOpOperandGetOperandNumber(usage) as usize;
            (
                OperationRef::from_raw(owner).expect("use has an owner"),
                index,
            )
        })
    }

    pub fn defining_operation(self) -> Option<OperationRef<'m>> {
        // Block arguments are values too, but have no defining operation.
        if !unsafe { ffi::mlirValueIsAOpResult(self.raw) } {
            return None;
        }
        OperationRef::from_raw(unsafe { ffi::mlirOpResultGetOwner(self.raw) })
    }

    pub fn type_name(self) -> String {
        let mut output = Vec::new();
        // The type and callback buffer remain live until printing returns.
        unsafe {
            ffi::mlirTypePrint(
                ffi::mlirValueGetType(self.raw),
                Some(append_bytes),
                data(&mut output),
            )
        };
        String::from_utf8_lossy(&output).into_owned()
    }

    pub fn xr_type(self) -> Result<Type, String> {
        let name = self.type_name();
        name.strip_prefix("!xr.")
            .and_then(Type::parse)
            .ok_or_else(|| format!("XR value type {name}"))
    }
}

#[derive(Clone, Copy)]
pub struct RegionRef<'m> {
    raw: ffi::MlirRegion,
    owner: PhantomData<&'m Module>,
}

impl<'m> RegionRef<'m> {
    pub fn blocks(self) -> impl Iterator<Item = BlockRef<'m>> {
        let first = BlockRef::from_raw(unsafe { ffi::mlirRegionGetFirstBlock(self.raw) });
        std::iter::successors(first, |block| {
            BlockRef::from_raw(unsafe { ffi::mlirBlockGetNextInRegion(block.raw) })
        })
    }
}

#[derive(Clone, Copy)]
pub struct BlockRef<'m> {
    raw: ffi::MlirBlock,
    owner: PhantomData<&'m Module>,
}

impl<'m> BlockRef<'m> {
    fn from_raw(raw: ffi::MlirBlock) -> Option<Self> {
        (!raw.ptr.is_null()).then_some(Self {
            raw,
            owner: PhantomData,
        })
    }

    pub fn operations(self) -> impl Iterator<Item = OperationRef<'m>> {
        let first = OperationRef::from_raw(unsafe { ffi::mlirBlockGetFirstOperation(self.raw) });
        std::iter::successors(first, |op| {
            OperationRef::from_raw(unsafe { ffi::mlirOperationGetNextInBlock(op.raw) })
        })
    }
}