-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathstatic_getitem.py
175 lines (155 loc) · 6.47 KB
/
static_getitem.py
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
from numba.core import errors, ir, types
from numba.core.rewrites import register_rewrite, Rewrite
@register_rewrite('before-inference')
class RewriteConstGetitems(Rewrite):
"""
Rewrite IR expressions of the kind `getitem(value=arr, index=$constXX)`
where `$constXX` is a known constant as
`static_getitem(value=arr, index=<constant value>)`.
"""
def match(self, func_ir, block, typemap, calltypes):
self.getitems = getitems = {}
self.block = block
# Detect all getitem expressions and find which ones can be
# rewritten
for expr in block.find_exprs(op='getitem'):
if expr.op == 'getitem':
try:
const = func_ir.infer_constant(expr.index)
except errors.ConstantInferenceError:
continue
getitems[expr] = const
return len(getitems) > 0
def apply(self):
"""
Rewrite all matching getitems as static_getitems.
"""
new_block = self.block.copy()
new_block.clear()
for inst in self.block.body:
if isinstance(inst, ir.Assign):
expr = inst.value
if expr in self.getitems:
const = self.getitems[expr]
new_expr = ir.Expr.static_getitem(value=expr.value,
index=const,
index_var=expr.index,
loc=expr.loc)
inst = ir.Assign(value=new_expr, target=inst.target,
loc=inst.loc)
new_block.append(inst)
return new_block
@register_rewrite('after-inference')
class RewriteStringLiteralGetitems(Rewrite):
"""
Rewrite IR expressions of the kind `getitem(value=arr, index=$XX)`
where `$XX` is a StringLiteral value as
`static_getitem(value=arr, index=<literal value>)`.
"""
def match(self, func_ir, block, typemap, calltypes):
"""
Detect all getitem expressions and find which ones have
string literal indexes
"""
self.getitems = getitems = {}
self.block = block
self.calltypes = calltypes
for expr in block.find_exprs(op='getitem'):
if expr.op == 'getitem':
index_ty = typemap[expr.index.name]
if isinstance(index_ty, types.StringLiteral):
getitems[expr] = (expr.index, index_ty.literal_value)
return len(getitems) > 0
def apply(self):
"""
Rewrite all matching getitems as static_getitems where the index
is the literal value of the string.
"""
new_block = ir.Block(self.block.scope, self.block.loc)
for inst in self.block.body:
if isinstance(inst, ir.Assign):
expr = inst.value
if expr in self.getitems:
const, lit_val = self.getitems[expr]
new_expr = ir.Expr.static_getitem(value=expr.value,
index=lit_val,
index_var=expr.index,
loc=expr.loc)
self.calltypes[new_expr] = self.calltypes[expr]
inst = ir.Assign(value=new_expr, target=inst.target,
loc=inst.loc)
new_block.append(inst)
return new_block
@register_rewrite('after-inference')
class RewriteStringLiteralSetitems(Rewrite):
"""
Rewrite IR expressions of the kind `setitem(value=arr, index=$XX, value=)`
where `$XX` is a StringLiteral value as
`static_setitem(value=arr, index=<literal value>, value=)`.
"""
def match(self, func_ir, block, typemap, calltypes):
"""
Detect all setitem expressions and find which ones have
string literal indexes
"""
self.setitems = setitems = {}
self.block = block
self.calltypes = calltypes
for inst in block.find_insts(ir.SetItem):
index_ty = typemap[inst.index.name]
if isinstance(index_ty, types.StringLiteral):
setitems[inst] = (inst.index, index_ty.literal_value)
return len(setitems) > 0
def apply(self):
"""
Rewrite all matching setitems as static_setitems where the index
is the literal value of the string.
"""
new_block = ir.Block(self.block.scope, self.block.loc)
for inst in self.block.body:
if isinstance(inst, ir.SetItem):
if inst in self.setitems:
const, lit_val = self.setitems[inst]
new_inst = ir.StaticSetItem(target=inst.target,
index=lit_val,
index_var=inst.index,
value=inst.value,
loc=inst.loc)
self.calltypes[new_inst] = self.calltypes[inst]
inst = new_inst
new_block.append(inst)
return new_block
@register_rewrite('before-inference')
class RewriteConstSetitems(Rewrite):
"""
Rewrite IR statements of the kind `setitem(target=arr, index=$constXX, ...)`
where `$constXX` is a known constant as
`static_setitem(target=arr, index=<constant value>, ...)`.
"""
def match(self, func_ir, block, typemap, calltypes):
self.setitems = setitems = {}
self.block = block
# Detect all setitem statements and find which ones can be
# rewritten
for inst in block.find_insts(ir.SetItem):
try:
const = func_ir.infer_constant(inst.index)
except errors.ConstantInferenceError:
continue
setitems[inst] = const
return len(setitems) > 0
def apply(self):
"""
Rewrite all matching setitems as static_setitems.
"""
new_block = self.block.copy()
new_block.clear()
for inst in self.block.body:
if inst in self.setitems:
const = self.setitems[inst]
new_inst = ir.StaticSetItem(inst.target, const,
inst.index, inst.value, inst.loc)
new_block.append(new_inst)
else:
new_block.append(inst)
return new_block