41
Self-Adjusting Stack Machines Matthew A. Hammer Georg Neis Yan Chen Umut A. Acar Max Planck Institute for Software Systems OOPSLA 2011 — October 27, 2011 Portland, Oregon, USA

Self-Adjusting Stack Machines€¦ · I Key Idea: Reusing traces e cient update Matthew A. Hammer Self-Adjusting Stack Machines 8. Challenges Existing work targets functional languages:

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Self-Adjusting Stack Machines€¦ · I Key Idea: Reusing traces e cient update Matthew A. Hammer Self-Adjusting Stack Machines 8. Challenges Existing work targets functional languages:

Self-Adjusting Stack Machines

Matthew A. HammerGeorg Neis Yan Chen Umut A. Acar

Max Planck Institute for Software Systems

OOPSLA 2011 — October 27, 2011

Portland, Oregon, USA

Page 2: Self-Adjusting Stack Machines€¦ · I Key Idea: Reusing traces e cient update Matthew A. Hammer Self-Adjusting Stack Machines 8. Challenges Existing work targets functional languages:

Static Computation Versus Dynamic Computation

Static Computation:

Fixed Input Compute Fixed Output

Dynamic Computation:

Changing Input Compute Changing Output

ReadChanges

UpdateWrite

Updates

Matthew A. Hammer Self-Adjusting Stack Machines 2

Page 3: Self-Adjusting Stack Machines€¦ · I Key Idea: Reusing traces e cient update Matthew A. Hammer Self-Adjusting Stack Machines 8. Challenges Existing work targets functional languages:

Dynamic Data is Everywhere

Software systems often consume/produce dynamic data

ScientificSimulation

Reactive SystemsAnalysis of

Internet data

Matthew A. Hammer Self-Adjusting Stack Machines 3

Page 4: Self-Adjusting Stack Machines€¦ · I Key Idea: Reusing traces e cient update Matthew A. Hammer Self-Adjusting Stack Machines 8. Challenges Existing work targets functional languages:

Tractability Requires Dynamic Computations

Changing Input Compute Changing Output

Static Case(Re-evaluation “from scratch”)

compute 1 sec# of changes 1 million

Total time 11.6 days

Dynamic Case(Uses update mechanism)

compute 10 secupdate 1× 10−3 sec

# of changes 1 millionTotal time 16.7 minutes

Speedup 1000x

Matthew A. Hammer Self-Adjusting Stack Machines 4

Page 5: Self-Adjusting Stack Machines€¦ · I Key Idea: Reusing traces e cient update Matthew A. Hammer Self-Adjusting Stack Machines 8. Challenges Existing work targets functional languages:

Tractability Requires Dynamic Computations

Changing Input Compute Changing Output

ReadChanges

UpdateWrite

Updates

Static Case(Re-evaluation “from scratch”)

compute 1 sec# of changes 1 million

Total time 11.6 days

Dynamic Case(Uses update mechanism)

compute 10 secupdate 1× 10−3 sec

# of changes 1 millionTotal time 16.7 minutes

Speedup 1000x

Matthew A. Hammer Self-Adjusting Stack Machines 4

Page 6: Self-Adjusting Stack Machines€¦ · I Key Idea: Reusing traces e cient update Matthew A. Hammer Self-Adjusting Stack Machines 8. Challenges Existing work targets functional languages:

Dynamic Computations can be Hand-Crafted

As an input sequence changes, maintain a sorted output.

1,7,3,6,5,2,4

Changing Input

compute 1,2,3,4,5,6,7

Changing Output

1,7,3,6/,5,2,4Remove 6 update 1,2,3,4,5,6/,7

1,7,3,6,5,2/,4Reinsert 6,Remove 2

update 1,2/,3,4,5,6,7

A binary search tree would suffice here (e.g., a splay tree)What about more exotic/complex computations?

Matthew A. Hammer Self-Adjusting Stack Machines 5

Page 7: Self-Adjusting Stack Machines€¦ · I Key Idea: Reusing traces e cient update Matthew A. Hammer Self-Adjusting Stack Machines 8. Challenges Existing work targets functional languages:

How to Program Dynamic Computations?

Can this programming be systematic?What are the right abstractions?

