Difference Constraints: An adequate Abstraction for Complexity Analysis of Imperative Programs Florian Zuleger Technische Universität Wien FMCAD, 28.09.2015 Joint work with Moritz Sinn, Helmut Veith
Bounds and Complexity foo(uint n) Local Bound(t1): x x = n; y = n; Variable that while(x > 0) decreases when t1 t1:{ x--; is executed. y = y + 2; } z = y; while(z > 0) t2: z--; TU Wien Florian Zuleger 2
Bounds and Complexity foo(uint n) Local Bound(t1): x x = n; Transition Bound(t1): n y = n; while(x > 0) t1:{ x--; y = y + 2; # of visits to } transition t1 z = y; while(z > 0) t2: z--; TU Wien Florian Zuleger 3
Bounds and Complexity foo(uint n) Local Bound(t1): x x = n; Transition Bound(t1): n y = n; while(x > 0) Local Bound(t2): z t1:{ x--; y = y + 2; Variable Bound(y): 3n } z = y; while(z > 0) Invariant of shape t2: z--; y · 3n TU Wien Florian Zuleger 4
Bounds and Complexity foo(uint n) Local Bound(t1): x x = n; Transition Bound(t1): n y = n; while(x > 0) Local Bound(t2): z t1:{ x--; y = y + 2; Variable Bound(y): 3n } z = y; Transition Bound(t2): 3n while(z > 0) t2: z--; Complexity: 4n TU Wien Florian Zuleger 5
Bounds and Complexity Bound Analysis: Introduce • # of visits to a transition a counter c • # of visits to multiple transitions and • # of iterations of a loop increment • resource consumption of a program at places • complexity of a program of interest • upper bound on the value of a variable Intuition: All these bound analysis problems are related and can be reduced to each other. TU Wien Florian Zuleger 6
Applications of Bound Analysis Verification: • Computing bounds on resource consumption (CPU time, memory, bandwidth ,…) • Termination analysis with quantitative information on program progress Program understanding: • Static profiling • Understanding program performance TU Wien Florian Zuleger 7
Bound Analysis and the Halting Problem For imperative programs: Halting Problem = termination analysis of loops → Bound analysis is a hard problem! while(n != 0) Typical while(n > 0) { if (n % 2 = 0) programs? m := n--; n = n / 2; while(m > 0 && ?) else m--; n = 3n+1; } TU Wien Florian Zuleger 8
Bound Analysis and the Halting Problem For imperative programs: Collatz Real-life Halting Problem = termination analysis Conjecture Programs of loops → Bound analysis is a hard problem! while(n != 0) Typical while(n > 0) { if (n % 2 = 0) programs? m := n--; n = n / 2; while(m > 0 && ?) else m--; n = 3n+1; } TU Wien Florian Zuleger 9
Methodological Approach Abstract Program Bounds Program Program Bound Abstraction Analysis Desired properties of our Design goals of our abstract program model: analysis: • simple • no refinement loop • good computational • fail fast • captures most common properties • motivates further loop patterns theoretical analysis TU Wien Florian Zuleger 10
Minimal Requirements for Abstract Program Model? foo(uint n) x = n; We need to model: y = n; while(x > 0) - counter variables { x--; • increments/ y = y + 2; decrements } • resets z = y; - finite control while(z > 0) z--; TU Wien Florian Zuleger 11
Difference Constraint Programs Conjunction of foo(uint n) u‘ ≤ v + k (k in Z ). x = n; y = n; x' ≤ n while(x > 0) y' ≤ n { x--; x' ≤ x - 1 y' ≤ y + 2 y = y + 2; z' ≤ y } z = y; z' ≤ z - 1 while(z > 0) Variables take z--; values over N . TU Wien Florian Zuleger 12
Difference Constraint Programs (DCPs) • Introduced by Ben-Amram (2008) • Termination is undecidable in general, but decidiable for deterministic DCPs ( deterministic = at most one constraint u‘ ≤ v + k for every variable u, this is a natural subclass: every variable assignement is abstracted to one constraint) • we show: DCPs can model interesting bound analysis problems TU Wien Florian Zuleger 13
Invariant Analysis Problems x' ≤ x - 1 x' ≤ n y' ≤ y x' ≤ n x' ≤ n x' ≤ n y' ≤ n a' ≤ n y' ≤ m2 y' ≤ n y' ≤ m1 a' ≤ n x' ≤ x - 1 x' ≤ x - 1 x' ≤ x y' ≤ y + 2 y' ≤ y + 2 y' ≤ y + 2 z' ≤ y z' ≤ y z' ≤ y a' ≤ a - 1 z' ≤ z - 1 z' ≤ z - 1 z' ≤ z - 1 Variable Bound(y): Variable Bound(y): Variable Bound(y): 3n n + 2max{m1,m2} 3n + 2n 2 TU Wien Florian Zuleger 14
Bound Analysis Algorithm: Intuition y modified on two Local Bound(t1): x transitions ta Transition Bound(t1): n x' ≤ n y' ≤ n t1 Local Bound(t2): z x' ≤ x - 1 y' ≤ y + 2 z' ≤ y Variable Bound(y): 3n t2 z' ≤ z - 1 Transition Bound(t2): 3n Variable Bound(y) = Mutual recursion between n * Transition Bound(ta) + Variable Bound and 2 * Transition Bound(t1) Transition Bound TU Wien Florian Zuleger 15
Bound Analysis Algorithm We assume a local bound for every transition, which decreases when the ta transition is executed: x' ≤ n LB(t1) = x y' ≤ n t1 LB(t2) = z x' ≤ x - 1 tb LB(ta) = 1 y' ≤ y + 2 z' ≤ y LB(tb) = 1 t2 We define increments and resets : z' ≤ z - 1 inc(t1,y) = 2 reset(ta,z) = y reset(tb,x) = n reset(tb,y) = n TU Wien Florian Zuleger 16
Bound Analysis Algorithm TB(t2) = ta = VB(reset(tb,LB(t2)) * TB(tb) = x' ≤ n = VB(reset(tb,z)) * 1 = y' ≤ n t1 = VB(y) = x' ≤ x - 1 tb = VB(reset(ta,y)) * TB(ta) + y' ≤ y + 2 z' ≤ y inc(t1,y) * TB(t1) = t2 = n * 1 + z' ≤ z - 1 2 * VB(reset(ta,LB(t1)) * TB(ta) = = n + 2 * VB(x) * 1 = 3n TU Wien Florian Zuleger 17
Bound Analysis Algorithm Let P be a set of transitions, where each transition t of P has local bound LB(t). For all t 2 P we define TB(t) = Σ s 2 P inc(s,LB(t))*TB(s) + Σ s 2 P VB(reset(s,LB(t)))*TB(s) VB(t) = Σ s 2 P inc(s,LB(t))*TB(s) + max s 2 P VB(reset(s,LB(t)))*TB(s) Mutual recursion terminates, if there are no cycles. (is the case for reasonable programs). TU Wien Florian Zuleger 18
Invariant Analysis Problems Related Work: x' ≤ x - 1 has also been x' ≤ n y' ≤ y x' ≤ n x' ≤ n x' ≤ n observed in earlier y' ≤ n a' ≤ n y' ≤ m2 y' ≤ n y' ≤ m1 a' ≤ n work on bound x' ≤ x - 1 x' ≤ x - 1 x' ≤ x analysis y' ≤ y + 2 y' ≤ y + 2 y' ≤ y + 2 • z' ≤ y z' ≤ y z' ≤ y SPEED a' ≤ a - 1 • KoAT Complementary technique for • z' ≤ z - 1 z' ≤ z - 1 z' ≤ z - 1 Loopus 2014 invariant computation Variable Bound(y): Variable Bound(y): Variable Bound(y): 3n n + 2max{m1,m2} 3n + 2n 2 TU Wien Florian Zuleger 19
Amoritzed Complexity Analysis foo(uint n) x = n; Local Bound(t1): x r = 0; while(x > 0) Transition Bound(t1): n {t1 : x--; r++; Local Bound(t2): p if(?) { p = r; while(p > 0) Variable Bound(p): n t2: p--; r = 0; Transition Bound(t2): n 2 ? } } TU Wien Florian Zuleger 20
Amoritzed Complexity Analysis r ist reset after foo(uint n) x = n; the inner loop Local Bound(t1): x r = 0; → every while(x > 0) increment r++ Transition Bound(t1): n {t1 : x--; leads to one loop r++; iteration Local Bound(t2): p if(?) { p = r; while(p > 0) Variable Bound(p): n t2: p--; r = 0; Transition Bound(t2): n } } We call r = 0 a context for p = r. Complexity: 2n TU Wien Florian Zuleger 21
Amoritzed Complexity Analysis foo(uint n) x = n; x' ≤ n r = 0; r' ≤ 0 while(x > 0) x' ≤ x { x--; x' ≤ x - 1 r' ≤ r r++; r' ≤ r + 1 p' ≤ r if(?) { p = r; x' ≤ x while(p > 0) r' ≤ r p--; x' ≤ x x' ≤ x r = 0; r' ≤ r x' ≤ x r' ≤ 0 } p' ≤ p - 1 r' ≤ r } Complexity: 2n TU Wien Florian Zuleger 22
Amortized Complexity in Real Code Amortization due to Dependencies between increments and resets 15 Examples found during our experiments Examples: forsyte.at/software/loopus TU Wien Florian Zuleger 23
Implementation • Tool: „Loopus“ based on LLVM and Z3 • Evaluation: CBench („Collective Benchmark“) – C programs – 1027 files with > 200 kLoc, > 4000 loops – 1751 functions • Comparison to the tools: – KoAT (TACAS 2014) – CoFloCo (APLAS 2014) – Loopus 2014 (CAV 2014) • First experimental comparison on real world code TU Wien Florian Zuleger 24
Experimental Results: Function Complexity n 2 n 3 n >3 2 n Succ 1 n Time TimeOut Loopus 15 806 205 489 97 13 2 0 15m 6 Loopus 14 431 200 188 43 0 0 0 40m 20 KoAT 430 253 138 35 2 0 2 5.6h 161 CoFloCo 386 200 148 38 0 0 0 4.7h 217 More details: forsyte.at/software/loopus TU Wien Florian Zuleger 25
Summary Contributions to bound/complexity analysis: • notions of increment/decrement and reset • first algorithm based on DCPs • we demonstrate the scalability and applicability of our algorithm • DCPs are an interesting model for further research TU Wien Florian Zuleger 26
Recommend
More recommend