Index
AssemblyCFGBuilder
¶
Incrementally builds control-flow graph for Finch Assembly IR.
Source code in src/finch/finch_assembly/cfg_builder.py
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 | |
AssemblyCopyPropagation
¶
Bases: AbstractAssemblyDataflow
Copy propagation for FinchAssembly.
Lattice:
- defs: mapping { var_name: sid | None } describing a unique reaching
definition id for each variable (None means "not uniquely defined").
- copies: mapping { dst_var: (src_var, src_def_id) } describing a direct
copy dst_var = src_var that is valid only if src_var still has the
same unique reaching definition src_def_id.
Source code in src/finch/finch_assembly/dataflow.py
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 | |
collect_copy_replacements()
¶
Collect per-statement copy replacements.
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict[tuple[int, str], str]
|
Mapping |
Source code in src/finch/finch_assembly/dataflow.py
direction()
¶
get_lattice_value(state, stmt)
¶
Collect lattice annotations for variables used in a stmt or expr.
Source code in src/finch/finch_assembly/dataflow.py
join(state_1, state_2)
¶
Meet operator for must copy-propagation.
- defs join: keep a def id only if both agree, else None.
- copies join: keep a copy only if both agree exactly, and it remains consistent with the joined defs.
Source code in src/finch/finch_assembly/dataflow.py
transfer(stmts, state)
¶
Transfer function over a sequence of statements.
Applies copy-propagation effects of each statement in order, returning the updated lattice mapping. Only copies of variables are recorded; any existing mappings that point to a variable being reassigned are invalidated first.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stmts
|
Iterable[Any]
|
Iterable of Assembly statements in a basic block. |
required |
state
|
dict
|
Incoming lattice mapping. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
The outgoing lattice mapping after processing |
Source code in src/finch/finch_assembly/dataflow.py
AssemblyExpression
¶
Bases: AssemblyNode, ExpressionTerm
Assembly AST expression base class.
An assembly expression is a program node which evaluates to a value.
Source code in src/finch/finch_assembly/nodes.py
result_type
abstractmethod
property
¶
Returns the type of the expression.
AssemblyInterpreter
¶
Bases: UnvalidatedForm, AssemblyLoader
An interpreter for FinchAssembly.
Source code in src/finch/finch_assembly/interpreter.py
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 | |
__call__(prgm, *, mode=None)
¶
scope(bindings=None, slots=None, types=None, loop_state=None, function_state=None, stdout=None, mode=None)
¶
Create a new scope for the interpreter. This allows for nested scopes and variable shadowing.
Source code in src/finch/finch_assembly/interpreter.py
should_halt()
¶
Check if the interpreter should halt execution. This is used to stop execution in loops or when a return statement is encountered.
Source code in src/finch/finch_assembly/interpreter.py
AssemblyInterpreterKernel
¶
Bases: AssemblyKernel
A kernel for interpreting FinchAssembly code. This is a simple interpreter that executes the assembly code.
Source code in src/finch/finch_assembly/interpreter.py
AssemblyKernel
¶
A callable implementation of an AssemblyKernelFType.
Source code in src/finch/finch_assembly/stages.py
AssemblyKernelFType
dataclass
¶
Bases: CallableFType
The identity and signature of a function defined in a Finch module.
Source code in src/finch/finch_assembly/stages.py
AssemblyLibrary
¶
Bases: ABC
Represents a module containing assembly kernels.
Source code in src/finch/finch_assembly/stages.py
AssemblyLoader
¶
Bases: Stage
Source code in src/finch/finch_assembly/stages.py
lower(term, *, mode=None)
abstractmethod
¶
AssemblyNode
¶
Bases: Term
AssemblyNode
Represents a FinchAssembly IR node. FinchAssembly is the final intermediate representation before code generation (translation to the output language). It is a low-level imperative description of the program, with control flow, linear memory regions called "buffers", and explicit memory management.
Source code in src/finch/finch_assembly/nodes.py
AssemblyReachingDefinitions
¶
Bases: AbstractAssemblyDataflow
reaching definitions, see the module docstring. a state maps variable names to frozensets of statement ids.
Source code in src/finch/finch_assembly/reaching_definitions.py
def_use_chains()
¶
map each definition to the statements whose reads it reaches
Source code in src/finch/finch_assembly/reaching_definitions.py
use_def_chains()
¶
map (statement id, variable it reads) to the definitions reaching it
Source code in src/finch/finch_assembly/reaching_definitions.py
AssemblyTransform
¶
Bases: Stage
Source code in src/finch/finch_assembly/stages.py
AssemblyTypeChecker
¶
A type checker for FinchAssembly
Source code in src/finch/finch_assembly/type_checker.py
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 | |
Assert
dataclass
¶
Bases: AssemblyTree, AssemblyStatement
Checks that an expression is true at runtime. Also used in dataflow analysis to record conditions in conditionals and loops.
Attributes:
| Name | Type | Description |
|---|---|---|
exp |
AssemblyExpression
|
Expression which is being asserted. |
Source code in src/finch/finch_assembly/nodes.py
children
property
¶
Returns the children of the node.
Assign
dataclass
¶
Bases: AssemblyTree, AssemblyStatement
Represents a logical AST statement that evaluates rhs, binding the result
to lhs.
Attributes:
| Name | Type | Description |
|---|---|---|
lhs |
Variable
|
The left-hand side of the binding. |
rhs |
AssemblyExpression
|
The right-hand side to evaluate. |
Source code in src/finch/finch_assembly/nodes.py
children
property
¶
Returns the children of the node.
Block
dataclass
¶
Bases: AssemblyTree, AssemblyStatement
Represents a statement that executes a sequence of statements bodies....
Attributes:
| Name | Type | Description |
|---|---|---|
bodies |
tuple[AssemblyStatement | AssemblyExpression, ...]
|
The sequence of statements to execute. |
Source code in src/finch/finch_assembly/nodes.py
children
property
¶
Returns the children of the node.
Break
dataclass
¶
Bases: AssemblyTree, AssemblyStatement
Represents a break statement that exits the current loop.
Source code in src/finch/finch_assembly/nodes.py
children
property
¶
Returns the children of the node.
Buffer
¶
Bases: FTyped[BufferFType[FT]], ABC
Abstract base class for buffer-like data structures. Buffers support random access, and can be resized. They are used to store data in a way that allows for efficient reading and writing of elements.
Source code in src/finch/finch_assembly/buffer.py
element_type
property
¶
Return the type of elements stored in the buffer. This is typically the same as the dtype used to create the buffer.
length_type
property
¶
Return the type of indices used to access elements in the buffer. This is typically an integer type.
owner
property
¶
Object retaining the storage, which may change on resize.
Raw pointers must be reacquired after resize. Borrowing implementations retain the previous allocation for existing views when they detach.
length()
abstractmethod
¶
resize(len)
abstractmethod
¶
to_numpy(*, copy=None)
abstractmethod
¶
Return logical values as a NumPy array.
copy=True returns independent storage, copy=False requires shared
storage, and copy=None shares storage when supported and copies otherwise.
Source code in src/finch/finch_assembly/buffer.py
BufferFType
¶
Abstract base class for the ftype of arguments. The ftype defines how the data structures store data, and can construct a data structure with the call method.
Source code in src/finch/finch_assembly/buffer.py
BufferLoop
dataclass
¶
Bases: AssemblyTree, AssemblyStatement
Represents a loop that iterates over the elements of a buffer.
Attributes:
| Name | Type | Description |
|---|---|---|
buffer |
AssemblyExpression
|
The buffer to iterate over. |
var |
Variable
|
The loop variable for each element in the buffer. |
body |
AssemblyStatement
|
The body of the loop to execute for each element. |
Source code in src/finch/finch_assembly/nodes.py
children
property
¶
Returns the children of the node.
Call
dataclass
¶
Bases: AssemblyExpression, AssemblyTree, CallTerm
Represents an expression for calling the function op on args....
Attributes:
| Name | Type | Description |
|---|---|---|
op |
AssemblyExpression
|
The function to call. |
args |
tuple[AssemblyExpression, ...]
|
The arguments to call on the function. |
Source code in src/finch/finch_assembly/nodes.py
ForLoop
dataclass
¶
Bases: AssemblyTree, AssemblyStatement
Represents a for loop that iterates over a range of values.
Attributes:
| Name | Type | Description |
|---|---|---|
var |
Variable
|
The loop variable. |
start |
AssemblyExpression
|
The starting value of the range. |
end |
AssemblyExpression
|
The ending value of the range. |
body |
AssemblyStatement
|
The body of the loop to execute. |
Source code in src/finch/finch_assembly/nodes.py
children
property
¶
Returns the children of the node.
Function
dataclass
¶
Bases: AssemblyTree
A module-level function definition with arguments args....
Attributes:
| Name | Type | Description |
|---|---|---|
name |
Variable
|
The function variable, annotated with its AssemblyKernelFType. |
args |
tuple[Variable, ...]
|
The arguments to the function. |
body |
AssemblyStatement
|
The body of the function. If it does not contain a return statement,
the function returns the value of |
Source code in src/finch/finch_assembly/nodes.py
GetAttr
dataclass
¶
Bases: AssemblyExpression, AssemblyTree
Represents a getter for an attribute attr of an object obj.
Attributes:
obj: The object to get the attribute from.
attr: The name of the attribute to get.
Source code in src/finch/finch_assembly/nodes.py
If
dataclass
¶
Bases: AssemblyTree, AssemblyStatement
Represents an if statement that executes the body if the condition is true.
Attributes:
| Name | Type | Description |
|---|---|---|
condition |
AssemblyExpression
|
The condition to evaluate for the if to execute the body. |
body |
AssemblyStatement
|
The body of the if statement to execute. |
Source code in src/finch/finch_assembly/nodes.py
children
property
¶
Returns the children of the node.
IfElse
dataclass
¶
Bases: AssemblyTree, AssemblyStatement
Represents an if-else statement that executes the body if the condition is true, otherwise executes else_body.
Attributes:
| Name | Type | Description |
|---|---|---|
condition |
AssemblyExpression
|
The condition to evaluate for the if to execute the body. |
body |
AssemblyStatement
|
The body of the if statement to execute. |
else_body |
AssemblyStatement
|
An alternative body to execute if the condition is false. |
Source code in src/finch/finch_assembly/nodes.py
children
property
¶
Returns the children of the node.
Length
dataclass
¶
Bases: AssemblyExpression, AssemblyTree
Represents getting the length of a buffer.
Attributes:
| Name | Type | Description |
|---|---|---|
buffer |
AssemblyExpression
|
The buffer whose length is queried. |
Source code in src/finch/finch_assembly/nodes.py
result_type
property
¶
Returns the type of the expression.
Literal
dataclass
¶
Bases: AssemblyExpression, LiteralTerm[T]
Represents the literal value val.
Attributes:
| Name | Type | Description |
|---|---|---|
val |
T
|
The literal value. |
Source code in src/finch/finch_assembly/nodes.py
result_type
property
¶
Returns the type of the expression.
Load
dataclass
¶
Bases: AssemblyExpression, AssemblyTree
Represents loading a value from a buffer at a given index.
Attributes:
| Name | Type | Description |
|---|---|---|
buffer |
AssemblyExpression
|
The buffer to load from. |
index |
AssemblyExpression
|
The index to load at. |
Source code in src/finch/finch_assembly/nodes.py
result_type
property
¶
Returns the type of the expression.
Module
dataclass
¶
Bases: AssemblyTree
Represents a group of functions. This is the toplevel translation unit for FinchAssembly.
Attributes:
| Name | Type | Description |
|---|---|---|
funcs |
tuple[Function, ...]
|
The functions defined in the module. |
Source code in src/finch/finch_assembly/nodes.py
children
property
¶
Returns the children of the node.
NumberedStatement
dataclass
¶
Bases: AssemblyStatement
Wrapper for AssemblyStatement that assigns a unique id to each statement for easier tracking in the CFG.
Attributes:
| Name | Type | Description |
|---|---|---|
stmt |
AssemblyStatement
|
The original AssemblyStatement being wrapped. |
sid |
int
|
A unique integer identifier for the statement. |
Source code in src/finch/finch_assembly/cfg_builder.py
Repack
dataclass
¶
Bases: AssemblyTree, AssemblyStatement
Registers updates from a symbolic object val with the original
object. The original object may now be accessed and modified.
Attributes:
| Name | Type | Description |
|---|---|---|
slot |
The symbolic object to read from. |
Source code in src/finch/finch_assembly/nodes.py
children
property
¶
Returns the children of the node.
Resize
dataclass
¶
Bases: AssemblyTree, AssemblyStatement
Represents resizing a buffer to a new size.
Attributes:
| Name | Type | Description |
|---|---|---|
buffer |
AssemblyExpression
|
The buffer to resize. |
new_size |
AssemblyExpression
|
The new size for the buffer. |
Source code in src/finch/finch_assembly/nodes.py
Return
dataclass
¶
Bases: AssemblyTree, AssemblyStatement
Represents a return statement that returns arg from the current function.
Halts execution of the function body.
Attributes:
| Name | Type | Description |
|---|---|---|
arg |
AssemblyExpression
|
The argument to return. |
Source code in src/finch/finch_assembly/nodes.py
children
property
¶
Returns the children of the node.
SetAttr
dataclass
¶
Bases: AssemblyTree, AssemblyStatement
Represents a setter for an attribute attr of an object obj.
Attributes:
obj: The object to set the attribute on.
attr: The name of the attribute to set.
value: The value to set the attribute to.
Source code in src/finch/finch_assembly/nodes.py
children
property
¶
Returns the children of the node.
Slot
dataclass
¶
Bases: AssemblyExpression
Represents a register to a symbolic object. Using a register in an expression creates a copy of the object.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
The name of the symbolic object to register. |
type |
Any
|
The type of the symbolic object. |
Source code in src/finch/finch_assembly/nodes.py
result_type
property
¶
Returns the type of the expression.
Store
dataclass
¶
Bases: AssemblyTree, AssemblyStatement
Represents storing a value into a buffer at a given index.
Attributes:
| Name | Type | Description |
|---|---|---|
buffer |
AssemblyExpression
|
The buffer to store into. |
index |
AssemblyExpression
|
The index to store at. |
value |
AssemblyExpression
|
The value to store. |
Source code in src/finch/finch_assembly/nodes.py
Unpack
dataclass
¶
Bases: AssemblyTree, AssemblyStatement
Attempts to convert rhs into a symbolic, which can be registerd with
lhs. The original object must not be accessed or modified until the
corresponding Repack node is reached.
Attributes:
| Name | Type | Description |
|---|---|---|
lhs |
Slot
|
The symbolic object to write to. |
rhs |
AssemblyExpression
|
The original object to read from. |
Source code in src/finch/finch_assembly/nodes.py
children
property
¶
Returns the children of the node.
Variable
dataclass
¶
Bases: AssemblyExpression, NamedTerm
Represents a logical AST expression for a variable named name, which
will hold a value of type type.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
The name of the variable. |
type |
FType
|
The type of the variable. |
Source code in src/finch/finch_assembly/nodes.py
result_type
property
¶
Returns the type of the expression.
WhileLoop
dataclass
¶
Bases: AssemblyTree, AssemblyStatement
Represents a while loop that executes as long as the condition is true.
Attributes:
| Name | Type | Description |
|---|---|---|
condition |
AssemblyExpression
|
The condition to evaluate for the loop to continue. |
body |
AssemblyStatement
|
The body of the loop to execute. |
Source code in src/finch/finch_assembly/nodes.py
children
property
¶
Returns the children of the node.
assembly_copy_propagation(node)
¶
Apply copy-propagation to a FinchAssembly node. Args: node: Root FinchAssembly node to optimize. Returns: AssemblyNode: The optimized FinchAssembly node.
Source code in src/finch/finch_assembly/dataflow.py
assembly_dataflow_analyze(node, analysis_cls)
¶
Run preprocessing + CFG build + analysis for a dataflow pass.
Returns:
| Type | Description |
|---|---|
tuple[AnalysisT, AssemblyNode]
|
(analysis_ctx, preprocessed_node) |
Source code in src/finch/finch_assembly/dataflow.py
assembly_dataflow_run(node, analysis_cls, apply)
¶
Run a full dataflow pass (preprocess -> analyze -> apply -> postprocess).
Source code in src/finch/finch_assembly/dataflow.py
length_type(arg)
¶
The length type of the given argument. The length type is the type of the value returned by len(arg).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arg
|
FTyped | FType
|
The object to determine the length type for. |
required |
Returns:
| Type | Description |
|---|---|
FDTypeInteger
|
The length type of the given object. |
Raises:
| Type | Description |
|---|---|
AttributeError
|
If the length type is not implemented for the given type. |
Source code in src/finch/finch_assembly/buffer.py
parse_assembly(code, vars, position_type=np.intp)
¶
Parse Finch Assembly code and convert it to assembly node objects.
Takes a string containing Finch Assembly code and transforms it into a structured representation using assembly nodes. The parser supports assignments, increments, for/while loops, if/if-else statements, buffer and field accesses, and calls.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
code
|
str
|
The Finch Assembly code to parse. Should start with "finch" or "finch-asm" followed by assembly statements. Comments (C/C++ style) are supported. |
required |
vars
|
dict[str, AssemblyExpression]
|
Dictionary mapping names to Assembly expressions. Used to resolve variable references in the assembly code. |
required |
position_type
|
Callable[[Any], Any]
|
NumPy integer type to use for integer literals. Affects the dtype of parsed integer constants. (default: np.intp) |
intp
|
Returns:
| Type | Description |
|---|---|
Block
|
A Finch Assembly Block representing the parsed code. |
Raises:
| Type | Description |
|---|---|
Exception
|
If the parser encounters unrecognized syntax or tree nodes. |
Example
from finch.finch_assembly import nodes as asm vars = {"i": asm.Variable("i", int), "arr": asm.Variable("arr", np.ndarray)} code = '''finch ... arr[i] = 42 ... ''' stmt = parse_assembly(code, vars)
Source code in src/finch/finch_assembly/parser.py
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 | |