midenc-hir-transform 0.4.1

Transformation passes and utilities for Miden HIR
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
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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
use alloc::vec::Vec;

use midenc_hir::{
    adt::SmallDenseMap,
    dominance::DominanceInfo,
    matchers::{self, Matcher},
    pass::{Pass, PassExecutionState, PostPassStatus},
    traits::{ConstantLike, Terminator},
    Backward, Builder, EntityMut, Forward, FxHashSet, OpBuilder, Operation, OperationName,
    OperationRef, ProgramPoint, RawWalk, Region, RegionBranchOpInterface,
    RegionBranchTerminatorOpInterface, RegionRef, Report, SmallVec, Usable, ValueRef,
};

/// This transformation sinks operations as close as possible to their uses, one of two ways:
///
/// 1. If there exists only a single use of the operation, move it before it's use so that it is
///    in an ideal position for code generation.
///
/// 2. If there exist multiple uses, materialize a duplicate operation for all but one of the uses,
///    placing them before the use. The last use will receive the original operation.
///
/// To make this rewrite even more useful, we take care to place the operation at a position before
/// the using op, such that when generating code, the operation value will be placed on the stack
/// at the appropriate place relative to the other operands of the using op. This makes the operand
/// stack scheduling optimizer's job easier.
///
/// The purpose of this rewrite is to improve the quality of generated code by reducing the live
/// ranges of values that are trivial to materialize on-demand.
///
/// # Restrictions
///
/// This transform will not sink operations under the following conditions:
///
/// * The operation has side effects
/// * The operation is a block terminator
/// * The operation has regions
///
/// # Implementation
///
/// Given a list of regions, perform control flow sinking on them. For each region, control-flow
/// sinking moves operations that dominate the region but whose only users are in the region into
/// the regions so that they aren't executed on paths where their results are not needed.
///
/// TODO: For the moment, this is a *simple* control-flow sink, i.e., no duplicating of ops. It
/// should be made to accept a cost model to determine whether duplicating a particular op is
/// profitable.
///
/// Example:
///
/// ```mlir
/// %0 = arith.addi %arg0, %arg1
/// scf.if %cond {
///   scf.yield %0
/// } else {
///   scf.yield %arg2
/// }
/// ```
///
/// After control-flow sink:
///
/// ```mlir
/// scf.if %cond {
///   %0 = arith.addi %arg0, %arg1
///   scf.yield %0
/// } else {
///   scf.yield %arg2
/// }
/// ```
///
/// If using the `control_flow_sink` function, callers can supply a callback
/// `should_move_into_region` that determines whether the given operation that only has users in the
/// given operation should be moved into that region. If this returns true, `move_into_region` is
/// called on the same operation and region.
///
/// `move_into_region` must move the operation into the region such that dominance of the operation
/// is preserved; for example, by moving the operation to the start of the entry block. This ensures
/// the preservation of SSA dominance of the operation's results.
pub struct ControlFlowSink;

impl Pass for ControlFlowSink {
    type Target = Operation;

    fn name(&self) -> &'static str {
        "control-flow-sink"
    }

    fn argument(&self) -> &'static str {
        "control-flow-sink"
    }

    fn can_schedule_on(&self, _name: &OperationName) -> bool {
        true
    }

    fn run_on_operation(
        &mut self,
        op: EntityMut<'_, Self::Target>,
        state: &mut PassExecutionState,
    ) -> Result<(), Report> {
        let op = op.into_entity_ref();
        log::debug!(target: "control-flow-sink", "sinking operations in {op}");

        let operation = op.as_operation_ref();
        drop(op);

        let dominfo = state.analysis_manager().get_analysis::<DominanceInfo>()?;

        let mut sunk = PostPassStatus::Unchanged;
        operation.raw_prewalk_all::<Forward, _>(|op: OperationRef| {
            let regions_to_sink = {
                let op = op.borrow();
                let Some(branch) = op.as_trait::<dyn RegionBranchOpInterface>() else {
                    return;
                };
                let mut regions = SmallVec::<[_; 4]>::default();
                // Get the regions are that known to be executed at most once.
                get_singly_executed_regions_to_sink(branch, &mut regions);
                regions
            };

            // Sink side-effect free operations.
            sunk = control_flow_sink(
                &regions_to_sink,
                &dominfo,
                |op: &Operation, _region: &Region| op.is_memory_effect_free(),
                |mut op: OperationRef, region: RegionRef| {
                    // Move the operation to the beginning of the region's entry block.
                    // This guarantees the preservation of SSA dominance of all of the
                    // operation's uses are in the region.
                    let entry_block = region.borrow().entry_block_ref().unwrap();
                    op.borrow_mut().move_to(ProgramPoint::at_start_of(entry_block));
                },
            );
        });

        state.set_post_pass_status(sunk);

        Ok(())
    }
}

