zirium 0.5.0

Lossless textual MLIR syntax parsing and semantic processing
Documentation
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
//! Native stream segments adapted to Zirql's collection and predicate execution.

use std::ops::Range;

use super::{
    AttributeId, AttributeValue, Document, EvaluationContext, EvaluationError, NativeValue,
    OperationId, OutputMode, QueryOutput, TypeId, core_error, decode_mlir_string,
    evaluate_host_stage, model,
};
use zirql::{Budget, Host, Located, NativeInput, Pipeline, PredicateKind, Stage, Truth};

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
enum Row {
    Operation(OperationId),
    String(String),
    Type(TypeId),
    Attribute(String, AttributeId),
}

#[derive(Clone, Copy, PartialEq, Eq)]
enum StreamKind {
    Operations,
    Strings,
    Types,
    Attributes,
}

fn stream_kind(output: &QueryOutput) -> Option<StreamKind> {
    match output {
        QueryOutput::Operations(_) => Some(StreamKind::Operations),
        QueryOutput::Values(_) => Some(StreamKind::Strings),
        QueryOutput::Native(NativeValue::Types(_)) => Some(StreamKind::Types),
        QueryOutput::Native(NativeValue::Attributes(_)) => Some(StreamKind::Attributes),
        _ => None,
    }
}

fn into_rows(output: QueryOutput) -> (StreamKind, Vec<Row>) {
    match output {
        QueryOutput::Operations(items) => (
            StreamKind::Operations,
            items.into_iter().map(Row::Operation).collect(),
        ),
        QueryOutput::Values(items) => (
            StreamKind::Strings,
            items.into_iter().map(Row::String).collect(),
        ),
        QueryOutput::Native(NativeValue::Types(items)) => (
            StreamKind::Types,
            items.into_iter().map(Row::Type).collect(),
        ),
        QueryOutput::Native(NativeValue::Attributes(items)) => (
            StreamKind::Attributes,
            items
                .into_iter()
                .map(|(name, id)| Row::Attribute(name, id))
                .collect(),
        ),
        _ => unreachable!("only native streams enter a core segment"),
    }
}

fn into_output(kind: StreamKind, rows: Vec<Row>) -> QueryOutput {
    match kind {
        StreamKind::Operations => QueryOutput::Operations(
            rows.into_iter()
                .map(|row| {
                    let Row::Operation(id) = row else {
                        unreachable!("host stream kind matches its rows")
                    };
                    id
                })
                .collect(),
        ),
        StreamKind::Strings => QueryOutput::Values(
            rows.into_iter()
                .map(|row| {
                    let Row::String(value) = row else {
                        unreachable!("host stream kind matches its rows")
                    };
                    value
                })
                .collect(),
        ),
        StreamKind::Types => QueryOutput::Native(NativeValue::Types(
            rows.into_iter()
                .map(|row| {
                    let Row::Type(id) = row else {
                        unreachable!("host stream kind matches its rows")
                    };
                    id
                })
                .collect(),
        )),
        StreamKind::Attributes => QueryOutput::Native(NativeValue::Attributes(
            rows.into_iter()
                .map(|row| {
                    let Row::Attribute(name, id) = row else {
                        unreachable!("host stream kind matches its rows")
                    };
                    (name, id)
                })
                .collect(),
        )),
    }
}

pub(super) fn is_stream_stage(stage: &model::Stage) -> bool {
    use model::{Stage as S, StructuredStage as N};
    matches!(
        stage,
        S::Filter { .. }
            | S::Head { .. }
            | S::Count { .. }
            | S::Unique { .. }
            | S::Tally { .. }
            | S::Input { .. }
            | S::Reachable { .. }
            | S::Closure { .. }
            | S::Slice { .. }
            | S::ForwardSlice { .. }
            | S::Defs { .. }
            | S::Users { .. }
            | S::Parent { .. }
            | S::Children { .. }
            | S::Subtree { .. }
            | S::Sort { .. }
            | S::Reverse { .. }
            | S::Tail { .. }
            | S::Min { .. }
            | S::Max { .. }
            | S::MinAll { .. }
            | S::MaxAll { .. }
            | S::Attr { .. }
            | S::Names { .. }
            | S::ResultTypes { .. }
            | S::OperandTypes { .. }
            | S::Structured {
                step: N::StringAttr(_) | N::Attributes(_) | N::Types { .. } | N::Spellings,
                ..
            }
    )
}

pub(super) fn is_stream(output: &QueryOutput) -> bool {
    stream_kind(output).is_some()
}

fn range(range: crate::source::TextRange) -> Range<usize> {
    range.start() as usize..range.end() as usize
}

