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
initial_value() -> Self
classmethod
¶
Returns an initial lattice value, typically the bottom (⊥) or uninitialized state of the lattice.
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
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
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
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
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
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
use_def_subscribers: set[DataFlowAnalysis] = set()
instance-attribute
¶
__init__(anchor: SSAValue)
¶
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
use_def_subscribe(analysis: DataFlowAnalysis) -> None
¶
Subscribe an analysis to be re-invoked on all users of this value whenever this lattice state changes.
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
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])
¶
initialize(op: Operation) -> None
¶
Source code in xdsl/analysis/sparse_analysis.py
visit(point: ProgramPoint) -> None
¶
Source code in xdsl/analysis/sparse_analysis.py
visit_operation(op: Operation) -> None
¶
Transfer function for an operation's results.
Source code in xdsl/analysis/sparse_analysis.py
visit_block(block: Block) -> None
¶
Transfer function for a block's arguments.
Source code in xdsl/analysis/sparse_analysis.py
join(lhs: PropagatingLatticeInvT, rhs: PropagatingLatticeInvT) -> None
¶
Joins the rhs lattice into the lhs and propagates if changed.
get_lattice_element(value: SSAValue) -> PropagatingLatticeInvT
¶
get_lattice_element_for(point: ProgramPoint, value: SSAValue) -> PropagatingLatticeInvT
¶
set_all_to_entry_state(lattices: list[PropagatingLatticeInvT]) -> None
¶
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
set_to_entry_state(lattice: PropagatingLatticeInvT) -> None
abstractmethod
¶
SparseBackwardDataFlowAnalysis
¶
Bases: DataFlowAnalysis, ABC, Generic[PropagatingLatticeInvT]
Base class for sparse backward data-flow analyses. It propagates lattices attached to SSA values against the direction of data flow: information placed on an operation's results is meet-combined into its operands.
Source code in xdsl/analysis/sparse_analysis.py
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 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 | |
lattice_type = lattice_type
instance-attribute
¶
__init__(solver: DataFlowSolver, lattice_type: type[PropagatingLatticeInvT])
¶
initialize(op: Operation) -> None
¶
Source code in xdsl/analysis/sparse_analysis.py
visit(point: ProgramPoint) -> None
¶
visit_operation(op: Operation) -> None
¶
Transfer function reading result lattices, writing operand lattices.
Source code in xdsl/analysis/sparse_analysis.py
meet(lhs: PropagatingLatticeInvT, rhs: PropagatingLatticeInvT) -> None
¶
Meets the rhs lattice into the lhs and propagates if changed.
get_lattice_element(value: SSAValue) -> PropagatingLatticeInvT
¶
get_lattice_element_for(point: ProgramPoint, value: SSAValue) -> PropagatingLatticeInvT
¶
set_all_to_exit_states(lattices: list[PropagatingLatticeInvT]) -> None
¶
visit_operation_impl(op: Operation, operand_lattices: list[PropagatingLatticeInvT], result_lattices: list[PropagatingLatticeInvT]) -> None
abstractmethod
¶
User-defined transfer function. Should read result_lattices and use
meet to push information into operand_lattices.
Source code in xdsl/analysis/sparse_analysis.py
set_to_exit_state(lattice: PropagatingLatticeInvT) -> None
abstractmethod
¶
Sets a lattice to its most pessimistic (exit) state. Called for values whose backward propagation reaches an external boundary, e.g. operands of public function returns or arguments of public function entry blocks — for those, the analysis cannot assume any structure and must conservatively widen.