1. How to describe dynamic computations?I Usability: Are these descriptions easy to write?I Generality: How much can they describe?

2. How to implement these descriptions?I Efficiency: Are updates faster than re-evaluation?I Consistency: Do updates provide the correct result?

Matthew A. Hammer Self-Adjusting Stack Machines 6

Page 8: Self-Adjusting Stack Machines€¦ · I Key Idea: Reusing traces e cient update Matthew A. Hammer Self-Adjusting Stack Machines 8. Challenges Existing work targets functional languages:

In Self-Adjusting Computation,Ordinary programs describe dynamic computations.

Self-Adjusting Program

C Source Compiler C Target + Run-time

The self-adjusting program:

1. Computes initial output from initial input

2. Automatically updates output when input changes

Matthew A. Hammer Self-Adjusting Stack Machines 7

Page 9: Self-Adjusting Stack Machines€¦ · I Key Idea: Reusing traces e cient update Matthew A. Hammer Self-Adjusting Stack Machines 8. Challenges Existing work targets functional languages:

Self-Adjusting Programs

Input Read Compute Write Output

ReadChanges

TraceWrite

Updates

Update

I Self-adjusting program maintains dynamicdependencies in an execution trace.

I Key Idea: Reusing traces efficient update

Matthew A. Hammer Self-Adjusting Stack Machines 8

Page 10: Self-Adjusting Stack Machines€¦ · I Key Idea: Reusing traces e cient update Matthew A. Hammer Self-Adjusting Stack Machines 8. Challenges Existing work targets functional languages:

Challenges

Existing work targets functional languages:

I Library support for SML and Haskell

I DeltaML extends MLton SML compiler

Our work targets low-level languages (e.g., C)

I stack-based

I imperative

I no strong type system

I no automatic memory management

Matthew A. Hammer Self-Adjusting Stack Machines 9

Page 11: Self-Adjusting Stack Machines€¦ · I Key Idea: Reusing traces e cient update Matthew A. Hammer Self-Adjusting Stack Machines 8. Challenges Existing work targets functional languages:

Challenges Low-Level Self-Adj. Computation

Efficient update complex resource interactions:

I execution trace, call stack, memory manager

Input Read Compute Write Output

ReadChanges

TraceWrite

Updates

Update

Matthew A. Hammer Self-Adjusting Stack Machines 10

Page 12: Self-Adjusting Stack Machines€¦ · I Key Idea: Reusing traces e cient update Matthew A. Hammer Self-Adjusting Stack Machines 8. Challenges Existing work targets functional languages:

Challenges Low-Level Self-Adj. Computation

Efficient update complex resource interactions:

I execution trace, call stack, memory manager

Input Read Compute Write Output

ReadChanges

TraceWrite

Updates

Update

Matthew A. Hammer Self-Adjusting Stack Machines 10

Page 13: Self-Adjusting Stack Machines€¦ · I Key Idea: Reusing traces e cient update Matthew A. Hammer Self-Adjusting Stack Machines 8. Challenges Existing work targets functional languages:

Challenges Low-Level Self-Adj. Computation

Efficient update complex resource interactions:

I execution trace, call stack, memory manager

coderevaluation

(make new trace,search old trace

)

found change found match

changepropagation(repair + editold trace

)Matthew A. Hammer Self-Adjusting Stack Machines 10

Page 14: Self-Adjusting Stack Machines€¦ · I Key Idea: Reusing traces e cient update Matthew A. Hammer Self-Adjusting Stack Machines 8. Challenges Existing work targets functional languages:

Example: Dynamic Expression Trees

Objective: As tree changes, maintain its valuation

+

+

3 4

0

5 6

((3 + 4) − 0) + (5 − 6) = 6

+

+

3 4

0

+

5 6

5

((3+4)−0)+((5−6)+5) = 11

