MLIR  20.0.0git
DataFlowFramework.cpp
Go to the documentation of this file.
1 //===- DataFlowFramework.cpp - A generic framework for data-flow analysis -===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
10 #include "mlir/IR/Location.h"
11 #include "mlir/IR/Operation.h"
12 #include "mlir/IR/Value.h"
13 #include "llvm/ADT/iterator.h"
14 #include "llvm/Config/abi-breaking.h"
15 #include "llvm/Support/Casting.h"
16 #include "llvm/Support/Debug.h"
17 #include "llvm/Support/raw_ostream.h"
18 
19 #define DEBUG_TYPE "dataflow"
20 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
21 #define DATAFLOW_DEBUG(X) LLVM_DEBUG(X)
22 #else
23 #define DATAFLOW_DEBUG(X)
24 #endif // LLVM_ENABLE_ABI_BREAKING_CHECKS
25 
26 using namespace mlir;
27 
28 //===----------------------------------------------------------------------===//
29 // GenericLatticeAnchor
30 //===----------------------------------------------------------------------===//
31 
33 
34 //===----------------------------------------------------------------------===//
35 // AnalysisState
36 //===----------------------------------------------------------------------===//
37 
39 
41  DataFlowAnalysis *analysis) {
42  auto inserted = dependents.insert({dependent, analysis});
43  (void)inserted;
45  if (inserted) {
46  llvm::dbgs() << "Creating dependency between " << debugName << " of "
47  << anchor << "\nand " << debugName << " on " << dependent
48  << "\n";
49  }
50  });
51 }
52 
53 void AnalysisState::dump() const { print(llvm::errs()); }
54 
55 //===----------------------------------------------------------------------===//
56 // ProgramPoint
57 //===----------------------------------------------------------------------===//
58 
59 void ProgramPoint::print(raw_ostream &os) const {
60  if (isNull()) {
61  os << "<NULL POINT>";
62  return;
63  }
64  if (!isBlockStart()) {
65  os << "<after operation>:";
66  return getPrevOp()->print(os, OpPrintingFlags().skipRegions());
67  }
68  os << "<before operation>:";
69  return getNextOp()->print(os, OpPrintingFlags().skipRegions());
70 }
71 
72 //===----------------------------------------------------------------------===//
73 // LatticeAnchor
74 //===----------------------------------------------------------------------===//
75 
76 void LatticeAnchor::print(raw_ostream &os) const {
77  if (isNull()) {
78  os << "<NULL POINT>";
79  return;
80  }
81  if (auto *LatticeAnchor = llvm::dyn_cast<GenericLatticeAnchor *>(*this))
82  return LatticeAnchor->print(os);
83  if (auto value = llvm::dyn_cast<Value>(*this)) {
84  return value.print(os, OpPrintingFlags().skipRegions());
85  }
86 
87  return get<ProgramPoint *>()->print(os);
88 }
89 
91  if (auto *LatticeAnchor = llvm::dyn_cast<GenericLatticeAnchor *>(*this))
92  return LatticeAnchor->getLoc();
93  if (auto value = llvm::dyn_cast<Value>(*this))
94  return value.getLoc();
95 
96  ProgramPoint *pp = get<ProgramPoint *>();
97  if (!pp->isBlockStart())
98  return pp->getPrevOp()->getLoc();
99  return pp->getBlock()->getParent()->getLoc();
100 }
101 
102 //===----------------------------------------------------------------------===//
103 // DataFlowSolver
104 //===----------------------------------------------------------------------===//
105 
107  // Initialize the analyses.
108  for (DataFlowAnalysis &analysis : llvm::make_pointee_range(childAnalyses)) {
109  DATAFLOW_DEBUG(llvm::dbgs()
110  << "Priming analysis: " << analysis.debugName << "\n");
111  if (failed(analysis.initialize(top)))
112  return failure();
113  }
114 
115  // Run the analysis until fixpoint.
116  do {
117  // Exhaust the worklist.
118  while (!worklist.empty()) {
119  auto [point, analysis] = worklist.front();
120  worklist.pop();
121 
122  DATAFLOW_DEBUG(llvm::dbgs() << "Invoking '" << analysis->debugName
123  << "' on: " << point << "\n");
124  if (failed(analysis->visit(point)))
125  return failure();
126  }
127 
128  // Iterate until all states are in some initialized state and the worklist
129  // is exhausted.
130  } while (!worklist.empty());
131 
132  return success();
133 }
134 
136  ChangeResult changed) {
137  if (changed == ChangeResult::Change) {
138  DATAFLOW_DEBUG(llvm::dbgs() << "Propagating update to " << state->debugName
139  << " of " << state->anchor << "\n"
140  << "Value: " << *state << "\n");
141  state->onUpdate(this);
142  }
143 }
144 
145 //===----------------------------------------------------------------------===//
146 // DataFlowAnalysis
147 //===----------------------------------------------------------------------===//
148 
150 
152 
154  ProgramPoint *point) {
155  state->addDependency(point, this);
156 }
157 
159  ChangeResult changed) {
160  solver.propagateIfChanged(state, changed);
161 }
#define DATAFLOW_DEBUG(X)
Base class for generic analysis states.
LLVM_DUMP_METHOD void dump() const
void addDependency(ProgramPoint *point, DataFlowAnalysis *analysis)
Add a dependency to this analysis state on a lattice anchor and an analysis.
virtual void print(raw_ostream &os) const =0
Print the contents of the analysis state.
virtual ~AnalysisState()
LatticeAnchor anchor
The lattice anchor to which the state belongs.
Region * getParent() const
Provide a 'getParent' method for ilist_node_with_parent methods.
Definition: Block.cpp:29
Base class for all data-flow analyses.
void addDependency(AnalysisState *state, ProgramPoint *point)
Create a dependency between the given analysis state and lattice anchor on this analysis.
void propagateIfChanged(AnalysisState *state, ChangeResult changed)
Propagate an update to a state if it changed.
DataFlowAnalysis(DataFlowSolver &solver)
Create an analysis with a reference to the parent solver.
The general data-flow analysis solver.
void propagateIfChanged(AnalysisState *state, ChangeResult changed)
Propagate an update to an analysis state if it changed by pushing dependent work items to the back of...
LogicalResult initializeAndRun(Operation *top)
Initialize the children analyses starting from the provided top-level operation and run the analysis ...
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:66
Set of flags used to control the behavior of the various IR print methods (e.g.
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
void print(raw_ostream &os, const OpPrintingFlags &flags=std::nullopt)
Location getLoc()
The source location the operation was defined or derived from.
Definition: Operation.h:223
Location getLoc()
Return a location for this region.
Definition: Region.cpp:31
Include the generated interface declarations.
ChangeResult
A result type used to indicate if a change happened.
Fundamental IR components are supported as first-class lattice anchor.
Location getLoc() const
Get the source location of the lattice anchor.
void print(raw_ostream &os) const
Print the lattice anchor.
Program point represents a specific location in the execution of a program.
bool isNull() const
Returns true if this program point is set.
bool isBlockStart() const
Operation * getPrevOp() const
Get the previous operation of this program point.
void print(raw_ostream &os) const
Print the program point.
Operation * getNextOp() const
Get the next operation of this program point.
Block * getBlock() const
Get the block contains this program point.