midenc-hir 0.7.2

High-level Intermediate Representation for Miden Assembly
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
use alloc::{boxed::Box, rc::Rc};
use core::{any::Any, fmt};

use super::*;
use crate::{Context, EntityMut, OperationName, OperationRef, Report};

/// A type-erased [Pass].
///
/// This is used to allow heterogenous passes to be operated on uniformly.
///
/// Semantically, an [OperationPass] behaves like a `Pass<Target = Operation>`.
#[allow(unused_variables)]
pub trait OperationPass {
    fn as_any(&self) -> &dyn Any;
    fn as_any_mut(&mut self) -> &mut dyn Any;
    fn into_any(self: Box<Self>) -> Box<dyn Any>;
    fn name(&self) -> &'static str;

    fn argument(&self) -> &'static str {
        // NOTE: Could we compute an argument string from the type name?
        ""
    }
    fn description(&self) -> &'static str {
        ""
    }
    fn info(&self) -> PassInfo {
        PassInfo::lookup(self.argument()).expect("could not find pass information")
    }
    /// The name of the operation that this pass operates on, or `None` if this is a generic pass.
    fn target_name(&self, context: &Context) -> Option<OperationName>;
    fn initialize_options(&mut self, options: &str) -> Result<(), Report> {
        Ok(())
    }
    fn print_as_textual_pipeline(&self, f: &mut fmt::Formatter) -> fmt::Result;
    fn has_statistics(&self) -> bool {
        !self.statistics().is_empty()
    }
    fn statistics(&self) -> &[Box<dyn Statistic>];
    fn statistics_mut(&mut self) -> &mut [Box<dyn Statistic>];
    fn initialize(&mut self, context: Rc<Context>) -> Result<(), Report> {
        Ok(())
    }
    fn can_schedule_on(&self, name: &OperationName) -> bool;
    fn run_on_operation(
        &mut self,
        op: OperationRef,
        state: &mut PassExecutionState,
    ) -> Result<(), Report>;
    fn run_pipeline(
        &mut self,
        pipeline: &mut OpPassManager,
        op: OperationRef,
        state: &mut PassExecutionState,
    ) -> Result<(), Report>;
}

impl<P> OperationPass for P
where
    P: Pass + 'static,
{
    fn as_any(&self) -> &dyn Any {
        <P as Pass>::as_any(self)
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        <P as Pass>::as_any_mut(self)
    }

    fn into_any(self: Box<Self>) -> Box<dyn Any> {
        <P as Pass>::into_any(self)
    }

    fn name(&self) -> &'static str {
        <P as Pass>::name(self)
    }

    fn argument(&self) -> &'static str {
        <P as Pass>::argument(self)
    }

    fn description(&self) -> &'static str {
        <P as Pass>::description(self)
    }

    fn info(&self) -> PassInfo {
        <P as Pass>::info(self)
    }

    fn target_name(&self, context: &Context) -> Option<OperationName> {
        <P as Pass>::target_name(self, context)
    }

    fn initialize_options(&mut self, options: &str) -> Result<(), Report> {
        <P as Pass>::initialize_options(self, options)
    }

    fn print_as_textual_pipeline(&self, f: &mut fmt::Formatter) -> fmt::Result {
        <P as Pass>::print_as_textual_pipeline(self, f)
    }

    fn has_statistics(&self) -> bool {
        <P as Pass>::has_statistics(self)
    }

    fn statistics(&self) -> &[Box<dyn Statistic>] {
        <P as Pass>::statistics(self)
    }

    fn statistics_mut(&mut self) -> &mut [Box<dyn Statistic>] {
        <P as Pass>::statistics_mut(self)
    }

    fn initialize(&mut self, context: Rc<Context>) -> Result<(), Report> {
        <P as Pass>::initialize(self, context)
    }

    fn can_schedule_on(&self, name: &OperationName) -> bool {
        <P as Pass>::can_schedule_on(self, name)
    }

    fn run_on_operation(
        &mut self,
        mut op: OperationRef,
        state: &mut PassExecutionState,
    ) -> Result<(), Report> {
        let op = <<P as Pass>::Target as PassTarget>::into_target_mut(&mut op);
        <P as Pass>::run_on_operation(self, op, state)
    }

    fn run_pipeline(
        &mut self,
        pipeline: &mut OpPassManager,
        op: OperationRef,
        state: &mut PassExecutionState,
    ) -> Result<(), Report> {
        <P as Pass>::run_pipeline(self, pipeline, op, state)
    }
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub enum PostPassStatus {
    Unchanged,
    Changed,
}

impl PostPassStatus {
    pub const fn ir_changed(&self) -> bool {
        matches!(self, Self::Changed)
    }
}