/// This transformation sinks constants as close as possible to their uses, one of two ways:
///
/// 1. If there exists only a single use of the constant, move it before it's use so that it is
///    in an ideal position for code generation.
///
/// 2. If there exist multiple uses, materialize a duplicate constant for all but one of the uses,
///    placing them before the use. The last use will receive the original constant.
///
/// To make this rewrite even more useful, we take care to place the constant at a position before
/// the using op, such that when generating code, the constant value will be placed on the stack
/// at the appropriate place relative to the other operands of the using op. This makes the operand
/// stack scheduling optimizer's job easier.
///
/// The purpose of this rewrite is to improve the quality of generated code by reducing the live
/// ranges of values that are trivial to materialize on-demand.
pub struct SinkOperandDefs;

impl Pass for SinkOperandDefs {
    type Target = Operation;

    fn name(&self) -> &'static str {
        "sink-operand-defs"
    }

    fn argument(&self) -> &'static str {
        "sink-operand-defs"
    }

    fn can_schedule_on(&self, _name: &OperationName) -> bool {
        true
    }

    fn run_on_operation(
        &mut self,
        op: EntityMut<'_, Self::Target>,
        state: &mut PassExecutionState,
    ) -> Result<(), Report> {
        let operation = op.as_operation_ref();
        drop(op);

        log::debug!(target: "sink-operand-defs", "sinking operand defs for regions of {}", operation.borrow());

        // For each operation, we enqueue it in this worklist, we then recurse on each of it's
        // dependency operations until all dependencies have been visited. We move up blocks from
        // the bottom, and skip any operations we've already visited. Once the queue is built, we
        // then process the worklist, moving everything into position.
        let mut worklist = alloc::collections::VecDeque::default();

        let mut changed = PostPassStatus::Unchanged;
        // Visit ops in "true" post-order (i.e. block bodies are visited bottom-up).
        operation.raw_postwalk_all::<Backward, _>(|operation: OperationRef| {
            // Determine if any of this operation's operands represent one of the following:
            //
            // 1. A constant value
            // 2. The sole use of the defining op's single result, and that op has no side-effects
            //
            // If 1, then we either materialize a fresh copy of the constant, or move the original
            // if there are no more uses.
            //
            // In both cases, to the extent possible, we order operand dependencies such that the
            // values will be on the Miden operand stack in the correct order. This means that we
            // visit operands in reverse order, and move defining ops directly before `op` when
            // possible. Some values may be block arguments, or refer to op's we're unable to move,
            // and thus those values be out of position on the operand stack, but the overall
            // result will reduce the amount of unnecessary stack movement.
            let op = operation.borrow();

            log::trace!(target: "sink-operand-defs", "visiting {op}");

            for operand in op.operands().iter().rev() {
                let value = operand.borrow();
                let value = value.value();
                let is_sole_user = value.iter_uses().all(|user| user.owner == operation);

                let Some(defining_op) = value.get_defining_op() else {
                    // Skip block arguments, nothing to move in that situation
                    //
                    // NOTE: In theory, we could move effect-free operations _up_ the block to place
                    // them closer to the block arguments they use, but that's unlikely to be all
                    // that profitable of a rewrite in practice.
                    log::trace!(target: "sink-operand-defs", "  ignoring block argument operand '{value}'");
                    continue;
                };

                log::trace!(target: "sink-operand-defs", "  evaluating operand '{value}'");

                let def = defining_op.borrow();
                if def.implements::<dyn ConstantLike>() {
                    log::trace!(target: "sink-operand-defs", "    defining '{}' is constant-like", def.name());
                    worklist.push_back(OpOperandSink::new(operation));
                    break;
                }

                let incorrect_result_count = def.num_results() != 1;
                let has_effects = !def.is_memory_effect_free();
                if !is_sole_user || incorrect_result_count || has_effects {
                    // Skip this operand if the defining op cannot be safely moved
                    //
                    // NOTE: For now we do not move ops that produce more than a single result, but
                    // if the other results are unused, or the users would still be dominated by
                    // the new location, then we could still move those ops.
                    log::trace!(target: "sink-operand-defs", "    defining '{}' cannot be moved:", def.name());
                    log::trace!(target: "sink-operand-defs", "      * op has multiple uses");
                    if incorrect_result_count {
                        log::trace!(target: "sink-operand-defs", "      * op has incorrect number of results ({})", def.num_results());
                    }
                    if has_effects {
                        log::trace!(target: "sink-operand-defs", "      * op has memory effects");
                    }
                } else {
                    log::trace!(target: "sink-operand-defs", "    defining '{}' is moveable, but is non-constant", def.name());
                    worklist.push_back(OpOperandSink::new(operation));
                    break;
                }
            }
        });

        for sinker in worklist.iter() {
            log::debug!(target: "sink-operand-defs", "sink scheduled for {}", sinker.operation.borrow());
        }

        let mut visited = FxHashSet::default();
        let mut erased = FxHashSet::default();
        'next_operation: while let Some(mut sink_state) = worklist.pop_front() {
            let mut operation = sink_state.operation;
            let op = operation.borrow();

            // If this operation is unused, remove it now if it has no side effects
            let is_memory_effect_free =
                op.is_memory_effect_free() || op.implements::<dyn ConstantLike>();
            if !op.is_used()
                && is_memory_effect_free
                && !op.implements::<dyn Terminator>()
                && !op.implements::<dyn RegionBranchTerminatorOpInterface>()
                && erased.insert(operation)
            {
                log::debug!(target: "sink-operand-defs", "erasing unused, effect-free, non-terminator op {op}");
                drop(op);
                operation.borrow_mut().erase();
                continue;
            }

            // If we've already worked this operation, skip it
            if !visited.insert(operation) && sink_state.next_operand_index == op.num_operands() {
                log::trace!(target: "sink-operand-defs", "already visited {}", operation.borrow());
                continue;
            } else {
                log::trace!(target: "sink-operand-defs", "visiting {}", operation.borrow());
            }

            let mut builder = OpBuilder::new(op.context_rc());
            builder.set_insertion_point(sink_state.ip);
            'next_operand: loop {
                // The next operand index starts at `op.num_operands()` when first initialized, so
                // we subtract 1 immediately to get the actual index of the current operand
                let Some(next_operand_index) = sink_state.next_operand_index.checked_sub(1) else {
                    // We're done processing this operation's operands
                    break;
                };

                log::debug!(target: "sink-operand-defs", "  sinking next operand def for {op} at index {next_operand_index}");

                let mut operand = op.operands()[next_operand_index];
                sink_state.next_operand_index = next_operand_index;
                let operand_value = operand.borrow().as_value_ref();
                log::trace!(target: "sink-operand-defs", "  visiting operand {operand_value}");

                // Reuse moved/materialized replacements when the same operand is used multiple times
                if let Some(replacement) = sink_state.replacements.get(&operand_value).copied() {
                    if replacement != operand_value {
                        log::trace!(target: "sink-operand-defs", "    rewriting operand {operand_value} as {replacement}");
                        operand.borrow_mut().set(replacement);

                        changed = PostPassStatus::Changed;
                        // If no other uses of this value remain, then remove the original
                        // operation, as it is now dead.
                        if !operand_value.borrow().is_used() {
                            log::trace!(target: "sink-operand-defs", "    {operand_value} is no longer used, erasing definition");
                            // Replacements are only ever for op results
                            let mut defining_op = operand_value.borrow().get_defining_op().unwrap();
                            defining_op.borrow_mut().erase();
                        }
                    }
                    continue 'next_operand;
                }

                let value = operand_value.borrow();
                let is_sole_user = value.iter_uses().all(|user| user.owner == operation);

                let Some(mut defining_op) = value.get_defining_op() else {
                    // Skip block arguments, nothing to move in that situation
                    //
                    // NOTE: In theory, we could move effect-free operations _up_ the block to place
                    // them closer to the block arguments they use, but that's unlikely to be all
                    // that profitable of a rewrite in practice.
                    log::trace!(target: "sink-operand-defs", "    {value} is a block argument, ignoring..");
                    continue 'next_operand;
                };

                log::trace!(target: "sink-operand-defs", "    is sole user of {value}? {is_sole_user}");

                let def = defining_op.borrow();
                if let Some(attr) = matchers::constant().matches(&*def) {
                    if !is_sole_user {
                        log::trace!(target: "sink-operand-defs", "    defining op is a constant with multiple uses, materializing fresh copy");
                        // Materialize a fresh copy of the original constant
                        let span = value.span();
                        let ty = value.ty();
                        let Some(new_def) =
                            def.dialect().materialize_constant(&mut builder, attr, ty, span)
                        else {
                            log::trace!(target: "sink-operand-defs", "    unable to materialize copy, skipping rewrite of this operand");
                            continue 'next_operand;
                        };
                        drop(def);
                        drop(value);
                        let replacement = new_def.borrow().results()[0] as ValueRef;
                        log::trace!(target: "sink-operand-defs", "    rewriting operand {operand_value} as {replacement}");
                        sink_state.replacements.insert(operand_value, replacement);
                        operand.borrow_mut().set(replacement);
                        changed = PostPassStatus::Changed;
                    } else {
                        log::trace!(target: "sink-operand-defs", "    defining op is a constant with no other uses, moving into place");
                        // The original op can be moved
                        drop(def);
                        drop(value);
                        defining_op.borrow_mut().move_to(*builder.insertion_point());
                        sink_state.replacements.insert(operand_value, operand_value);
                    }
                } else if !is_sole_user || def.num_results() != 1 || !def.is_memory_effect_free() {
                    // Skip this operand if the defining op cannot be safely moved
                    //
                    // NOTE: For now we do not move ops that produce more than a single result, but
                    // if the other results are unused, or the users would still be dominated by
                    // the new location, then we could still move those ops.
                    log::trace!(target: "sink-operand-defs", "    defining op is unsuitable for sinking, ignoring this operand");
                } else {
                    // The original op can be moved
                    //
                    // Determine if we _should_ move it:
                    //
                    // 1. If the use is inside a loop, and the def is outside a loop, do not
                    //    move the defining op into the loop unless it is profitable to do so,
                    //    i.e. a cost model indicates it is more efficient than the equivalent
                    //    operand stack movement instructions
                    //
                    // 2.
                    drop(def);
                    drop(value);
                    log::trace!(target: "sink-operand-defs", "    defining op can be moved and has no other uses, moving into place");
                    defining_op.borrow_mut().move_to(*builder.insertion_point());
                    sink_state.replacements.insert(operand_value, operand_value);

                    // Enqueue the defining op to be visited before continuing with this op's operands
                    log::trace!(target: "sink-operand-defs", "    enqueing defining op for immediate processing");
                    //sink_state.ip = *builder.insertion_point();
                    sink_state.ip = ProgramPoint::before(operation);
                    worklist.push_front(sink_state);
                    worklist.push_front(OpOperandSink::new(defining_op));
                    continue 'next_operation;
                }
            }
        }

        state.set_post_pass_status(changed);
        Ok(())
    }
}

