diff --git a/mlir/include/mlir/Analysis/DataFlowFramework.h b/mlir/include/mlir/Analysis/DataFlowFramework.h index b6d10ba0bea2d..a3714c4332fbb 100644 --- a/mlir/include/mlir/Analysis/DataFlowFramework.h +++ b/mlir/include/mlir/Analysis/DataFlowFramework.h @@ -401,6 +401,8 @@ class DataFlowSolver { /// Propagate an update to an analysis state if it changed by pushing /// dependent work items to the back of the queue. + /// This should only be used when DataFlowSolver is running. + /// Otherwise, the solver won't process the work items. void propagateIfChanged(AnalysisState *state, ChangeResult changed); /// Get the configuration of the solver. @@ -410,6 +412,9 @@ class DataFlowSolver { /// Configuration of the dataflow solver. DataFlowConfig config; + /// The solver is working on the worklist. + bool isRunning = false; + /// The solver's work queue. Work items can be inserted to the front of the /// queue to be processed greedily, speeding up computations that otherwise /// quickly degenerate to quadratic due to propagation of state updates. diff --git a/mlir/lib/Analysis/DataFlowFramework.cpp b/mlir/lib/Analysis/DataFlowFramework.cpp index d2742c6e4b966..028decbae31c3 100644 --- a/mlir/lib/Analysis/DataFlowFramework.cpp +++ b/mlir/lib/Analysis/DataFlowFramework.cpp @@ -10,6 +10,7 @@ #include "mlir/IR/Location.h" #include "mlir/IR/Operation.h" #include "mlir/IR/Value.h" +#include "llvm/ADT/ScopeExit.h" #include "llvm/ADT/iterator.h" #include "llvm/Config/abi-breaking.h" #include "llvm/Support/Casting.h" @@ -104,6 +105,10 @@ Location LatticeAnchor::getLoc() const { //===----------------------------------------------------------------------===// LogicalResult DataFlowSolver::initializeAndRun(Operation *top) { + // Enable enqueue to the worklist. + isRunning = true; + auto guard = llvm::make_scope_exit([&]() { isRunning = false; }); + // Initialize the analyses. for (DataFlowAnalysis &analysis : llvm::make_pointee_range(childAnalyses)) { DATAFLOW_DEBUG(llvm::dbgs() @@ -134,6 +139,8 @@ LogicalResult DataFlowSolver::initializeAndRun(Operation *top) { void DataFlowSolver::propagateIfChanged(AnalysisState *state, ChangeResult changed) { + assert(isRunning && + "DataFlowSolver is not running, should not use propagateIfChanged"); if (changed == ChangeResult::Change) { DATAFLOW_DEBUG(llvm::dbgs() << "Propagating update to " << state->debugName << " of " << state->anchor << "\n"