Introduction to Partial Evaluation Compose Project IRISA / - - PowerPoint PPT Presentation

introduction to partial evaluation
SMART_READER_LITE
LIVE PREVIEW

Introduction to Partial Evaluation Compose Project IRISA / - - PowerPoint PPT Presentation

Introduction to Partial Evaluation Compose Project IRISA / University of Rennes 1- INRIA March 1998 IRISA - Compose 1 Goal of Partial Evaluation Fact: many programs have layers of interpretation. Action: removal of interpretation


slide-1
SLIDE 1

1

IRISA - Compose

Introduction to Partial Evaluation

Compose Project IRISA / University of Rennes 1- INRIA

March 1998

slide-2
SLIDE 2

2

IRISA - Compose

Goal of Partial Evaluation

◆ Fact: many programs have layers of

interpretation.

◆ Action: removal of interpretation layers.

slide-3
SLIDE 3

3

IRISA - Compose

Example: Text Formatting

◆ A procedure that produces formatted text

with respect to

– A control string. – A sequence of values.

◆ It exists in most programming languages

– format in Scheme and Common Lisp. – printf in C.

◆ Interpretation layer: the control string

slide-4
SLIDE 4

4

IRISA - Compose

void mini_printf(char* fmt, int *value) { int i; for (i=0 ; *fmt != ’\0’ ; fmt++) if(*fmt != ’%’) putchar(*fmt); else switch(*++fmt) { case ’d’: putint(value[i++]); break; case ’%’: putchar(’%’); break; default: error(); break; } }

Example: mini_printf

slide-5
SLIDE 5

5

IRISA - Compose

Example: mini_printf

Specialization w.r.t. control string "n = %d"

void mini_printf_fmt(int* value) { putchar(’n’); putchar(’ ’); putchar(’=’); putchar(’ ’); putchar(value[0]); }

slide-6
SLIDE 6

6

IRISA - Compose

Assessment

◆ For any sequence of integer values values mini_printf_fmt(values) = mini_printf("n = %d", values) ◆ The control string has been interpreted. ◆ The specialized mini_printf is faster

than the original procedure.

slide-7
SLIDE 7

7

IRISA - Compose

Partial Evaluation: Summary

◆ Program specialization. ◆ Automatic transformation. ◆ Systematic transformation. ◆ Interpretation removal. ◆ Faster specialized programs

(usually).

◆ Smaller specialized programs

(sometimes).

Program

Computations performed at specialization time Computations performed after specialization time

slide-8
SLIDE 8

8

IRISA - Compose

Partial Evaluation: Range of Applications

◆ Operating systems. ◆ Graphics. ◆ Scientific computing. ◆ Software engineering. ◆ Simulation. ◆ ...

slide-9
SLIDE 9

9

IRISA - Compose

Outline of the Rest of this Introduction

◆ The Basics of Partial Evaluation. ◆ Disclaimers. ◆ Compile-Time Run-Time Specialization. ◆ Program Data Specialization. ◆ State of the Art. ◆ Related Techniques.

slide-10
SLIDE 10

10

IRISA - Compose

The Basics of Partial Evaluation

◆ Partial Evaluation: What? ◆ Partial Evaluation: Why? ◆ Partial Evaluation: When? ◆ Partial Evaluation: How?

slide-11
SLIDE 11

11

IRISA - Compose

Partial Evaluation: What?

◆ A form of program specialization. ◆ Exploit specialization information. ◆ Automatic process.

slide-12
SLIDE 12

12

IRISA - Compose

Partial Evaluation

D P

R

D2 P D1

R

P

  • P
  • D1

P

R

D2 P

R

D1 D2

slide-13
SLIDE 13

13

IRISA - Compose

Specialization Information

◆ Partial input (configuration parameters,

usage patterns, ...).

◆ Properties on inputs (size, shape, ...). ◆ Abstract data types (sequence as lists,

vectors, …).

◆ Hardware features. ◆ ...

slide-14
SLIDE 14

14

IRISA - Compose

Partial Evaluation: Why?

Genericity

Parameterized, Modular, Re-usable, Platform-independent Instantiated, Monolithic, Specific, Machine-dependant optimizations

