-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinduction_variables.py
420 lines (350 loc) · 15 KB
/
induction_variables.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
"""
Implementation of Induction Variable Elimination for Loops
ASSSUMED NOT TO BE IN SSA FORM
"""
from copy import deepcopy
import sys
import json
import click
from collections import OrderedDict
from reaching_definitions import reaching_defs_func
from licm import insert_preheaders, identify_loop_invariant_instrs, insert_into_bb, LOOP_INVARIANT
from cfg import form_cfg_w_blocks, join_cfg, INSTRS
from dominator_utilities import get_natural_loops, get_dominators_w_cfg
from bril_core_constants import *
from bril_core_utilities import is_add, is_mul, is_const, is_int
UNIQUE_VAR_NAME = "unique_var"
UNIQUE_VAR_IDX = 0
BEFORE_I = 0
AFTER_I = 1
def gen_new_var():
global UNIQUE_VAR_IDX
UNIQUE_VAR_IDX += 1
return f"{UNIQUE_VAR_NAME}_{UNIQUE_VAR_IDX}"
class InductionVariable(object):
"""
Abstract Induction Variable Class
"""
def __init__(self):
pass
class BasicInductionVariable(InductionVariable):
"""
Basic Induction Variable
Represents i += e
where i is a defintiion defined exactly once in the loop and e is loop invariant
"""
def __init__(self, i_id, i, e, e_val, basic_block) -> None:
self.i_id = i_id
self.i = i
self.e = e
self.e_val = e_val
self.basic_block = basic_block
def __str__(self) -> str:
return f"{self.i} += {self.e} @{self.i_id}"
def __repr__(self) -> str:
return self.__str__()
class MulInvariant(InductionVariable):
"""
Multiplied Induction Variable
Represents a = c * i where c is a loop invariant value and i is a basic induction variable
"""
def __init__(self, a_id, a, c, i) -> None:
self.a_id = a_id
self.a = a
self.c = c
self.i = i
def __str__(self) -> str:
return f"{self.a} = {self.c} * {self.i} @{self.a_id}"
def __repr__(self) -> str:
return self.__str__()
class DerivedInductionVariable(InductionVariable):
"""
Derived Induction Variable
Represents j = c * i + d where identifier is the python id of the defining
instruction for j.
"""
def __init__(self, j_id, j, c, i, d, basic_block) -> None:
self.j_id = j_id
self.c = c
self.d = d
self.i = i
self.j = j
self.basic_block = basic_block
def __str__(self) -> str:
return f"{self.j} = {self.c} * {self.i} + {self.d} @{self.j_id}, {self.basic_block}"
def __repr__(self) -> str:
return self.__str__()
def find_basic_ivs(cfg, loop_blocks, var_invariant_map):
"""
Find all instructions in loop_basic_block of cfg that satisfy
the form
i += e
where i is defined exactly once in the loop and e is loop invariant
var_invariant_map maps a variable to whether variable is loop ivnariant or not
loop_blocks are all the blocks in the loop
"""
basic_ivs = OrderedDict()
for loop_basic_block in loop_blocks:
for instr in cfg[loop_basic_block][INSTRS]:
if is_add(instr):
# check i defined only once in the loop
defined_once = True
for b in loop_blocks:
for b_instr in cfg[b][INSTRS]:
if DEST in b_instr and id(b_instr) != id(instr) and b_instr[DEST] == instr[DEST]:
defined_once = False
if not defined_once:
continue
# check i = i + e form
def_var = instr[DEST]
args = instr[ARGS]
if def_var not in args:
continue
# check e is loop invariant
other_arg_invariant = False
other_arg = None
for a in args:
if a in var_invariant_map and var_invariant_map[a] == LOOP_INVARIANT:
other_arg_invariant = True
other_arg = a
if not other_arg_invariant:
continue
# to be restrictive about e, check it is a cosntant
is_constant = False
constant_instr = None
for other_block in loop_blocks:
for other_instr in cfg[other_block][INSTRS]:
if DEST in other_instr and other_instr[DEST] == other_arg:
constant_instr = other_instr
is_constant = is_const(
constant_instr) and is_int(constant_instr)
if not is_constant:
continue
# add loop invariant instructions
assert other_arg != None and constant_instr != None
basic_ivs[def_var] = BasicInductionVariable(
id(instr), def_var, other_arg, constant_instr[VALUE], loop_basic_block)
return basic_ivs
def find_derived_ivs(cfg, loop_blocks, var_invariant_map, basic_variable_map):
"""
Find defintions like j = c * i + d
where i is a basic indution variable and c, d are loop invariant
We look for definitions j = a + d where d itself is loop invariant
and a is c * i, c is loop invariant, i is basic.
"""
derived_ivs = OrderedDict()
mul_invariant_map = OrderedDict()
const_map = OrderedDict()
has_changed = True
while has_changed:
old_derived_ivs = deepcopy(derived_ivs)
old_mul_invariant_map = deepcopy(mul_invariant_map)
old_const_map = deepcopy(const_map)
for loop_basic_block in loop_blocks:
for instr in cfg[loop_basic_block][INSTRS]:
instr_id = id(instr)
if is_mul(instr):
def_var = instr[DEST]
# check only 1 defintion of this variable in the loop
has_one_def = True
for other_block in loop_blocks:
for other_instr in cfg[other_block][INSTRS]:
if DEST in other_instr and id(other_instr) != instr_id and other_instr[DEST] == def_var:
has_one_def = False
if has_one_def:
left = instr[ARGS][0]
right = instr[ARGS][1]
if left in basic_variable_map and right in var_invariant_map:
mul_inv = MulInvariant(
instr_id, def_var, right, left)
mul_invariant_map[def_var] = mul_inv
elif right in basic_variable_map and left in var_invariant_map:
mul_inv = MulInvariant(
instr_id, def_var, left, right)
mul_invariant_map[def_var] = mul_inv
elif is_const(instr):
def_var = instr[DEST]
# check only 1 defintion of this variable in the loop
has_one_def = True
for other_block in loop_blocks:
for other_instr in cfg[other_block][INSTRS]:
if DEST in other_instr and id(other_instr) != instr_id and other_instr[DEST] == def_var:
has_one_def = False
if has_one_def and instr[TYPE] == INT:
val = instr[VALUE]
const_map[def_var] = val
elif is_add(instr):
def_var = instr[DEST]
# check only 1 defintion of this variable in the loop
has_one_def = True
for other_block in loop_blocks:
for other_instr in cfg[other_block][INSTRS]:
if DEST in other_instr and id(other_instr) != instr_id and other_instr[DEST] == def_var:
has_one_def = False
if has_one_def:
left = instr[ARGS][0]
right = instr[ARGS][1]
if left in mul_invariant_map and right in const_map:
derived_inv = DerivedInductionVariable(
instr_id, def_var, mul_invariant_map[left].c, mul_invariant_map[left].i, right, loop_basic_block)
derived_ivs[def_var] = derived_inv
elif right in mul_invariant_map and left in const_map:
derived_inv = DerivedInductionVariable(
instr_id, def_var, mul_invariant_map[right].c, mul_invariant_map[right].i, left, loop_basic_block)
derived_ivs[def_var] = derived_inv
has_changed = old_derived_ivs == derived_ivs and old_mul_invariant_map == mul_invariant_map and old_const_map == const_map
return derived_ivs, const_map
def replace_ivs(cfg, dom, derived_ivs, basic_ivs, const_map, loop_preheader):
"""
Replaces Induction Variables by changing j's update in the loop after i
as well as the initialization of j in the preheader
"""
for _, derived_iv in derived_ivs.items():
assert type(derived_iv) == DerivedInductionVariable
# insert initialization of j in the preheader
j = derived_iv.j
i = derived_iv.i
c = derived_iv.c
d = derived_iv.d
e = basic_ivs[i]
e_val = e.e_val
# we must have a definite domination relation between j's bb and i's bb
j_bb = derived_iv.basic_block
j_id = derived_iv.j_id
i_bb = e.basic_block
i_id = e.i_id
i_j_domination_relation = None
if i_bb == j_bb:
i_idx = None
j_idx = None
for i, instr in enumerate(cfg[i_bb][INSTRS]):
if id(instr) == i_id:
i_idx = i
elif id(instr) == j_id:
j_idx = i
assert i_idx != None and j_idx != None
assert i_idx != j_idx
i_j_domination_relation = AFTER_I if i_idx < j_idx else BEFORE_I
elif j_bb in dom[i_bb]:
i_j_domination_relation = AFTER_I
elif i_bb in dom[j_bb]:
i_j_domination_relation = BEFORE_I
# cannot figure out what relation j and i have, bail out
if i_j_domination_relation == None:
continue
# insert initializer into preheader
j_init_instr1 = {DEST: j, TYPE: INT, OP: CONST, VALUE: const_map[d]}
insert_into_bb(cfg, loop_preheader, j_init_instr1)
# insert update of j in the loop
update_var1 = gen_new_var()
update_var2 = gen_new_var()
j_update_instr1 = {DEST: update_var1,
TYPE: INT, OP: CONST, VALUE: e_val}
j_update_instr2 = {DEST: update_var2,
TYPE: INT, OP: MUL, ARGS: [c, update_var1]}
j_update_instr3 = {DEST: j, TYPE: INT,
OP: ADD, ARGS: [j, update_var2]}
# split into cases on where to insert instructions
if i_j_domination_relation == AFTER_I:
bb_instrs = cfg[j_bb][INSTRS]
new_instrs = []
for instr in bb_instrs:
if id(instr) == j_id:
# do not add back original instr, as a deletion of j's update
new_instrs.append(j_update_instr1)
new_instrs.append(j_update_instr2)
new_instrs.append(j_update_instr3)
else:
new_instrs.append(instr)
cfg[j_bb][INSTRS] = new_instrs
elif i_j_domination_relation == BEFORE_I:
# delete old j update
bb_instrs = cfg[j_bb][INSTRS]
new_instrs = []
for instr in bb_instrs:
if id(instr) == j_id:
pass
else:
new_instrs.append(instr)
cfg[j_bb][INSTRS] = new_instrs
# insert after i updates
bb_instrs = cfg[i_bb][INSTRS]
new_instrs = []
for instr in bb_instrs:
if id(instr) == i_id:
new_instrs.append(instr)
new_instrs.append(j_update_instr1)
new_instrs.append(j_update_instr2)
new_instrs.append(j_update_instr3)
else:
new_instrs.append(instr)
cfg[i_bb][INSTRS] = new_instrs
else:
raise RuntimeError(
f"Cannot handle i_j_domination_relation {i_j_domination_relation}.")
def loop_induction_variables(func_args, cfg, dom, reaching_definitions, natural_loop, preheadermap):
"""
Calculate and move induction variables for a single loop corresponding to natural loop
"""
(natural_loop_blocks, _, natural_loop_header, _) = natural_loop
loop_instrs = []
for block in natural_loop_blocks:
for instr in cfg[block][INSTRS]:
loop_instrs.append((instr, block))
_, var_invariant_map = identify_loop_invariant_instrs(
cfg, func_args, natural_loop_blocks, loop_instrs, natural_loop_header, reaching_definitions)
basic_ivs = find_basic_ivs(cfg, natural_loop_blocks, var_invariant_map)
derived_ivs, const_map = find_derived_ivs(
cfg, natural_loop_blocks, var_invariant_map, basic_ivs)
replace_ivs(cfg, dom, derived_ivs, basic_ivs, const_map,
preheadermap[natural_loop_header])
def func_induction_variables(func):
"""
Calculate and move induction variables for a single function
"""
natural_loops = get_natural_loops(func)
# grab args
func_args = []
if ARGS in func:
for a in func[ARGS]:
func_args.append(a[NAME])
# add preheaders to loops in func
old_cfg = form_cfg_w_blocks(func)
instrs_w_blocks = []
for block in old_cfg:
for instr in old_cfg[block][INSTRS]:
instrs_w_blocks.append((instr, block))
preheadermap, new_instrs = insert_preheaders(
natural_loops, instrs_w_blocks)
func[INSTRS] = new_instrs
cfg = form_cfg_w_blocks(func)
reaching_definitions = reaching_defs_func(func)
dom, _ = get_dominators_w_cfg(cfg, list(cfg.keys())[0])
for natural_loop in natural_loops:
loop_induction_variables(
func_args, cfg, dom, reaching_definitions, natural_loop, preheadermap)
return join_cfg(cfg)
def induction_variables(prog):
"""
Apply Induction Variable Elimination to a Program
"""
for func in prog[FUNCTIONS]:
new_instrs = func_induction_variables(func)
func[INSTRS] = new_instrs
return prog
@click.command()
@click.option('--pretty-print', default=False, help='Pretty Print Original Program.')
@click.option('--ive', default=False, help='Run Induction Variable Elimination Original Program.')
def main(pretty_print, ive):
prog = json.load(sys.stdin)
if pretty_print == 'True':
print(json.dumps(prog, indent=4, sort_keys=True))
if ive == 'True':
final_prog = induction_variables(prog)
else:
final_prog = prog
if pretty_print == 'True':
print(json.dumps(final_prog, indent=4, sort_keys=True))
print(json.dumps(final_prog))
if __name__ == "__main__":
main()