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 // GenericProgramPoint
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  << point << "\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 (auto *programPoint = llvm::dyn_cast<GenericProgramPoint *>(*this))
65  return programPoint->print(os);
66  if (auto *op = llvm::dyn_cast<Operation *>(*this))
67  return op->print(os, OpPrintingFlags().skipRegions());
68  if (auto value = llvm::dyn_cast<Value>(*this))
69  return value.print(os, OpPrintingFlags().skipRegions());
70  return get<Block *>()->print(os);
71 }
72 
74  if (auto *programPoint = llvm::dyn_cast<GenericProgramPoint *>(*this))
75  return programPoint->getLoc();
76  if (auto *op = llvm::dyn_cast<Operation *>(*this))
77  return op->getLoc();
78  if (auto value = llvm::dyn_cast<Value>(*this))
79  return value.getLoc();
80  return get<Block *>()->getParent()->getLoc();
81 }
82 
83 //===----------------------------------------------------------------------===//
84 // DataFlowSolver
85 //===----------------------------------------------------------------------===//
86 
88  // Initialize the analyses.
89  for (DataFlowAnalysis &analysis : llvm::make_pointee_range(childAnalyses)) {
90  DATAFLOW_DEBUG(llvm::dbgs()
91  << "Priming analysis: " << analysis.debugName << "\n");
92  if (failed(analysis.initialize(top)))
93  return failure();
94  }
95 
96  // Run the analysis until fixpoint.
97  do {
98  // Exhaust the worklist.
99  while (!worklist.empty()) {
100  auto [point, analysis] = worklist.front();
101  worklist.pop();
102 
103  DATAFLOW_DEBUG(llvm::dbgs() << "Invoking '" << analysis->debugName
104  << "' on: " << point << "\n");
105  if (failed(analysis->visit(point)))
106  return failure();
107  }
108 
109  // Iterate until all states are in some initialized state and the worklist
110  // is exhausted.
111  } while (!worklist.empty());
112 
113  return success();
114 }
115 
117  ChangeResult changed) {
118  if (changed == ChangeResult::Change) {
119  DATAFLOW_DEBUG(llvm::dbgs() << "Propagating update to " << state->debugName
120  << " of " << state->point << "\n"
121  << "Value: " << *state << "\n");
122  state->onUpdate(this);
123  }
124 }
125 
126 //===----------------------------------------------------------------------===//
127 // DataFlowAnalysis
128 //===----------------------------------------------------------------------===//
129 
131 
133 
135  state->addDependency(point, this);
136 }
137 
139  ChangeResult changed) {
140  solver.propagateIfChanged(state, changed);
141 }
#define DATAFLOW_DEBUG(X)
Base class for generic analysis states.
LLVM_DUMP_METHOD void dump() const
virtual void print(raw_ostream &os) const =0
Print the contents of the analysis state.
void addDependency(ProgramPoint dependent, DataFlowAnalysis *analysis)
Add a dependency to this analysis state on a program point and an analysis.
virtual ~AnalysisState()
ProgramPoint point
The program point to which the state belongs.
Base class for all data-flow analyses.
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.
void addDependency(AnalysisState *state, ProgramPoint point)
Create a dependency between the given analysis state and program point on this analysis.
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:63
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
Include the generated interface declarations.
ChangeResult
A result type used to indicate if a change happened.
Fundamental IR components are supported as first-class program points.
Location getLoc() const
Get the source location of the program point.
void print(raw_ostream &os) const
Print the program point.