Examples of generic structures: software layers, simulators, interpreters, ...

Performance

slide-15
SLIDE 15

15

IRISA - Compose

Genericity and Performance?

◆ Manual optimization. ◆ Application/code generators. ◆ Partial evaluation.

slide-16
SLIDE 16

16

IRISA - Compose

Partial Evaluation: When?

Information available at various stages:

– Configuration. – Compilation. – Linking. – Execution (sessions).

slide-17
SLIDE 17

17

IRISA - Compose

Partial Evaluation: How?

◆ How does partial evaluation work? ◆ Impact on the degree of specialization?

– Parallel compilation. – Type checking.

slide-18
SLIDE 18

18

IRISA - Compose

How Does Partial Evaluation Work?

Outline:

– Constant propagation. – Code folding. – Procedure unfolding (inlining). – Procedure specialization (cloning/customization). – Loop unrolling. – Dead-code elimination.

slide-19
SLIDE 19

19

IRISA - Compose

Constant Propagation

Constants are propagated as much as possible throughout all syntactic constructs.

– Intra-procedural propagation. – Inter-procedural propagation. – All data types (pointers, arrays, structures, …).

slide-20
SLIDE 20

20

IRISA - Compose

Example: Constant Propagation Throughout Assignments

Values of early bound variables are propagated at specialization time

(assuming no redefinition of x before it is used).

x = 5; ... y = x; x = 5; ... y = 5;

slide-21
SLIDE 21

21

IRISA - Compose

Code Folding

◆ Syntactic constructs are folded if their

evaluation solely depends on available data.

◆ This operation mimics to the standard

evaluation semantics.

slide-22
SLIDE 22

22

IRISA - Compose

Folding of Arithmetic Operations

Early bound computations are performed at specialization time.

x = 5; ... y = x + 10; x = 5; ... y = 15;

slide-23
SLIDE 23

23

IRISA - Compose

Procedure Unfolding

◆ Replacing a procedure call by its body. ◆ Substituting formals by actuals.

f(v, w); ... void f(int x, int y) { if(y) update(x, y); else update(x,-1); } { if(w) update(v,w); else update(v,-1); } ... void f(int x, int y) {...}

slide-24
SLIDE 24

24

IRISA - Compose

Procedure Specialization

◆ Propagating constant actuals into the called procedure. ◆ Creating a specialized version of the called procedure.

... f(v, 0); ... void f(int x, int y) { if(y) update(x, y); else update(x,-1); } ... f_1(v); ... void f_1(int x) { update(x,-1); }

slide-25
SLIDE 25

25

IRISA - Compose

Procedure Specialization

Specialized versions are given a unique name.

f(v, 0); ... f(v, 0); void f(int x, int y) { ... } f_1(v); ... f_1(v); void f_1(int x) { ... }

slide-26
SLIDE 26

26

IRISA - Compose

Loop Unrolling

Unroll the loop when the test is determined early. for (i=0; *fmt !=’\0’; fmt++) if(*fmt!=’%’) putchar(*fmt); else switch(*++fmt)

{

case ’d’: putint(value[i++]); break; case ’%’: putchar(’%’); break; default: error() ; break; } /* */ { putchar(’n’); putchar(’ ’); putchar(’=’); putchar(’ ’); putchar(value[0]); }

slide-27
SLIDE 27

27

IRISA - Compose

Dead-Code Elimination

Eliminating code whose computated results or effects are not used.

(assuming debug is false).

if(debug) { printf("entering ..."); ++cnt_debug; }

slide-28
SLIDE 28

28

IRISA - Compose

Degree of Specialization

Outline:

– Amount and kind of data available. – Partial evaluation strategies. – Structure of the source program.

slide-29
SLIDE 29

29

IRISA - Compose

Amount of Data Available: Complete Data (1/2)

◆ All the computations can be performed at

specialization time.

◆ Specializing f w.r.t. 1 and 2 ◆ Result: a constant function.

int f(int i,int j) { return i+j; } int f_1() { return 3; }

slide-30
SLIDE 30

30

IRISA - Compose

Amount of Data Available: Complete Data (2/2)