Consistency: Output is correct valuationEfficiency: Update time is O(#affected intermediate results)

Matthew A. Hammer Self-Adjusting Stack Machines 11

Page 15: Self-Adjusting Stack Machines€¦ · I Key Idea: Reusing traces e cient update Matthew A. Hammer Self-Adjusting Stack Machines 8. Challenges Existing work targets functional languages:

Example: Dynamic Expression Trees

Objective: As tree changes, maintain its valuation

+

+

3 4

0

5 6

((3 + 4) − 0) + (5 − 6) = 6

+

+

3 4

0

+

5 6

5

((3+4)−0)+((5−6)+5) = 11

Consistency: Output is correct valuationEfficiency: Update time is O(#affected intermediate results)

Matthew A. Hammer Self-Adjusting Stack Machines 11

Page 16: Self-Adjusting Stack Machines€¦ · I Key Idea: Reusing traces e cient update Matthew A. Hammer Self-Adjusting Stack Machines 8. Challenges Existing work targets functional languages:

Expression Tree Evaluation in C

1 typedef struct node s* node t;2 struct node s {3 enum { LEAF, BINOP } tag;4 union { int leaf;5 struct { enum { PLUS, MINUS } op;6 node t left, right;7 } binop;8 } u; }

1 int eval (node t root) {2 if (root->tag == LEAF)3 return root->u.leaf;4 else {5 int l = eval (root->u.binop.left);6 int r = eval (root->u.binop.right);

7 if (root->u.binop.op == PLUS) return (l + r);8 else return (l - r);9 } }

Matthew A. Hammer Self-Adjusting Stack Machines 12

Page 17: Self-Adjusting Stack Machines€¦ · I Key Idea: Reusing traces e cient update Matthew A. Hammer Self-Adjusting Stack Machines 8. Challenges Existing work targets functional languages:

The Stack “Shapes” the Computation

int eval (node t root) {if (root->tag == LEAF)

return root->u.leaf;else {

int l = eval (root->u.binop.left);int r = eval (root->u.binop.right);

if (root->u.binop.op == PLUS) return (l + r);else return (l - r);

} }

Stack usage breaks computation into three parts:

I Part A: Return value if LEAFOtherwise, evaluate BINOP, starting with left child

I Part B: Evaluate the right child

I Part C: Apply BINOP to intermediate results; return

Matthew A. Hammer Self-Adjusting Stack Machines 13

Page 18: Self-Adjusting Stack Machines€¦ · I Key Idea: Reusing traces e cient update Matthew A. Hammer Self-Adjusting Stack Machines 8. Challenges Existing work targets functional languages:

The Stack “Shapes” the Computation

int eval (node t root) {if (root->tag == LEAF)

return root->u.leaf;else {

int l = eval (root->u.binop.left);int r = eval (root->u.binop.right);

if (root->u.binop.op == PLUS) return (l + r);else return (l - r);

} }

Stack usage breaks computation into three parts:

I Part A: Return value if LEAFOtherwise, evaluate BINOP, starting with left child

I Part B: Evaluate the right child

I Part C: Apply BINOP to intermediate results; return

Matthew A. Hammer Self-Adjusting Stack Machines 13

Page 19: Self-Adjusting Stack Machines€¦ · I Key Idea: Reusing traces e cient update Matthew A. Hammer Self-Adjusting Stack Machines 8. Challenges Existing work targets functional languages:

The Stack “Shapes” the Computation

int eval (node t root) {if (root->tag == LEAF)

return root->u.leaf;else {

int l = eval (root->u.binop.left);int r = eval (root->u.binop.right);

if (root->u.binop.op == PLUS) return (l + r);else return (l - r);

} }

Stack usage breaks computation into three parts:

I Part A: Return value if LEAFOtherwise, evaluate BINOP, starting with left child

I Part B: Evaluate the right child

I Part C: Apply BINOP to intermediate results; return

Matthew A. Hammer Self-Adjusting Stack Machines 13

Page 20: Self-Adjusting Stack Machines€¦ · I Key Idea: Reusing traces e cient update Matthew A. Hammer Self-Adjusting Stack Machines 8. Challenges Existing work targets functional languages:

The Stack “Shapes” the Computation

int eval (node t root) {if (root->tag == LEAF)

return root->u.leaf;else {

int l = eval (root->u.binop.left);int r = eval (root->u.binop.right);

if (root->u.binop.op == PLUS) return (l + r);else return (l - r);

} }

Stack usage breaks computation into three parts:

I Part A: Return value if LEAFOtherwise, evaluate BINOP, starting with left child

I Part B: Evaluate the right child

I Part C: Apply BINOP to intermediate results; return

Matthew A. Hammer Self-Adjusting Stack Machines 13

Page 21: Self-Adjusting Stack Machines€¦ · I Key Idea: Reusing traces e cient update Matthew A. Hammer Self-Adjusting Stack Machines 8. Challenges Existing work targets functional languages:

Dynamic Execution Traces

Input Tree+

+

3 4

0

5 6

Execution Trace

A+ B+ C+

A− B− C− A− B− C−

A+ B+ C+ A0 A5 A6

A3 A4

Matthew A. Hammer Self-Adjusting Stack Machines 14

Page 22: Self-Adjusting Stack Machines€¦ · I Key Idea: Reusing traces e cient update Matthew A. Hammer Self-Adjusting Stack Machines 8. Challenges Existing work targets functional languages:

How to Update the Output?

Original Input+

+

3 4

0

5 6

Changed Input+

+

3 4

0

+

5 6

5

Goals:

I Consistency: Respect the (static) program’s meaning

I Efficiency: Reuse original computation when possible

Idea: Transform the first trace into second trace

Matthew A. Hammer Self-Adjusting Stack Machines 15

Page 23: Self-Adjusting Stack Machines€¦ · I Key Idea: Reusing traces e cient update Matthew A. Hammer Self-Adjusting Stack Machines 8. Challenges Existing work targets functional languages:

How to Update the Output?

Original Input+

+

3 4

0

5 6

Changed Input+

+

3 4

0

+

5 6

5

Goals:

I Consistency: Respect the (static) program’s meaning

I Efficiency: Reuse original computation when possible

Idea: Transform the first trace into second trace

Matthew A. Hammer Self-Adjusting Stack Machines 15

Page 24: Self-Adjusting Stack Machines€¦ · I Key Idea: Reusing traces e cient update Matthew A. Hammer Self-Adjusting Stack Machines 8. Challenges Existing work targets functional languages:

+

+

3 4

0

+

5 6

5

Unaffected/Reuse

Affected/Re-eval

New Evaluation

Unaffected/Reuse

Affected/Re-eval

A+ B+ C+

A− B− C− A+ B+ C+

A+ B+ C+ A0 A− B− C− A5

A3 A4 A5 A6

Matthew A. Hammer Self-Adjusting Stack Machines 16

Page 25: Self-Adjusting Stack Machines€¦ · I Key Idea: Reusing traces e cient update Matthew A. Hammer Self-Adjusting Stack Machines 8. Challenges Existing work targets functional languages:

Before Update

A+ B+ C+

A− B− C− A− B− C−

A+ B+ C+ A0 A5 A6

A3 A4

After Update

A+ B+ C+

A− B− C− A+ B+ C+

A+ B+ C+ A0 A− B− C− A5

A3 A4 A5 A6

Matthew A. Hammer Self-Adjusting Stack Machines 17

Page 26: Self-Adjusting Stack Machines€¦ · I Key Idea: Reusing traces e cient update Matthew A. Hammer Self-Adjusting Stack Machines 8. Challenges Existing work targets functional languages:

How to Program Dynamic Computations?

1. How to describe dynamic computations?

X Usability: Are these descriptions easy to write?X Generality: How much can they describe?

2. How to implement this description?

? Correctness: Do updates provide the correct result?? Efficiency: Are updates faster than re-evaluation?

Matthew A. Hammer Self-Adjusting Stack Machines 18

Page 27: Self-Adjusting Stack Machines€¦ · I Key Idea: Reusing traces e cient update Matthew A. Hammer Self-Adjusting Stack Machines 8. Challenges Existing work targets functional languages:

Overview of Formal Semantics

I IL: Intermediate language for C-like programs

I IL has instructions for:

I Mutable memory: alloc, read, writeI Managing local state via a stack: push, popI Saving/restoring local state: memo, update

I Transition semantics: two abstract stack machines:

I Reference machine: defines “normal” semanticsI Tracing machine: defines self-adjusting semantics

Can compute an output and a traceCan update output/trace when memory changesAutomatically marks garbage in memory

I We prove that these stack machines are consistent

Matthew A. Hammer Self-Adjusting Stack Machines 19

Page 28: Self-Adjusting Stack Machines€¦ · I Key Idea: Reusing traces e cient update Matthew A. Hammer Self-Adjusting Stack Machines 8. Challenges Existing work targets functional languages:

Overview of Formal Semantics

I IL: Intermediate language for C-like programs

I IL has instructions for:

I Mutable memory: alloc, read, writeI Managing local state via a stack: push, popI Saving/restoring local state: memo, update

I Transition semantics: two abstract stack machines:

I Reference machine: defines “normal” semanticsI Tracing machine: defines self-adjusting semantics

Can compute an output and a traceCan update output/trace when memory changesAutomatically marks garbage in memory

I We prove that these stack machines are consistent

Matthew A. Hammer Self-Adjusting Stack Machines 19

Page 29: Self-Adjusting Stack Machines€¦ · I Key Idea: Reusing traces e cient update Matthew A. Hammer Self-Adjusting Stack Machines 8. Challenges Existing work targets functional languages:

Consistency theorem, Part 1: No Reuse

Trace

Input Tracing Machine Run (P) Output

q qInput Reference Machine Run (P) Output

Tracing machine is consistent with reference machine(when tracing machine runs “from-scratch”, with no reuse)

Matthew A. Hammer Self-Adjusting Stack Machines 20

Page 30: Self-Adjusting Stack Machines€¦ · I Key Idea: Reusing traces e cient update Matthew A. Hammer Self-Adjusting Stack Machines 8. Challenges Existing work targets functional languages:

Consistency theorem, Part 2: Reuse vs No Reuse

Trace0

Input Tracing Machine Run (P) Trace

Output

q qInput Tracing Machine Run (P) Trace

Output

Tracing machine is consistent with from-scratch runs(When it reuses some existing trace Trace0)

Matthew A. Hammer Self-Adjusting Stack Machines 21

Page 31: Self-Adjusting Stack Machines€¦ · I Key Idea: Reusing traces e cient update Matthew A. Hammer Self-Adjusting Stack Machines 8. Challenges Existing work targets functional languages:

Consistency theorem: Main result

Trace0 Trace

Input Tracing Machine Run (P) Output

q qInput Reference Machine Run (P) Output

Main result uses Part 1 and Part 2 together:

Tracing machine is consistent with reference machine

Matthew A. Hammer Self-Adjusting Stack Machines 22

Page 32: Self-Adjusting Stack Machines€¦ · I Key Idea: Reusing traces e cient update Matthew A. Hammer Self-Adjusting Stack Machines 8. Challenges Existing work targets functional languages:

How to Program Dynamic Computations?

1. How to describe dynamic computations?

X Usability: Are these descriptions easy to write?X Generality: How much can they describe?

2. How to implement this description?

X Correctness: Do updates provide the correct result?? Efficiency: Are updates faster than re-evaluation?

Matthew A. Hammer Self-Adjusting Stack Machines 23

Page 33: Self-Adjusting Stack Machines€¦ · I Key Idea: Reusing traces e cient update Matthew A. Hammer Self-Adjusting Stack Machines 8. Challenges Existing work targets functional languages:

Overview of Our Implementation

CEAL Compiler

C

Translate

IL

Transform

IL

Translate

C + RT

I Compiler: produces C targets from C-like source code

I Run-time: maintains traces, performs efficient updates

Matthew A. Hammer Self-Adjusting Stack Machines 24

Page 34: Self-Adjusting Stack Machines€¦ · I Key Idea: Reusing traces e cient update Matthew A. Hammer Self-Adjusting Stack Machines 8. Challenges Existing work targets functional languages:

Dynamic Expression Trees: From-Scratch Time

0.00.20.40.60.81.01.21.41.6

0 250K 500K 750K

Tim

e (s

)

Input Size

Exptrees From-Scratch

Self-AdjStatic

Matthew A. Hammer Self-Adjusting Stack Machines 25

Page 35: Self-Adjusting Stack Machines€¦ · I Key Idea: Reusing traces e cient update Matthew A. Hammer Self-Adjusting Stack Machines 8. Challenges Existing work targets functional languages:

Dynamic Expression Trees: Ave Update Time

0.0110.0120.0130.0140.0150.0160.0170.0180.0190.0200.0210.022

250K 500K 750K

Tim

e (m

s)

Input Size

Exptrees Ave Update

Self-Adj

Matthew A. Hammer Self-Adjusting Stack Machines 26

Page 36: Self-Adjusting Stack Machines€¦ · I Key Idea: Reusing traces e cient update Matthew A. Hammer Self-Adjusting Stack Machines 8. Challenges Existing work targets functional languages:

Dynamic Expression Trees: Speed up

0.0 x 100

5.0 x 103

1.0 x 104

1.5 x 104

2.0 x 104

2.5 x 104

0 250K 500K 750K

Spe

edup

Input Size

Exptrees Speedup

Self-Adj

Matthew A. Hammer Self-Adjusting Stack Machines 27

Page 37: Self-Adjusting Stack Machines€¦ · I Key Idea: Reusing traces e cient update Matthew A. Hammer Self-Adjusting Stack Machines 8. Challenges Existing work targets functional languages:

Summary of Empirical Results

Benchmark N Initial Overhead Speed-up(Compute / Static) (Static / Update)

exptrees 106 8.5 1.4× 104

map 106 18.4 3.0× 104

reverse 106 18.4 3.8× 104

filter 106 10.7 4.9× 104

sum 106 9.6 1.5× 103

minimum 106 7.7 1.4× 104

quicksort 105 8.2 6.9× 102

mergesort 105 7.2 7.8× 102

quickhull 105 3.7 2.2× 103

diameter 105 3.4 1.8× 103

distance 105 3.4 7.9× 102

Matthew A. Hammer Self-Adjusting Stack Machines 28

Page 38: Self-Adjusting Stack Machines€¦ · I Key Idea: Reusing traces e cient update Matthew A. Hammer Self-Adjusting Stack Machines 8. Challenges Existing work targets functional languages:

Our Contributions

A consistent self-adjusting semantics for low-level programs

Our abstract machine semantics

I Describes trace editing & memory management implementation of run-time system

I But requires programs with a particular structure implementation of compiler transformations

Our intermediate language is low-level, yet abstract

I orthogonal annotations for self-adjusting behavior

I no type system needed

implementation of C front end

Matthew A. Hammer Self-Adjusting Stack Machines 29

Page 39: Self-Adjusting Stack Machines€¦ · I Key Idea: Reusing traces e cient update Matthew A. Hammer Self-Adjusting Stack Machines 8. Challenges Existing work targets functional languages:

Our Contributions

A consistent self-adjusting semantics for low-level programs

Our abstract machine semantics

I Describes trace editing & memory management implementation of run-time system

I But requires programs with a particular structure implementation of compiler transformations

Our intermediate language is low-level, yet abstract

I orthogonal annotations for self-adjusting behavior

I no type system needed

implementation of C front end

Matthew A. Hammer Self-Adjusting Stack Machines 29

Page 40: Self-Adjusting Stack Machines€¦ · I Key Idea: Reusing traces e cient update Matthew A. Hammer Self-Adjusting Stack Machines 8. Challenges Existing work targets functional languages:

Our Contributions

A consistent self-adjusting semantics for low-level programs

Our abstract machine semantics

I Describes trace editing & memory management implementation of run-time system

I But requires programs with a particular structure implementation of compiler transformations

Our intermediate language is low-level, yet abstract

I orthogonal annotations for self-adjusting behavior

I no type system needed

implementation of C front end

Matthew A. Hammer Self-Adjusting Stack Machines 29

Page 41: Self-Adjusting Stack Machines€¦ · I Key Idea: Reusing traces e cient update Matthew A. Hammer Self-Adjusting Stack Machines 8. Challenges Existing work targets functional languages:

Thank You! Questions?

Self-adjusting computation is a language-based techniqueto derive dynamic programs from static programs.

Summary of contributions:

I A self-adjusting semantics for low-level programs.This semantics defines self-adjusting stack machines.

I A compiler and run-time that implement the semantics.

I A front end that embeds much of C.

Matthew A. Hammer Self-Adjusting Stack Machines 30