fn lower_predicate(predicate: &model::Predicate) -> zirql::Predicate<&model::Predicate> {
    use model::Predicate as P;
    let value = match predicate {
        P::Not { predicate, .. } => PredicateKind::Not(Box::new(lower_predicate(predicate))),
        P::Group { predicate, .. } => PredicateKind::Group(Box::new(lower_predicate(predicate))),
        P::And { predicates, .. } => {
            PredicateKind::And(predicates.iter().map(lower_predicate).collect())
        }
        P::Or { predicates, .. } => {
            PredicateKind::Or(predicates.iter().map(lower_predicate).collect())
        }
        P::Expression { .. }
        | P::SameValue { .. }
        | P::Bool { .. }
        | P::Op { .. }
        | P::OpValue { .. }
        | P::Computed { .. }
        | P::Dialect { .. }
        | P::ResultType { .. }
        | P::HasAttr { .. }
        | P::Attr { .. } => PredicateKind::Leaf(predicate),
    };
    Located::new(value, range(predicate.range()))
}

fn evaluation_error(error: zirql::EvaluationError<EvaluationError>) -> EvaluationError {
    match error {
        zirql::EvaluationError::Core(error) => core_error(error),
        zirql::EvaluationError::Host(error) => error,
    }
}

pub(super) type NativePipeline<'q> = Pipeline<&'q model::Predicate, &'q model::Stage>;

pub(super) fn lower(stages: &[model::Stage]) -> Result<NativePipeline<'_>, EvaluationError> {
    let stages = stages
        .iter()
        .map(|stage| {
            let value = match stage {
                model::Stage::Filter { predicate, .. } => Stage::Filter(lower_predicate(predicate)),
                model::Stage::Head { count, .. } => Stage::Head(*count),
                model::Stage::Tail { count, .. } => Stage::Tail(*count),
                model::Stage::Reverse { .. } => Stage::Reverse,
                model::Stage::Unique { .. } => Stage::Unique,
                model::Stage::Count { .. } => Stage::Count,
                model::Stage::Tally { .. } => Stage::Tally,
                _ => Stage::Host(stage),
            };
            Located::new(value, range(stage.range()))
        })
        .collect::<Vec<_>>();
    let location = stages
        .first()
        .expect("a core segment has at least one stage")
        .range
        .start..stages.last().unwrap().range.end;
    Pipeline::with_group_not_nesting(stages, location, zirql::MAX_NESTING).map_err(core_error)
}

pub(super) fn evaluate(
    pipeline: &NativePipeline<'_>,
    context: &mut EvaluationContext<'_, '_, '_>,
    input: QueryOutput,
    bindings: &zirql::Bindings<QueryOutput>,
    budget: &mut Budget,
) -> Result<QueryOutput, EvaluationError> {
    evaluate_access(pipeline, Access::Direct(context, bindings), input, budget)
}
pub(super) fn evaluate_with_arguments<'h, 'd, 'q, 'f>(
    pipeline: &NativePipeline<'_>,
    arguments: &mut dyn zirql::NativeArguments<EvaluationContext<'h, 'd, 'q>, super::report::ReportHost<'f>>,
    input: QueryOutput,
    budget: &mut Budget,
) -> Result<QueryOutput, EvaluationError> {
    evaluate_access(pipeline, Access::Prepared(arguments), input, budget)
}
fn evaluate_access(
    pipeline: &NativePipeline<'_>,
    access: Access<'_, '_, '_, '_, '_>,
    input: QueryOutput,
    budget: &mut Budget,
) -> Result<QueryOutput, EvaluationError> {
    let location = pipeline.range();
    let (kind, items) = into_rows(input);
    let input = NativeInput::from_admitted(
        StreamContext { access, kind },
        items,
        budget,
        zirql::Location::Query(location),
    )
    .map_err(core_error)?;
    let mut result =
        zirql::evaluate(pipeline, &StreamHost, input, budget).map_err(evaluation_error)?;
    Ok(match result.value {
        zirql::NativeValue::Stream(rows) => into_output(result.context.kind, rows),
        zirql::NativeValue::Count(count) => QueryOutput::Count(count),
        zirql::NativeValue::Tally(counts) => match result.context.access.parts().0.output {
            OutputMode::Native => QueryOutput::Native(NativeValue::Map(
                counts
                    .into_iter()
                    .map(|(key, count)| (key, QueryOutput::Count(count)))
                    .collect(),
            )),
            OutputMode::Textual => QueryOutput::Map(
                counts
                    .into_iter()
                    .map(|(key, count)| (key, serde_json::json!(count)))
                    .collect(),
            ),
        },
    })
}