◆ Some computations cannot be performed at

specialization time.

◆ Assuming printf cannot be evaluated at

specialization time.

◆ The residual program contains a call to printf.

int f(int i, int j) { printf("i=%d / j=%d", i , j); return i+j; } int f_1() { printf("i=%d / j=%d", 1 , 2); return 3; }

slide-31
SLIDE 31

31

IRISA - Compose

Amount of Data Available: No Data (1/2)

When no data are available, the program is reconstructed.

int f(int i,int j) { return i+j; } int f(int i,int j) { return i+j; }

slide-32
SLIDE 32

32

IRISA - Compose

Amount of Data Available: No Data (2/2)

Yet, some input-independent optimizations can be performed.

– Procedure unfolding. – Use of program constants.

slide-33
SLIDE 33

33

IRISA - Compose

Amount of Data Available: Partial Data

◆ When some (but not all) of the data are

available, the program is

– Partly evaluated. – Partly reconstructed.

◆ Mixed computations.

slide-34
SLIDE 34

34

IRISA - Compose

Approaches To Partial Evaluation

Outline:

– Online partial evaluation. – Offline partial evaluation.

slide-35
SLIDE 35

35

IRISA - Compose

Online Partial Evaluation: Principles

◆ All decisions made on the fly. ◆ Based on specialization values. ◆ Interpretation of specialization.

slide-36
SLIDE 36

36

IRISA - Compose

Online Partial Evaluation

Specialization Values Program Partial Evaluator Residual Program

slide-37
SLIDE 37

37

IRISA - Compose

Online Partial Evaluation: Example

Assuming test may be known or unknown at specialization time: 3 possible cases. if (test) x=3; else x=unknown_value; do_this(x+10);

  • do_this(13);
  • do_this(unknown_value+10);
slide-38
SLIDE 38

38

IRISA - Compose

Online Partial Evaluation: Assessment

◆ Specialization is precise. ◆ The degree of specialization is difficult to

predict.

◆ On-the-fly decision causes overhead for

repeatedly processed code when the specialization context is unchanged.

◆ How to perform specialization efficiently at

run time?

slide-39
SLIDE 39

39

IRISA - Compose

Terminology

In the program transformation context,

– data refers to available data, – data refers to unavailable data.

slide-40
SLIDE 40

40

IRISA - Compose

Offline Partial Evaluation: Principles

◆ Two phases:

– Binding-time analysis (BTA): determining known computations. – Specialization: performing transformations based on known (actual) data and binding- times.

◆ The specialization is “compiled”.

slide-41
SLIDE 41

41

IRISA - Compose

Offline Partial Evaluation

Specialization Values Binding-time Annotated Program Specializer Residual Program Program Binding-time Values Binding-Time Analysis

  • Specialization:

for all specialization values.

  • Preprocessing:
  • nce for a

binding-time description.

slide-42
SLIDE 42

42

IRISA - Compose

Binding-Time Analysis

◆ Given

– a program, – a binding-time description of its input.

BTA determines the computations that can be performed at specialization time.

◆ The binding-time properties produced by

the BTA are valid relative to the binding- times of the input program.

slide-43
SLIDE 43

43

IRISA - Compose

void mini_printf(char* fmt, int *value) { int i; for (i=0 ; *fmt != ’\0’ ; fmt++) if(*fmt != ’%’) putchar(*fmt); else switch(*++fmt) { case ’d’: putint(value[i++]); break; case ’%’: putchar(’%’); break; default: error() ; break; } }

Binding-Time Analysis Example: mini-printf

slide-44
SLIDE 44

44

IRISA - Compose

Specialization

◆ It exploits binding-time information. ◆ It determines and performs the program

transformation, given specialization values.

slide-45
SLIDE 45

45

IRISA - Compose

Offline Partial Evaluation: Assessment

◆ The specialization process is compiled:

Faster than online partial evaluation.

◆ Easier to perform specialization both at

compile time and run time.

◆ The specialization is approximative:

Driven by binding-time properties, not by specialization values.

slide-46
SLIDE 46

46

IRISA - Compose

Offline Partial Evaluation: Example of Approximation

Assuming test is known at specialization time.

