pliron 0.15.0

Programming Languages Intermediate RepresentatiON
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
//! Utilities to work with [SymbolTableInterface] Ops.

use rustc_hash::FxHashMap;
use thiserror::Error;

use crate::builtin::op_interfaces::{SymbolOpInterface, SymbolTableInterface};
use crate::graph::walkers::interruptible::immutable::walk_region;
use crate::graph::walkers::interruptible::{WalkResult, walk_advance, walk_skip};
use crate::graph::walkers::{IRNode, WALKCONFIG_PREORDER_FORWARD};
use crate::irbuild::inserter::{IRInserter, Inserter, OpInsertionPoint};
use crate::irbuild::listener::DummyListener;
use crate::location::Located;
use crate::printable::Printable;
use crate::{arg_err, arg_error};

use crate::context::{Context, Ptr};
use crate::identifier::Identifier;
use crate::linked_list::ContainsLinkedList;
use crate::op::op_cast;
use crate::operation::Operation;
use crate::result::Result;

/// A utility to efficiently lookup and update [Symbol](SymbolOpInterface)s
/// in a [SymbolTableInterface] Op. Similar to its counterpart in MLIR,
/// inserting into and erasing from this [SymbolTable] will also insert and
/// erase from the Operation given to it at construction.
pub struct SymbolTable {
    symbol_table_op: Box<dyn SymbolTableInterface>,
    symbol_table: FxHashMap<Identifier, Box<dyn SymbolOpInterface>>,
}

#[derive(Error, Debug)]

pub enum SymbolTableErr {
    #[error("Op does not implement SymbolTableInterface")]
    DoesNotImplementSymbolTableInterface,
    #[error("Symbol redefined: {0}")]
    SymbolRedefined(String),
    #[error("Op does not implement SymbolOpInterface")]
    DoesNotImplementSymbolOpInterface,
    #[error("Symbol Op cannot be inserted as it is already in another symbol table")]
    SymbolOpAlreadyInAnotherTable(String),
    #[error("Insertion point must already be inside the same symbol table")]
    InsertionPointNotInSameTable,
    #[error("Symbol op does not belong to the symbol table op")]
    SymbolOpNotInTableOp,
}

impl SymbolTable {
    /// Create a new [SymbolTable] from a [SymbolTableInterface] Op.
    pub fn new(ctx: &Context, symbol_table_op: Box<dyn SymbolTableInterface>) -> Self {
        // Go through the all operations in symbol_table_op and build a table.
        let mut symbol_table = FxHashMap::<Identifier, Box<dyn SymbolOpInterface>>::default();
        let table_ops_block = symbol_table_op.get_body(ctx, 0);
        for op in table_ops_block.deref(ctx).iter(ctx) {
            if let Some(sym_op) =
                op_cast::<dyn SymbolOpInterface>(Operation::get_op_dyn(op, ctx).as_ref())
            {
                let sym = sym_op.get_symbol_name(ctx);
                if let Some(prev_op) =
                    symbol_table.insert(sym.clone(), dyn_clone::clone_box(sym_op))
                {
                    // It's the job of the verifier to have caught this. So we panic.
                    panic!(
                        "Symbol {} defined at {} was previously defined at {}",
                        sym,
                        symbol_table_op.loc(ctx).disp(ctx),
                        prev_op.loc(ctx).disp(ctx)
                    );
                }
            }
        }

        Self {
            symbol_table_op,
            symbol_table,
        }
    }

