Sparse analysis
sparse_analysis
The sparse analysis module provides data flow analysis utilities for sparse lattices.
For more information on lattices, refer to this Wikipedia article.
AbstractLatticeValueInvT = TypeVar('AbstractLatticeValueInvT', bound=AbstractLatticeValue)
module-attribute
PropagatingLatticeInvT = TypeVar('PropagatingLatticeInvT', bound=PropagatingLattice)
module-attribute
AbstractLatticeValue
Bases: Protocol
Protocol for the mathematical lattice value types used within Lattice wrappers. A lattice is a mathematical structure with a partial ordering and two operations:
- join (∨): computes the least upper bound (union of information)
- meet (∧): computes the greatest lower bound (intersection of information)
Classes implementing this protocol should provide implementations for the meet
and/or join methods. The class should also define the classmethod initial_value
that takes no additional arguments and returns an initial lattice value.
This protocol represents the actual lattice element (the abstract value being tracked), separate from the propagation infrastructure. For example:
- In constant propagation: the lattice value might be
⊥ (bottom) | Constant(n) | ⊤ (top) - In sign analysis: the lattice value might be
Positive | Negative | Zero | Unknown - In range analysis: the lattice value might be
Interval(min, max)
Source code in xdsl/analysis/sparse_analysis.py
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 | |
initial_value() -> Self
classmethod
Returns an initial lattice value, typically the bottom (⊥) or uninitialized state of the lattice.
Source code in xdsl/analysis/sparse_analysis.py
46 47 48 49 50 51 52 | |
meet(other: Self) -> Self
Computes the greatest lower bound (intersection of information) of two lattice values.
a.meet(b) (or a ∧ b) produces the most precise value that is less than or equal
to both a and b in the lattice ordering. It represents the combination of two
abstract values where we keep only information guaranteed to hold in both.
In other words, meet refines information by taking their common part.
Examples:
- In constant propagation:
Constant(3) ∧ Constant(3) = Constant(3), butConstant(3) ∧ Constant(4) = ⊥ (bottom) - In sign analysis:
Positive ∧ NonZero = Positive - In range analysis:
[0, 10] ∧ [5, 15] = [5, 10]
Source code in xdsl/analysis/sparse_analysis.py
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | |
join(other: Self) -> Self
Computes the least upper bound (union of information) of two lattice values.
a.join(b) (or a ∨ b) produces the least precise value that is greater than or
equal to both a and b in the lattice ordering. It represents the merging of
two abstract values where we keep any information that could hold in either.
In other words, join generalizes information by taking their union.
Examples:
- In constant propagation:
Constant(3) ∨ Constant(4) = ⊤ (top) - In sign analysis:
Positive ∨ Negative = Unknown - In range analysis:
[0, 10] ∨ [5, 15] = [0, 15]
Source code in xdsl/analysis/sparse_analysis.py
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | |
AbstractSparseLattice
Bases: Protocol
Protocol for sparse lattice elements used in data flow analysis.
See AbstractLatticeValue for more information about lattices and their operations.
In contrast to AbstractLatticeValue,
the meet and join methods in this protocol are required to return a ChangeResult,
signaling whether the lattice element has changed.
Source code in xdsl/analysis/sparse_analysis.py
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 | |
join(other: Self) -> ChangeResult
Join two lattice elements. Returns ChangeResult.CHANGE if the lattice element has changed,
otherwise ChangeResult.NO_CHANGE.
For more information about the join operation, see
AbstractLatticeValue.join.
Source code in xdsl/analysis/sparse_analysis.py
104 105 106 107 108 109 110 111 112 | |
meet(other: Self) -> ChangeResult
Meet two lattice elements. Returns ChangeResult.CHANGE if the lattice element has changed,
otherwise ChangeResult.NO_CHANGE.
For more information about the meet operation, see
AbstractLatticeValue.meet.
Source code in xdsl/analysis/sparse_analysis.py
114 115 116 117 118 119 120 121 122 | |
PropagatingLattice
Bases: AnalysisState[SSAValue], AbstractSparseLattice, ABC
Base class for sparse lattice elements attached to SSA values.
This class implements the infrastructure for propagating lattice changes through the data flow analysis framework. When a lattice element changes (e.g., a value becomes a known constant), this class ensures that:
- All operations that use this SSA value are re-analyzed
- Subscribed analyses are notified of the change
- The solver's work queue is updated appropriately
The propagation follows use-def chains: when a lattice attached to an SSA value changes, all operations that use that value are marked for re-visiting by any analyses that have subscribed to this lattice.
Subclasses must implement the lattice operations (join/meet) and can override on_update() to customize propagation behavior beyond simple use-def chains.
For the concept of lattices in data flow analysis, see
PropagatingLattice.
Use this as a base class when you need custom propagation logic (e.g., tracking equivalence classes, pointer aliases, or context-sensitive information). For simple cases, use the Lattice wrapper instead.
Source code in xdsl/analysis/sparse_analysis.py
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 | |
use_def_subscribers: set[DataFlowAnalysis] = set()
instance-attribute
__init__(anchor: SSAValue)
Source code in xdsl/analysis/sparse_analysis.py
154 155 156 | |
on_update(solver: DataFlowSolver) -> None
When a sparse lattice changes, we propagate the change to explicit dependents and also to all users of the underlying SSA value for subscribed analyses.
Source code in xdsl/analysis/sparse_analysis.py
158 159 160 161 162 163 164 165 166 167 168 169 170 | |
use_def_subscribe(analysis: DataFlowAnalysis) -> None
Subscribe an analysis to be re-invoked on all users of this value whenever this lattice state changes.
Source code in xdsl/analysis/sparse_analysis.py
172 173 174 175 176 177 | |
Lattice
Bases: PropagatingLattice, Generic[AbstractLatticeValueInvT]
Generic wrapper that combines a lattice value with sparse propagation infrastructure.
See AbstractLatticeValue for more information around
the meet and join operations that need to be implemented.
If you need meet/join functionality that does not match with what Lattice provides,
consider using PropagatingLattice directly.
Source code in xdsl/analysis/sparse_analysis.py
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 | |
value_cls: type[AbstractLatticeValueInvT]
instance-attribute
value: AbstractLatticeValueInvT
property
__init__(anchor: SSAValue, value: AbstractLatticeValueInvT | None = None)
Source code in xdsl/analysis/sparse_analysis.py
199 200 201 202 203 204 205 206 207 208 | |
meet(other: Self) -> ChangeResult
Source code in xdsl/analysis/sparse_analysis.py
214 215 216 217 218 219 220 221 | |
join(other: Self) -> ChangeResult
Source code in xdsl/analysis/sparse_analysis.py
223 224 225 226 227 228 229 230 | |
__str__() -> str
Source code in xdsl/analysis/sparse_analysis.py
232 233 | |
SparseForwardDataFlowAnalysis
Bases: DataFlowAnalysis, ABC, Generic[PropagatingLatticeInvT]
Base class for sparse forward data-flow analyses. It propagates lattices attached to SSA values along the direction of data flow.
Source code in xdsl/analysis/sparse_analysis.py
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 | |
lattice_type = lattice_type
instance-attribute
__init__(solver: DataFlowSolver, lattice_type: type[PropagatingLatticeInvT])
Source code in xdsl/analysis/sparse_analysis.py
247 248 249 250 251 | |
initialize(op: Operation) -> None
Source code in xdsl/analysis/sparse_analysis.py
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | |
visit(point: ProgramPoint) -> None
Source code in xdsl/analysis/sparse_analysis.py
278 279 280 281 282 283 284 | |
visit_operation(op: Operation) -> None
Transfer function for an operation's results.
Source code in xdsl/analysis/sparse_analysis.py
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 | |
visit_block(block: Block) -> None
Transfer function for a block's arguments.
Source code in xdsl/analysis/sparse_analysis.py
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 | |
join(lhs: PropagatingLatticeInvT, rhs: PropagatingLatticeInvT) -> None
Joins the rhs lattice into the lhs and propagates if changed.
Source code in xdsl/analysis/sparse_analysis.py
353 354 355 | |
get_lattice_element(value: SSAValue) -> PropagatingLatticeInvT
Source code in xdsl/analysis/sparse_analysis.py
357 358 | |
get_lattice_element_for(point: ProgramPoint, value: SSAValue) -> PropagatingLatticeInvT
Source code in xdsl/analysis/sparse_analysis.py
360 361 362 363 364 365 | |
set_all_to_entry_state(lattices: list[PropagatingLatticeInvT]) -> None
Source code in xdsl/analysis/sparse_analysis.py
367 368 369 | |
visit_operation_impl(op: Operation, operands: list[PropagatingLatticeInvT], results: list[PropagatingLatticeInvT]) -> None
abstractmethod
The user-defined transfer function for a generic operation.
Source code in xdsl/analysis/sparse_analysis.py
371 372 373 374 375 376 377 378 379 | |
set_to_entry_state(lattice: PropagatingLatticeInvT) -> None
abstractmethod
Sets a lattice to its most pessimistic (entry) state.
Source code in xdsl/analysis/sparse_analysis.py
381 382 383 384 | |