if (test) x=3; else x=unknown_value; do_this(x+10);

slide-47
SLIDE 47

47

IRISA - Compose

Terminology

In the binding-time analysis context,

– refers to available data, – refers to unavailable data.

slide-48
SLIDE 48

48

IRISA - Compose

Online Offline Partial Evaluation

◆ Online partial evaluation:

– Partitioning between known and unknown computations is determined on the fly. – Partitioning depends on known/unknown program inputs and their specific values. – Online partial evaluation is more precise.

◆ Offline partial evaluation:

– Partitioning between static and dynamic computations is fixed. – Partitioning only depends on the static/dynamic program inputs. – Offline partial evaluation is more predictable.

Program

Computations performed at specialization time Computations performed after specialization time

slide-49
SLIDE 49

49

IRISA - Compose

Disclaimers

◆ Partial evaluation is simple and automatic

but it is not magic.

◆ Potential weaknesses.

Outline:

– Limited algebraic transformations. – Code explosion. – Unsafe static computations. – Non-termination.

slide-50
SLIDE 50

50

IRISA - Compose

Limited Algebraic Transformations

◆ Few, if any, algebraic transformations are

  • ffered.

◆ This strategy is aimed at making the partial

evaluation process:

– Simple. – Automatic. – Predictable. – Efficient.

slide-51
SLIDE 51

51

IRISA - Compose

... (x + y) + z ...

Limited Algebraic Transformations: Example

Assuming x=unknown, y=4, z=5

... x + (y + z) ...

Unless the partial evaluator is instructed that addition is associative. Partial evaluators usually do not include algebraic transformations.

... (x + 4) + 5 ... ... x + 9 ...

slide-52
SLIDE 52

52

IRISA - Compose

Space Explosion

It can happen with

– Procedure unfolding. – Procedure specialization. – Loop unrolling.

slide-53
SLIDE 53

53

IRISA - Compose

Unsafe Static Computations

For a conditional statement Typically a partial evaluator processes both branches speculatively.

if (unknown_test) then_stmt; else else_stmt;

slide-54
SLIDE 54

54

IRISA - Compose

Unsafe Static Computations

Speculative processing of conditionals may be

– Unnecessary:

» The specialized branch may never be executed.

– Unsafe:

» A branch may loop. » A branch may perform a side-effect (e.g., an error).

slide-55
SLIDE 55

55

IRISA - Compose

Problems of Non-Termination

Outline

– Infinite unfolding. – Infinite specialization. – Infinite unrolling.

slide-56
SLIDE 56

56

IRISA - Compose

Infinite Unfolding

