Skip to main content

wave_compiler/optimize/
pass.rs

1// Copyright 2026 Ojima Abraham
2// SPDX-License-Identifier: Apache-2.0
3
4//! Optimization pass trait definition.
5//!
6//! All optimization passes implement this trait, allowing them to be
7//! composed in a pipeline and run until a fixed point is reached.
8
9use crate::mir::function::MirFunction;
10
11/// Trait for MIR optimization passes.
12pub trait Pass {
13    /// Returns the name of this pass.
14    fn name(&self) -> &'static str;
15
16    /// Run the pass on a MIR function. Returns true if any changes were made.
17    fn run(&self, func: &mut MirFunction) -> bool;
18}
19
20#[cfg(test)]
21mod tests {
22    use super::*;
23    use crate::mir::value::BlockId;
24
25    struct NoopPass;
26    impl Pass for NoopPass {
27        fn name(&self) -> &'static str {
28            "noop"
29        }
30        fn run(&self, func: &mut MirFunction) -> bool {
31            let _ = func.name.len();
32            false
33        }
34    }
35
36    #[test]
37    fn test_pass_trait() {
38        let pass = NoopPass;
39        assert_eq!(pass.name(), "noop");
40    }
41
42    #[test]
43    fn test_pass_returns_false_when_no_changes() {
44        let pass = NoopPass;
45        let mut func = MirFunction::new("test".into(), BlockId(0));
46        assert!(!pass.run(&mut func));
47    }
48}