The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics...

81
ESOP March 19, 2013 Mihai Budiu, Microsoft Research, Silicon Valley Joel Galenson, UC Berkeley Gordon Plotkin, University of Edinburgh

Transcript of The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics...

Page 1: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

ESOP

March 19, 2013

Mihai Budiu, Microsoft Research, Silicon Valley

Joel Galenson, UC Berkeley

Gordon Plotkin, University of Edinburgh

Page 2: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

• Motivating example

–Declarative parallel programming with LINQ and DryadLINQ

• Divide-and-conquer compilation

–Compilers and Partial Compilers

• Building real compilers – LINQ, DryadLINQ, and matrix computations

2

Page 3: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

LINQ Summary

Input

3

.Net objects of type T

Page 4: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

LINQ Summary

Where (filter)

Input

4

.Net objects of type T

Page 5: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

LINQ Summary

Where (filter)

Select (map)

Input

5

.Net objects of type T

Page 6: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

LINQ Summary

Where (filter)

Select (map)

GroupBy

Input

6

.Net objects of type T

Page 7: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

LINQ Summary

Where (filter)

Select (map)

GroupBy

OrderBy (sort)

Input

7

.Net objects of type T

Page 8: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

LINQ Summary

Where (filter)

Select (map)

GroupBy

OrderBy (sort)

Aggregate (fold)

Input

8

.Net objects of type T

Page 9: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

LINQ Summary

Where (filter)

Select (map)

GroupBy

OrderBy (sort)

Aggregate (fold)

Join

Input

9

.Net objects of type T

Page 10: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

Distributed Collections

10

Page 11: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

Distributed Collections

11

Partition

Distributed Collection

Page 12: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

Select (Map)

f( )=

12

Page 13: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

Select (Map)

f( )=

Select(x => f(x))

13

Page 14: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

Select (Map)

f( )=

Select(x => f(x))

+

14

Page 15: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

Select (Map)

f( )=

Select(x => f(x))

+

+

15

Page 16: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

Aggregate (Fold)

f( , )=

associative

16

Page 17: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

Aggregate (Fold)

f( , )=

Aggregate((x,y) => f(x,y))

associative

17

Page 18: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

Aggregate (Fold)

f( , )=

Aggregate((x,y) => f(x,y))

+

associative

18

Page 19: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

Aggregate (Fold)

f( , )=

Aggregate((x,y) => f(x,y))

+

associative

+

19

Page 20: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

Aggregate (Fold)

f( , )=

Aggregate((x,y) => f(x,y))

+

associative

+

20

Page 21: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

GroupBy

GroupBy(x => K(x))

21

Page 22: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

GroupBy

GroupBy(x => K(x))

o

22

Page 23: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

GroupBy

GroupBy(x => K(x))

o

o

PartitionBy(x => K(x))

23

Page 24: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

GroupBy

GroupBy(x => K(x))

o

o

o

PartitionBy(x => K(x))

GroupBy(x => K(x))

Equal under permutation

24

Page 25: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

• Motivating example

–Declarative parallel programming with LINQ and DryadLINQ

• Divide-and-conquer compilation

–Compilers and Partial Compilers

• Building real compilers – LINQ, DryadLINQ, and matrix computations

25

Page 26: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

Compilers

Compiler

S T

26

𝐶 ∶ source → target

Page 27: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

Partial compilers

Child

Compiler

target source

source’ target’

Partial compiler

27

Page 28: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

Partial compilers

Child

Compiler

target source

source’ target’

Partial compiler

28

𝑃𝐶 ∶ source → source′ × target′ → target

Page 29: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

Partial compilers

Child

Compiler

target source

source’ target’

Partial compiler R

re

du

ce G

gen

erat

e

29

𝑃𝐶 ∶ source → source′ × target′ → target

Page 30: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

Compiler Composition target source

source’ target’

PC

C

30

Page 31: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

Compiler Composition target source

source’ target’

PC

C

31

Page 32: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

PC’

Partial Compiler Composition

PC

32

target source

source’ target’

source’’ target’’

Page 33: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

Composition Laws

33

Page 34: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

Composition Laws

Id

34

Page 35: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

Compilers as First-Class Objects

