| @@ -104,3 +104,147 @@ pub fn main() -> Int { |
| 104 | 104 | .unwrap_or_else(|| panic!("no fusion loop in:\n{whole}")); |
| 105 | 105 | assert!(!loop_fn.contains("Cons#"), "{loop_fn}"); |
| 106 | 106 | } |
| 107 | + |
| 108 | +/// Compile `src`, run the whole optimiser as the driver does, and return |
| 109 | +/// the rules that fired and the program as text. |
| 110 | +fn optimised(src: &str) -> (Vec<&'static str>, String) { |
| 111 | + let compiled = compile("main", "main.lse", src).unwrap(); |
| 112 | + let entry = compiled.entry("main").unwrap(); |
| 113 | + let mut program = compiled.program; |
| 114 | + let fired = louise_core::opt::run_in(&mut program, &[entry]); |
| 115 | + ( |
| 116 | + fired.into_iter().map(|f| f.rule).collect(), |
| 117 | + program.to_string(), |
| 118 | + ) |
| 119 | +} |
| 120 | + |
| 121 | +#[test] |
| 122 | +fn a_pipeline_in_an_if_arm_fuses() { |
| 123 | + // The epic's pipeline, but inside the `else` arm: ANF wraps the arm |
| 124 | + // in a let-bound block, and the pass descends into it. |
| 125 | + let src = r#" |
| 126 | +import std/list |
| 127 | + |
| 128 | +fn go(k: Int) -> Int { |
| 129 | + if k == 0 { |
| 130 | + 0 |
| 131 | + } else { |
| 132 | + let doubled = list.map(list.range(1, 100 + k), fn(x) { x * 2 }) |
| 133 | + list.fold(list.filter(doubled, fn(x) { x % 3 == 0 }), 0, fn(a, x) { a + x }) |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +pub fn main() -> Int { |
| 138 | + go(10) |
| 139 | +} |
| 140 | +"#; |
| 141 | + let (fired, whole) = optimised(src); |
| 142 | + assert!(fired.contains(&"fold ∘ filter"), "{fired:?}"); |
| 143 | + assert!(fired.contains(&"fold ∘ map"), "{fired:?}"); |
| 144 | + assert!(fired.contains(&"fold ∘ range"), "{fired:?}"); |
| 145 | + let go = whole |
| 146 | + .split("\n\n") |
| 147 | + .find(|f| f.starts_with("fn main.go(")) |
| 148 | + .unwrap_or_else(|| panic!("no go in:\n{whole}")); |
| 149 | + assert!(!go.contains("std/list.map("), "{go}"); |
| 150 | + assert!(!go.contains("std/list.filter("), "{go}"); |
| 151 | + assert!(!go.contains("std/list.fold("), "{go}"); |
| 152 | +} |
| 153 | + |
| 154 | +#[test] |
| 155 | +fn a_pipeline_in_a_match_arm_fuses() { |
| 156 | + let src = r#" |
| 157 | +import std/list |
| 158 | + |
| 159 | +pub fn main() -> Int { |
| 160 | + match Some(10) { |
| 161 | + None -> 0, |
| 162 | + Some(k) -> { |
| 163 | + let doubled = list.map(list.range(1, 100 + k), fn(x) { x * 2 }) |
| 164 | + list.fold(list.filter(doubled, fn(x) { x % 3 == 0 }), 0, fn(a, x) { a + x }) |
| 165 | + } |
| 166 | + } |
| 167 | +} |
| 168 | +"#; |
| 169 | + let (fired, _) = optimised(src); |
| 170 | + assert!(fired.contains(&"fold ∘ filter"), "{fired:?}"); |
| 171 | + assert!(fired.contains(&"fold ∘ map"), "{fired:?}"); |
| 172 | + assert!(fired.contains(&"fold ∘ range"), "{fired:?}"); |
| 173 | +} |
| 174 | + |
| 175 | +#[test] |
| 176 | +fn a_pipeline_in_nested_blocks_fuses() { |
| 177 | + let src = r#" |
| 178 | +import std/list |
| 179 | + |
| 180 | +pub fn main() -> Int { |
| 181 | + if 1 == 2 { |
| 182 | + 0 |
| 183 | + } else { |
| 184 | + if 3 == 4 { |
| 185 | + 1 |
| 186 | + } else { |
| 187 | + let doubled = list.map(list.range(1, 100), fn(x) { x * 2 }) |
| 188 | + list.fold(list.filter(doubled, fn(x) { x % 3 == 0 }), 0, fn(a, x) { a + x }) |
| 189 | + } |
| 190 | + } |
| 191 | +} |
| 192 | +"#; |
| 193 | + let (fired, _) = optimised(src); |
| 194 | + assert!(fired.contains(&"fold ∘ filter"), "{fired:?}"); |
| 195 | + assert!(fired.contains(&"fold ∘ map"), "{fired:?}"); |
| 196 | + assert!(fired.contains(&"fold ∘ range"), "{fired:?}"); |
| 197 | +} |
| 198 | + |
| 199 | +#[test] |
| 200 | +fn an_effectful_stage_in_an_arm_is_not_fused() { |
| 201 | + let src = r#" |
| 202 | +import std/io |
| 203 | +import std/list |
| 204 | + |
| 205 | +pub fn main() -> Int / <io> { |
| 206 | + if 1 == 2 { |
| 207 | + 0 |
| 208 | + } else { |
| 209 | + let doubled = list.map([1, 2], fn(x) { x * 2 }) |
| 210 | + list.fold(list.map(doubled, fn(x) { |
| 211 | + io.println("{x}") |
| 212 | + x + 1 |
| 213 | + }), 0, fn(a, x) { a + x }) |
| 214 | + } |
| 215 | +} |
| 216 | +"#; |
| 217 | + let (fired, _) = optimised(src); |
| 218 | + // The printing stage performs io: no rule fires. |
| 219 | + assert!(fired.is_empty(), "{fired:?}"); |
| 220 | +} |
| 221 | + |
| 222 | +#[test] |
| 223 | +fn a_producer_outside_the_arm_is_consumed_inside_it() { |
| 224 | + // `doubled` is bound before the `if`, its single use is the consumer |
| 225 | + // inside the arm: the rules fire and its `let` is dropped (the block |
| 226 | + // runs at most once, so the fused stage runs no more often than the |
| 227 | + // unfused one did — see the module doc). |
| 228 | + let src = r#" |
| 229 | +import std/list |
| 230 | + |
| 231 | +pub fn main() -> Int { |
| 232 | + let doubled = list.map([1, 2, 3], fn(x) { x * 2 }) |
| 233 | + if 1 == 2 { |
| 234 | + 0 |
| 235 | + } else { |
| 236 | + list.fold(list.filter(doubled, fn(x) { x % 3 == 0 }), 0, fn(a, x) { a + x }) |
| 237 | + } |
| 238 | +} |
| 239 | +"#; |
| 240 | + let (fired, whole) = optimised(src); |
| 241 | + assert!(fired.contains(&"fold ∘ filter"), "{fired:?}"); |
| 242 | + assert!(fired.contains(&"fold ∘ map"), "{fired:?}"); |
| 243 | + let main = whole |
| 244 | + .split("\n\n") |
| 245 | + .find(|f| f.starts_with("fn main.main(")) |
| 246 | + .unwrap_or_else(|| panic!("no main in:\n{whole}")); |
| 247 | + assert!(!main.contains("std/list.map("), "{main}"); |
| 248 | + assert!(!main.contains("std/list.filter("), "{main}"); |
| 249 | + assert!(!main.contains("std/list.fold("), "{main}"); |
| 250 | +} |