    /// Insert a new [Symbol](SymbolOpInterface) into the [SymbolTable].
    /// If an insertion point is provided, that point must be inside self.symbol_table_op.
    /// If no insertion point is provided, the symbol op will be inserted at the end
    /// (before the terminator if one exists). symbol_op's parent must be either `None`,
    /// or the same as self.symbol_table_op.
    pub fn insert(
        &mut self,
        ctx: &Context,
        symbol_op: Box<dyn SymbolOpInterface>,
        insert_pt: Option<OpInsertionPoint>,
    ) -> Result<()> {
        let symbol_name = symbol_op.get_symbol_name(ctx);
        if let Some(prev_op) = self
            .symbol_table
            .insert(symbol_name.clone(), symbol_op.clone())
        {
            return arg_err!(
                symbol_op.loc(ctx),
                arg_error!(
                    prev_op.loc(ctx),
                    SymbolTableErr::SymbolRedefined(symbol_name.to_string())
                )
            );
        }

        let symbol_table_op = self.symbol_table_op.get_operation();
        let symbol_op = symbol_op.get_operation();

        if let Some(symbol_op_parent) = symbol_op.deref(ctx).get_parent_op(ctx) {
            if symbol_table_op != symbol_op_parent {
                return arg_err!(
                    symbol_op.deref(ctx).loc(),
                    SymbolTableErr::SymbolOpAlreadyInAnotherTable(symbol_name.to_string())
                );
            }
            // Nothing more to do.
        } else if let Some(insert_pt) = insert_pt {
            let insert_pt_parent = insert_pt
                .get_insertion_block(ctx)
                .expect("Invalid insertion point")
                .deref(ctx)
                .get_parent_op(ctx);
            match insert_pt_parent {
                Some(parent) if parent == symbol_table_op => {
                    let mut inserter = IRInserter::<DummyListener>::new(insert_pt);
                    inserter.insert_operation(ctx, symbol_op);
                }
                _ => {
                    return arg_err!(
                        symbol_op.deref(ctx).loc(),
                        SymbolTableErr::InsertionPointNotInSameTable
                    );
                }
            }
        } else {
            // Insert at the end, before the terminator if one exists.
            let block = self.symbol_table_op.get_body(ctx, 0);
            let terminator = block.deref(ctx).get_terminator(ctx);
            match terminator {
                Some(terminator) => symbol_op.insert_before(ctx, terminator),
                None => symbol_op.insert_at_back(block, ctx),
            }
        }

        Ok(())
    }

    /// Remove `symbol_op` from this [SymbolTable].
    /// The operation is unlinked from its parent, but is not erased.
    pub fn remove(&mut self, ctx: &Context, symbol_op: Box<dyn SymbolOpInterface>) -> Result<()> {
        let symbol_op_opr = symbol_op.get_operation();
        if !matches!(symbol_op_opr.deref(ctx).get_parent_op(ctx), Some(parent_op) if parent_op == self.symbol_table_op.get_operation())
        {
            return arg_err!(symbol_op.loc(ctx), SymbolTableErr::SymbolOpNotInTableOp);
        }

        let symbol = symbol_op.get_symbol_name(ctx);
        self.symbol_table.remove(&symbol);

        symbol_op_opr.unlink(ctx);

        Ok(())
    }

    /// Remove `symbol_op` from this [SymbolTable] and erase it.
    /// This will panic if `symbol_op` has SSA uses.
    pub fn erase(
        &mut self,
        ctx: &mut Context,
        symbol_op: Box<dyn SymbolOpInterface>,
    ) -> Result<()> {
        self.remove(ctx, symbol_op.clone())?;
        Operation::erase(symbol_op.get_operation(), ctx);
        Ok(())
    }

    /// Get the [Symbol](SymbolOpInterface) with the given name.
    pub fn lookup(&self, symbol_name: &Identifier) -> Option<Box<dyn SymbolOpInterface>> {
        self.symbol_table.get(symbol_name).cloned()
    }
}

pub type SymbolTableWalkerCallback<State> =
    fn(&Context, &mut State, Ptr<Operation>) -> WalkResult<()>;

/// Walk all (nested) operations within the region of `symbol_table_op`,
/// without traversing into any nested symbol tables.
pub fn walk_symbol_table<State>(
    symbol_table_op: Box<dyn SymbolTableInterface>,
    ctx: &Context,
    state: &mut State,
    callback: SymbolTableWalkerCallback<State>,
) {
    struct StateWithCallback<'a, State> {
        callback: SymbolTableWalkerCallback<State>,
        state: &'a mut State,
    }

    fn skip_nested_symbol_tables_callback<State>(
        ctx: &Context,
        state: &mut StateWithCallback<State>,
        node: IRNode,
    ) -> WalkResult<()> {
        if let IRNode::Operation(opr) = node {
            let op = Operation::get_op_dyn(opr, ctx);
            if op_cast::<dyn SymbolTableInterface>(op.as_ref()).is_some() {
                return walk_skip();
            }
            (state.callback)(ctx, state.state, opr)
        } else {
            walk_advance()
        }
    }

    let _ = walk_region(
        ctx,
        &mut StateWithCallback { callback, state },
        &WALKCONFIG_PREORDER_FORWARD,
        symbol_table_op.get_region(ctx),
        skip_nested_symbol_tables_callback,
    );
}

