Title: Design-Driven Compilation
1Design-Driven Compilation
- Radu Rugina and Martin Rinard
- Laboratory for Computer Science
- Massachusetts Institute of Technology
2Overview
Analysis Problems Points-to Analysis, Region
Analysis
Two Potential Solutions
Evaluation
3Example - Divide and Conquer Sort
4
7
6
1
5
3
8
2
4Example - Divide and Conquer Sort
4
7
6
1
5
3
8
2
Divide
5Example - Divide and Conquer Sort
4
7
6
1
5
3
8
2
Divide
2
8
5
3
1
6
7
4
Conquer
6Example - Divide and Conquer Sort
4
7
6
1
5
3
8
2
Divide
2
8
5
3
1
6
7
4
Conquer
4
1
6
7
3
2
5
8
Combine
7Example - Divide and Conquer Sort
4
7
6
1
5
3
8
2
Divide
2
8
5
3
1
6
7
4
Conquer
4
1
6
7
3
2
5
8
Combine
2
1
3
4
6
5
7
8
8Divide and Conquer Algorithms
- Lots of Generated Concurrency
- Solve Subproblems in Parallel
9Divide and Conquer Algorithms
- Lots of Recursively Generated Concurrency
- Recursively Solve Subproblems in Parallel
10Divide and Conquer Algorithms
- Lots of Recursively Generated Concurrency
- Recursively Solve Subproblems in Parallel
- Combine Results in Parallel
11Sort n Items in d, Using t as Temporary Storage
void sort(int d, int t, int n) if (n gt
CUTOFF) sort(d,t,n/4) sort(dn/4,tn/4,n/4
) sort(d2(n/2),t2(n/2),n/4) sort(d3(n/4)
,t3(n/4),n-3(n/4)) merge(d,dn/4,dn/2,t) m
erge(dn/2,d3(n/4),dn,tn/2) merge(t,tn/2,t
n,d) else insertionSort(d,dn)
12Recursively Sort Four Quarters of d
void sort(int d, int t, int n) if (n gt
CUTOFF) sort(d,t,n/4) sort(dn/4,tn/4,n/4
) sort(d2(n/2),t2(n/2),n/4) sort(d3(n/4)
,t3(n/4),n-3(n/4)) merge(d,dn/4,dn/2,t) m
erge(dn/2,d3(n/4),dn,tn/2) merge(t,tn/2,t
n,d) else insertionSort(d,dn)
Divide array into subarrays and recursively sort
subarrays
13Recursively Sort Four Quarters of d
void sort(int d, int t, int n) if (n gt
CUTOFF) sort(d,t,n/4) sort(dn/4,tn/4,n/4
) sort(d2(n/2),t2(n/2),n/4) sort(d3(n/4)
,t3(n/4),n-3(n/4)) merge(d,dn/4,dn/2,t) m
erge(dn/2,d3(n/4),dn,tn/2) merge(t,tn/2,t
n,d) else insertionSort(d,dn)
Subproblems Identified Using Pointers Into
Middle of Array
d
dn/4
dn/2
d3(n/4)
14Recursively Sort Four Quarters of d
void sort(int d, int t, int n) if (n gt
CUTOFF) sort(d,t,n/4) sort(dn/4,tn/4,n/4
) sort(d2(n/2),t2(n/2),n/4) sort(d3(n/4)
,t3(n/4),n-3(n/4)) merge(d,dn/4,dn/2,t) m
erge(dn/2,d3(n/4),dn,tn/2) merge(t,tn/2,t
n,d) else insertionSort(d,dn)
d
dn/4
dn/2
d3(n/4)
15Recursively Sort Four Quarters of d
void sort(int d, int t, int n) if (n gt
CUTOFF) sort(d,t,n/4) sort(dn/4,tn/4,n/4
) sort(d2(n/2),t2(n/2),n/4) sort(d3(n/4)
,t3(n/4),n-3(n/4)) merge(d,dn/4,dn/2,t) m
erge(dn/2,d3(n/4),dn,tn/2) merge(t,tn/2,t
n,d) else insertionSort(d,dn)
Sorted Results Written Back Into Input Array
d
dn/4
dn/2
d3(n/4)
16Merge Sorted Quarters of d Into Halves of t
void sort(int d, int t, int n) if (n gt
CUTOFF) sort(d,t,n/4) sort(dn/4,tn/4,n/4
) sort(d2(n/2),t2(n/2),n/4) sort(d3(n/4)
,t3(n/4),n-3(n/4)) merge(d,dn/4,dn/2,t) m
erge(dn/2,d3(n/4),dn,tn/2) merge(t,tn/2,t
n,d) else insertionSort(d,dn)
d
t
tn/2
17Merge Sorted Halves of t Back Into d
void sort(int d, int t, int n) if (n gt
CUTOFF) sort(d,t,n/4) sort(dn/4,tn/4,n/4
) sort(d2(n/2),t2(n/2),n/4) sort(d3(n/4)
,t3(n/4),n-3(n/4)) merge(d,dn/4,dn/2,t) m
erge(dn/2,d3(n/4),dn,tn/2) merge(t,tn/2,t
n,d) else insertionSort(d,dn)
d
t
tn/2
18Use a Simple Sort for Small Problem Sizes
void sort(int d, int t, int n) if (n gt
CUTOFF) sort(d,t,n/4) sort(dn/4,tn/4,n/4
) sort(d2(n/2),t2(n/2),n/4) sort(d3(n/4)
,t3(n/4),n-3(n/4)) merge(d,dn/4,dn/2,t) m
erge(dn/2,d3(n/4),dn,tn/2) merge(t,tn/2,t
n,d) else insertionSort(d,dn)
d
dn
19Use a Simple Sort for Small Problem Sizes
void sort(int d, int t, int n) if (n gt
CUTOFF) sort(d,t,n/4) sort(dn/4,tn/4,n/4
) sort(d2(n/2),t2(n/2),n/4) sort(d3(n/4)
,t3(n/4),n-3(n/4)) merge(d,dn/4,dn/2,t) m
erge(dn/2,d3(n/4),dn,tn/2) merge(t,tn/2,t
n,d) else insertionSort(d,dn)
d
dn
20Parallel Sort
void sort(int d, int t, int n) if (n gt
CUTOFF) spawn sort(d,t,n/4) spawn
sort(dn/4,tn/4,n/4) spawn sort(d2(n/2),t2(
n/2),n/4) spawn sort(d3(n/4),t3(n/4),n-3(n/
4)) sync spawn merge(d,dn/4,dn/2,t) spawn
merge(dn/2,d3(n/4),dn,tn/2) sync merge(t,
tn/2,tn,d) else insertionSort(d,dn)
21What Do You Need To Know To Exploit This Form of
Parallelism?
Points-to Information (data blocks that pointers
point to) Region Information (accessed regions
within data blocks)
22Information Needed To Exploit Parallelism
- d and t point to different memory blocks
- Calls to sort access disjoint parts of d and t
- Together, calls access d,dn-1 and t,tn-1
- sort(d,t,n/4)
- sort(dn/4,tn/4,n/4)
- sort(dn/2,tn/2,n/4)
- sort(d3(n/4),t3(n/4),
- n-3(n/4))
-
d
dn-1
t
tn-1
d
dn-1
t
tn-1
d
dn-1
t
tn-1
d
dn-1
t
tn-1
23Information Needed To Exploit Parallelism
- d and t point to different memory blocks
- First two calls to merge access disjoint parts of
d,t - Together, calls access d,dn-1 and t,tn-1
- merge(d,dn/4,dn/2,t)
- merge(dn/2,d3(n/4),
- dn,tn/2)
- merge(t,tn/2,tn,d)
-
d
dn-1
t
tn-1
d
dn-1
t
tn-1
d
dn-1
t
tn-1
24Information Needed To Exploit Parallelism
Calls to insertionSort access d,dn-1
insertionSort(d,dn)
d
dn-1
25What Do You Need To Know To Exploit This Form of
Parallelism?
Points-to Information (d and t point to different
data blocks) Symbolic Region Information (accesse
d regions within d and t blocks)
26How Hard Is It To Figure These Things Out?
27How Hard Is It To Figure These Things Out?
Challenging
28How Hard Is It To Figure These Things Out?
- void insertionSort(int l, int h)
- int p, q, k
- for (p l1 p lt h p)
- for (k p, q p-1 l lt q k lt q q--)
- (q1) q
- (q1) k
-
-
- Not immediately obvious that
- insertionSort(l,h) accesses l,h-1
29How Hard Is It To Figure These Things Out?
void merge(int l1, intm, int h2, int d)
int h1 m int l2 m while ((l1 lt h1)
(l2 lt h2)) if (l1 lt l2) d l1 else
d l2 while (l1 lt h1) d
l1 while (l2 lt h2) d l2 Not
immediately obvious that merge(l,m,h,d) accesses
l,h-1 and d,d(h-l)-1
30Issues
- Heavy Use of Pointers
- Pointers into Middle of Arrays
- Pointer Arithmetic
- Pointer Comparison
- Multiple Procedures
- sort(int d, int t, n)
- insertionSort(int l, int h)
- merge(int l, int m, int h, int t)
- Recursion
31Fully Automatic Solution
- Whole-program pointer analysis
- Context-sensitive, flow-sensitive
- Rugina and Rinard, PLDI 1999
- Whole-program region analysis
- Symbolic constraint systems
- Solve by reducing to linear programs
- Rugina and Rinard, PLDI 2000
32Key Complication
- Need for sophisticated interprocedural analyses
- Pointer analysis
- Propagate analysis results through call graph
- Fixed-point algorithm for recursive programs
- Region analysis
- Formulation avoids fixed-point algorithms
- Single constraint system for each strongly
connected component - Need to have whole program in analyzable form
33Bigger Picture
- Points-to and region information is (implicitly)
part of the interface of each procedure - Programmer understands procedure interfaces
- Programmer knows
- Points-to relationships on entry
- Effect of procedure on points-to relationships
- Regions of memory blocks that procedure accesses
34Idea
- Enhance procedure interface to make points-to and
region information explicit - Points-to language
- Points-to graphs at entry and exit
- Effect on points-to relationships
- Region language
- Symbolic specification of accessed regions
- Programmer provides information
- Analysis verifies that it is correct
35Points-to Language
- f(p, q, n)
- context
- entry p-gt_a, q-gt_b
- exit p-gt_a, _a-gt_c,
- q-gt_b, _b-gt_d
-
- context
- entry p-gt_a, q-gt_a
- exit p-gt_a, _a-gt_c,
- q-gt_a
-
-
36Points-to Language
f(p, q, n) context entry p-gt_a,
q-gt_b exit p-gt_a, _a-gt_c, q-gt_b,
_b-gt_d context entry p-gt_a,
q-gt_a exit p-gt_a, _a-gt_c, q-gt_a
Contexts for f(p,q,n)
entry
exit
37Verifying Points-to Information
- One (flow sensitive) analysis per context
- f(p,q,n)
- .
- .
- .
Contexts for f(p,q,n)
entry
exit
38Verifying Points-to Information
Start with entry points-to graph f(p,q,n)
. . .
Contexts for f(p,q,n)
entry
exit
39Verifying Points-to Information
Analyze procedure f(p,q,n) . . .
Contexts for f(p,q,n)
entry
p
q
exit
40Verifying Points-to Information
Analyze procedure f(p,q,n) . . .
Contexts for f(p,q,n)
entry
exit
41Verifying Points-to Information
Check result against exit points-to
graph f(p,q,n) . . .
Contexts for f(p,q,n)
entry
exit
42Verifying Points-to Information
Similarly for other context f(p,q,n)
. . .
Contexts for f(p,q,n)
entry
exit
43Verifying Points-to Information
Start with entry points-to graph f(p,q,n)
. . .
Contexts for f(p,q,n)
entry
exit
44Verifying Points-to Information
Analyze procedure f(p,q,n) . . .
Contexts for f(p,q,n)
entry
exit
45Verifying Points-to Information
Check result against exit points-to
graph f(p,q,n) . . .
Contexts for f(p,q,n)
entry
exit
46Analysis of Call Statements
g(r,n) . . f(r,s,n) . .
47Analysis of Call Statements
- Analysis produces points-graph before call
- g(r,n)
- .
- .
- f(r,s,n)
- .
- .
-
r
s
48Analysis of Call Statements
Retrieve declared contexts from callee g(r,n)
. . f(r,s,n) . .
Contexts for f(p,q,n)
entry
r
s
exit
49Analysis of Call Statements
Find context with matching entry graph g(r,n)
. . f(r,s,n) . .
Contexts for f(p,q,n)
entry
r
s
exit
50Analysis of Call Statements
Find context with matching entry graph g(r,n)
. . f(r,s,n) . .
Contexts for f(p,q,n)
entry
r
s
exit
51Analysis of Call Statements
Apply corresponding exit points-to graph g(r,n)
. . f(r,s,n) . .
Contexts for f(p,q,n)
entry
r
s
exit
52Analysis of Call Statements
Continue analysis after call g(r,n)
. . f(r,s,n) . .
53Analysis of Call Statements
g(r,n) . . f(r,s,n) . .
- Result
- Points-to declarations separate analysis of
multiple procedures - Transformed
- global, whole-program analysis into
- local analysis that operates on each procedure
independently
54Region Language
h(p,n) reads p,pn-1 writes p,pn-1
55Region Language
- h(p,n)
- reads p,pn-1
- writes p,pn-1
reads p
pn-1
writes p
pn-1
56Verifying Region Information
- Two region containment requirements
- Direct Accesses
- Locations directly accessed by procedure must be
contained in declared regions - Callees
- Regions accessed by callees must be contained in
declared regions of caller
57Verifying Region Information
h(p,n) if (n lt k) for (i0iltni) pi
pi-1 else h(p,n/2) h(pn/2,n-n/2)
58Verifying Region Information
- Extract directly accessed regions
- h(p,n)
- if (n lt k)
- for (i0iltni)
- pi pi-1
- else
- h(p,n/2)
- h(pn/2,n-n/2)
-
-
reads p
pn-1
Directly Accessed Regions
writes p
pn-1
59Verifying Region Information
Check inclusion within declared regions h(p,n)
if (n lt k) for (i0iltni) pi
pi-1 else h(p,n/2) h(pn/2,n-n/2)
reads p
pn-1
Declared Regions for h(p,n)
writes p
pn-1
reads p
pn-1
Directly Accessed Regions
writes p
pn-1
60Verifying Region Information
Check inclusion for accesses of callees h(p,n)
if (n lt k) for (i0iltni) pi
pi-1 else h(p,n/2) h(pn/2,n-n/2)
Callees
61Verifying Region Information
Start with call to h(p,n/2) h(p,n) if (n lt
k) for (i0iltni) pi pi-1 else
h(p,n/2) h(pn/2,n-n/2)
62Verifying Region Information
Extract and translate regions for
h(p,n/2) h(p,n) if (n lt k) for
(i0iltni) pi pi-1 else
h(p,n/2) h(pn/2,n-n/2)
reads p
Translated Regions from h(p,n/2)
pn-1
writes p
pn-1
63Verifying Region Information
Check inclusion in declared regions of
caller h(p,n) if (n lt k) for
(i0iltni) pi pi-1 else
h(p,n/2) h(pn/2,n-n/2)
reads p
pn-1
Declared Regions for h(p,n)
writes p
pn-1
reads p
Translated Regions from h(p,n/2)
pn-1
writes p
pn-1
64Verifying Region Information
Similarly for call h(pn/2,n-n/2) h(p,n) if
(n lt k) for (i0iltni) pi
pi-1 else h(p,n/2) h(pn/2,n-n/2)
reads p
pn-1
Translated Regions from h(pn/2,n-n/2)
writes p
pn-1
65Verifying Region Information
Check inclusion in declared regions of
caller h(p,n) if (n lt k) for
(i0iltni) pi pi-1 else
h(p,n/2) h(pn/2,n-n/2)
reads p
pn-1
Declared Regions for h(p,n)
writes p
pn-1
reads p
pn-1
Translated Regions from h(pn/2,n-n/2)
writes p
pn-1
66Verifying Region Information
- Result
- Region declarations separate analysis of multiple
procedures - Transformed
- global, whole-program analysis into
- local analysis that operates on each procedure
independently
h(p,n) if (n lt k) for (i0iltni) pi
pi-1 else h(p,n/2) h(pn/2,n-n/2)
67Experience
68Experience
- Implemented points-to and region languages
- Integrated with points-to and region analyses
- Obtained Divide and Conquer Benchmarks
- Quicksort (QS)
- Mergesort (MS)
- Matrix multiply (MM)
- LU decomposition (LU)
- Heat (H)
- Written in C
- We added points-to and region information
Sorting Programs
Dense Matrix Computations
Scientific Computation
69Results
- With points-to and region information, could
parallelize all benchmarks - Points-to information speeds up points-to
analysis significantly (up to factor of two) - Region information has no significant effect on
how fast region analysis runs
70Programming Overhead
- Proportion of C Code, Region Declarations, and
Points-to Declarations
1.00
C Code
0.75
0.50
0.25
0.00
QS
MS
MM
LU
H
71Evaluation
- How difficult is it to provide declarations?
- Not that difficult.
- Have to write comparatively little code
- Must know information anyway
- How much benefit does compiler obtain?
- Substantial benefit.
- Simpler analysis software (no complex
interprocedural analysis) - More scalable, precise analysis
72Evaluation
- Software Engineering Benefits of Points-to and
Region Declarations - Analysis reflects programmers intention
- Enhanced code reliability
- Enhanced interface information
- Analyze incomplete programs
- Programs that use libraries
- Programs under development
73Evaluation
- Drawbacks of Points-to and Region Declarations
- Have to learn new language
- Have to integrate into development process
- Legacy software issues (programmer
may not know points-to and region information)
74Related Work
- Extended Type Systems
- FX/87 GJLS87
- Dependent Types XF99
- Issue where put extended type information?
- Integrated with rest of program
- Separated from rest of program
- Program Verification
- ESC DLNS98
- PVS ORRSS96
75Related Work
- Pointer Analysis
- Landi, Ryder, Zhang PLDI93
- Emami, Ghiya, Hendren PLDI94
- Wilson, Lam PLDI96
- Rugina, Rinard PLDI99
- Rountev, Ryder CC01
- Region Analysis
- Triolet, Irigoin, Feautrier- PLDI86
- Havlak, Kennedy IEEE TPDS91
- Rugina, Rinard PLDI00
- Pointer Specifications
- Hendren, Hummel, Nicolau PLDI92
- Guyer, Lin LCPC00
76Conclusion
- Basic idea
- Programmer provides
- Points-to information
- Region information
- Analysis
- Verifies correctness
- Uses information to enable further analyses and
transformations - Lots of benefits to compiler and programmer