New applications of program synthesis Armando SolarLezama Synthesis



![Example Sketch Spec bit[W] avg. Spec(bit[W] x, bit[W] y){ bit[W] avg(bit[W] x, bit[W] y) Example Sketch Spec bit[W] avg. Spec(bit[W] x, bit[W] y){ bit[W] avg(bit[W] x, bit[W] y)](https://slidetodoc.com/presentation_image_h/7ae478050c73cbd2d889d9d6f9d4944b/image-4.jpg)














![Legacy to Halide for (k=y_min-2; k<=y_max+2; k++) { for (j=x_min-2; j<=x_max+2; j++) { post_vol[((x_max+5)*(k-(y_min-2))+(j)-(x_min-2))] Legacy to Halide for (k=y_min-2; k<=y_max+2; k++) { for (j=x_min-2; j<=x_max+2; j++) { post_vol[((x_max+5)*(k-(y_min-2))+(j)-(x_min-2))]](https://slidetodoc.com/presentation_image_h/7ae478050c73cbd2d889d9d6f9d4944b/image-19.jpg)


![Example out = 0 for(int i=0; i<n-1; ++i){ out[i+1] = in[i]; } How do Example out = 0 for(int i=0; i<n-1; ++i){ out[i+1] = in[i]; } How do](https://slidetodoc.com/presentation_image_h/7ae478050c73cbd2d889d9d6f9d4944b/image-22.jpg)
![Inductive hypothesis out = 0 for(int i=0; i<n-1; ++i){ out[i+1] = in[i]; } Also Inductive hypothesis out = 0 for(int i=0; i<n-1; ++i){ out[i+1] = in[i]; } Also](https://slidetodoc.com/presentation_image_h/7ae478050c73cbd2d889d9d6f9d4944b/image-23.jpg)


![out = 0 for(int i=0; i<n-1; ++i){ out[i+1] = in[i]; } Loop invariant out = 0 for(int i=0; i<n-1; ++i){ out[i+1] = in[i]; } Loop invariant](https://slidetodoc.com/presentation_image_h/7ae478050c73cbd2d889d9d6f9d4944b/image-26.jpg)





- Slides: 31

New applications of program synthesis Armando Solar-Lezama

Synthesis: 1980 s view Complete Formal Specification

Synthesis: modern view Space of programs Reference Implementation Input/Output Examples Test Harnesses
![Example Sketch Spec bitW avg SpecbitW x bitW y bitW avgbitW x bitW y Example Sketch Spec bit[W] avg. Spec(bit[W] x, bit[W] y){ bit[W] avg(bit[W] x, bit[W] y)](https://slidetodoc.com/presentation_image_h/7ae478050c73cbd2d889d9d6f9d4944b/image-4.jpg)
Example Sketch Spec bit[W] avg. Spec(bit[W] x, bit[W] y){ bit[W] avg(bit[W] x, bit[W] y) implements avg. Spec{ bit[2*W] xx = extend@signed(x, 2*W); bit[2*W] yy = extend@signed(y, 2*W); return expr@signed({x, y}, 4); } bit[2*W] r = rshift@signed(xx+yy, 1); return (r[0: : W]); } expr : : = const | var | expr>>? ? | ~expr | expr + expr | expr ^ expr | expr & expr

And 8 seconds later… (x & y) + (x ^ y) >> 1 Cool! Now can you synthesize programs with more than 1 line of code?

Synthesis of distributed memory algorithms (SC 14) • Synthesizer can help with non-trivial distributed memory implementations • Scalability of resulting code is comparable with hand-crafted Fortran

So how much can you synthesize? • A little more if you are synthesizing from scratch • 5 or 6 LOC in one shot • But… • We can synthesize many more if there is independence • We can synthesize them within larger pieces of code • 2 -4 K LOC in many cases • We can do it very reliably So what can you do if you can synthesize small expressions in a large program?

High-Level Language Graphical programming Compiler Sketch C-like language with holes and assertions Automated Tutoring Synthesis Solution

Analysis Tool Graphical programming Synthesis Subproblems Sketch C-like language with holes and assertions Automated Tutoring Synthesis Program Optimization Solution Solver Synthesis

Optimization with synthesis

Java to SQL Methods SQL Queries ORM libraries Objects Application Relations Database

Java to SQL Methods SQL Queries ORM libraries Objects Application Relations Database

Java to SQL List get. Users. With. Roles () { List users = User. get. All. Users(); List roles = Role. get. All. Roles(); List results = new Array. List(); SELECT * FROM user SELECT * FROM role for (User u : users) { for (Role r : roles) { if (u. role. Id == r. id) results. add(u); }} return results; } List get. Users. With. Roles () { convert to return execute. Query( “SELECT u FROM user u, role r WHERE u. role. Id == r. id ORDER BY u. role. Id, r. id”; }

Join Query 1 K original inferred Page load time (ms) 0 K Nested-loop join Hash join! O(n 2) O(n) 0 K 0 K 100 0 6/17/2013 0 K 0 K Number of roles / users in DB PLDI 2013 0 K 15

Real-world Evaluation Wilos (project management application) – 62 k LOC Operation type 6/17/2013 # Fragments found # Fragments converted Projection 1 1 Selection 13 10 Join 7 7 Aggregation 11 10 Total 33 28 PLDI 2013 16

Real-world Evaluation i. Tracker (bug tracking system) – 61 k LOC Operation type 6/17/2013 # Fragments found # Fragments converted Projection 3 2 Selection 3 2 Join 1 1 Aggregation 9 7 Total 16 12 PLDI 2013 17

Beyond SQL • This is a general idea Synthesis Source Code Proof of Equivalence DSL Program Enable optimization by raising the level of abstraction!

Legacy code to Halide Synthesis Legacy Fortran/C++ Code Proof of Equivalence Stencil DLS (Halide)
![Legacy to Halide for kymin2 kymax2 k for jxmin2 jxmax2 j postvolxmax5kymin2jxmin2 Legacy to Halide for (k=y_min-2; k<=y_max+2; k++) { for (j=x_min-2; j<=x_max+2; j++) { post_vol[((x_max+5)*(k-(y_min-2))+(j)-(x_min-2))]](https://slidetodoc.com/presentation_image_h/7ae478050c73cbd2d889d9d6f9d4944b/image-19.jpg)
Legacy to Halide for (k=y_min-2; k<=y_max+2; k++) { for (j=x_min-2; j<=x_max+2; j++) { post_vol[((x_max+5)*(k-(y_min-2))+(j)-(x_min-2))] =volume[((x_max+4)*(k-(y_min-2))+(j)-(x_min-2))] + vol_flux_y[((x_max+4)*(k+1 -(y_min-2))+(j)-(x_min-2))] - vol_flux_y[((x_max+4)*(k-(y_min-2))+(j)-(x_min-2))]; } }

Speedups on 24 cores

How does it work?
![Example out 0 forint i0 in1 i outi1 ini How do Example out = 0 for(int i=0; i<n-1; ++i){ out[i+1] = in[i]; } How do](https://slidetodoc.com/presentation_image_h/7ae478050c73cbd2d889d9d6f9d4944b/image-22.jpg)
Example out = 0 for(int i=0; i<n-1; ++i){ out[i+1] = in[i]; } How do you prove that the code implies the formula? Induction!
![Inductive hypothesis out 0 forint i0 in1 i outi1 ini Also Inductive hypothesis out = 0 for(int i=0; i<n-1; ++i){ out[i+1] = in[i]; } Also](https://slidetodoc.com/presentation_image_h/7ae478050c73cbd2d889d9d6f9d4944b/image-23.jpg)
Inductive hypothesis out = 0 for(int i=0; i<n-1; ++i){ out[i+1] = in[i]; } Also called a loop invariant

Proofs about loops •

Abstract view • Verification conditions
![out 0 forint i0 in1 i outi1 ini Loop invariant out = 0 for(int i=0; i<n-1; ++i){ out[i+1] = in[i]; } Loop invariant](https://slidetodoc.com/presentation_image_h/7ae478050c73cbd2d889d9d6f9d4944b/image-26.jpg)
out = 0 for(int i=0; i<n-1; ++i){ out[i+1] = in[i]; } Loop invariant

Problem • Invariant and Spec are unknown!

Synthesis problem • Find Spec and invariant that satisfy verification conditions

12 hrs It can be slow 18000 Synthesis Time (s) 14400 10800 7200 3600 0 ackl 91 ackl 92 ackl 94 ackl 95 amkl 100 amkl 101 amkl 103 amkl 105 amkl 107 amkl 98 amkl 99 Benchmark div 0 heat 0 grad 0 mgl 5 Synthesis time with parallel synthesis on 24 cores

But we know how to parallelize it

Moving forward • Applications • Synthesis as a core tool for a variety of problems • Techniques • Data driven synthesis • Leveraging big code • Synthesis for synthesizers