PC PC’ ⊗

Tensor: 𝑃𝐶 ⊗ 𝑃𝐶′ 35

Page 36: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

Multiple Children

SEQ

C1 C2

S T

36

Page 37: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

Multiple Children

SEQ

C1 C2

Example: 𝑆 = 𝑓 ∘ 𝑔 𝑇 = 𝐶1 𝑓 ∘ 𝐶2(𝑔)

S T

37

Page 38: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

Star

38

C

*

source* target*

Page 39: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

Star

39

C

*

source* target*

Page 40: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

Star

40

C

*

source* target* … …

Page 41: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

Conditional

if gpu

GPU CPU

gpu : source -> bool

41

Page 42: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

Case

42

case

Select GroupBy Aggregate

Page 43: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

• Motivating example

–Declarative parallel programming with LINQ and DryadLINQ

• Divide-and-conquer compilation

–Compilers and Partial Compilers

• Building real compilers – LINQ, DryadLINQ, and matrix computations

43

Page 44: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

A LINQ Compiler

44

Select GroupBy Aggregate

Page 45: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

A LINQ Compiler

45

case

Select GroupBy Aggregate

Page 46: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

A LINQ Compiler

46

case

Select GroupBy Aggregate

*

Page 47: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

A LINQ Compiler

47

case

Select GroupBy Aggregate

*

SEQ

Page 48: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

A Multi-Core LINQ Compiler

48

case

Select GroupBy Aggregate

MC

*

SEQ

Page 49: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

A Multi-Core LINQ Compiler

49

case

Select GroupBy Aggregate

MC

*

SEQ

MC

Page 50: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

A Multi-Core LINQ Compiler

50

case

Select GroupBy Aggregate

MC

*

SEQ

MC

S

T

Page 51: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

A Multi-Core LINQ Compiler

51

case

Select GroupBy Aggregate

MC

*

SEQ

MC

S

o

o

T

T T

Page 52: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

A Distributed, Multi-Core

LINQ Compiler

52

case

Select GroupBy Aggregate

MC

*

Cluster

SEQ

Page 53: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

A Distributed, Multi-Core

LINQ Compiler

53

case

Select GroupBy Aggregate

MC

*

Cluster

SEQ

Cluster

Page 54: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

A Distributed, Multi-Core

LINQ Compiler

54

case

Select GroupBy Aggregate

MC

*

Cluster

SEQ

Cluster

S

T

Page 55: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

A Distributed, Multi-Core

LINQ Compiler

55

case

Select GroupBy Aggregate

MC

*

Cluster

SEQ

Cluster

S

o

o

T

T T

Page 56: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

A Distributed Matrix Compiler

56

*

Page 57: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

A Distributed Matrix Compiler

57

Tile Tile *

Page 58: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

Cluster

A Distributed Matrix Compiler

58

Matrix

*

SEQ

Tile

Tile Tile *

Page 59: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

Cluster

A Distributed Matrix Compiler

59

Matrix

*

SEQ

case

+ * …

Tile Tile *

Page 60: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

M1.Times(M2).Plus(M3)

60

M1

M2

M3

*

+

Join. GroupBy.

Select( Aggregate) Concat

GroupBy. Select(

Aggregate)

+

PCTILE

PCTILE

PCTILE

CLINQ

CLINQ

*

PCMATRIX

+

PCMATRIX

PCSEQ

Page 61: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

M1.Times(M2).Plus(M3)

61

M1

M2

M3

*

+

Join. GroupBy.

Select( Aggregate) Concat

GroupBy. Select(

Aggregate)

+

PCTILE

PCTILE

PCTILE

CLINQ

CLINQ

*

PCMATRIX

+

PCMATRIX

PCSEQ

Page 62: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

M1.Times(M2).Plus(M3)

62

M1

M2

M3

*

+

Join. GroupBy.

Select( Aggregate) Concat

GroupBy. Select(

Aggregate)

+

PCTILE

PCTILE

PCTILE

CLINQ

CLINQ

PCMATRIX

PCMATRIX

PCSEQ

Page 63: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

M1.Times(M2).Plus(M3)

63

Page 64: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

Partial Compiler Correctness

64

if gpu