impl From<bool> for PostPassStatus {
    fn from(ir_was_changed: bool) -> Self {
        if ir_was_changed {
            PostPassStatus::Changed
        } else {
            PostPassStatus::Unchanged
        }
    }
}

impl core::ops::BitOrAssign for PostPassStatus {
    fn bitor_assign(&mut self, rhs: Self) {
        if rhs.ir_changed() {
            *self = PostPassStatus::Changed;
        }
    }
}

/// A compiler pass which operates on an [Operation] of some kind.
#[allow(unused_variables)]
pub trait Pass: Sized + Any {
    /// The concrete/trait type targeted by this pass.
    ///
    /// Calls to `get_operation` will return a reference of this type.
    type Target: ?Sized + PassTarget;

    /// Used for downcasting
    #[inline(always)]
    fn as_any(&self) -> &dyn Any {
        self as &dyn Any
    }

    /// Used for downcasting
    #[inline(always)]
    fn as_any_mut(&mut self) -> &mut dyn Any {
        self as &mut dyn Any
    }

    /// Used for downcasting
    #[inline(always)]
    fn into_any(self: Box<Self>) -> Box<dyn Any> {
        self as Box<dyn Any>
    }

    /// The display name of this pass
    fn name(&self) -> &'static str;
    /// The command line option name used to control this pass
    fn argument(&self) -> &'static str {
        // NOTE: Could we compute an argument string from the type name or `self.name()`?
        ""
    }
    /// A description of what this pass does.
    fn description(&self) -> &'static str {
        ""
    }
    /// Obtain the underlying [PassInfo] object for this pass.
    fn info(&self) -> PassInfo {
        PassInfo::lookup(self.argument()).expect("pass is not currently registered")
    }
    /// The name of the operation that this pass operates on, or `None` if this is a generic pass.
    fn target_name(&self, context: &Context) -> Option<OperationName> {
        <<Self as Pass>::Target as PassTarget>::target_name(context)
    }
    /// If command-line options are provided for this pass, implementations must parse the raw
    /// options here, returning `Err` if parsing fails for some reason.
    ///
    /// By default, this is a no-op.
    fn initialize_options(&mut self, options: &str) -> Result<(), Report> {
        Ok(())
    }
    /// Prints out the pass in the textual representation of pipelines.
    ///
    /// If this is an adaptor pass, print its pass managers.
    fn print_as_textual_pipeline(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let argument = self.argument();
        if !argument.is_empty() {
            write!(f, "{argument}")
        } else {
            write!(f, "unknown<{}>", self.name())
        }
    }
    /// Returns true if this pass has associated statistics
    fn has_statistics(&self) -> bool {
        !self.statistics().is_empty()
    }
    /// Get pass statistics associated with this pass
    fn statistics(&self) -> &[Box<dyn Statistic>] {
        &[]
    }
    /// Get mutable access to the pass statistics associated with this pass
    fn statistics_mut(&mut self) -> &mut [Box<dyn Statistic>] {
        &mut []
    }
    /// Initialize any complex state necessary for running this pass.
    ///
    /// This hook should not rely on any state accessible during the execution of a pass. For
    /// example, `context`/`get_operation`/`get_analysis`/etc. should not be invoked within this
    /// hook.
    ///
    /// This method is invoked after all dependent dialects for the pipeline are loaded, and is not
    /// allowed to load any further dialects (override the `get_dependent_dialects()` hook for this
    /// purpose instead). Returns `Err` with a diagnostic if initialization fails, in which case the
    /// pass pipeline won't execute.
    fn initialize(&mut self, context: Rc<Context>) -> Result<(), Report> {
        Ok(())
    }
    /// Query if this pass can be scheduled to run on the given operation type.
    fn can_schedule_on(&self, name: &OperationName) -> bool;
    /// Run this pass on the current operation
    fn run_on_operation(
        &mut self,
        op: EntityMut<'_, Self::Target>,
        state: &mut PassExecutionState,
    ) -> Result<(), Report>;
    /// Schedule an arbitrary pass pipeline on the provided operation.
    ///
    /// This can be invoke any time in a pass to dynamic schedule more passes. The provided
    /// operation must be the current one or one nested below.
    fn run_pipeline(
        &mut self,
        pipeline: &mut OpPassManager,
        op: OperationRef,
        state: &mut PassExecutionState,
    ) -> Result<(), Report> {
        state.run_pipeline(pipeline, op)
    }
}

