Skip to content

Commit cb60213

Browse files
authored
Merge pull request #23 from costa-group/var_order
Var order
2 parents a0e2728 + 920824c commit cb60213

File tree

3 files changed

+40
-22
lines changed

3 files changed

+40
-22
lines changed

scripts/compare_blocks.py

+16-5
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,16 @@ def is_terminal(block):
173173
def process_terminal_blocks(blocks, num_pops):
174174
terminal_blocks = 0
175175
total_pops = []
176+
total_ins_terminal = 0
177+
176178
for bl in blocks:
177179
process_pops(bl, total_pops)
178180
if is_terminal(bl):
179181
terminal_blocks+=1
182+
total_ins_terminal+=len(bl)
180183
process_pops(bl,num_pops)
181184

182-
return terminal_blocks, sum(total_pops)
185+
return terminal_blocks, sum(total_pops), total_ins_terminal
183186

184187
def count_num_ins(evm: str):
185188
"""
@@ -190,15 +193,17 @@ def count_num_ins(evm: str):
190193
num_pop = []
191194
terminal_blocks = 0
192195
total_pops = 0
196+
total_ins_terminal = 0
193197
for region in code_regions:
194198
blocks = get_blocks(remove_auxdata(region))
195-
num_tblocks, numtotal_pops = process_terminal_blocks(blocks, num_pop)
199+
num_tblocks, numtotal_pops, ins_terminal = process_terminal_blocks(blocks, num_pop)
196200
terminal_blocks+=num_tblocks
197201
total_pops+=numtotal_pops
198-
202+
total_ins_terminal+=ins_terminal
203+
199204
#print("TERMINAL BLOCKS: " +str(terminal_blocks))
200205
#print("NUM_POPS: "+ str(sum(num_pop)))
201-
return (terminal_blocks, sum(num_pop), total_pops)
206+
return (terminal_blocks, sum(num_pop), total_pops, total_ins_terminal)
202207

203208

204209
def execute_function(origin_file, log_opt_file):
@@ -217,6 +222,9 @@ def execute_function(origin_file, log_opt_file):
217222

218223
all_pops_opt = 0
219224
all_pops_sol = 0
225+
226+
total_ins_terminal_opt = 0
227+
total_ins_terminal_sol = 0
220228

221229
for c in evm_opt:
222230
evm = evm_opt[c]
@@ -226,6 +234,7 @@ def execute_function(origin_file, log_opt_file):
226234
total_terminal+=opt[0]
227235
total_pops+=opt[1]
228236
all_pops_opt+=opt[2]
237+
total_ins_terminal_opt = opt[3]
229238

230239
evm_dict = js.loads(evm_origin)
231240
contracts = evm_dict["contracts"]
@@ -239,10 +248,12 @@ def execute_function(origin_file, log_opt_file):
239248
bytecode = json[c.strip()]["evm"]["bytecode"]["object"]
240249
# print("ORIGINAL")
241250
origin_ins =count_num_ins(bytecode.strip())
251+
242252
total_sol_terminal+=origin_ins[0]
243253
total_sol_pops+=origin_ins[1]
244254
all_pops_sol+=origin_ins[2]
245-
return (total_terminal, total_pops, all_pops_opt, total_sol_terminal, total_sol_pops, all_pops_sol)
255+
total_ins_terminal_sol+=origin_ins[3]
256+
return (total_terminal, total_pops, all_pops_opt, total_sol_terminal, total_sol_pops, all_pops_sol, total_ins_terminal_opt, total_ins_terminal_sol)
246257

247258
if __name__ == '__main__':
248259
origin_file = sys.argv[1]

scripts/sum_instructions.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ def cuartiles(original, optimizado, res):
6262
total_origin_pops = 0
6363
all_pops_origin = 0
6464

65+
total_ins_terminal_sol = 0
66+
total_ins_terminal_opt = 0
67+
6568
for i in range(len(origin_number)):
6669
original = origin_number[i]
6770
optimizado = opt_number[i]
@@ -79,14 +82,16 @@ def cuartiles(original, optimizado, res):
7982

8083
fname_without_ext = fname.rstrip("log")
8184

82-
tsol, pops_sol, allpops, torigin, pops_origin , allpops_orig = compare_blocks.execute_function(fname_without_ext+"output", fname_without_ext+"log")
85+
tsol, pops_sol, allpops, torigin, pops_origin , allpops_orig, inst_opt, inst_sol = compare_blocks.execute_function(fname_without_ext+"output", fname_without_ext+"log")
8386

8487
total_sol_terminal+=tsol
8588
total_sol_pops+=pops_sol
8689
all_pops_sol+= allpops
8790
total_origin_terminal+=torigin
8891
total_origin_pops+=pops_origin
8992
all_pops_origin+=allpops_orig
93+
total_ins_terminal_sol+=inst_sol
94+
total_ins_terminal_opt+=inst_opt
9095

9196
worse_files[fname] = (original, optimizado)
9297
# print("PAREJA: ("+str(original)+","+str(optimizado)+")")
@@ -114,6 +119,9 @@ def cuartiles(original, optimizado, res):
114119

115120
print("TOTAL POPS IN SOLUTION: "+str(all_pops_sol))
116121
print("TOTAL POPS IN ORIGINAL: "+str(all_pops_origin))
122+
123+
print("TOTAL INS TERMINAL BLOCKS IN SOLUTION: "+str(total_ins_terminal_opt))
124+
print("TOTAL INS TERMINAL BLOCKS IN ORIGINAL: "+str(total_ins_terminal_sol))
117125
print()
118126

119127
print(" ===== NUM INSTRUCTIONS STATISTICS ===== ")

src/liveness/stack_layout_methods.py

+15-16
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def remove_backwards_edges(current_node, visited: Set, current_path: Set, cfg:nx
5353

5454

5555
def compute_variable_depth(liveness_info: Dict[str, LivenessAnalysisInfoSSA], topological_order: List) -> Dict[
56-
str, Dict[str, Tuple[int, int]]]:
56+
str, Dict[str, Tuple[int, int, int]]]:
5757
"""
5858
For each variable at every point in the CFG, returns the corresponding depth of the last time a variable was used
5959
and the position being used in the current block (if any). Useful for determining in which order
@@ -63,19 +63,17 @@ def compute_variable_depth(liveness_info: Dict[str, LivenessAnalysisInfoSSA], to
6363
variable_depth_out = dict()
6464
variable_depth_in = dict()
6565
max_depth = len(topological_order) + 1
66+
max_instr_idx = 100
6667

6768
for node in reversed(topological_order):
6869
block_info = liveness_info[node].block_info
6970
instructions = block_info.instructions
70-
max_instr_idx = len(instructions) + 1
7171

7272
current_variable_depth_out = dict()
7373

7474
# Initialize variables in the live_in set to len(topological_order) + 1
75-
for input_variable in liveness_info[node].in_state.live_vars:
76-
current_variable_depth_out[input_variable] = max_depth, max_instr_idx
77-
78-
# Link each variable to the position being used in the instructions
75+
for input_variable in liveness_info[node].out_state.live_vars:
76+
current_variable_depth_out[input_variable] = max_depth, max_instr_idx, max_instr_idx
7977

8078
# For each successor, compute the variable depth information and update the corresponding map
8179
for succ_node in block_info.successors:
@@ -84,27 +82,28 @@ def compute_variable_depth(liveness_info: Dict[str, LivenessAnalysisInfoSSA], to
8482
# as we will visit it later
8583
previous_variable_depth = variable_depth_in.get(succ_node, dict())
8684

87-
for variable, depth in previous_variable_depth.items():
85+
for variable, previous_variable_info in previous_variable_depth.items():
8886
# Update the depth if it already appears in the dict
8987
variable_info = current_variable_depth_out.get(variable, None)
9088
if variable_info is not None:
91-
var_depth, instr_pos = variable_info
9289
# If the depth of the successor variable is less than the one we have actually
93-
if variable_info >= depth:
94-
current_variable_depth_out[variable] = variable_info
90+
if variable_info > previous_variable_info:
91+
current_variable_depth_out[variable] = previous_variable_info[0] + 1, previous_variable_info[1], previous_variable_info[2]
9592
else:
96-
current_variable_depth_out[variable] = depth[0] + 1, max_instr_idx
93+
current_variable_depth_out[variable] = previous_variable_info[0] + 1, previous_variable_info[1], previous_variable_info[2]
9794

95+
# Afterwards, we update the information of the in set
9896
current_variable_depth_in = current_variable_depth_out.copy()
9997

100-
# Finally, we update the corresponding variables that are defined in the blocks
101-
# for used_variable in set(block_info.uses).union(block_info.phi_uses):
102-
# current_variable_depth_out[used_variable] = 0
98+
# Link each variable to the position being used in the instructions
99+
for i, instruction in enumerate(instructions):
100+
for j, in_arg in enumerate(instruction.in_args):
101+
current_variable_depth_in[in_arg] = 0, i, j
103102

104103
variable_depth_out[node] = current_variable_depth_out
105104
variable_depth_in[node] = current_variable_depth_in
106105

107-
return variable_depth_out
106+
return variable_depth_in
108107

109108

110109
def compute_block_level(dominance_tree: nx.DiGraph, start: str) -> Dict[str, int]:
@@ -161,7 +160,7 @@ def output_stack_layout(input_stack: List[str], final_stack_elements: List[str],
161160
vars_to_place = live_vars.difference(set(final_stack_elements + bottom_output_stack))
162161

163162
# Sort the vars to place according to the variable depth info order in reversed order
164-
vars_to_place_sorted = sorted(vars_to_place, key=lambda x: (*variable_depth_info[x], x), reverse=True)
163+
vars_to_place_sorted = sorted(vars_to_place, key=lambda x: (variable_depth_info[x], x), reverse=True)
165164

166165
# Try to place the variables in reversed order
167166
i, j = len(bottom_output_stack) - 1, 0

0 commit comments

Comments
 (0)