SlideShare ist ein Scribd-Unternehmen logo
1 von 46
Downloaden Sie, um offline zu lesen
Composing Source-to-Source
Data-Flow Transformations with
Dependent Dynamic Rewrite Rules
Program Transformation 2004–2005

Eelco Visser
Institute of Information & Computing Sciences
Utrecht University,
The Netherlands

March 3, 2005
Outline

1

Data-flow transformation strategies

2

Dependencies in data-flow transformation rules

3

Generic data-flow transformation strategies

http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Part I
Data-Flow Transformation Strategies

http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Flow-Sensitive Constant Propagation

(x := 3;
y := x + 1;
if foo(x) then
(y := 2 * x;
x := y - 2)
else
(x := y;
y := 23);
z := x + y)

http://www.strategoxt.org

(x := 3;
y := 4;
if foo(3) then
(y := 6;
x := 4)
else
(x := 4;
y := 23);
z := 4 + y)

Composing Source-to-Source Data-Flow Transformations with
x := 3
x := 3
x -> 3
y := x + 1
y := 4
x -> 3
y -> 4
if foo(x)
if foo(3)
x -> 3
y -> 4
y := 2 * x
y := 6

x -> 3
y -> 4
x := y
x := 4

x -> 3
y -> 6

x -> 4
y -> 4

x := y - 2
x := 4

y := 23
y := 23

x -> 4
y -> 6

x -> 4
y -> 23

x -> 4
y z := x + y
z := 4 + y
Strategy for Basic Constant Propagation
prop-const = PropConst <+ prop-const-assign
<+ prop-const-declare <+ prop-const-let <+ prop-const-if
<+ prop-const-while <+ (all(prop-const); try(EvalBinOp))
prop-const-assign =
|[ x := <prop-const => e> ]|
; if <is-value> e
then rules( PropConst.x : |[ x ]| -> |[ e ]| )
else rules( PropConst.x :- |[ x ]| ) end
prop-const-declare =
|[ var x := <prop-const => e> ]|
; if <is-value> e
then rules( PropConst+x : |[ x ]| -> |[ e ]| )
else rules( PropConst+x :- |[ x ]| ) end
prop-const-let =
?|[ let d* in e* end ]|; {| PropConst : all(prop-const) |}
http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Intersection of Rule Sets

prop-const-if =
|[ if <prop-const> then <id> else <id> ]|
; (|[ if <id> then <prop-const> else <id> ]|
/PropConst |[ if <id> then <id> else <prop-const> ]|)

http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Intersection of Rule Sets

x
let var x := 1 var y := z 1
var z := 3 var a := 4 1
in x := x + z;
4
a := 5;
4
if y then
(y := y + 5;
4
z := 8)
4
else
(x := a + 21;
26
y := x + 1;
26
z := a + z);
26
b := a + z;
z := z + x end
-

y
-

z
3
3
3

a
4
4
5

b
-

-

3
8

5
5

-

27
27
-

3
3
8
8
8
8

5
5
5
5
5
5

13
13

http://www.strategoxt.org

let var x := 1 var y := z
var z := 3 var a := 4
in x := 4;
a := 5;
if y then
(y := y + 5;
z := 8)
else
(x := 26;
y := 27;
z := 8);
b := 13;
z := 8 + x end

Composing Source-to-Source Data-Flow Transformations with
Fixed-Point Intersection of Rule Sets
let var w := 20 var x := 20
var y := 20 var z := 10
in while SomethingUnknown()
(if x = 20 then w := 20
if y = 20 then x := 20
if z = 20 then y := 20
w; x; y; z end

do
else w := 10;
else x := 10;
else y := 10);

1
2

let var w := 20 var x := 20
var y := 20 var z := 10
in while SomethingUnknown() do
(if x = 20 then w := 20 else w := 10;
if y = 20 then x := 20 else x := 10;
y := 10);
w; x; y; 10 end

http://www.strategoxt.org

3
4

w
20
20
20
20
20
-

x
20
20
20
-

y
20
10
10
10
10
-

z
10
10
10
10
10
10
10
10
10

Composing Source-to-Source Data-Flow Transformations with
Fixed-Point Intersection of Rule Sets

prop-const-while =
?|[ while e1 do e2 ]|
; (/PropConst* |[ while <prop-const> do <prop-const> ]|)

http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Unreachable Code Elimination

let var x := 0 var y := 0
in x := 10;
while A do
(if x = 10
then dosomething()
else (dosomethingelse();
x := x + 1));
y := x
end

http://www.strategoxt.org

let var x := 0
var y := 0
in x := 10;
while A do
dosomething();
y := 10
end

Composing Source-to-Source Data-Flow Transformations with
Unreachable Code Elimination
prop-const-if =
|[ if <prop-const> then <id> else <id> ]|
; (EvalIf; prop-const
<+ (|[ if <id> then <prop-const> else <id> ]|
/PropConst
|[ if <id> then <id> else <prop-const> ]|))
prop-const-while =
?|[ while e1 do e2 ]|
; (|[ while <prop-const> do <id> ]|; EvalWhile
<+ (/PropConst*
|[ while <prop-const> do <prop-const> ]|))
EvalIf : |[ if
EvalIf : |[ if
where
EvalWhile : |[

0 then e1 else e2 ]| -> |[ e2 ]|
i then e1 else e2 ]| -> |[ e1 ]|
<not(eq)>(|[ i ]|, |[ 0 ]|)
while 0 do e ]| -> |[ () ]|

http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Dead Code Elimination

(x := foo(b);
y := bar(h);
a := c + 23;
if 4 > x then
(d := b + a;
g := 4 + y)
else
(b := 2;
a := y + 3;
a := 4 + x);
print(a))

{c,b}
{x,c}
{x,c}
{x,a}
{a}
{a}
{x}
{x}
{x}
{a}

http://www.strategoxt.org

(x := foo(b);
a := c + 23;
if not(4> x) then

a := 4 + x;
print(a))

Composing Source-to-Source Data-Flow Transformations with
Dead Code Elimination
dce = VarNeeded <+ ElimAssign <+ dce-assign
<+ dce-seq <+ dce-if <+ dce-while <+ all(dce)
ElimAssign :
|[ x := e ]| -> |[ () ]|
where <not(Needed)> |[ x ]|
VarNeeded =
?|[ x ]|
; rules(Needed : |[ x ]|)
dce-assign =
?|[ x := e ]|
; rules(Needed :- |[ x ]|)
; |[ <id> := <dce> ]|

http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Dead Code Elimination – Control-Flow

