160 likes | 304 Views
Generating Compiler Optimizations from Proofs. Ross Tate Michael Stepp Sorin Lerner University of California, San Diego. Optimizing by Hand. Original. Optimized. for (i = 0; i < 5 0 ; i++) for (j = 0; j < 5 0 ; j++) *(img++) = f(i, j); Executes more efficiently.
E N D
Generating Compiler Optimizations from Proofs Ross Tate Michael Stepp Sorin Lerner University of California, San Diego
Optimizing by Hand Original Optimized for (i = 0; i < 50; i++) for (j = 0; j < 50; j++) *(img++) = f(i, j); Executes more efficiently for (i = 0; i < 50; i++) for (j = 0; j < 50; j++) img[i*50 + j] = f(i, j); • Easier to understand Make the compiler do it! Train the compiler to do it! Many compilers do not perform this optimization
Generalizing Optimizations Original Optimized for (i = 0; i < 50; i++) for (j = 0; j < 50; j++) *(img++) = f(i, j); for (i = 0; i < w; i++) for (j = 0; j < h; j++) use(*(img++)) for (i = 0; i < 50; i++) for (j = 0; j < 50; j++) img[i*50 + j] = f(i, j); for (i = 0; i < w; i++) for (j = 0; j < h; j++) use(img[i*h+ j]) Generalize Generalized h is loop-invariant use does not modify i, j, or img
Generalizing Automatically Original Optimized for (i = 0; i < 50; i++) for (j = 0; j < 50; j++) *(img++) = f(i, j); for (i = 0; i < 50; i++) for (j = 0; j < 50; j++) img[i*50 + j] = f(i, j);
Proof informs which details in the programs are actually important Instantiation Translation Validator Proof Generalizer • Programmers can teach the compiler • with just one concrete example • using a language they already know
8 + 8 – 8 Translation Validator 8 + 8 – 8 = 8 8
Generalized Proof ∃e. e – e = 0 ∃c,d. c = 0 ⇒ d + c = d Uses fact i Adds fact ii Adds fact i Translation Validator Proof Generalizer Proof Generalizer a d + e – e d + c c a ∅ i) e – e = 0 i) c = 0 ii) a = b i) c = 0 ii) a = b i) c = 0 ii) d + c = b i) c = 0 ii) d + c = d i) e – e = 0 ii) d + e – e = d c = 0 c c a = b a c b ∀x. x – x = 0 ∀x,y. x = 0 ⇒y + x = y b d b Instantiation: d,e↦ 8 Uses fact i Adds fact ii Adds fact i 8 + 8 – 8 ∅ i) 8 – 8 = 0 i) 8 – 8 = 0 ii) 8 + 8 – 8 = 8 8
Most General Optimization for this proof d + e – e Translation Validator Proof Generalizer Learned Optimization d by examining a proof of equivalence 8 + 8 – 8 8
Abstract Algorithm • Formalized using category theory • Pushouts • Pullbacks • Pushout Completions • Different categories for different logics • Different representations of programs • Domains besides program optimizations Proof Generalizer
Training the Optimizer • Need a more expressive logic • Program Expression Graphs [POPL ‘09] Translation Validator Proof Generalizer
Training the Optimizer • Inter-Loop Strength & Bound Reduction • Loop Induction Strength & Bound Reduction • Partial & Specialized Inlining • Temporary Object Removal • Loop Operation Factoring & Distribution • Entire Loop Strength Reduction • Array Copy Propagation • Design Pattern Overhead Reduction As translation validation improves, so will optimization learning, for free!
Speeding Up Optimization • Ran on a ray tracer • Rewriter produced high-quality code Advanced Optimizer Optimization Generalizer • We want • advanced optimizations • the speed of the rewriter Decomposer Fast Rewriter
Flexible Proof Generalization • Learn database query optimizations • Improve type error messages in Haskell • Assist with contract debugging in Spec# • Infer polymorphism in typed programs Proof Generalizer τ
Conclusion • Algorithm to learn compiler optimizations • from programmers • from superoptimizers • Abstract proof generalization algorithm • Applicable to other logics • Applicable to other domains Thank You