enum Access<'a, 'host, 'document, 'query, 'f> {
    Direct(
        &'a mut EvaluationContext<'host, 'document, 'query>,
        &'a zirql::Bindings<QueryOutput>,
    ),
    Prepared(
        &'a mut dyn zirql::NativeArguments<
            EvaluationContext<'host, 'document, 'query>,
            super::report::ReportHost<'f>,
        >,
    ),
}
impl<'h, 'd, 'q> Access<'_, 'h, 'd, 'q, '_> {
    fn parts(
        &mut self,
    ) -> (
        &mut EvaluationContext<'h, 'd, 'q>,
        &zirql::Bindings<QueryOutput>,
    ) {
        match self {
            Self::Direct(context, bindings) => (context, bindings),
            Self::Prepared(arguments) => arguments.parts(),
        }
    }
}
struct StreamContext<'a, 'host, 'document, 'query, 'f> {
    access: Access<'a, 'host, 'document, 'query, 'f>,
    kind: StreamKind,
}

struct StreamHost;

impl<'stage>
    Host<Row, StreamContext<'_, '_, '_, '_, '_>, &'stage model::Predicate, &'stage model::Stage>
    for StreamHost
{
    type Error = EvaluationError;
    type Key = Row;

    fn stage_work(&self, items: &[Row], _: &StreamContext<'_, '_, '_, '_, '_>) -> usize {
        items.len().max(1)
    }

    fn collection_item_work(&self) -> usize {
        0
    }

    fn prepare_stage(
        &self,
        stage: &Located<Stage<&'stage model::Predicate, &'stage model::Stage>>,
        context: &mut StreamContext<'_, '_, '_, '_, '_>,
        _: &mut Budget,
    ) -> Result<(), zirql::EvaluationError<Self::Error>> {
        match stage.value {
            Stage::Filter(_) if context.kind != StreamKind::Operations => {
                Err(zirql::EvaluationError::Host(EvaluationError::new(
                    "filter requires operations, but the current stream contains values",
                )))
            }
            Stage::Tally if context.kind != StreamKind::Strings => {
                Err(zirql::EvaluationError::Host(EvaluationError::new(
                    "tally requires a value stream; use names or attr first",
                )))
            }
            _ => Ok(()),
        }
    }

    fn prepare_filter_item(
        &self,
        _: &Row,
        _: &mut StreamContext<'_, '_, '_, '_, '_>,
        _: &mut Budget,
        _: &Range<usize>,
    ) -> Result<(), zirql::EvaluationError<Self::Error>> {
        Ok(())
    }

    fn evaluate_leaf(
        &self,
        leaf: &&'stage model::Predicate,
        item: &Row,
        context: &mut StreamContext<'_, '_, '_, '_, '_>,
        budget: &mut Budget,
        _: &Range<usize>,
    ) -> Result<Truth, zirql::EvaluationError<Self::Error>> {
        let Row::Operation(operation) = item else {
            unreachable!("filter preparation requires operations")
        };
        let input = QueryOutput::Operations(vec![*operation]);
        match &mut context.access {
            Access::Prepared(arguments) => {
                super::report::evaluate_prepared_leaf(leaf, input, None, *arguments, budget)
            }
            Access::Direct(evaluation, bindings) => {
                super::report::evaluate_predicate_leaf(leaf, input, evaluation, bindings, budget)
                    .map_err(zirql::EvaluationError::Host)
            }
        }
    }

    fn evaluate_stage(
        &self,
        stage: &&'stage model::Stage,
        items: Vec<Row>,
        context: &mut StreamContext<'_, '_, '_, '_, '_>,
        budget: &mut Budget,
        _: &Range<usize>,
    ) -> Result<Vec<Row>, zirql::EvaluationError<Self::Error>> {
        let kind = context.kind;
        let (evaluation, bindings) = context.access.parts();
        let output = evaluate_host_stage(
            stage,
            evaluation,
            into_output(kind, items),
            bindings,
            budget,
        )
        .map_err(zirql::EvaluationError::Host)?;
        let (kind, rows) = into_rows(output);
        context.kind = kind;
        Ok(rows)
    }

    fn identity_key(
        &self,
        item: &Row,
        _: &mut StreamContext<'_, '_, '_, '_, '_>,
        _: &mut Budget,
        _: &Range<usize>,
    ) -> Result<Row, zirql::EvaluationError<Self::Error>> {
        Ok(item.clone())
    }

    fn tally_key(
        &self,
        item: Row,
        _: &mut StreamContext<'_, '_, '_, '_, '_>,
        _: &mut Budget,
        _: &Range<usize>,
    ) -> Result<String, zirql::EvaluationError<Self::Error>> {
        let Row::String(value) = item else {
            unreachable!("tally preparation requires strings")
        };
        Ok(value)
    }
}