/// A collection of [SymbolTable]s, to simplify algorithms that run
/// recursively on nested [SymbolTable]s. The actual tables are constructed lazily.
/// See MLIR's [SymbolTableCollection](https://mlir.llvm.org/doxygen/classmlir_1_1SymbolTableCollection.html).
#[derive(Default)]
pub struct SymbolTableCollection {
    symbol_tables: FxHashMap<Ptr<Operation>, SymbolTable>,
}

/// Returns the nearest symbol table from a given operation `from`
pub fn nearest_symbol_table(
    ctx: &Context,
    from: Ptr<Operation>,
) -> Option<Box<dyn SymbolTableInterface>> {
    let mut op = from;
    loop {
        if let Some(symbol_table) =
            op_cast::<dyn SymbolTableInterface>(Operation::get_op_dyn(op, ctx).as_ref())
        {
            return Some(dyn_clone::clone_box(symbol_table));
        }
        if let Some(parent) = op.deref(ctx).get_parent_op(ctx) {
            op = parent;
        } else {
            return None;
        }
    }
}

impl SymbolTableCollection {
    /// Create a new empty [SymbolTableCollection].
    pub fn new() -> Self {
        Self::default()
    }

    /// Lookup symbol in the given [SymbolTableInterface] Op.
    /// A symbol table is computed on demand and cached in the collection.
    pub fn lookup_symbol_in_table(
        &mut self,
        ctx: &Context,
        symbol_table_op: Box<dyn SymbolTableInterface>,
        symbol_name: &Identifier,
    ) -> Option<Box<dyn SymbolOpInterface>> {
        self.get_symbol_table(ctx, symbol_table_op)
            .lookup(symbol_name)
    }

    /// Get the symbol table for the symbol table operation, constructing it if necessary.
    pub fn get_symbol_table(
        &mut self,
        ctx: &Context,
        symbol_table_op: Box<dyn SymbolTableInterface>,
    ) -> &mut SymbolTable {
        self.symbol_tables
            .entry(symbol_table_op.get_operation())
            .or_insert_with(|| SymbolTable::new(ctx, symbol_table_op))
    }

