Repository navigation
Expand file tree
/
Copy patharithmetic_demo.py
More file actions
149 lines (118 loc) · 4.16 KB
/
Copy patharithmetic_demo.py
File metadata and controls
149 lines (118 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
from quiche import EGraph, MinimumCostExtractor
from quiche.rewrite import Rule
from quiche.lang.expr_lang import ExprNode, ExprNodeCost, ExprTree
from quiche.lang.expr_constant_folding import ExprConstantFolding
def egraph_intro_slide_8():
expr = (ExprNode('a', ()) * 2) / 2
quiche_tree = ExprTree(expr)
egraph = EGraph(quiche_tree)
def egraph_add_shift_term_slide_9():
# setup
expr = (ExprNode('a', ()) * 2) / 2
quiche_tree = ExprTree(expr)
egraph = EGraph(quiche_tree)
# on slide
shift_expr = ExprNode('a', ()) << 1
egraph.add(ExprTree(shift_expr))
def eclass_merge_example_slide_11():
# setup
expr = (ExprNode('a', ()) * 2) / 2
quiche_tree = ExprTree(expr)
egraph = EGraph(quiche_tree)
shift_expr = ExprNode('a', ()) << 1
# on slide
shift_eclass = egraph.add(ExprTree(shift_expr))
times_node = ExprNode('a', ()) * 2
times_eclass = egraph.add(ExprTree(times_node))
egraph.merge(times_eclass, shift_eclass)
egraph.rebuild()
def ematching_example_slide_15():
# setup
expr = (ExprNode('a', ()) * 2) / 2
quiche_tree = ExprTree(expr)
egraph = EGraph(quiche_tree)
shift_expr = ExprNode('a', ()) << 1
egraph.add(ExprTree(shift_expr))
# on slide
pattern = ExprTree(ExprNode('x', ()) * 2)
matches = egraph.ematch(pattern, egraph.eclasses())
print(matches)
def apply_a_rule_slide_16():
# setup
expr = (ExprNode('a', ()) * 2) / 2
quiche_tree = ExprTree(expr)
egraph = EGraph(quiche_tree)
shift_expr = ExprNode('a', ()) << 1
# on slide
shift_eclass = egraph.add(ExprTree(shift_expr))
rule = ExprTree.make_rule(lambda x: (x * 2, x << 1))
Rule.apply_rules([rule], egraph)
print("Shift e-class: ", shift_eclass)
print("Shift e-class.find(): ", shift_eclass.find())
def apply_rules_to_eqsat_slide_22():
# setup
expr = (ExprNode('a', ()) * 2) / 2
quiche_tree = ExprTree(expr)
egraph = EGraph(quiche_tree)
shift_expr = ExprNode('a', ()) << 1
egraph.add(ExprTree(shift_expr))
rule = ExprTree.make_rule(lambda x: (x * 2, x << 1))
Rule.apply_rules([rule], egraph)
# on slide
rules = [
ExprTree.make_rule(lambda x, y, z: ((x * y) / z, x * (y / z))),
ExprTree.make_rule(lambda x: (x / x, ExprNode(1, ()))),
ExprTree.make_rule(lambda x: (x * 1, x))
]
while not egraph.is_saturated():
Rule.apply_rules(rules, egraph)
aeclass = egraph.add(ExprTree(ExprNode('a', ())))
assert aeclass.find() == egraph.root.find()
# print("a eclass: ", aeclass.find())
# print("root eclass: ", egraph.root.find())
def constant_folding_usage_slide_28():
expr = (ExprNode('a', ()) * 2) / 2
quiche_tree = ExprTree(expr)
egraph = EGraph(quiche_tree, ExprConstantFolding())
four_eclass = egraph.add(ExprTree(ExprNode(4, ())))
a_eclass = egraph.add(ExprTree(ExprNode("a", ())))
egraph.merge(a_eclass, four_eclass)
egraph.rebuild()
assert egraph.root.data == 4
def term_extraction_example_slide_31():
# setup
expr = (ExprNode('a', ()) * 2) / 2
quiche_tree = ExprTree(expr)
egraph = EGraph(quiche_tree)
rules = [
# Multiply x by 2 === shift x left by 1
ExprTree.make_rule(lambda x: (x * 2, x << 1)),
# Reassociate *,/
ExprTree.make_rule(lambda x, y, z: ((x * y) / z, x * (y / z))),
# x/x = 1
ExprTree.make_rule(lambda x: (x / x, ExprNode(1, ()))),
# Simplify x * 1 === x
ExprTree.make_rule(lambda x: (x * 1, x)),
]
while not egraph.is_saturated():
Rule.apply_rules(rules, egraph)
# Verify extracted term is correct
cost_model = ExprNodeCost()
extractor = MinimumCostExtractor()
extracted = extractor.extract(
cost_model, egraph, egraph.root.find(), ExprTree.make_node
)
assert str(extracted) == "a"
def main():
egraph_intro_slide_8()
egraph_add_shift_term_slide_9()
eclass_merge_example_slide_11()
ematching_example_slide_15()
apply_a_rule_slide_16()
apply_rules_to_eqsat_slide_22()
constant_folding_usage_slide_28()
term_extraction_example_slide_31()
def test_main():
main()
if __name__ == "__main__":
main()