Skip to content

[mlir][dataflow] Allow re-run all analyses in DataFlowSolver #120881

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 23, 2024

Conversation

ZenithalHourlyRate
Copy link
Member

In downstream (check google/heir#1228, especially this commit; also check google/heir#1154) we often need to re-run the analysis during the transformation pass as IR get changed based on the analysis result and analysis continuously get invalidated.

There are solutions to it like getOrCreateState for newly created Value (AnchorT), but warning is that the new state does not propagate! This is quite unexpected as user of analysis would expect it to propagate. We downstream used to use solver->propagateIfChanged but that turned out to be not working, see detailed writeup in google/heir#1153.

Just call initializeAndRun repeatedly also does not solve the problem as analysisStates is not cleared and the monotonicity of AnalysisState will make the analysis invalid as join will not work as expected (the first join is no longer join(uninitialized, init value), instead it becomes join(higher value, init value).

To correctly re-run the analysis, either a new DataFlowSolver is created, or we can just clear the analysisState.

Cc @j2kun for downstream. Cc @Mogball for review.

@llvmbot llvmbot added the mlir label Dec 22, 2024
@llvmbot
Copy link
Member

llvmbot commented Dec 22, 2024

@llvm/pr-subscribers-mlir

Author: Hongren Zheng (ZenithalHourlyRate)

Changes

In downstream (check google/heir#1228, especially this commit; also check google/heir#1154) we often need to re-run the analysis during the transformation pass as IR get changed based on the analysis result and analysis continuously get invalidated.

There are solutions to it like getOrCreateState for newly created Value (AnchorT), but warning is that the new state does not propagate! This is quite unexpected as user of analysis would expect it to propagate. We downstream used to use solver->propagateIfChanged but that turned out to be not working, see detailed writeup in google/heir#1153.

Just call initializeAndRun repeatedly also does not solve the problem as analysisStates is not cleared and the monotonicity of AnalysisState will make the analysis invalid as join will not work as expected (the first join is no longer join(uninitialized, init value), instead it becomes join(higher value, init value).

To correctly re-run the analysis, either a new DataFlowSolver is created, or we can just clear the analysisState.

Cc @j2kun for downstream. Cc @Mogball for review.


Full diff: https://github.com/llvm/llvm-project/pull/120881.diff

1 Files Affected:

  • (modified) mlir/include/mlir/Analysis/DataFlowFramework.h (+7)
diff --git a/mlir/include/mlir/Analysis/DataFlowFramework.h b/mlir/include/mlir/Analysis/DataFlowFramework.h
index 969664dc7a4fe3..dfd358e7017a4e 100644
--- a/mlir/include/mlir/Analysis/DataFlowFramework.h
+++ b/mlir/include/mlir/Analysis/DataFlowFramework.h
@@ -308,6 +308,10 @@ class DataFlowConfig {
 ///    according to their dependency relations until a fixed point is reached.
 /// 3. Query analysis state results from the solver.
 ///
+/// Steps to re-run a data-flow analysis when IR changes:
+/// 1. Erase all analysis states as they are no longer valid.
+/// 2. Re-run the analysis using `initializeAndRun`.
+///
 /// TODO: Optimize the internal implementation of the solver.
 class DataFlowSolver {
 public:
@@ -346,6 +350,9 @@ class DataFlowSolver {
     }
   }
 
+  // Erase all analysis states
+  void eraseAllStates() { analysisStates.clear(); }
+
   /// Get a uniqued lattice anchor instance. If one is not present, it is
   /// created with the provided arguments.
   template <typename AnchorT, typename... Args>

Copy link
Contributor

@j2kun j2kun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like a relatively harmless change. LGTM!

@j2kun j2kun merged commit a60050c into llvm:main Dec 23, 2024
10 checks passed
Copy link
Contributor

@Mogball Mogball left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

ZenithalHourlyRate added a commit that referenced this pull request Jan 28, 2025
…lowSolver (#120885)

Detailed writeup is in google/heir#1153. See
also #120881. In short,
`propagateIfChanged` is used outside of the `DataFlowAnalysis` scope,
because it is public, but it does not propagate as expected as the
`DataFlowSolver` has stopped running.

To solve such misuse, `propagateIfChanged` should be made
protected/private.

For downstream users affected by this, to correctly propagate the
change, the Analysis should be re-run (check #120881) instead of just a
`propagateIfChanged`

The change to `IntegerRangeAnalysis` is just a expansion of the
`solver->propagateIfChanged`. The `Lattice` has already been updated by
the `join`. Propagation is done by `onUpdate`.

Cc @Mogball for review
github-actions bot pushed a commit to arm/arm-toolchain that referenced this pull request Jan 28, 2025
…d for DataFlowSolver (#120885)

Detailed writeup is in google/heir#1153. See
also llvm/llvm-project#120881. In short,
`propagateIfChanged` is used outside of the `DataFlowAnalysis` scope,
because it is public, but it does not propagate as expected as the
`DataFlowSolver` has stopped running.

To solve such misuse, `propagateIfChanged` should be made
protected/private.

For downstream users affected by this, to correctly propagate the
change, the Analysis should be re-run (check #120881) instead of just a
`propagateIfChanged`

The change to `IntegerRangeAnalysis` is just a expansion of the
`solver->propagateIfChanged`. The `Lattice` has already been updated by
the `join`. Propagation is done by `onUpdate`.

Cc @Mogball for review
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants