|
| 1 | +//! Benchmark for fixpoint iteration cycle resolution. |
| 2 | +//! |
| 3 | +//! This benchmark simulates a (very simplified) version of a real dataflow analysis using fixpoint |
| 4 | +//! iteration. |
| 5 | +use codspeed_criterion_compat::{criterion_group, criterion_main, BatchSize, Criterion}; |
| 6 | +use salsa::{CycleRecoveryAction, Database as Db, Setter}; |
| 7 | +use std::collections::BTreeSet; |
| 8 | +use std::iter::IntoIterator; |
| 9 | + |
| 10 | +/// A Use of a symbol. |
| 11 | +#[salsa::input] |
| 12 | +struct Use { |
| 13 | + reaching_definitions: Vec<Definition>, |
| 14 | +} |
| 15 | + |
| 16 | +/// A Definition of a symbol, either of the form `base + increment` or `0 + increment`. |
| 17 | +#[salsa::input] |
| 18 | +struct Definition { |
| 19 | + base: Option<Use>, |
| 20 | + increment: usize, |
| 21 | +} |
| 22 | + |
| 23 | +#[derive(Eq, PartialEq, Clone, Debug, salsa::Update)] |
| 24 | +enum Type { |
| 25 | + Bottom, |
| 26 | + Values(Box<[usize]>), |
| 27 | + Top, |
| 28 | +} |
| 29 | + |
| 30 | +impl Type { |
| 31 | + fn join(tys: impl IntoIterator<Item = Type>) -> Type { |
| 32 | + let mut result = Type::Bottom; |
| 33 | + for ty in tys.into_iter() { |
| 34 | + result = match (result, ty) { |
| 35 | + (result, Type::Bottom) => result, |
| 36 | + (_, Type::Top) => Type::Top, |
| 37 | + (Type::Top, _) => Type::Top, |
| 38 | + (Type::Bottom, ty) => ty, |
| 39 | + (Type::Values(a_ints), Type::Values(b_ints)) => { |
| 40 | + let mut set = BTreeSet::new(); |
| 41 | + set.extend(a_ints); |
| 42 | + set.extend(b_ints); |
| 43 | + Type::Values(set.into_iter().collect()) |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + result |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +#[salsa::tracked(cycle_fn=use_cycle_recover, cycle_initial=use_cycle_initial)] |
| 52 | +fn infer_use<'db>(db: &'db dyn Db, u: Use) -> Type { |
| 53 | + let defs = u.reaching_definitions(db); |
| 54 | + match defs[..] { |
| 55 | + [] => Type::Bottom, |
| 56 | + [def] => infer_definition(db, def), |
| 57 | + _ => Type::join(defs.iter().map(|&def| infer_definition(db, def))), |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +#[salsa::tracked(cycle_fn=def_cycle_recover, cycle_initial=def_cycle_initial)] |
| 62 | +fn infer_definition<'db>(db: &'db dyn Db, def: Definition) -> Type { |
| 63 | + let increment_ty = Type::Values(Box::from([def.increment(db)])); |
| 64 | + if let Some(base) = def.base(db) { |
| 65 | + let base_ty = infer_use(db, base); |
| 66 | + add(&base_ty, &increment_ty) |
| 67 | + } else { |
| 68 | + increment_ty |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +fn def_cycle_initial(_db: &dyn Db, _def: Definition) -> Type { |
| 73 | + Type::Bottom |
| 74 | +} |
| 75 | + |
| 76 | +fn def_cycle_recover( |
| 77 | + _db: &dyn Db, |
| 78 | + value: &Type, |
| 79 | + count: u32, |
| 80 | + _def: Definition, |
| 81 | +) -> CycleRecoveryAction<Type> { |
| 82 | + cycle_recover(value, count) |
| 83 | +} |
| 84 | + |
| 85 | +fn use_cycle_initial(_db: &dyn Db, _use: Use) -> Type { |
| 86 | + Type::Bottom |
| 87 | +} |
| 88 | + |
| 89 | +fn use_cycle_recover( |
| 90 | + _db: &dyn Db, |
| 91 | + value: &Type, |
| 92 | + count: u32, |
| 93 | + _use: Use, |
| 94 | +) -> CycleRecoveryAction<Type> { |
| 95 | + cycle_recover(value, count) |
| 96 | +} |
| 97 | + |
| 98 | +fn cycle_recover(value: &Type, count: u32) -> CycleRecoveryAction<Type> { |
| 99 | + match value { |
| 100 | + Type::Bottom => CycleRecoveryAction::Iterate, |
| 101 | + Type::Values(_) => { |
| 102 | + if count > 4 { |
| 103 | + CycleRecoveryAction::Fallback(Type::Top) |
| 104 | + } else { |
| 105 | + CycleRecoveryAction::Iterate |
| 106 | + } |
| 107 | + } |
| 108 | + Type::Top => CycleRecoveryAction::Iterate, |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +fn add(a: &Type, b: &Type) -> Type { |
| 113 | + match (a, b) { |
| 114 | + (Type::Bottom, _) | (_, Type::Bottom) => Type::Bottom, |
| 115 | + (Type::Top, _) | (_, Type::Top) => Type::Top, |
| 116 | + (Type::Values(a_ints), Type::Values(b_ints)) => { |
| 117 | + let mut set = BTreeSet::new(); |
| 118 | + set.extend( |
| 119 | + a_ints |
| 120 | + .into_iter() |
| 121 | + .flat_map(|a| b_ints.into_iter().map(move |b| a + b)), |
| 122 | + ); |
| 123 | + Type::Values(set.into_iter().collect()) |
| 124 | + } |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +fn dataflow(criterion: &mut Criterion) { |
| 129 | + criterion.bench_function("converge_diverge", |b| { |
| 130 | + b.iter_batched_ref( |
| 131 | + || { |
| 132 | + let mut db = salsa::DatabaseImpl::new(); |
| 133 | + |
| 134 | + let defx0 = Definition::new(&db, None, 0); |
| 135 | + let defy0 = Definition::new(&db, None, 0); |
| 136 | + let defx1 = Definition::new(&db, None, 0); |
| 137 | + let defy1 = Definition::new(&db, None, 0); |
| 138 | + let use_x = Use::new(&db, vec![defx0, defx1]); |
| 139 | + let use_y = Use::new(&db, vec![defy0, defy1]); |
| 140 | + defx1.set_base(&mut db).to(Some(use_y)); |
| 141 | + defy1.set_base(&mut db).to(Some(use_x)); |
| 142 | + |
| 143 | + // prewarm cache |
| 144 | + let _ = infer_use(&db, use_x); |
| 145 | + let _ = infer_use(&db, use_y); |
| 146 | + |
| 147 | + (db, defx1, use_x, use_y) |
| 148 | + }, |
| 149 | + |(db, defx1, use_x, use_y)| { |
| 150 | + // Set the increment on x to 0. |
| 151 | + defx1.set_increment(db).to(0); |
| 152 | + |
| 153 | + // Both symbols converge on 0. |
| 154 | + assert_eq!(infer_use(db, *use_x), Type::Values(Box::from([0]))); |
| 155 | + assert_eq!(infer_use(db, *use_y), Type::Values(Box::from([0]))); |
| 156 | + |
| 157 | + // Set the increment on x to 1. |
| 158 | + defx1.set_increment(db).to(1); |
| 159 | + |
| 160 | + // Now the loop diverges and we fall back to Top. |
| 161 | + assert_eq!(infer_use(db, *use_x), Type::Top); |
| 162 | + assert_eq!(infer_use(db, *use_y), Type::Top); |
| 163 | + }, |
| 164 | + BatchSize::LargeInput, |
| 165 | + ); |
| 166 | + }); |
| 167 | +} |
| 168 | + |
| 169 | +criterion_group!(benches, dataflow); |
| 170 | +criterion_main!(benches); |
0 commit comments