impl<P> Pass for Box<P>
where
    P: Pass,
{
    type Target = <P as Pass>::Target;

    fn as_any(&self) -> &dyn Any {
        <P as Pass>::as_any(self)
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        <P as Pass>::as_any_mut(self)
    }

    fn into_any(self: Box<Self>) -> Box<dyn Any> {
        let pass = Box::into_inner(self);
        <P as Pass>::into_any(pass)
    }

    #[inline]
    fn name(&self) -> &'static str {
        <P as Pass>::name(self)
    }

    #[inline]
    fn argument(&self) -> &'static str {
        (**self).argument()
    }

    #[inline]
    fn description(&self) -> &'static str {
        (**self).description()
    }

    #[inline]
    fn info(&self) -> PassInfo {
        (**self).info()
    }

    #[inline]
    fn target_name(&self, context: &Context) -> Option<OperationName> {
        (**self).target_name(context)
    }

    #[inline]
    fn initialize_options(&mut self, options: &str) -> Result<(), Report> {
        (**self).initialize_options(options)
    }

    #[inline]
    fn print_as_textual_pipeline(&self, f: &mut fmt::Formatter) -> fmt::Result {
        (**self).print_as_textual_pipeline(f)
    }

    #[inline]
    fn has_statistics(&self) -> bool {
        (**self).has_statistics()
    }

    #[inline]
    fn statistics(&self) -> &[Box<dyn Statistic>] {
        (**self).statistics()
    }

    #[inline]
    fn statistics_mut(&mut self) -> &mut [Box<dyn Statistic>] {
        (**self).statistics_mut()
    }

    #[inline]
    fn initialize(&mut self, context: Rc<Context>) -> Result<(), Report> {
        (**self).initialize(context)
    }

    #[inline]
    fn can_schedule_on(&self, name: &OperationName) -> bool {
        (**self).can_schedule_on(name)
    }

    #[inline]
    fn run_on_operation(
        &mut self,
        op: EntityMut<'_, Self::Target>,
        state: &mut PassExecutionState,
    ) -> Result<(), Report> {
        (**self).run_on_operation(op, state)
    }

    #[inline]
    fn run_pipeline(
        &mut self,
        pipeline: &mut OpPassManager,
        op: OperationRef,
        state: &mut PassExecutionState,
    ) -> Result<(), Report> {
        (**self).run_pipeline(pipeline, op, state)
    }
}

pub type DynamicPipelineExecutor =
    dyn FnMut(&mut OpPassManager, OperationRef) -> Result<(), Report>;

/// The state for a single execution of a pass. This provides a unified
/// interface for accessing and initializing necessary state for pass execution.
pub struct PassExecutionState {
    /// The operation being transformed
    op: OperationRef,
    context: Rc<Context>,
    analysis_manager: AnalysisManager,
    /// The set of preserved analyses for the current execution
    preserved_analyses: PreservedAnalyses,
    // Callback in the pass manager that allows one to schedule dynamic pipelines that will be
    // rooted at the provided operation.
    #[allow(unused)]
    pipeline_executor: Option<Box<DynamicPipelineExecutor>>,
    post_pass_status: PostPassStatus,
}
impl PassExecutionState {
    pub fn new(
        op: OperationRef,
        context: Rc<Context>,
        analysis_manager: AnalysisManager,
        pipeline_executor: Option<Box<DynamicPipelineExecutor>>,
    ) -> Self {
        Self {
            op,
            context,
            analysis_manager,
            preserved_analyses: Default::default(),
            pipeline_executor,
            post_pass_status: PostPassStatus::Unchanged,
        }
    }

    #[inline(always)]
    pub fn context(&self) -> Rc<Context> {
        self.context.clone()
    }

    #[inline(always)]
    pub const fn current_operation(&self) -> &OperationRef {
        &self.op
    }

    #[inline(always)]
    pub const fn analysis_manager(&self) -> &AnalysisManager {
        &self.analysis_manager
    }

    #[inline(always)]
    pub const fn preserved_analyses(&self) -> &PreservedAnalyses {
        &self.preserved_analyses
    }

    #[inline(always)]
    pub fn preserved_analyses_mut(&mut self) -> &mut PreservedAnalyses {
        &mut self.preserved_analyses
    }

    #[inline(always)]
    pub fn post_pass_status(&self) -> &PostPassStatus {
        &self.post_pass_status
    }

    #[inline(always)]
    pub fn set_post_pass_status(&mut self, post_pass_status: PostPassStatus) {
        self.post_pass_status = post_pass_status;
    }

    pub fn run_pipeline(
        &mut self,
        pipeline: &mut OpPassManager,
        op: OperationRef,
    ) -> Result<(), Report> {
        if let Some(pipeline_executor) = self.pipeline_executor.as_deref_mut() {
            pipeline_executor(pipeline, op)
        } else {
            Ok(())
        }
    }
}