GPU CPU

Page 65: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

Partial Compiler Correctness

65

𝑔𝑝𝑢 𝑆 𝐶𝐺𝑃𝑈 {¬𝑔𝑝𝑢 𝑆 }𝐶𝐶𝑃𝑈

𝑇 𝐼𝐹 𝑔𝑝𝑢 𝑇𝐻𝐸𝑁 𝐶𝐺𝑃𝑈 𝑒𝑙𝑠𝑒 𝐶𝐶𝑃𝑈 if gpu

GPU CPU

Page 66: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

Partial Compiler Correctness

66

𝑔𝑝𝑢 𝑆 𝐶𝐺𝑃𝑈 {¬𝑔𝑝𝑢 𝑆 }𝐶𝐶𝑃𝑈

𝑇 𝐼𝐹 𝑔𝑝𝑢 𝑇𝐻𝐸𝑁 𝐶𝐺𝑃𝑈 𝑒𝑙𝑠𝑒 𝐶𝐶𝑃𝑈

Totally correct compiler from partially correct parts!

if gpu

GPU CPU

Page 67: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

Related Work

• Dialectica category

– Inspired partial compilers and their operators.

• Milner’s tactics

– Partial compilers are a typed form of tactics.

• Multistage compilers

– Fit as a composition of unary partial compilers.

• Federated databases, cooperating analyses

– Interesting applications.

67

Page 68: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

Thank you

68

Page 69: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

Backup slides

69

Page 70: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

Homomorphisms

70

ℎ ∶ 𝐴 → 𝐵 ℎ 𝑎1 +𝐴 𝑎2 = ℎ 𝑎1 +𝐵 ℎ(𝑎2)

collections

concatenation

concatenation

Page 71: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

Nested Parallelism

71

Machine

Page 72: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

Nested Parallelism

72

Machine Core

Page 73: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

Correctness Definition

73

⊨ ⊆ 𝑡𝑎𝑟𝑔𝑒𝑡 × 𝑠𝑜𝑢𝑟𝑐𝑒 𝑇 ⊨ 𝑆

T implements the meaning of S

𝐶 ∶ 𝑠𝑜𝑢𝑟𝑐𝑒 → 𝑡𝑎𝑟𝑔𝑒𝑡 𝐶 𝑖𝑠 𝑐𝑜𝑟𝑟𝑒𝑐𝑡 𝑤. 𝑟𝑒𝑠𝑝. 𝑡𝑜 ⊨ iff

∀𝑆 ∈ 𝑠𝑜𝑢𝑟𝑐𝑒. 𝐶 𝑆 ⊨ 𝑆

C

T S

Page 74: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

Partial Compiler Correctness

74

⊨ ⊆ 𝑠𝑜𝑢𝑟𝑐𝑒 × 𝑡𝑎𝑟𝑔𝑒𝑡 ⊨′ ⊆ 𝑠𝑜𝑢𝑟𝑐𝑒′ × 𝑡𝑎𝑟𝑔𝑒𝑡′

PC is correct iff ∀𝑆, 𝑇′. 𝑇′ ⊨′ 𝑆′ ⇒ 𝑇 ⊨ 𝑆

T S

T’ S’

Page 75: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

Correctness Theorems

75

PC, C correct ⇒ correct

𝑃𝐶 ⊗ 𝑃𝐶′ correct

𝐶∗ correct

etc.

Page 76: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

Partial Correctness

76

Correct only for some programs

Correct only for some programs

Generated sub-programs satisfy this predicate

Page 77: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

Partial Correctness

77

Correct only for some programs

Correct only for some programs

Generated sub-programs satisfy this predicate

{Precondition} {Postcondition}

Page 78: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

Functor

78

functor

CPU

Trans Id

Id : target -> target’ Trans : source -> source’

Page 79: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

Iteration

79

iteration

Opt

Page 80: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

A Distributed Matrix Compiler

80

Matrix -> sets of tiles

Matrix operations Distributed LINQ

Tile operations LINQ

Page 81: The Compiler Forest · –Inspired partial compilers and their operators. •Milner’s tactics –Partial compilers are a typed form of tactics. •Multistage compilers –Fit as

Staged Compilers

81