struct OpOperandSink {
    operation: OperationRef,
    ip: ProgramPoint,
    replacements: SmallDenseMap<ValueRef, ValueRef, 4>,
    next_operand_index: usize,
}

impl OpOperandSink {
    pub fn new(operation: OperationRef) -> Self {
        Self {
            operation,
            ip: ProgramPoint::before(operation),
            replacements: SmallDenseMap::new(),
            next_operand_index: operation.borrow().num_operands(),
        }
    }
}

/// A helper struct for control-flow sinking.
struct Sinker<'a, P, F> {
    /// Dominance info to determine op user dominance with respect to regions.
    dominfo: &'a DominanceInfo,
    /// The callback to determine whether an op should be moved in to a region.
    should_move_into_region: P,
    /// The calback to move an operation into the region.
    move_into_region: F,
    /// The number of operations sunk
    num_sunk: usize,
}
impl<'a, P, F> Sinker<'a, P, F>
where
    P: Fn(&Operation, &Region) -> bool,
    F: Fn(OperationRef, RegionRef),
{
    /// Create an operation sinker with given dominance info.
    pub fn new(
        dominfo: &'a DominanceInfo,
        should_move_into_region: P,
        move_into_region: F,
    ) -> Self {
        Self {
            dominfo,
            should_move_into_region,
            move_into_region,
            num_sunk: 0,
        }
    }

    /// Given a list of regions, find operations to sink and sink them.
    ///
    /// Returns the number of operations sunk.
    pub fn sink_regions(mut self, regions: &[RegionRef]) -> usize {
        for region in regions.iter().copied() {
            if !region.borrow().is_empty() {
                self.sink_region(region);
            }
        }

        self.num_sunk
    }

    /// Given a region and an op which dominates the region, returns true if all
    /// users of the given op are dominated by the entry block of the region, and
    /// thus the operation can be sunk into the region.
    fn all_users_dominated_by(&self, op: &Operation, region: &Region) -> bool {
        assert!(
            region.find_ancestor_op(op.as_operation_ref()).is_none(),
            "expected op to be defined outside the region"
        );
        let region_entry = region.entry_block_ref().unwrap();
        op.results().iter().all(|result| {
            let result = result.borrow();
            result.iter_uses().all(|user| {
                // The user is dominated by the region if its containing block is dominated
                // by the region's entry block.
                self.dominfo.dominates(&region_entry, &user.owner.parent().unwrap())
            })
        })
    }

    /// Given a region and a top-level op (an op whose parent region is the given
    /// region), determine whether the defining ops of the op's operands can be
    /// sunk into the region.
    ///
    /// Add moved ops to the work queue.
    fn try_to_sink_predecessors(
        &mut self,
        user: OperationRef,
        region: RegionRef,
        stack: &mut Vec<OperationRef>,
    ) {
        log::trace!(target: "control-flow-sink", "contained op: {}", user.borrow());
        let user = user.borrow();
        for operand in user.operands().iter() {
            let op = operand.borrow().value().get_defining_op();
            // Ignore block arguments and ops that are already inside the region.
            if op.is_none_or(|op| op.grandparent().is_some_and(|r| r == region)) {
                continue;
            }

            let op = unsafe { op.unwrap_unchecked() };

            log::trace!(target: "control-flow-sink", "try to sink op: {}", op.borrow());

            // If the op's users are all in the region and it can be moved, then do so.
            let (all_users_dominated_by, should_move_into_region) = {
                let op = op.borrow();
                let region = region.borrow();
                let all_users_dominated_by = self.all_users_dominated_by(&op, &region);
                let should_move_into_region = (self.should_move_into_region)(&op, &region);
                (all_users_dominated_by, should_move_into_region)
            };
            if all_users_dominated_by && should_move_into_region {
                (self.move_into_region)(op, region);

                self.num_sunk += 1;

                // Add the op to the work queue
                stack.push(op);
            }
        }
    }

    /// Iterate over all the ops in a region and try to sink their predecessors.
    /// Recurse on subgraphs using a work queue.
    fn sink_region(&mut self, region: RegionRef) {
        // Initialize the work queue with all the ops in the region.
        let mut stack = Vec::new();
        for block in region.borrow().body() {
            for op in block.body() {
                stack.push(op.as_operation_ref());
            }
        }

        // Process all the ops depth-first. This ensures that nodes of subgraphs are sunk in the
        // correct order.
        while let Some(op) = stack.pop() {
            self.try_to_sink_predecessors(op, region, &mut stack);
        }
    }
}

