Bases: ABC
Base class for performing data flow analyses over a ControlFlowGraph.
This abstract class implements a generic work-list algorithm that can run either
in a "forward" or "backward" direction. Subclasses must provide IR-specific
semantics by implementing the abstract methods: transfer, join, stmt_str and
direction.
Key attributes
- cfg: the ControlFlowGraph to analyze.
- input_states: a mapping from basic block id to the lattice state that holds
at the entry of the block.
- output_states: a mapping from basic block id to the lattice state that holds
at the exit of the block.
Source code in src/finch/symbolic/dataflow.py
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 | class DataFlowAnalysis(ABC):
"""
Base class for performing data flow analyses over a ControlFlowGraph.
This abstract class implements a generic work-list algorithm that can run either
in a "forward" or "backward" direction. Subclasses must provide IR-specific
semantics by implementing the abstract methods: transfer, join, stmt_str and
direction.
Key attributes
- cfg: the ControlFlowGraph to analyze.
- input_states: a mapping from basic block id to the lattice state that holds
at the entry of the block.
- output_states: a mapping from basic block id to the lattice state that holds
at the exit of the block.
"""
def __init__(self, cfg: ControlFlowGraph):
self.cfg: ControlFlowGraph = cfg
self.input_states: dict[str, dict] = {
block.id: {} for block in cfg.blocks.values()
}
self.output_states: dict[str, dict] = {
block.id: {} for block in cfg.blocks.values()
}
def __str__(self) -> str:
"""
Return a string representation describing the current dataflow
analysis state over the control-flow graph (CFG).
The output lists each basic block in the order
produced by self.cfg.blocks.values().
For every block the printed form is:
<block.id>: #succs=[<succ_id>, ...]
<stmt_representation_for_first_stmt>
<stmt_representation_for_second_stmt>
...
"""
lines: list[str] = []
blocks = list(self.cfg.blocks.values())
for block in blocks:
# get successors of the block
if block.successors:
succ_names = [succ.id for succ in block.successors]
succ_str = f"#succs=[{', '.join(succ_names)}]"
else:
succ_str = "#succs=[]"
lines.append(f"{block.id}: {succ_str}")
# get the input state for this block
input_state = self.input_states.get(block.id, {})
if self.direction() == "forward":
# deep copy the input_state, otherwise original state
# in the input_states will be changed,
# which can lead to errors in future
state = copy.deepcopy(input_state)
# print each statement using subclasses stmt_str method
for stmt in block.statements:
lines.append(f" {self.stmt_str(stmt, state)}")
# advance state with current statement
state = self.transfer([stmt], state)
else:
# TODO: implement "backward" direction printing
lines.extend(
f" {self.stmt_str(stmt, input_state)}"
for stmt in block.statements
)
lines.append("")
return "\n".join(lines)
@abstractmethod
def stmt_str(self, stmt, state: dict) -> str:
"""
Format a single statement given the current lattice state.
Implement in subclasses or IR-specific abstract bases.
"""
...
@abstractmethod
def transfer(self, stmts, state: dict) -> dict:
"""
Transfer function for the data flow analysis.
This should be implemented by subclasses.
"""
...
@abstractmethod
def join(self, state_1: dict, state_2: dict) -> dict:
"""
Join function for the data flow analysis.
This should be implemented by subclasses.
"""
...
@abstractmethod
def direction(self) -> str:
"""
Return the direction of the data flow analysis, either "forward" or "backward".
This should be implemented by subclasses.
"""
...
def analyze(self) -> None:
"""
Perform the data flow analysis on the control flow graph.
This method initializes the work list and processes each block.
"""
work_list: list[BasicBlock] = list(self.cfg.blocks.values())
if self.direction() == "forward":
while work_list:
block = work_list.pop(0)
# get current input state based on the predecessors output states
if not block.predecessors:
input_state = {}
else:
pred_outputs = [
self.output_states.get(pred.id, {})
for pred in block.predecessors
]
input_state = pred_outputs[0].copy()
for pred_output in pred_outputs[1:]:
input_state = self.join(input_state, pred_output)
self.input_states[block.id] = input_state
# perform transfer based on the statements in the current basic block
output_state = self.transfer(block.statements, input_state)
# check if output_state changed
if output_state != self.output_states.get(block.id, {}):
self.output_states[block.id] = output_state
for successor in block.successors:
if successor not in work_list:
work_list.append(successor)
else:
while work_list:
block = work_list.pop(0)
# get current input state based on the successors output states
if not block.successors:
input_state = {}
else:
succ_outputs = [
self.output_states.get(succ.id, {}) for succ in block.successors
]
input_state = succ_outputs[0].copy()
for succ_output in succ_outputs[1:]:
input_state = self.join(input_state, succ_output)
self.input_states[block.id] = input_state
# perform trasnfer based on the statements in the current basic block
output_state = self.transfer(block.statements, input_state)
# check if output state changed
if output_state != self.output_states.get(block.id, {}):
self.output_states[block.id] = output_state
for predecessor in block.predecessors:
if predecessor not in work_list:
work_list.append(predecessor)
|
__str__()
Return a string representation describing the current dataflow
analysis state over the control-flow graph (CFG).
The output lists each basic block in the order
produced by self.cfg.blocks.values().
For every block the printed form is:
<block.id>: #succs=[<succ_id>, ...]
<stmt_representation_for_first_stmt>
<stmt_representation_for_second_stmt>
...
Source code in src/finch/symbolic/dataflow.py
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 | def __str__(self) -> str:
"""
Return a string representation describing the current dataflow
analysis state over the control-flow graph (CFG).
The output lists each basic block in the order
produced by self.cfg.blocks.values().
For every block the printed form is:
<block.id>: #succs=[<succ_id>, ...]
<stmt_representation_for_first_stmt>
<stmt_representation_for_second_stmt>
...
"""
lines: list[str] = []
blocks = list(self.cfg.blocks.values())
for block in blocks:
# get successors of the block
if block.successors:
succ_names = [succ.id for succ in block.successors]
succ_str = f"#succs=[{', '.join(succ_names)}]"
else:
succ_str = "#succs=[]"
lines.append(f"{block.id}: {succ_str}")
# get the input state for this block
input_state = self.input_states.get(block.id, {})
if self.direction() == "forward":
# deep copy the input_state, otherwise original state
# in the input_states will be changed,
# which can lead to errors in future
state = copy.deepcopy(input_state)
# print each statement using subclasses stmt_str method
for stmt in block.statements:
lines.append(f" {self.stmt_str(stmt, state)}")
# advance state with current statement
state = self.transfer([stmt], state)
else:
# TODO: implement "backward" direction printing
lines.extend(
f" {self.stmt_str(stmt, input_state)}"
for stmt in block.statements
)
lines.append("")
return "\n".join(lines)
|
analyze()
Perform the data flow analysis on the control flow graph.
This method initializes the work list and processes each block.
Source code in src/finch/symbolic/dataflow.py
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 | def analyze(self) -> None:
"""
Perform the data flow analysis on the control flow graph.
This method initializes the work list and processes each block.
"""
work_list: list[BasicBlock] = list(self.cfg.blocks.values())
if self.direction() == "forward":
while work_list:
block = work_list.pop(0)
# get current input state based on the predecessors output states
if not block.predecessors:
input_state = {}
else:
pred_outputs = [
self.output_states.get(pred.id, {})
for pred in block.predecessors
]
input_state = pred_outputs[0].copy()
for pred_output in pred_outputs[1:]:
input_state = self.join(input_state, pred_output)
self.input_states[block.id] = input_state
# perform transfer based on the statements in the current basic block
output_state = self.transfer(block.statements, input_state)
# check if output_state changed
if output_state != self.output_states.get(block.id, {}):
self.output_states[block.id] = output_state
for successor in block.successors:
if successor not in work_list:
work_list.append(successor)
else:
while work_list:
block = work_list.pop(0)
# get current input state based on the successors output states
if not block.successors:
input_state = {}
else:
succ_outputs = [
self.output_states.get(succ.id, {}) for succ in block.successors
]
input_state = succ_outputs[0].copy()
for succ_output in succ_outputs[1:]:
input_state = self.join(input_state, succ_output)
self.input_states[block.id] = input_state
# perform trasnfer based on the statements in the current basic block
output_state = self.transfer(block.statements, input_state)
# check if output state changed
if output_state != self.output_states.get(block.id, {}):
self.output_states[block.id] = output_state
for predecessor in block.predecessors:
if predecessor not in work_list:
work_list.append(predecessor)
|
direction()
abstractmethod
Return the direction of the data flow analysis, either "forward" or "backward".
This should be implemented by subclasses.
Source code in src/finch/symbolic/dataflow.py
187
188
189
190
191
192
193 | @abstractmethod
def direction(self) -> str:
"""
Return the direction of the data flow analysis, either "forward" or "backward".
This should be implemented by subclasses.
"""
...
|
join(state_1, state_2)
abstractmethod
Join function for the data flow analysis.
This should be implemented by subclasses.
Source code in src/finch/symbolic/dataflow.py
179
180
181
182
183
184
185 | @abstractmethod
def join(self, state_1: dict, state_2: dict) -> dict:
"""
Join function for the data flow analysis.
This should be implemented by subclasses.
"""
...
|
stmt_str(stmt, state)
abstractmethod
Format a single statement given the current lattice state.
Implement in subclasses or IR-specific abstract bases.
Source code in src/finch/symbolic/dataflow.py
163
164
165
166
167
168
169 | @abstractmethod
def stmt_str(self, stmt, state: dict) -> str:
"""
Format a single statement given the current lattice state.
Implement in subclasses or IR-specific abstract bases.
"""
...
|
transfer(stmts, state)
abstractmethod
Transfer function for the data flow analysis.
This should be implemented by subclasses.
Source code in src/finch/symbolic/dataflow.py
171
172
173
174
175
176
177 | @abstractmethod
def transfer(self, stmts, state: dict) -> dict:
"""
Transfer function for the data flow analysis.
This should be implemented by subclasses.
"""
...
|