1
IRISA - Compose
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
1
IRISA - Compose
2
IRISA - Compose
3
IRISA - Compose
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; } }
5
IRISA - Compose
void mini_printf_fmt(int* value) { putchar(’n’); putchar(’ ’); putchar(’=’); putchar(’ ’); putchar(value[0]); }
6
IRISA - Compose
7
IRISA - Compose
Program
Computations performed at specialization time Computations performed after specialization time
8
IRISA - Compose
9
IRISA - Compose
10
IRISA - Compose
11
IRISA - Compose
12
IRISA - Compose
D P
D2 P D1
P
P
D2 P
D1 D2
13
IRISA - Compose
14
IRISA - Compose
15
IRISA - Compose
16
IRISA - Compose
17
IRISA - Compose
18
IRISA - Compose
19
IRISA - Compose
20
IRISA - Compose
x = 5; ... y = x; x = 5; ... y = 5;
21
IRISA - Compose
22
IRISA - Compose
x = 5; ... y = x + 10; x = 5; ... y = 15;
23
IRISA - Compose
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) {...}
24
IRISA - Compose
◆ 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); }
25
IRISA - Compose
f(v, 0); ... f(v, 0); void f(int x, int y) { ... } f_1(v); ... f_1(v); void f_1(int x) { ... }
26
IRISA - Compose
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]); }
27
IRISA - Compose
if(debug) { printf("entering ..."); ++cnt_debug; }
28
IRISA - Compose
29
IRISA - Compose
int f(int i,int j) { return i+j; } int f_1() { return 3; }
30
IRISA - Compose
◆ Some computations cannot be performed at
◆ Assuming printf cannot be evaluated at
◆ 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; }
31
IRISA - Compose
int f(int i,int j) { return i+j; } int f(int i,int j) { return i+j; }
32
IRISA - Compose
33
IRISA - Compose
34
IRISA - Compose
35
IRISA - Compose
36
IRISA - Compose
Specialization Values Program Partial Evaluator Residual Program
37
IRISA - Compose
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);
38
IRISA - Compose
39
IRISA - Compose
40
IRISA - Compose
41
IRISA - Compose
Specialization Values Binding-time Annotated Program Specializer Residual Program Program Binding-time Values Binding-Time Analysis
for all specialization values.
binding-time description.
42
IRISA - Compose
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; } }
44
IRISA - Compose
45
IRISA - Compose
46
IRISA - Compose
if (test) x=3; else x=unknown_value; do_this(x+10);
47
IRISA - Compose
48
IRISA - Compose
◆ 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
49
IRISA - Compose
50
IRISA - Compose
51
IRISA - Compose
... (x + y) + z ...
... x + (y + z) ...
... (x + 4) + 5 ... ... x + 9 ...
52
IRISA - Compose
53
IRISA - Compose
if (unknown_test) then_stmt; else else_stmt;
54
IRISA - Compose
55
IRISA - Compose
56
IRISA - Compose
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) { ...
57
IRISA - Compose
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; } ...
58
IRISA - Compose
... while(test) something; ... something; something; something; ...
59
IRISA - Compose
... while(true) something_else; ... something_else; something_else; something_else; ...
60
IRISA - Compose
61
IRISA - Compose
62
IRISA - Compose
63
IRISA - Compose
64
IRISA - Compose
65
IRISA - Compose
66
IRISA - Compose
67
IRISA - Compose
68
IRISA - Compose
69
IRISA - Compose
... for(i=0 ; i < max_i ; i++) for(j=0 ; j < max_j ; j++) f(i, max_i, j); ...
70
IRISA - Compose
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
71
IRISA - Compose
... { 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]); } ...
72
IRISA - Compose
73
IRISA - Compose
74
IRISA - Compose
75
IRISA - Compose
◆ Applicative languages:
◆ Logic languages:
◆ Imperative languages:
76
IRISA - Compose
77
IRISA - Compose
78
IRISA - Compose
79
IRISA - Compose
80
IRISA - Compose
◆ Different target programs:
◆ Different constraints:
81
IRISA - Compose
82
IRISA - Compose
83
IRISA - Compose
84
IRISA - Compose
85
IRISA - Compose
86
IRISA - Compose
87
IRISA - Compose
◆ Performance improvement. ◆ Number of uses.
88
IRISA - Compose