    /// Returns the [SymbolOpInterface] Op registered with the given symbol within the
    /// closest [SymbolTableInterface] parent Op of, or including, 'from'.
    pub fn lookup_symbol_in_nearest_table(
        &mut self,
        ctx: &Context,
        from: Ptr<Operation>,
        symbol_name: &Identifier,
    ) -> Option<Box<dyn SymbolOpInterface>> {
        let symbol_table = nearest_symbol_table(ctx, from)?;
        self.lookup_symbol_in_table(ctx, symbol_table, symbol_name)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::builtin::ops::{FuncOp, ModuleOp};
    use crate::builtin::types::FunctionType;
    use crate::context::Context;
    use crate::identifier::Identifier;
    use crate::op::Op;

    #[test]
    fn test_symbol_table() -> Result<()> {
        let ctx = &mut Context::new();
        let module_op = ModuleOp::new(ctx, Identifier::try_from("module").unwrap());
        let mut symbol_table = SymbolTable::new(ctx, Box::new(module_op));

        let fty = FunctionType::get(ctx, vec![], vec![]);
        let f1 = FuncOp::new(ctx, "f1".try_into().unwrap(), fty);

        symbol_table.insert(ctx, Box::new(f1), None)?;
        assert!(
            f1.get_operation().deref(ctx).get_parent_op(ctx) == Some(module_op.get_operation())
        );
        let f1_lookedup = symbol_table.lookup(&"f1".try_into().unwrap()).unwrap();
        assert!(f1_lookedup.get_operation() == f1.get_operation());

        let f2 = FuncOp::new(ctx, "f2".try_into().unwrap(), fty);
        symbol_table.insert(
            ctx,
            Box::new(f2),
            Some(OpInsertionPoint::BeforeOperation(f1.get_operation())),
        )?;
        assert!(
            f2.get_operation().deref(ctx).get_parent_op(ctx) == Some(module_op.get_operation())
        );
        let f2_lookedup = symbol_table.lookup(&"f2".try_into().unwrap()).unwrap();
        assert!(f2_lookedup.get_operation() == f2.get_operation());

        symbol_table.remove(ctx, Box::new(f1))?;
        assert!(symbol_table.lookup(&"f1".try_into().unwrap()).is_none());
        assert!(f1.get_operation().deref(ctx).get_parent_op(ctx).is_none());

        Ok(())
    }

    #[test]
    #[should_panic(expected = "Dangling Ptr deref")]
    fn test_symbol_table_erase() {
        let ctx = &mut Context::new();
        let module_op = ModuleOp::new(ctx, Identifier::try_from("module").unwrap());
        let mut symbol_table = SymbolTable::new(ctx, Box::new(module_op));

        let fty = FunctionType::get(ctx, vec![], vec![]);
        let f1 = FuncOp::new(ctx, "f1".try_into().unwrap(), fty);

        symbol_table.insert(ctx, Box::new(f1), None).unwrap();
        assert!(
            f1.get_operation().deref(ctx).get_parent_op(ctx) == Some(module_op.get_operation())
        );
        let f1_lookedup = symbol_table.lookup(&"f1".try_into().unwrap()).unwrap();
        assert!(f1_lookedup.get_operation() == f1.get_operation());

        symbol_table.erase(ctx, Box::new(f1)).unwrap();
        f1.get_operation().deref(ctx);
    }

    #[test]
    fn test_symbol_table_collection() -> Result<()> {
        let ctx = &mut Context::new();

        let module_op = ModuleOp::new(ctx, Identifier::try_from("module").unwrap());
        let nested_module_op = ModuleOp::new(ctx, Identifier::try_from("nested_module").unwrap());
        let mut symbol_table_collection = SymbolTableCollection::new();

        let fty = FunctionType::get(ctx, vec![], vec![]);
        let f1 = FuncOp::new(ctx, "f1".try_into().unwrap(), fty);

        symbol_table_collection
            .get_symbol_table(ctx, Box::new(module_op))
            .insert(ctx, Box::new(f1), None)?;

        let f1_lookedup = symbol_table_collection
            .lookup_symbol_in_table(ctx, Box::new(module_op), &"f1".try_into().unwrap())
            .unwrap();
        assert!(f1_lookedup.get_operation() == f1.get_operation());

        let f2 = FuncOp::new(ctx, "f2".try_into().unwrap(), fty);
        symbol_table_collection
            .get_symbol_table(ctx, Box::new(nested_module_op))
            .insert(ctx, Box::new(f2), None)?;

        let f2_lookedup = symbol_table_collection
            .lookup_symbol_in_table(ctx, Box::new(nested_module_op), &"f2".try_into().unwrap())
            .unwrap();

        assert!(f2_lookedup.get_operation() == f2.get_operation());

        symbol_table_collection
            .get_symbol_table(ctx, Box::new(module_op))
            .insert(ctx, Box::new(nested_module_op), None)?;

        let nested_module_lookedup = symbol_table_collection
            .lookup_symbol_in_nearest_table(
                ctx,
                f1.get_operation(),
                &"nested_module".try_into().unwrap(),
            )
            .unwrap();
        assert!(nested_module_lookedup.get_operation() == nested_module_op.get_operation());

        let f1_lookedup = symbol_table_collection
            .lookup_symbol_in_nearest_table(ctx, f1.get_operation(), &"f1".try_into().unwrap())
            .unwrap();
        assert!(f1_lookedup.get_operation() == f1.get_operation());

        Ok(())
    }
}