dce-seq =
|[ (<* reverse-filter(dce; not(?|[ () ]|)) >) ]|
dce-if =
(|[ if <id> then <dce> else <id> ]|
Needed/ |[ if <id> then <id> else <dce> ]|)
; |[ if <dce> then <id> else <id> ]|
; try(ElimIf)
dce-while =
|[ while <id> do <id> ]|
; (Needed/* |[ while <dce> do <dce> ]|)

http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Part II
Dependencies in Data-Flow Transformation Rules

http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Copy Propagation
Replace copies x produced by assignments of the form x := y by
original y
a := b;
c := d + a

http://www.strategoxt.org

a := b;
c := d + b

Composing Source-to-Source Data-Flow Transformations with
Copy Propagation
Replace copies x produced by assignments of the form x := y by
original y
a := b;
c := d + b

a := b;
c := d + a
First attempt using dynamic rules (wrong)

copy-prop-assign =
?|[ x := y ]|;
if <not(eq)>(x,y) then
rules( CopyProp.x : |[ x ]| -> |[ y ]| )
else
rules( CopyProp.x :- |[ x ]| )
end

http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Problem: Insufficient Dependencies
(a := b;
b := foo();
c := d + a)

(a := b;
b := foo();
c := d + b)

copy-prop-assign =
?|[ x := y ]|;
if <not(eq)>(x,y) then
rules( CopyProp.x : |[ x ]| -> |[ y ]| )
else
rules( CopyProp.x :- |[ x ]| )
end

http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Problem: Insufficient Dependencies
(a := b;
b := foo();
c := d + a)

(a := b;
b := foo();
c := d + b)

Problem: rule not undefined when variable in rhs changed
Solution: undefine rule when any of its variables is modified
copy-prop-assign =
?|[ x := y ]|;
if <not(eq)>(x,y) then
rules( CopyProp.x : |[ x ]| -> |[ y ]| )
else
rules( CopyProp.x :- |[ x ]| )
end

http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Problem: Free Variable Capture
let var a := bar()
var b := baz()
in a := b;
let var b := foo()
in print(a)
end
end

let var a := bar()
var b := baz()
in a := b;
let var b := foo()
in print(b) // wrong!
end
end

copy-prop-assign =
?|[ x := y ]|;
if <not(eq)>(x,y) then
rules( CopyProp.x : |[ x ]| -> |[ y ]| )
else
rules( CopyProp.x :- |[ x ]| )
end
http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Problem: Free Variable Capture
let var a := bar()
var b := baz()
in a := b;
let var b := foo()
in print(a)
end
end

let var a := bar()
var b := baz()
in a := b;
let var b := foo()
in print(b) // wrong!
end
end

Problem: rule not undefined when variables become shadowed
Solution: undefine rule locally when some variable shadowed
copy-prop-assign =
?|[ x := y ]|;
if <not(eq)>(x,y) then
rules( CopyProp.x : |[ x ]| -> |[ y ]| )
else
rules( CopyProp.x :- |[ x ]| )
end
http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Problem: Escaping Variables (1)
let var a := bar()
in let var b := foo()
in a := b
end;
print(a)
end

let var a := bar()
in let var b := foo()
in a := b
end;
print(b) // wrong!
end

copy-prop-assign =
?|[ x := y ]|;
if <not(eq)>(x,y) then
rules( CopyProp.x : |[ x ]| -> |[ y ]| )
else
rules( CopyProp.x :- |[ x ]| )
end
http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Problem: Escaping Variables (1)
let var a := bar()
in let var b := foo()
in a := b
end;
print(a)
end

let var a := bar()
in let var b := foo()
in a := b
end;
print(b) // wrong!
end

Problem: rule not undefined when a variable goes out of scope
Solution: (re)define rule in local scope
copy-prop-assign =
?|[ x := y ]|;
if <not(eq)>(x,y) then
rules( CopyProp.x : |[ x ]| -> |[ y ]| )
else
rules( CopyProp.x :- |[ x ]| )
end
http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Problem: Escaping Variables (2)
let var
var
in let
in

a := bar()
c := baz()
var b := foo()
a := b;
a := c
end;
print(a)

let var
var
in let
in

a := bar()
c := baz()
var b := foo()
a := b;
a := c
end;
print(c) // ok!

end

end

copy-prop-assign = ?|[ x := y ]|;
if <not(eq)>(x,y) then
rules( CopyProp.x : |[ x ]| -> |[ y ]| )
else rules( CopyProp.x :- |[ x ]| ) end
http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Problem: Escaping Variables (2)
let var
var
in let
in

a := bar()
c := baz()
var b := foo()
a := b;
a := c
end;
print(a)

let var
var
in let
in

a := bar()
c := baz()
var b := foo()
a := b;
a := c
end;
print(c) // ok!

end

end

Problem: definition in local scope is too restricted
Solution: (re)define rule in innermost scope of all variables
involved
copy-prop-assign = ?|[ x := y ]|;
if <not(eq)>(x,y) then
rules( CopyProp.x : |[ x ]| -> |[ y ]| )
else rules( CopyProp.x :- |[ x ]| ) end
http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Common-Subexpression Elimination
(x
y
z
a
z

:=
:=
:=
:=
:=

a + b;
a + b;
a + c;
1;
(a + c) + (a + b))

http://www.strategoxt.org

⇒

(x
y
z
a
z

:=
:=
:=
:=
:=

a + b;
x;
a + c;
1;
(a + c) + (a + b))

Composing Source-to-Source Data-Flow Transformations with
Common-Subexpression Elimination
(x
y
z
a
z

:=
:=
:=
:=
:=

a + b;
a + b;
a + c;
1;
(a + c) + (a + b))

⇒

(x
y
z
a
z

:=
:=
:=
:=
:=

a + b;
x;
a + c;
1;
(a + c) + (a + b))

Assignment
x := e
Propagation rule
|[ e ]| -> |[ x ]|
Dependencies in common-subexpression elimination
all variables in assignment x := e

http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Common-Subexpression Elimination
cse = cse-assign <+ (all(cse); try(ReplaceExp))
cse-assign =
|[ x := <cse => e> ]|
; where(<undefine-subexpressions> |[ x ]|)
; if <not(is-subterm(||[ x ]|))> |[ e ]| then
rules(ReplaceExp : |[ e ]| -> |[ x ]|)
; where(<register-subexpressions(|e)> |[ x := e ]|)
end
register-subexpressions(|e) =
get-vars; map({y : ?|[ y ]|
; rules(UsedInExp :+ |[ y ]| -> e)})
undefine-subexpressions =
bagof-UsedInExp; map({?e; rules(ReplaceExp :- |[ e ]|)})
get-vars = collect({?|[ x ]|})
http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Dependent Dynamic Rules

Declare rule dependencies
R.lab : p1 -> p2
depends on [(lab1,dep1),...,(labn,depn)]
Undefine all rules depending on dep
undefine-R(|dep)
Locally undefine all rules depending on dep
new-R(|lab, dep)
and label current scope with lab

http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Copy Propagation – Assignments
copy-prop =
repeat1(CopyProp)
<+ copy-prop-assign
<+ copy-prop-declare
<+ copy-prop-let <+ copy-prop-if <+ copy-prop-while
<+ all(copy-prop)
copy-prop-declare =
|[ var x ta := <copy-prop => e> ]|
; where( new-CopyProp(|x, x) )
; where( try(<copy-prop-assign-aux> |[ x := e ]|) )
copy-prop-assign =
|[ x := <copy-prop => e> ]|
; where( undefine-CopyProp(|x) )
; where( try(copy-prop-assign-aux) )

http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Copy Propagation – Propagation Rule

copy-prop-assign-aux =
? |[ x := y ]|
; where( <not(eq)>(x,y) )
; where( innermost-scope-CopyProp => z )
; rules(
CopyProp.z : |[ x ]| -> |[ y ]|
depends on [(x,x), (y,y)]
)
innermost-scope-CopyProp =
get-var-names => vars
; innermost-scope-CopyProp(elem-of(|vars))

http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Copy Propagation – Control-Flow

copy-prop-let =
|[ let <*id> in <*id> end ]|
; {| CopyProp : all(copy-prop) |}
copy-prop-if =
|[ if <copy-prop> then <id> else <id> ]|
; ( |[ if <id> then <copy-prop> else <id> ]|
/CopyProp |[ if <id> then <id> else <copy-prop> ]|)
copy-prop-while =
|[ while <id> do <id> ]|
; (/CopyProp* |[ while <copy-prop> do <copy-prop> ]|)

http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Common-Subexpression Elimination – Assignments

cse =
cse-assign <+ cse-vardec <+ cse-let <+ cse-if
<+ cse-while <+ all(cse); try(CSE)
cse-vardec =
|[ var x ta := <cse => e> ]|
; new-CSE(|x, x)
; where( try(<cse-assign-aux> |[ x := e ]|) )
cse-assign =
|[ x := <cse => e> ]|
; undefine-CSE(|x)
; where(try(cse-assign-aux))

http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Common-Subexpression Elimination – Propagation

cse-assign-aux =
? |[ x := e ]|
; where( <not(oncetd(?|[ x ]|)); pure> |[ e ]| )
; where( get-var-names; map(!(<id>,<id>)) => xs )
; where( innermost-scope-CSE => z )
; rules( CSE.z : |[ e ]| -> |[ x ]| depends on xs )
pure =
?|[ i ]| + ?|[ x ]| + |[ <bo:id>(<pure>, <pure>) ]|
innermost-scope-CSE =
get-var-names => vars
; innermost-scope-CSE(where(<elem>(<id>, vars)))

http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Common-Subexpression Elimination – Control-Flow

cse-let =
|[ let <*id> in <*id> end ]|
; {| CSE : all(cse) |}
cse-if =
|[ if <cse> then <id> else <id> ]|
; ( |[ if <id> then <cse> else <id> ]|
/CSE |[ if <id> then <id> else <cse> ]|)
cse-while =
|[ while <id> do <id> ]|
; (/CSE* |[ while <cse> do <cse> ]|)

http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Part III
Generic Data-Flow Transformation Strategies

http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Generic Data-Flow Transformation Strategies

Data-flow transformation strategies are similar
Factor out underlying strategy
Requires generalization over combinators
new-dynamic-rules(|Rs,x,x)
undefine-dynamic-rules(|Rs,x)
/~Rs1~Rs2/

Allows very concise specifications for specific transformations
Combination of transformations

http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Generic Strategy – Framework
forward-prop(transform, before, after | Rs1, Rs2, Rs3) =
<conc>(Rs1, Rs2, Rs3) => RsSc;
<conc>(Rs1, Rs2) => RsDf;
let
fp = prop-assign <+ prop-declare <+ prop-let
<+ prop-if <+ prop-while
<+ transform(fp)
<+ (before; all(fp); after)
prop-assign = ...
prop-declare = ...
prop-let = ...
prop-if = ...
prop-while = ...
in fp
end
http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Generic Strategy – Assignments
prop-assign =
|[ <id> := <fp> ]|
; (transform(fp)
<+ before
; ?|[ x := e ]|
; undefine-dynamic-rules(|RsDf,x)
; after)
prop-declare =
|[ var <id> := <fp> ]|
; (transform(fp)
<+ before; ?|[ var x := e ]|
; new-dynamic-rules(|RsSc,x,x);after)
prop-let =
?|[ let d* in e* end ]|
; (transform(fp)
<+ {|~RsSc : before; all(fp); after |})
http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Generic Strategy – Control Flow

prop-if =
|[ if <fp> then <id> else <id> ]|
; (transform(fp)
<+ before
; (|[ if <id> then <fp> else <id> ]|
/~Rs1~Rs2/ |[ if <id> then <id> else <fp> ]|)
; after)
prop-while =
?|[ while e1 do e2 ]|
; (transform(fp)
<+ before
; /~Rs1~Rs2/* |[ while <fp> do <fp> ]|
; after)

http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Instantation: Constant Propagation
prop-const = forward-prop(prop-const-transform, id,
prop-const-after | ["PropConst"],[],[])
prop-const-transform(recur) =
EvalFor <+ EvalIf; recur
<+ |[ while <recur> do <id> ]|; EvalWhile
prop-const-after =
try(prop-const-assign <+ prop-const-declare
<+ PropConst <+ EvalBinOp)
prop-const-assign =
?|[ x := e ]|; where( <is-value> e )
; rules( PropConst.x : |[ x ]| -> |[ e ]|
depends on [(x,x)] )
prop-const-declare =
?|[var x ta := e]|; where(<prop-const-assign>|[x := e]|)
http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Instantation: Copy Propagation
copy-prop =
forward-prop(no-transform,id,copy-prop-after
|["CopyProp"],[],[])
copy-prop-after =
try(copy-prop-assign <+ copy-prop-declare
<+ repeat1(CopyProp))
copy-prop-declare =
? |[ var x ta := e ]|
; where(try(<copy-prop-assign> |[ x := e ]|))
copy-prop-assign =
? |[ x := y ]|
; where( <not(eq)> (x, y) )
; where( get-var-dependencies => xs )
; where( innermost-scope-CopyProp => z )
; rules( CopyProp.z : |[ x ]| -> |[ y ]| depends on xs )
http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Instantation: Common-Subexpression Elimination

cse =
forward-prop(no-transform, id, cse-after|["CSE"],[],[])
cse-after =
try(cse-assign <+ cse-declare <+ CSE)
cse-declare =
?|[ var x := e ]|; where( <cse-assign> |[ x := e ]| )
cse-assign
; where(
; where(
; where(
; rules(

= ?|[ x := e ]|
<pure-and-not-trivial(|x)> |[ e ]| )
get-var-dependencies => xs )
innermost-scope-CSE => z )
CSE.z : |[ e ]| -> |[ x ]| depends on xs )

http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with
Instantation: A Super Optimizer
super-opt =
forward-prop(
prop-const-transform
, bvr-before
, bvr-after
; copy-prop-after
; prop-const-after
; cse-after
| ["PropConst", "CopyProp", "CSE"], [], ["RenameVar"])
Combination of
constant propagation
copy propagation
common-subexpression elimination
bound variable renaming
http://www.strategoxt.org

Composing Source-to-Source Data-Flow Transformations with

Weitere ähnliche Inhalte

Was ist angesagt?

Data Analysis and Programming in R
Data Analysis and Programming in RData Analysis and Programming in R
Data Analysis and Programming in R
Eshwar Sai
 
R statistics with mongo db
R statistics with mongo dbR statistics with mongo db
R statistics with mongo db
MongoDB
 

Was ist angesagt? (20)

R Programming: Learn To Manipulate Strings In R
R Programming: Learn To Manipulate Strings In RR Programming: Learn To Manipulate Strings In R
R Programming: Learn To Manipulate Strings In R
 
Introduction to R Programming
Introduction to R ProgrammingIntroduction to R Programming
Introduction to R Programming
 
R Programming: Importing Data In R
R Programming: Importing Data In RR Programming: Importing Data In R
R Programming: Importing Data In R
 
4 R Tutorial DPLYR Apply Function
4 R Tutorial DPLYR Apply Function4 R Tutorial DPLYR Apply Function
4 R Tutorial DPLYR Apply Function
 
R programming language
R programming languageR programming language
R programming language
 
R programming by ganesh kavhar
R programming by ganesh kavharR programming by ganesh kavhar
R programming by ganesh kavhar
 
R Programming: Mathematical Functions In R
R Programming: Mathematical Functions In RR Programming: Mathematical Functions In R
R Programming: Mathematical Functions In R
 
RDataMining slides-regression-classification
RDataMining slides-regression-classificationRDataMining slides-regression-classification
RDataMining slides-regression-classification
 
Next Generation Programming in R
Next Generation Programming in RNext Generation Programming in R
Next Generation Programming in R
 
Engineering Fast Indexes for Big-Data Applications: Spark Summit East talk by...
Engineering Fast Indexes for Big-Data Applications: Spark Summit East talk by...Engineering Fast Indexes for Big-Data Applications: Spark Summit East talk by...
Engineering Fast Indexes for Big-Data Applications: Spark Summit East talk by...
 
Vasia Kalavri – Training: Gelly School
Vasia Kalavri – Training: Gelly School Vasia Kalavri – Training: Gelly School
Vasia Kalavri – Training: Gelly School
 
Data Analysis and Programming in R
Data Analysis and Programming in RData Analysis and Programming in R
Data Analysis and Programming in R
 
Merge Multiple CSV in single data frame using R
Merge Multiple CSV in single data frame using RMerge Multiple CSV in single data frame using R
Merge Multiple CSV in single data frame using R
 
R language
R languageR language
R language
 
The Very ^ 2 Basics of R
The Very ^ 2 Basics of RThe Very ^ 2 Basics of R
The Very ^ 2 Basics of R
 
R for Pirates. ESCCONF October 27, 2011
R for Pirates. ESCCONF October 27, 2011R for Pirates. ESCCONF October 27, 2011
R for Pirates. ESCCONF October 27, 2011
 
R statistics with mongo db
R statistics with mongo dbR statistics with mongo db
R statistics with mongo db
 
2. R-basics, Vectors, Arrays, Matrices, Factors
2. R-basics, Vectors, Arrays, Matrices, Factors2. R-basics, Vectors, Arrays, Matrices, Factors
2. R-basics, Vectors, Arrays, Matrices, Factors
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
 
Cocoaheads Meetup / Alex Zimin / Swift magic
Cocoaheads Meetup / Alex Zimin / Swift magicCocoaheads Meetup / Alex Zimin / Swift magic
Cocoaheads Meetup / Alex Zimin / Swift magic
 

Ähnlich wie Dependent dynamic rules

Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with Clojure
Dmitry Buzdin
 

Ähnlich wie Dependent dynamic rules (20)

Scoped dynamic rewrite rules
Scoped dynamic rewrite rulesScoped dynamic rewrite rules
Scoped dynamic rewrite rules
 
Compiler Construction | Lecture 10 | Data-Flow Analysis
Compiler Construction | Lecture 10 | Data-Flow AnalysisCompiler Construction | Lecture 10 | Data-Flow Analysis
Compiler Construction | Lecture 10 | Data-Flow Analysis
 
Composing Source-to-Source Data-Flow Transformations with Rewriting Strategie...
Composing Source-to-Source Data-Flow Transformations with Rewriting Strategie...Composing Source-to-Source Data-Flow Transformations with Rewriting Strategie...
Composing Source-to-Source Data-Flow Transformations with Rewriting Strategie...
 
Java 8 DOs and DON'Ts - javaBin Oslo May 2015
Java 8 DOs and DON'Ts - javaBin Oslo May 2015Java 8 DOs and DON'Ts - javaBin Oslo May 2015
Java 8 DOs and DON'Ts - javaBin Oslo May 2015
 
Building .NET Apps using Couchbase Lite
Building .NET Apps using Couchbase LiteBuilding .NET Apps using Couchbase Lite
Building .NET Apps using Couchbase Lite
 
Let's build a parser!
Let's build a parser!Let's build a parser!
Let's build a parser!
 
Future features for openCypher: Schema, Constraints, Subqueries, Configurable...
Future features for openCypher: Schema, Constraints, Subqueries, Configurable...Future features for openCypher: Schema, Constraints, Subqueries, Configurable...
Future features for openCypher: Schema, Constraints, Subqueries, Configurable...
 
Online partial evaluation
Online partial evaluationOnline partial evaluation
Online partial evaluation
 
Compiler Construction | Lecture 11 | Monotone Frameworks
Compiler Construction | Lecture 11 | Monotone FrameworksCompiler Construction | Lecture 11 | Monotone Frameworks
Compiler Construction | Lecture 11 | Monotone Frameworks
 
Data science at the command line
Data science at the command lineData science at the command line
Data science at the command line
 
Software engineering
Software engineeringSoftware engineering
Software engineering
 
NSC #2 - D2 06 - Richard Johnson - SAGEly Advice
NSC #2 - D2 06 - Richard Johnson - SAGEly AdviceNSC #2 - D2 06 - Richard Johnson - SAGEly Advice
NSC #2 - D2 06 - Richard Johnson - SAGEly Advice
 
Relaxing global-as-view in mediated data integration from linked data
Relaxing global-as-view in mediated data integration from linked dataRelaxing global-as-view in mediated data integration from linked data
Relaxing global-as-view in mediated data integration from linked data
 
Pyretic - A new programmer friendly language for SDN
Pyretic - A new programmer friendly language for SDNPyretic - A new programmer friendly language for SDN
Pyretic - A new programmer friendly language for SDN
 
SLE2015: Distributed ATL
SLE2015: Distributed ATLSLE2015: Distributed ATL
SLE2015: Distributed ATL
 
Google cloud Dataflow & Apache Flink
Google cloud Dataflow & Apache FlinkGoogle cloud Dataflow & Apache Flink
Google cloud Dataflow & Apache Flink
 
JAZOON'13 - Paul Brauner - A backend developer meets the web: my Dart experience
JAZOON'13 - Paul Brauner - A backend developer meets the web: my Dart experienceJAZOON'13 - Paul Brauner - A backend developer meets the web: my Dart experience
JAZOON'13 - Paul Brauner - A backend developer meets the web: my Dart experience
 
Introduction to Apache Flink
Introduction to Apache FlinkIntroduction to Apache Flink
Introduction to Apache Flink
 
Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with Clojure
 
Clojure+ClojureScript Webapps
Clojure+ClojureScript WebappsClojure+ClojureScript Webapps
Clojure+ClojureScript Webapps
 

Mehr von Eelco Visser

Declarative Type System Specification with Statix
Declarative Type System Specification with StatixDeclarative Type System Specification with Statix
Declarative Type System Specification with Statix
Eelco Visser
 

Mehr von Eelco Visser (20)

CS4200 2019 | Lecture 5 | Transformation by Term Rewriting
CS4200 2019 | Lecture 5 | Transformation by Term RewritingCS4200 2019 | Lecture 5 | Transformation by Term Rewriting
CS4200 2019 | Lecture 5 | Transformation by Term Rewriting
 
CS4200 2019 | Lecture 4 | Syntactic Services
CS4200 2019 | Lecture 4 | Syntactic ServicesCS4200 2019 | Lecture 4 | Syntactic Services
CS4200 2019 | Lecture 4 | Syntactic Services
 
CS4200 2019 | Lecture 3 | Parsing
CS4200 2019 | Lecture 3 | ParsingCS4200 2019 | Lecture 3 | Parsing
CS4200 2019 | Lecture 3 | Parsing
 
CS4200 2019 | Lecture 2 | syntax-definition
CS4200 2019 | Lecture 2 | syntax-definitionCS4200 2019 | Lecture 2 | syntax-definition
CS4200 2019 | Lecture 2 | syntax-definition
 
CS4200 2019 Lecture 1: Introduction
CS4200 2019 Lecture 1: IntroductionCS4200 2019 Lecture 1: Introduction
CS4200 2019 Lecture 1: Introduction
 
A Direct Semantics of Declarative Disambiguation Rules
A Direct Semantics of Declarative Disambiguation RulesA Direct Semantics of Declarative Disambiguation Rules
A Direct Semantics of Declarative Disambiguation Rules
 
Declarative Type System Specification with Statix
Declarative Type System Specification with StatixDeclarative Type System Specification with Statix
Declarative Type System Specification with Statix
 
Compiler Construction | Lecture 17 | Beyond Compiler Construction
Compiler Construction | Lecture 17 | Beyond Compiler ConstructionCompiler Construction | Lecture 17 | Beyond Compiler Construction
Compiler Construction | Lecture 17 | Beyond Compiler Construction
 
Domain Specific Languages for Parallel Graph AnalytiX (PGX)
Domain Specific Languages for Parallel Graph AnalytiX (PGX)Domain Specific Languages for Parallel Graph AnalytiX (PGX)
Domain Specific Languages for Parallel Graph AnalytiX (PGX)
 
Compiler Construction | Lecture 15 | Memory Management
Compiler Construction | Lecture 15 | Memory ManagementCompiler Construction | Lecture 15 | Memory Management
Compiler Construction | Lecture 15 | Memory Management
 
Compiler Construction | Lecture 14 | Interpreters
Compiler Construction | Lecture 14 | InterpretersCompiler Construction | Lecture 14 | Interpreters
Compiler Construction | Lecture 14 | Interpreters
 
Compiler Construction | Lecture 13 | Code Generation
Compiler Construction | Lecture 13 | Code GenerationCompiler Construction | Lecture 13 | Code Generation
Compiler Construction | Lecture 13 | Code Generation
 
Compiler Construction | Lecture 12 | Virtual Machines
Compiler Construction | Lecture 12 | Virtual MachinesCompiler Construction | Lecture 12 | Virtual Machines
Compiler Construction | Lecture 12 | Virtual Machines
 
Compiler Construction | Lecture 9 | Constraint Resolution
Compiler Construction | Lecture 9 | Constraint ResolutionCompiler Construction | Lecture 9 | Constraint Resolution
Compiler Construction | Lecture 9 | Constraint Resolution
 
Compiler Construction | Lecture 8 | Type Constraints
Compiler Construction | Lecture 8 | Type ConstraintsCompiler Construction | Lecture 8 | Type Constraints
Compiler Construction | Lecture 8 | Type Constraints
 
Compiler Construction | Lecture 7 | Type Checking
Compiler Construction | Lecture 7 | Type CheckingCompiler Construction | Lecture 7 | Type Checking
Compiler Construction | Lecture 7 | Type Checking
 
Compiler Construction | Lecture 6 | Introduction to Static Analysis
Compiler Construction | Lecture 6 | Introduction to Static AnalysisCompiler Construction | Lecture 6 | Introduction to Static Analysis
Compiler Construction | Lecture 6 | Introduction to Static Analysis
 
Compiler Construction | Lecture 5 | Transformation by Term Rewriting
Compiler Construction | Lecture 5 | Transformation by Term RewritingCompiler Construction | Lecture 5 | Transformation by Term Rewriting
Compiler Construction | Lecture 5 | Transformation by Term Rewriting
 
Compiler Construction | Lecture 4 | Parsing
Compiler Construction | Lecture 4 | Parsing Compiler Construction | Lecture 4 | Parsing
Compiler Construction | Lecture 4 | Parsing
 
Compiler Construction | Lecture 3 | Syntactic Editor Services
Compiler Construction | Lecture 3 | Syntactic Editor ServicesCompiler Construction | Lecture 3 | Syntactic Editor Services
Compiler Construction | Lecture 3 | Syntactic Editor Services
 

Kürzlich hochgeladen

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 

Kürzlich hochgeladen (20)

Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 

Dependent dynamic rules

  • 1. Composing Source-to-Source Data-Flow Transformations with Dependent Dynamic Rewrite Rules Program Transformation 2004–2005 Eelco Visser Institute of Information & Computing Sciences Utrecht University, The Netherlands March 3, 2005
  • 2. Outline 1 Data-flow transformation strategies 2 Dependencies in data-flow transformation rules 3 Generic data-flow transformation strategies http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 3. Part I Data-Flow Transformation Strategies http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 4. Flow-Sensitive Constant Propagation (x := 3; y := x + 1; if foo(x) then (y := 2 * x; x := y - 2) else (x := y; y := 23); z := x + y) http://www.strategoxt.org (x := 3; y := 4; if foo(3) then (y := 6; x := 4) else (x := 4; y := 23); z := 4 + y) Composing Source-to-Source Data-Flow Transformations with
  • 5. x := 3 x := 3 x -> 3 y := x + 1 y := 4 x -> 3 y -> 4 if foo(x) if foo(3) x -> 3 y -> 4 y := 2 * x y := 6 x -> 3 y -> 4 x := y x := 4 x -> 3 y -> 6 x -> 4 y -> 4 x := y - 2 x := 4 y := 23 y := 23 x -> 4 y -> 6 x -> 4 y -> 23 x -> 4 y z := x + y z := 4 + y
  • 6.
  • 7. Strategy for Basic Constant Propagation prop-const = PropConst <+ prop-const-assign <+ prop-const-declare <+ prop-const-let <+ prop-const-if <+ prop-const-while <+ (all(prop-const); try(EvalBinOp)) prop-const-assign = |[ x := <prop-const => e> ]| ; if <is-value> e then rules( PropConst.x : |[ x ]| -> |[ e ]| ) else rules( PropConst.x :- |[ x ]| ) end prop-const-declare = |[ var x := <prop-const => e> ]| ; if <is-value> e then rules( PropConst+x : |[ x ]| -> |[ e ]| ) else rules( PropConst+x :- |[ x ]| ) end prop-const-let = ?|[ let d* in e* end ]|; {| PropConst : all(prop-const) |} http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 8. Intersection of Rule Sets prop-const-if = |[ if <prop-const> then <id> else <id> ]| ; (|[ if <id> then <prop-const> else <id> ]| /PropConst |[ if <id> then <id> else <prop-const> ]|) http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 9. Intersection of Rule Sets x let var x := 1 var y := z 1 var z := 3 var a := 4 1 in x := x + z; 4 a := 5; 4 if y then (y := y + 5; 4 z := 8) 4 else (x := a + 21; 26 y := x + 1; 26 z := a + z); 26 b := a + z; z := z + x end - y - z 3 3 3 a 4 4 5 b - - 3 8 5 5 - 27 27 - 3 3 8 8 8 8 5 5 5 5 5 5 13 13 http://www.strategoxt.org let var x := 1 var y := z var z := 3 var a := 4 in x := 4; a := 5; if y then (y := y + 5; z := 8) else (x := 26; y := 27; z := 8); b := 13; z := 8 + x end Composing Source-to-Source Data-Flow Transformations with
  • 10. Fixed-Point Intersection of Rule Sets let var w := 20 var x := 20 var y := 20 var z := 10 in while SomethingUnknown() (if x = 20 then w := 20 if y = 20 then x := 20 if z = 20 then y := 20 w; x; y; z end do else w := 10; else x := 10; else y := 10); 1 2 let var w := 20 var x := 20 var y := 20 var z := 10 in while SomethingUnknown() do (if x = 20 then w := 20 else w := 10; if y = 20 then x := 20 else x := 10; y := 10); w; x; y; 10 end http://www.strategoxt.org 3 4 w 20 20 20 20 20 - x 20 20 20 - y 20 10 10 10 10 - z 10 10 10 10 10 10 10 10 10 Composing Source-to-Source Data-Flow Transformations with
  • 11. Fixed-Point Intersection of Rule Sets prop-const-while = ?|[ while e1 do e2 ]| ; (/PropConst* |[ while <prop-const> do <prop-const> ]|) http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 12. Unreachable Code Elimination let var x := 0 var y := 0 in x := 10; while A do (if x = 10 then dosomething() else (dosomethingelse(); x := x + 1)); y := x end http://www.strategoxt.org let var x := 0 var y := 0 in x := 10; while A do dosomething(); y := 10 end Composing Source-to-Source Data-Flow Transformations with
  • 13. Unreachable Code Elimination prop-const-if = |[ if <prop-const> then <id> else <id> ]| ; (EvalIf; prop-const <+ (|[ if <id> then <prop-const> else <id> ]| /PropConst |[ if <id> then <id> else <prop-const> ]|)) prop-const-while = ?|[ while e1 do e2 ]| ; (|[ while <prop-const> do <id> ]|; EvalWhile <+ (/PropConst* |[ while <prop-const> do <prop-const> ]|)) EvalIf : |[ if EvalIf : |[ if where EvalWhile : |[ 0 then e1 else e2 ]| -> |[ e2 ]| i then e1 else e2 ]| -> |[ e1 ]| <not(eq)>(|[ i ]|, |[ 0 ]|) while 0 do e ]| -> |[ () ]| http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 14. Dead Code Elimination (x := foo(b); y := bar(h); a := c + 23; if 4 > x then (d := b + a; g := 4 + y) else (b := 2; a := y + 3; a := 4 + x); print(a)) {c,b} {x,c} {x,c} {x,a} {a} {a} {x} {x} {x} {a} http://www.strategoxt.org (x := foo(b); a := c + 23; if not(4> x) then a := 4 + x; print(a)) Composing Source-to-Source Data-Flow Transformations with
  • 15. Dead Code Elimination dce = VarNeeded <+ ElimAssign <+ dce-assign <+ dce-seq <+ dce-if <+ dce-while <+ all(dce) ElimAssign : |[ x := e ]| -> |[ () ]| where <not(Needed)> |[ x ]| VarNeeded = ?|[ x ]| ; rules(Needed : |[ x ]|) dce-assign = ?|[ x := e ]| ; rules(Needed :- |[ x ]|) ; |[ <id> := <dce> ]| http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 16. Dead Code Elimination – Control-Flow dce-seq = |[ (<* reverse-filter(dce; not(?|[ () ]|)) >) ]| dce-if = (|[ if <id> then <dce> else <id> ]| Needed/ |[ if <id> then <id> else <dce> ]|) ; |[ if <dce> then <id> else <id> ]| ; try(ElimIf) dce-while = |[ while <id> do <id> ]| ; (Needed/* |[ while <dce> do <dce> ]|) http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 17. Part II Dependencies in Data-Flow Transformation Rules http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 18. Copy Propagation Replace copies x produced by assignments of the form x := y by original y a := b; c := d + a http://www.strategoxt.org a := b; c := d + b Composing Source-to-Source Data-Flow Transformations with
  • 19. Copy Propagation Replace copies x produced by assignments of the form x := y by original y a := b; c := d + b a := b; c := d + a First attempt using dynamic rules (wrong) copy-prop-assign = ?|[ x := y ]|; if <not(eq)>(x,y) then rules( CopyProp.x : |[ x ]| -> |[ y ]| ) else rules( CopyProp.x :- |[ x ]| ) end http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 20. Problem: Insufficient Dependencies (a := b; b := foo(); c := d + a) (a := b; b := foo(); c := d + b) copy-prop-assign = ?|[ x := y ]|; if <not(eq)>(x,y) then rules( CopyProp.x : |[ x ]| -> |[ y ]| ) else rules( CopyProp.x :- |[ x ]| ) end http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 21. Problem: Insufficient Dependencies (a := b; b := foo(); c := d + a) (a := b; b := foo(); c := d + b) Problem: rule not undefined when variable in rhs changed Solution: undefine rule when any of its variables is modified copy-prop-assign = ?|[ x := y ]|; if <not(eq)>(x,y) then rules( CopyProp.x : |[ x ]| -> |[ y ]| ) else rules( CopyProp.x :- |[ x ]| ) end http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 22. Problem: Free Variable Capture let var a := bar() var b := baz() in a := b; let var b := foo() in print(a) end end let var a := bar() var b := baz() in a := b; let var b := foo() in print(b) // wrong! end end copy-prop-assign = ?|[ x := y ]|; if <not(eq)>(x,y) then rules( CopyProp.x : |[ x ]| -> |[ y ]| ) else rules( CopyProp.x :- |[ x ]| ) end http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 23. Problem: Free Variable Capture let var a := bar() var b := baz() in a := b; let var b := foo() in print(a) end end let var a := bar() var b := baz() in a := b; let var b := foo() in print(b) // wrong! end end Problem: rule not undefined when variables become shadowed Solution: undefine rule locally when some variable shadowed copy-prop-assign = ?|[ x := y ]|; if <not(eq)>(x,y) then rules( CopyProp.x : |[ x ]| -> |[ y ]| ) else rules( CopyProp.x :- |[ x ]| ) end http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 24. Problem: Escaping Variables (1) let var a := bar() in let var b := foo() in a := b end; print(a) end let var a := bar() in let var b := foo() in a := b end; print(b) // wrong! end copy-prop-assign = ?|[ x := y ]|; if <not(eq)>(x,y) then rules( CopyProp.x : |[ x ]| -> |[ y ]| ) else rules( CopyProp.x :- |[ x ]| ) end http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 25. Problem: Escaping Variables (1) let var a := bar() in let var b := foo() in a := b end; print(a) end let var a := bar() in let var b := foo() in a := b end; print(b) // wrong! end Problem: rule not undefined when a variable goes out of scope Solution: (re)define rule in local scope copy-prop-assign = ?|[ x := y ]|; if <not(eq)>(x,y) then rules( CopyProp.x : |[ x ]| -> |[ y ]| ) else rules( CopyProp.x :- |[ x ]| ) end http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 26. Problem: Escaping Variables (2) let var var in let in a := bar() c := baz() var b := foo() a := b; a := c end; print(a) let var var in let in a := bar() c := baz() var b := foo() a := b; a := c end; print(c) // ok! end end copy-prop-assign = ?|[ x := y ]|; if <not(eq)>(x,y) then rules( CopyProp.x : |[ x ]| -> |[ y ]| ) else rules( CopyProp.x :- |[ x ]| ) end http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 27. Problem: Escaping Variables (2) let var var in let in a := bar() c := baz() var b := foo() a := b; a := c end; print(a) let var var in let in a := bar() c := baz() var b := foo() a := b; a := c end; print(c) // ok! end end Problem: definition in local scope is too restricted Solution: (re)define rule in innermost scope of all variables involved copy-prop-assign = ?|[ x := y ]|; if <not(eq)>(x,y) then rules( CopyProp.x : |[ x ]| -> |[ y ]| ) else rules( CopyProp.x :- |[ x ]| ) end http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 28. Common-Subexpression Elimination (x y z a z := := := := := a + b; a + b; a + c; 1; (a + c) + (a + b)) http://www.strategoxt.org ⇒ (x y z a z := := := := := a + b; x; a + c; 1; (a + c) + (a + b)) Composing Source-to-Source Data-Flow Transformations with
  • 29. Common-Subexpression Elimination (x y z a z := := := := := a + b; a + b; a + c; 1; (a + c) + (a + b)) ⇒ (x y z a z := := := := := a + b; x; a + c; 1; (a + c) + (a + b)) Assignment x := e Propagation rule |[ e ]| -> |[ x ]| Dependencies in common-subexpression elimination all variables in assignment x := e http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 30. Common-Subexpression Elimination cse = cse-assign <+ (all(cse); try(ReplaceExp)) cse-assign = |[ x := <cse => e> ]| ; where(<undefine-subexpressions> |[ x ]|) ; if <not(is-subterm(||[ x ]|))> |[ e ]| then rules(ReplaceExp : |[ e ]| -> |[ x ]|) ; where(<register-subexpressions(|e)> |[ x := e ]|) end register-subexpressions(|e) = get-vars; map({y : ?|[ y ]| ; rules(UsedInExp :+ |[ y ]| -> e)}) undefine-subexpressions = bagof-UsedInExp; map({?e; rules(ReplaceExp :- |[ e ]|)}) get-vars = collect({?|[ x ]|}) http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 31. Dependent Dynamic Rules Declare rule dependencies R.lab : p1 -> p2 depends on [(lab1,dep1),...,(labn,depn)] Undefine all rules depending on dep undefine-R(|dep) Locally undefine all rules depending on dep new-R(|lab, dep) and label current scope with lab http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 32. Copy Propagation – Assignments copy-prop = repeat1(CopyProp) <+ copy-prop-assign <+ copy-prop-declare <+ copy-prop-let <+ copy-prop-if <+ copy-prop-while <+ all(copy-prop) copy-prop-declare = |[ var x ta := <copy-prop => e> ]| ; where( new-CopyProp(|x, x) ) ; where( try(<copy-prop-assign-aux> |[ x := e ]|) ) copy-prop-assign = |[ x := <copy-prop => e> ]| ; where( undefine-CopyProp(|x) ) ; where( try(copy-prop-assign-aux) ) http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 33. Copy Propagation – Propagation Rule copy-prop-assign-aux = ? |[ x := y ]| ; where( <not(eq)>(x,y) ) ; where( innermost-scope-CopyProp => z ) ; rules( CopyProp.z : |[ x ]| -> |[ y ]| depends on [(x,x), (y,y)] ) innermost-scope-CopyProp = get-var-names => vars ; innermost-scope-CopyProp(elem-of(|vars)) http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 34. Copy Propagation – Control-Flow copy-prop-let = |[ let <*id> in <*id> end ]| ; {| CopyProp : all(copy-prop) |} copy-prop-if = |[ if <copy-prop> then <id> else <id> ]| ; ( |[ if <id> then <copy-prop> else <id> ]| /CopyProp |[ if <id> then <id> else <copy-prop> ]|) copy-prop-while = |[ while <id> do <id> ]| ; (/CopyProp* |[ while <copy-prop> do <copy-prop> ]|) http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 35. Common-Subexpression Elimination – Assignments cse = cse-assign <+ cse-vardec <+ cse-let <+ cse-if <+ cse-while <+ all(cse); try(CSE) cse-vardec = |[ var x ta := <cse => e> ]| ; new-CSE(|x, x) ; where( try(<cse-assign-aux> |[ x := e ]|) ) cse-assign = |[ x := <cse => e> ]| ; undefine-CSE(|x) ; where(try(cse-assign-aux)) http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 36. Common-Subexpression Elimination – Propagation cse-assign-aux = ? |[ x := e ]| ; where( <not(oncetd(?|[ x ]|)); pure> |[ e ]| ) ; where( get-var-names; map(!(<id>,<id>)) => xs ) ; where( innermost-scope-CSE => z ) ; rules( CSE.z : |[ e ]| -> |[ x ]| depends on xs ) pure = ?|[ i ]| + ?|[ x ]| + |[ <bo:id>(<pure>, <pure>) ]| innermost-scope-CSE = get-var-names => vars ; innermost-scope-CSE(where(<elem>(<id>, vars))) http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 37. Common-Subexpression Elimination – Control-Flow cse-let = |[ let <*id> in <*id> end ]| ; {| CSE : all(cse) |} cse-if = |[ if <cse> then <id> else <id> ]| ; ( |[ if <id> then <cse> else <id> ]| /CSE |[ if <id> then <id> else <cse> ]|) cse-while = |[ while <id> do <id> ]| ; (/CSE* |[ while <cse> do <cse> ]|) http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 38. Part III Generic Data-Flow Transformation Strategies http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 39. Generic Data-Flow Transformation Strategies Data-flow transformation strategies are similar Factor out underlying strategy Requires generalization over combinators new-dynamic-rules(|Rs,x,x) undefine-dynamic-rules(|Rs,x) /~Rs1~Rs2/ Allows very concise specifications for specific transformations Combination of transformations http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 40. Generic Strategy – Framework forward-prop(transform, before, after | Rs1, Rs2, Rs3) = <conc>(Rs1, Rs2, Rs3) => RsSc; <conc>(Rs1, Rs2) => RsDf; let fp = prop-assign <+ prop-declare <+ prop-let <+ prop-if <+ prop-while <+ transform(fp) <+ (before; all(fp); after) prop-assign = ... prop-declare = ... prop-let = ... prop-if = ... prop-while = ... in fp end http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 41. Generic Strategy – Assignments prop-assign = |[ <id> := <fp> ]| ; (transform(fp) <+ before ; ?|[ x := e ]| ; undefine-dynamic-rules(|RsDf,x) ; after) prop-declare = |[ var <id> := <fp> ]| ; (transform(fp) <+ before; ?|[ var x := e ]| ; new-dynamic-rules(|RsSc,x,x);after) prop-let = ?|[ let d* in e* end ]| ; (transform(fp) <+ {|~RsSc : before; all(fp); after |}) http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 42. Generic Strategy – Control Flow prop-if = |[ if <fp> then <id> else <id> ]| ; (transform(fp) <+ before ; (|[ if <id> then <fp> else <id> ]| /~Rs1~Rs2/ |[ if <id> then <id> else <fp> ]|) ; after) prop-while = ?|[ while e1 do e2 ]| ; (transform(fp) <+ before ; /~Rs1~Rs2/* |[ while <fp> do <fp> ]| ; after) http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 43. Instantation: Constant Propagation prop-const = forward-prop(prop-const-transform, id, prop-const-after | ["PropConst"],[],[]) prop-const-transform(recur) = EvalFor <+ EvalIf; recur <+ |[ while <recur> do <id> ]|; EvalWhile prop-const-after = try(prop-const-assign <+ prop-const-declare <+ PropConst <+ EvalBinOp) prop-const-assign = ?|[ x := e ]|; where( <is-value> e ) ; rules( PropConst.x : |[ x ]| -> |[ e ]| depends on [(x,x)] ) prop-const-declare = ?|[var x ta := e]|; where(<prop-const-assign>|[x := e]|) http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 44. Instantation: Copy Propagation copy-prop = forward-prop(no-transform,id,copy-prop-after |["CopyProp"],[],[]) copy-prop-after = try(copy-prop-assign <+ copy-prop-declare <+ repeat1(CopyProp)) copy-prop-declare = ? |[ var x ta := e ]| ; where(try(<copy-prop-assign> |[ x := e ]|)) copy-prop-assign = ? |[ x := y ]| ; where( <not(eq)> (x, y) ) ; where( get-var-dependencies => xs ) ; where( innermost-scope-CopyProp => z ) ; rules( CopyProp.z : |[ x ]| -> |[ y ]| depends on xs ) http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 45. Instantation: Common-Subexpression Elimination cse = forward-prop(no-transform, id, cse-after|["CSE"],[],[]) cse-after = try(cse-assign <+ cse-declare <+ CSE) cse-declare = ?|[ var x := e ]|; where( <cse-assign> |[ x := e ]| ) cse-assign ; where( ; where( ; where( ; rules( = ?|[ x := e ]| <pure-and-not-trivial(|x)> |[ e ]| ) get-var-dependencies => xs ) innermost-scope-CSE => z ) CSE.z : |[ e ]| -> |[ x ]| depends on xs ) http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with
  • 46. Instantation: A Super Optimizer super-opt = forward-prop( prop-const-transform , bvr-before , bvr-after ; copy-prop-after ; prop-const-after ; cse-after | ["PropConst", "CopyProp", "CSE"], [], ["RenameVar"]) Combination of constant propagation copy propagation common-subexpression elimination bound variable renaming http://www.strategoxt.org Composing Source-to-Source Data-Flow Transformations with