[Mlir-commits] [mlir] 2c384c3 - [MLIR][DataFlowAnalysis] Use a queue to maintain the worklist

Vaivaswatha Nagaraj llvmlistbot at llvm.org
Wed Jan 5 20:27:18 PST 2022


Author: Vaivaswatha Nagaraj
Date: 2022-01-06T09:55:22+05:30
New Revision: 2c384c37727660f11f63fda461210d1a6f5d2afe

URL: https://github.com/llvm/llvm-project/commit/2c384c37727660f11f63fda461210d1a6f5d2afe
DIFF: https://github.com/llvm/llvm-project/commit/2c384c37727660f11f63fda461210d1a6f5d2afe.diff

LOG: [MLIR][DataFlowAnalysis] Use a queue to maintain the worklist

Since the analysis is described to be suitable for a forward
data-flow analysis, maintaining the worklist as a queue mimics
RPO ordering of block visits, thus reaching the fixpoint earlier.

Differential Revision: https://reviews.llvm.org/D116393

Added: 
    

Modified: 
    mlir/lib/Analysis/DataFlowAnalysis.cpp
    mlir/lib/Transforms/SCCP.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/lib/Analysis/DataFlowAnalysis.cpp b/mlir/lib/Analysis/DataFlowAnalysis.cpp
index ca17e953e9904..b8e801fea6db8 100644
--- a/mlir/lib/Analysis/DataFlowAnalysis.cpp
+++ b/mlir/lib/Analysis/DataFlowAnalysis.cpp
@@ -12,6 +12,8 @@
 #include "mlir/Interfaces/ControlFlowInterfaces.h"
 #include "llvm/ADT/SmallPtrSet.h"
 
+#include <queue>
+
 using namespace mlir;
 using namespace mlir::detail;
 
@@ -165,7 +167,7 @@ class ForwardDataFlowSolver {
   template <typename ValuesT>
   void markAllPessimisticFixpoint(Operation *op, ValuesT values) {
     markAllPessimisticFixpoint(values);
-    opWorklist.push_back(op);
+    opWorklist.push(op);
   }
   template <typename ValuesT>
   void markAllPessimisticFixpointAndVisitUsers(ValuesT values) {
@@ -195,10 +197,10 @@ class ForwardDataFlowSolver {
   DenseSet<std::pair<Block *, Block *>> executableEdges;
 
   /// A worklist containing blocks that need to be processed.
-  SmallVector<Block *, 64> blockWorklist;
+  std::queue<Block *> blockWorklist;
 
   /// A worklist of operations that need to be processed.
-  SmallVector<Operation *, 64> opWorklist;
+  std::queue<Operation *> opWorklist;
 
   /// The callable operations that have their argument/result state tracked.
   DenseMap<Operation *, CallableLatticeState> callableLatticeState;
@@ -229,12 +231,18 @@ ForwardDataFlowSolver::ForwardDataFlowSolver(
 void ForwardDataFlowSolver::solve() {
   while (!blockWorklist.empty() || !opWorklist.empty()) {
     // Process any operations in the op worklist.
-    while (!opWorklist.empty())
-      visitUsers(*opWorklist.pop_back_val());
+    while (!opWorklist.empty()) {
+      Operation *nextOp = opWorklist.front();
+      opWorklist.pop();
+      visitUsers(*nextOp);
+    }
 
     // Process any blocks in the block worklist.
-    while (!blockWorklist.empty())
-      visitBlock(blockWorklist.pop_back_val());
+    while (!blockWorklist.empty()) {
+      Block *nextBlock = blockWorklist.front();
+      blockWorklist.pop();
+      visitBlock(nextBlock);
+    }
   }
 }
 
@@ -368,7 +376,7 @@ void ForwardDataFlowSolver::visitOperation(Operation *op) {
 
   // Visit the current operation.
   if (analysis.visitOperation(op, operandLattices) == ChangeResult::Change)
-    opWorklist.push_back(op);
+    opWorklist.push(op);
 
   // `visitOperation` is required to define all of the result lattices.
   assert(llvm::none_of(
@@ -477,7 +485,7 @@ void ForwardDataFlowSolver::visitRegionSuccessors(
       // region operation can provide information for certain results that
       // aren't part of the control flow.
       if (succArgs.size() != results.size()) {
-        opWorklist.push_back(parentOp);
+        opWorklist.push(parentOp);
         if (succArgs.empty()) {
           markAllPessimisticFixpoint(results);
           continue;
@@ -713,7 +721,7 @@ ForwardDataFlowSolver::markEntryBlockExecutable(Region *region,
 ChangeResult ForwardDataFlowSolver::markBlockExecutable(Block *block) {
   bool marked = executableBlocks.insert(block).second;
   if (marked)
-    blockWorklist.push_back(block);
+    blockWorklist.push(block);
   return marked ? ChangeResult::Change : ChangeResult::NoChange;
 }
 
@@ -749,7 +757,7 @@ bool ForwardDataFlowSolver::isAtFixpoint(Value value) const {
 void ForwardDataFlowSolver::join(Operation *owner, AbstractLatticeElement &to,
                                  const AbstractLatticeElement &from) {
   if (to.join(from) == ChangeResult::Change)
-    opWorklist.push_back(owner);
+    opWorklist.push(owner);
 }
 
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/lib/Transforms/SCCP.cpp b/mlir/lib/Transforms/SCCP.cpp
index 7a2f9949a7463..11d55e7454a0a 100644
--- a/mlir/lib/Transforms/SCCP.cpp
+++ b/mlir/lib/Transforms/SCCP.cpp
@@ -23,6 +23,9 @@
 #include "mlir/Pass/Pass.h"
 #include "mlir/Transforms/FoldUtils.h"
 #include "mlir/Transforms/Passes.h"
+#include "llvm/Support/Debug.h"
+
+#define DEBUG_TYPE "sccp"
 
 using namespace mlir;
 
@@ -70,6 +73,9 @@ struct SCCPAnalysis : public ForwardDataFlowAnalysis<SCCPLatticeValue> {
   ChangeResult
   visitOperation(Operation *op,
                  ArrayRef<LatticeElement<SCCPLatticeValue> *> operands) final {
+
+    LLVM_DEBUG(llvm::dbgs() << "SCCP: Visiting operation: " << *op << "\n");
+
     // Don't try to simulate the results of a region operation as we can't
     // guarantee that folding will be out-of-place. We don't allow in-place
     // folds as the desire here is for simulated execution, and not general


        


More information about the Mlir-commits mailing list