f(i, j); ... void f(int x, int y) { something; if(test) f(v, w); else return; } { something; if(test) { something; if(test) { something; if(test) { ...

slide-57
SLIDE 57

57

IRISA - Compose

Infinite Specialization

f(i, j); ... void f(int x, int y) { something; if(test) f(x, ++y); else return; } f_1(i); ... void f_1(int x) { something; if(test) f_2(x); else return; } void f_2(int x) { something; if(test) f_3(x); else return; } ...

slide-58
SLIDE 58

58

IRISA - Compose

Infinite Unrolling (1)

... while(test) something; ... something; something; something; ...

Dynamic test of a loop being unrolled.

slide-59
SLIDE 59

59

IRISA - Compose

Infinite Unrolling (2)

... while(true) something_else; ... something_else; something_else; something_else; ...

Static test of a forever loop with dynamic test to exit it (break).

slide-60
SLIDE 60

60

IRISA - Compose

When To Specialize?

◆ Compile-time specialization. ◆ Run-time specialization.

slide-61
SLIDE 61

61

IRISA - Compose

Compile-Time Specialization

◆ When the specialization values:

– are known prior to run time, – do not change at run time, – change at run time but can only have few variations which are known at compile time.

◆ Two binding times:

– static = compile time, – dynamic = run time.

slide-62
SLIDE 62

62

IRISA - Compose

Compile-Time Specialization

◆ Partial evaluation can either be online or

  • ffline.

◆ It essentially consists of the

transformations described earlier.

◆ It is done prior to running the residual

program: no cost.

slide-63
SLIDE 63

63

IRISA - Compose

Run-Time Specialization

◆ Specialization values are available at run

time.

◆ They may not be valid throughout the

execution.

◆ Two binding times within the run time:

– static = early, – dynamic = late.

◆ Specialized code is built at run time.

slide-64
SLIDE 64

64

IRISA - Compose

Run-Time Specialization: Cost/Benefits

◆ Run-time specialization process:

– Good quality code / slow. – Poor quality code / fast.

◆ For a given run-time specialized code:

– Amortizing the cost of specialization.

» Performance improvement. » Number of uses.

slide-65
SLIDE 65

65

IRISA - Compose

Another Form of Specialization: Data Specialization

A form of specialization complementary to program specialization.

slide-66
SLIDE 66

66

IRISA - Compose

Limitations of Program Specialization

◆ Code explosion (loop). ◆ Inappropriate for some programs

(large data sets).

◆ Complex program transformation process.

slide-67
SLIDE 67

67

IRISA - Compose

Principles of Data Specialization (1/2)

◆ Expensive computations which are known

are stored in

– = program specialization, – = data specialization.

◆ Optimization of the data flow. ◆ No optimization of the control flow.

slide-68
SLIDE 68

68

IRISA - Compose

Principles of Data Specialization (2/2)

Basic components.

– Cache:

» Data structure where known values are stored.

– Loader -- known computations:

» Performs the known computations. » Stores the resulting values in the cache.

– Reader -- unknown computations:

» Uses the values contained in the cache. » Performs the unknown computations.

slide-69
SLIDE 69

69

IRISA - Compose

Example (1/3)

◆ Known variables: i and max_i. ◆ Unknown variable: j. ◆ Data specialization: caching the expensive

computations on i and max_i.

... for(i=0 ; i < max_i ; i++) for(j=0 ; j < max_j ; j++) f(i, max_i, j); ...

slide-70
SLIDE 70

70

IRISA - Compose

Example (2/3)

int f(int x, int y, int z) { return (x*y + z) / z; }

void f_load(int x, int y, int *cache) { *cache = x * y; } int f_read(int z, int cache) { return(cache + z)/z; }

Expensive known computations

slide-71
SLIDE 71

71

IRISA - Compose

Example (3/3)

... { int cache[MAX]; for(i=0 ; i<max_i ; i++) { for(j=0 ; j<max_j ; j++) f_load(i, max_i, cache+j); for(j=0 ; j<max_j ; j++) f_read(j, cache[j]); } ...

slide-72
SLIDE 72

72

IRISA - Compose

Specializing Shaders

Geometric renderer

– Scene:

» Objects, orientation, coordinates. » Shades, textures, position of lights, colors.

– Using a pre-computed geometric image.

» Varying parameters: shading, reflectance. » Non-varying parameters: eye point, orientation,

  • bject coordinates.

– Data specialization.

» Intermediate results pre-computed, cached, and re- used. » Speedup factor: 60

slide-73
SLIDE 73

73

IRISA - Compose

State of the Art

Outline:

– Languages. – Partial evaluators. – Applications.

slide-74
SLIDE 74

74

IRISA - Compose

State of the Art: Languages

◆ Applicative languages (Lisp, Scheme, ML). ◆ Logic languages (Prolog, CLP). ◆ Imperative languages (Pascal, Fortran, C). ◆ Object-oriented languages (simple OO

dialect).

◆ Concurrent languages (formally).

slide-75
SLIDE 75

75

IRISA - Compose

State of the Art: Partial Evaluators

◆ Applicative languages:

– Fuse: pure Scheme, online, [Weise 1990] – Schism: pure Scheme, offline, [Consel 1990].

◆ Logic languages:

– Promix: pure Prolog, online [Lakhotia 1991]

◆ Imperative languages:

– FSpec: Fortran subset, offline, [Glueck 1995]. – C-Mix: C, offline, [Andersen 1994]. – Tempo: C, offline, [Consel 1996].

slide-76
SLIDE 76

76

IRISA - Compose

State of the Art: Applications (1/3)

◆ Interpreters (compiler generation). ◆ String matching. ◆ Pattern matching.

slide-77
SLIDE 77

77

IRISA - Compose

State of the Art: Applications (2/3)

◆ Numerical programs:

– Fast Fourier Transform. – Convolution.

◆ Graphics:

– Ray tracer. – Dithering.

◆ Operating systems:

– Sun Remote Procedure Call. – Chorus Inter-Process Communication. – Berkeley Packet Filter.

slide-78
SLIDE 78

78

IRISA - Compose

State of the Art: Applications (3/3)

◆ Domain-specific languages:

– Video device drivers. – Active networks.

◆ Software architectures:

– Selective broadcast. – Software layers (RPC). – Interpreters (DSL).

slide-79
SLIDE 79

79

IRISA - Compose

Related Techniques

Outline:

– Optimizing compilation. – Manual Code Generation. – Dynamic Compilation.

slide-80
SLIDE 80

80

IRISA - Compose

Related Techniques: Optimizing Compilation (1/2)

◆ Different target programs:

– Optimizing compilers need to treat a wide variety of program patterns. – Partial evaluators focus on generic programs.

◆ Different constraints:

– Optimizing compilers offer accurate analyses only if they pay off on common cases. – Partial evaluators have a much more narrow spectrum: they offer accurate analyses to enable high-degree of specialization.

slide-81
SLIDE 81

81

IRISA - Compose

Related Techniques: Optimizing Compilation (2/2)

Partial evaluation features, not present in

  • ptimizing compilers:

– Intra/inter-procedural propagation of non-scalar constants. – Partially known data. – Run-time specialization. – Providing specialization information.

slide-82
SLIDE 82

82

IRISA - Compose

Related Techniques: Manual Code Generation (1/2)

◆ Language extensions to allow a user to

compute programs at run time.

◆ Low-level program representation:

– It enables low-level code optimizations (instruction scheduling, register allocation, ...) – Example: DCG [Engler & Proebsting]

◆ Source-level program representation:

– It enables source-level optimizations. – Example: Tick C [Engler ]: similar to backquote mechanism in Lisp.

slide-83
SLIDE 83

83

IRISA - Compose

Related Techniques: Manual Code Generation (2/2)

◆ Advantages:

– Very precise. – Very flexible.

◆ Drawbacks:

– Error prone. – Laborious.

slide-84
SLIDE 84

84

IRISA - Compose

Related Techniques: Dynamic Compilation (1/4)

Postponing part of the compilation process until run time:

– To exploit run-time values:

» Algebraic simplifications. » Program specialization.

– To exploit run-time control patterns:

» Loop optimization. » Register allocation. » Instruction selection/scheduling.

slide-85
SLIDE 85

85

IRISA - Compose

Related Techniques: Dynamic Compilation (2/4)

Fabius [Leone & Lee]:

– First-order, pure functional language. – Special-purpose compiler. – Preprocessing phase includes a form of binding-time analysis. – Compiler generates:

» Instructions to perform static computations. » Instructions to generate the code at run time for the dynamic computations. » Instructions to perform optimizations.

slide-86
SLIDE 86

86

IRISA - Compose

Related Techniques: Dynamic Compilation (3/4)

DyC [Auslander ]

– C language and declarations (regions and transformations). – Modified version of the Multiflow compiler. – Preprocessing phase includes a binding-time analysis. – Compiler generates:

» Instructions to perform static computations. » Binary templates for dynamic computations. » Instructions to fill and assemble templates.

slide-87
SLIDE 87

87

IRISA - Compose

Related Techniques: Dynamic compilation (4/4)

◆ Cost/Benefit:

Like run-time specialization.

– Dynamic compilation:

» Highly-optimized code / slow. » Poor quality code / fast. » Architecture dependent.

– For a given dynamically compiled code.

» Amortizing the cost of compilation.

◆ Performance improvement. ◆ Number of uses.

slide-88
SLIDE 88

88

IRISA - Compose

Summary of the Introduction to Partial Evaluation

◆ The Basics of Partial Evaluation. ◆ Disclaimers. ◆ Compile-Time Run-Time Specialization. ◆ Program Data Specialization. ◆ State of the Art. ◆ Related Techniques.