@@ -213,9 +213,43 @@ fn o2_removes_dead_seed_store_across_noalias_call() { |
| 213 | 213 | "raw loop body should still contain the seed store and the real fill store:\n{}", |
| 214 | 214 | raw_body |
| 215 | 215 | ); |
| 216 | + // Detect "seed-zero" pattern: a store whose value is a `const_int 0`. |
| 217 | + // This invariant is robust to loop transformations like partial |
| 218 | + // unrolling that multiply store counts — DSE removing the dead |
| 219 | + // seed eliminates the const-zero feeding any store. |
| 220 | + fn count_store_of_zero(func_section: &str) -> usize { |
| 221 | + use std::collections::HashSet; |
| 222 | + // Collect every value id defined as `const_int 0`. |
| 223 | + let zeros: HashSet<&str> = func_section |
| 224 | + .lines() |
| 225 | + .filter_map(|l| { |
| 226 | + let t = l.trim(); |
| 227 | + let after_eq = t.strip_prefix("%")?; |
| 228 | + let (id, rest) = after_eq.split_once(" = const_int 0 ")?; |
| 229 | + let _ = rest; |
| 230 | + Some(id) |
| 231 | + }) |
| 232 | + .collect(); |
| 233 | + // Count `store %ID, ...` where %ID is one of those. |
| 234 | + func_section |
| 235 | + .lines() |
| 236 | + .filter_map(|l| l.trim().strip_prefix("store %")) |
| 237 | + .filter(|rest| { |
| 238 | + let id = rest.split(',').next().unwrap_or("").trim(); |
| 239 | + zeros.contains(id) |
| 240 | + }) |
| 241 | + .count() |
| 242 | + } |
| 243 | + let raw_zeros = count_store_of_zero(raw_fill); |
| 244 | + let opt_zeros = count_store_of_zero(opt_fill); |
| 216 | 245 | assert!( |
| 217 | | - opt_body.matches("store ").count() < raw_body.matches("store ").count(), |
| 218 | | - "O2 should remove the dead seed store while keeping the real fill:\n{}", |
| 219 | | - opt_body |
| 246 | + raw_zeros >= 1, |
| 247 | + "raw seed_and_fill should have at least one store-of-zero (the dead seed):\n{}", |
| 248 | + raw_fill |
| 249 | + ); |
| 250 | + assert_eq!( |
| 251 | + opt_zeros, 0, |
| 252 | + "O2 DSE should remove the dead store-of-zero (raw had {}):\n{}", |
| 253 | + raw_zeros, opt_fill |
| 220 | 254 | ); |
| 221 | 255 | } |