// Only domain leaves live here. Zirql owns every Boolean wrapper and short circuit.
pub(super) fn evaluate_leaf(
    predicate: &model::Predicate,
    document: &Document,
    operation: OperationId,
) -> Truth {
    let value = match predicate {
        model::Predicate::SameValue { .. } => unreachable!("preflight refuses same_value outside native matches"),
        model::Predicate::Bool { value, .. } => *value,
        model::Predicate::Op { name, .. } => document.operation_name(operation) == Some(name),
        model::Predicate::Expression {..} | model::Predicate::OpValue {..} | model::Predicate::Computed {..}=>unreachable!("value arguments resolve with their active scope"),
        model::Predicate::Dialect { name, .. } => document.operation_name(operation).and_then(|op| op.split_once('.')).is_some_and(|(dialect, _)| dialect == name),
        model::Predicate::ResultType { spelling, .. } => document.result_types(operation).unwrap_or(&[]).iter().any(|&ty| document.type_spelling(ty) == Some(spelling)),
        model::Predicate::HasAttr { name, .. } => document.attribute_entries(operation).is_some_and(|mut entries| entries.any(|(attribute, _)| attribute == name)),
        model::Predicate::Attr { name, value, .. } => document.attribute_entries(operation).and_then(|mut entries| entries.find(|(attribute, _)| attribute == name)).and_then(|(_, id)| document.attribute_value(id)).is_some_and(|attribute| matches!(attribute, AttributeValue::String(spelling) if decode_mlir_string(spelling).as_deref() == Some(value))),
        model::Predicate::Not { .. } | model::Predicate::And { .. } | model::Predicate::Or { .. } | model::Predicate::Group { .. } => unreachable!("lowering retains only domain leaves"),
    };
    if value { Truth::True } else { Truth::False }
}

pub(super) fn root_filter(
    predicate: &model::Predicate,
) -> Result<NativePipeline<'_>, EvaluationError> {
    let range = range(predicate.range());
    Pipeline::with_group_not_nesting(
        vec![Located::new(
            Stage::Filter(lower_predicate(predicate)),
            range.clone(),
        )],
        range,
        zirql::MAX_NESTING,
    )
    .map_err(core_error)
}

pub(super) fn evaluate_roots(
    predicate: &model::Predicate,
    selected: Vec<OperationId>,
    context: &mut EvaluationContext<'_, '_, '_>,
    bindings: &zirql::Bindings<QueryOutput>,
    budget: &mut Budget,
) -> Result<Vec<OperationId>, EvaluationError> {
    let filter = root_filter(predicate)?;
    roots_by(predicate, selected, budget, |operation, budget| {
        let input = NativeInput::from_admitted(
            StreamContext {
                access: Access::Direct(context, bindings),
                kind: StreamKind::Operations,
            },
            vec![Row::Operation(operation)],
            budget,
            zirql::Location::Query(filter.range()),
        )
        .map_err(core_error)?;
        let mut result =
            zirql::evaluate(&filter, &StreamHost, input, budget).map_err(evaluation_error)?;
        let zirql::NativeValue::Stream(rows) = result.value else {
            unreachable!("root filter returns a stream")
        };
        Ok((
            !rows.is_empty(),
            super::operation_parent(result.context.access.parts().0.document, operation),
        ))
    })
}
pub(super) fn evaluate_roots_with_arguments(
    predicate: &model::Predicate,
    selected: Vec<OperationId>,
    arguments: &mut dyn zirql::NativeArguments<EvaluationContext<'_, '_, '_>, super::report::ReportHost<'_>>,
    budget: &mut Budget,
) -> Result<Vec<OperationId>, EvaluationError> {
    let filter = root_filter(predicate)?;
    roots_by(predicate, selected, budget, |operation, budget| {
        let input = NativeInput::from_admitted(
            StreamContext {
                access: Access::Prepared(arguments),
                kind: StreamKind::Operations,
            },
            vec![Row::Operation(operation)],
            budget,
            zirql::Location::Query(filter.range()),
        )
        .map_err(core_error)?;
        let mut result =
            zirql::evaluate(&filter, &StreamHost, input, budget).map_err(evaluation_error)?;
        let zirql::NativeValue::Stream(rows) = result.value else {
            unreachable!("root filter returns a stream")
        };
        Ok((
            !rows.is_empty(),
            super::operation_parent(result.context.access.parts().0.document, operation),
        ))
    })
}
fn roots_by(
    predicate: &model::Predicate,
    selected: Vec<OperationId>,
    budget: &mut Budget,
    mut test: impl FnMut(
        OperationId,
        &mut Budget,
    ) -> Result<(bool, Option<OperationId>), EvaluationError>,
) -> Result<Vec<OperationId>, EvaluationError> {
    let mut roots = Vec::new();
    for operation in selected {
        let mut candidate = Some(operation);
        while let Some(operation) = candidate {
            budget
                .charge(1, zirql::Location::Query(range(predicate.range())))
                .map_err(core_error)?;
            let (matched, parent) = test(operation, budget)?;
            if matched {
                super::program::charge_results(budget, 1, &range(predicate.range()))?;
                roots.push(operation);
                break;
            }
            candidate = parent;
        }
    }
    Ok(roots)
}