pub fn control_flow_sink<P, F>(
    regions: &[RegionRef],
    dominfo: &DominanceInfo,
    should_move_into_region: P,
    move_into_region: F,
) -> PostPassStatus
where
    P: Fn(&Operation, &Region) -> bool,
    F: Fn(OperationRef, RegionRef),
{
    let sinker = Sinker::new(dominfo, should_move_into_region, move_into_region);
    let sunk_regions = sinker.sink_regions(regions);
    (sunk_regions > 0).into()
}

/// Populates `regions` with regions of the provided region branch op that are executed at most once
/// at that are reachable given the current operands of the op. These regions can be passed to
/// `control_flow_sink` to perform sinking on the regions of the operation.
fn get_singly_executed_regions_to_sink(
    branch: &dyn RegionBranchOpInterface,
    regions: &mut SmallVec<[RegionRef; 4]>,
) {
    use midenc_hir::matchers::Matcher;

    // Collect constant operands.
    let mut operands = SmallVec::<[_; 4]>::with_capacity(branch.num_operands());

    for operand in branch.operands().iter() {
        let matcher = matchers::foldable_operand();
        operands.push(matcher.matches(operand));
    }

    // Get the invocation bounds.
    let bounds = branch.get_region_invocation_bounds(&operands);

    // For a simple control-flow sink, only consider regions that are executed at most once.
    for (region, bound) in branch.regions().iter().zip(bounds) {
        use core::range::Bound;
        match bound.max() {
            Bound::Unbounded => continue,
            Bound::Excluded(bound) if *bound > 2 => continue,
            Bound::Excluded(0) => continue,
            Bound::Included(bound) if *bound > 1 => continue,
            _ => {
                regions.push(region.as_region_ref());
            }
        }
    }
}